private bool IsRuleApplicable(RichIdentity sourceUserIdentity, DomainMapping rule, bool leftToRight)
        {
            string fromDomainName = (leftToRight ? rule.Left : rule.Right);

            if (string.IsNullOrEmpty(fromDomainName))
            {
                return(false);
            }

            bool mappingRuleIsKnown = false;

            switch (rule.MappingRule)
            {
            case MappingRules.SimpleReplacement:
            case MappingRules.Ignore:
            case MappingRules.FormatStringComposition:
            case MappingRules.FormatStringDecomposition:
                mappingRuleIsKnown = true;
                break;

            default:
                TraceManager.TraceError("Unknown DisplayNameMapping.MappingRule type");
                mappingRuleIsKnown = false;
                break;
            }

            if (!mappingRuleIsKnown)
            {
                return(false);
            }

            return(fromDomainName.Equals(UserIdentityMappingConfigSymbols.ANY, StringComparison.OrdinalIgnoreCase) ||
                   fromDomainName.Equals(sourceUserIdentity.Domain ?? string.Empty, StringComparison.OrdinalIgnoreCase));
        }
        private void ApplyRule(RichIdentity sourceUserIdentity, bool leftToRight, RichIdentity mappedUserIdentity, UserMapping rule)
        {
            string toAlias  = (leftToRight ? rule.RightUser.Alias : rule.LeftUser.Alias);
            string toDomain = (leftToRight ? rule.RightUser.Domain : rule.LeftUser.Domain);

            if (toAlias.Equals(UserIdentityMappingConfigSymbols.ANY, StringComparison.OrdinalIgnoreCase))
            {
                mappedUserIdentity.Alias = sourceUserIdentity.Alias;
            }
            else
            {
                mappedUserIdentity.Alias = toAlias;
            }

            if (string.IsNullOrEmpty(toDomain))
            {
                mappedUserIdentity.Domain = string.Empty;
            }
            else if (toDomain.Equals(UserIdentityMappingConfigSymbols.ANY, StringComparison.OrdinalIgnoreCase))
            {
                mappedUserIdentity.Domain = sourceUserIdentity.Domain;
            }
            else
            {
                mappedUserIdentity.Domain = toDomain;
            }
        }
        private bool IsRuleApplicable(RichIdentity sourceUserIdentity, UserMapping rule, bool leftToRight)
        {
            string fromAlias  = (leftToRight ? rule.LeftUser.Alias : rule.RightUser.Alias);
            string fromDomain = (leftToRight ? rule.LeftUser.Domain : rule.RightUser.Domain);
            string toAlias    = (leftToRight ? rule.RightUser.Alias : rule.LeftUser.Alias);

            if (string.IsNullOrEmpty(fromAlias) || string.IsNullOrEmpty(toAlias))
            {
                Debug.Assert(false, "Alias in a user mapping rule should always be an Non-empty string");
                return(false);
            }

            bool aliasRuleIsApplicable = (fromAlias.Equals(UserIdentityMappingConfigSymbols.ANY, StringComparison.OrdinalIgnoreCase) ||
                                          fromAlias.Equals(sourceUserIdentity.Alias ?? string.Empty, StringComparison.OrdinalIgnoreCase));

            if (!aliasRuleIsApplicable)
            {
                return(false);
            }

            bool domainRuleIsApplicable = (string.IsNullOrEmpty(fromDomain) ||
                                           fromDomain.Equals(UserIdentityMappingConfigSymbols.ANY, StringComparison.OrdinalIgnoreCase) ||
                                           fromDomain.Equals(sourceUserIdentity.Domain ?? string.Empty, StringComparison.OrdinalIgnoreCase));

            return(aliasRuleIsApplicable && domainRuleIsApplicable);
        }
        public bool TryMapUserIdentity(
            RichIdentity sourceUserIdentity,
            IdentityLookupContext context,
            out RichIdentity mappedUserIdentity)
        {
            mappedUserIdentity = new RichIdentity();

            // 1. figure out the mapping direction in the identity's host session, e.g. left to right or the opposite
            if (!m_lookupContextManager.TrySetupContext(context))
            {
                TraceManager.TraceError(
                    "Identity look up failed - Migration Source '{0}' and '{1}' in the lookup context does not belong to the same session",
                    context.SourceMigrationSourceId.ToString(), context.TargetMigrationSourceId.ToString());
                return(false);
            }

            bool mapped = false;

            // 2. use the most specific mapping: user identity mapping
            mapped |= m_userMappingAlg.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);

            // 3. then use the display name mapping
            mapped |= m_dispNameMappingAlg.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);

            // 4. and then use the alias and/or domain name mapping together
            bool aliasMappingRslt  = m_aliasMappingAlg.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            bool domainMappingRslt = m_domainMappingAlg.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);

            return(mapped | (aliasMappingRslt & domainMappingRslt));
        }
 private bool TryApplyMappingRule(RichIdentity sourceUserIdentity, bool leftToRight, RichIdentity mappingOutput, DomainMapping rule)
 {
     if (IsRuleApplicable(sourceUserIdentity, rule, leftToRight))
     {
         return(ApplyRule(sourceUserIdentity, leftToRight, mappingOutput, rule));
     }
     else
     {
         return(false);
     }
 }
        private bool ApplyRule(RichIdentity sourceUserIdentity, bool leftToRight, RichIdentity mappingOutput, DomainMapping rule)
        {
            string toDomainName = (leftToRight ? rule.Right : rule.Left);
            IStringManipulationRule stringManipulationRule = StringManipulationRuleFactory.GetInstance(rule.MappingRule);

            if (stringManipulationRule != null)
            {
                mappingOutput.Domain = stringManipulationRule.Apply(sourceUserIdentity.Domain, toDomainName);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Try map the user identity based on the mappings rules in the configuration
        /// </summary>
        /// <param name="sourceUserIdentity"></param>
        /// <param name="context"></param>
        /// <param name="mappedUserIdentity"></param>
        /// <returns>True if a mapping rule is applied to the sourceUserIdentity; FALSE otherwise</returns>
        public bool TryMapUserIdentity(
            RichIdentity sourceUserIdentity,
            IdentityLookupContext context,
            RichIdentity mappedUserIdentity)
        {
            switch (context.MappingDirection)
            {
            case MappingDirectionEnum.LeftToRight:
                return(MapUser(sourceUserIdentity, true, mappedUserIdentity));

            case MappingDirectionEnum.RightToLeft:
                return(MapUser(sourceUserIdentity, false, mappedUserIdentity));

            default:
                TraceManager.TraceError("Unknown context.MappingDirection: {0}", context.MappingDirection.ToString());
                return(false);
            }
        }
        public override void Translate(IMigrationAction action, Guid migrationSourceIdOfChangeGroup)
        {
            MigrationAction migrAction = action as MigrationAction;

            Debug.Assert(migrAction != null, "action is not a MigrationActin");

            migrAction.Path     = GetMappedPath(action.Path, migrationSourceIdOfChangeGroup);
            migrAction.FromPath = GetMappedPath(action.FromPath, migrationSourceIdOfChangeGroup);

            if (UserIdLookupService.IsConfigured)
            {
                if (action.ChangeGroup.ChangeGroupId != m_cachedUserIdLookupResult.Key)
                {
                    IdentityLookupContext context = new IdentityLookupContext(
                        migrationSourceIdOfChangeGroup, m_migratinSourcePair[migrationSourceIdOfChangeGroup]);

                    UserIdPropertyNameEnum srcDefaultUserIdProperty;
                    UserIdPropertyNameEnum targetDefaultUserIdProperty;
                    if (UserIdLookupService.TryGetDefaultUserIdProperty(context.SourceMigrationSourceId, out srcDefaultUserIdProperty) &&
                        UserIdLookupService.TryGetDefaultUserIdProperty(context.TargetMigrationSourceId, out targetDefaultUserIdProperty))
                    {
                        RichIdentity srcUserId = new RichIdentity();
                        srcUserId[srcDefaultUserIdProperty.ToString()] = action.ChangeGroup.Owner; // todo: parse qualified name?

                        RichIdentity mappedUserId;
                        if (UserIdLookupService.TryLookup(srcUserId, context, out mappedUserId))
                        {
                            action.ChangeGroup.Owner = mappedUserId[targetDefaultUserIdProperty.ToString()];
                        }
                    }

                    m_cachedUserIdLookupResult = new KeyValuePair <long, string>(action.ChangeGroup.ChangeGroupId, action.ChangeGroup.Owner);
                }
                else
                {
                    action.ChangeGroup.Owner = m_cachedUserIdLookupResult.Value;
                }
            }
        }
        private bool MapUser(
            RichIdentity sourceUserIdentity,
            bool leftToRight,
            RichIdentity mappedUserIdentity)
        {
            m_mappingRuleComparer.MapFromLeftToRight = leftToRight;

            NotifyingCollection <DomainMapping> unidirectionalRules;

            if (leftToRight)
            {
                if (!m_perDirectionMappings.ContainsKey(MappingDirectionEnum.LeftToRight))
                {
                    return(false);
                }
                unidirectionalRules = m_perDirectionMappings[MappingDirectionEnum.LeftToRight];
            }
            else
            {
                if (!m_perDirectionMappings.ContainsKey(MappingDirectionEnum.RightToLeft))
                {
                    return(false);
                }
                unidirectionalRules = m_perDirectionMappings[MappingDirectionEnum.RightToLeft];
            }

            bool mapped = false;

            DomainMapping appliedUnidirectionalRule   = null;
            RichIdentity  unidirectionalMappingOutput = null;

            foreach (DomainMapping rule in unidirectionalRules)
            {
                RichIdentity mappingOutput = new RichIdentity();
                if (TryApplyMappingRule(sourceUserIdentity, leftToRight, mappingOutput, rule))
                {
                    if (appliedUnidirectionalRule == null ||
                        m_mappingRuleComparer.Compare(rule, appliedUnidirectionalRule) < 0)
                    {
                        appliedUnidirectionalRule   = rule;
                        unidirectionalMappingOutput = mappingOutput;
                    }
                    mapped = true;
                }
            }

            if (mapped)
            {
                mappedUserIdentity.Domain = unidirectionalMappingOutput.Domain;
                return(true);
            }

            DomainMapping appliedBidirectionalRule   = null;
            RichIdentity  bidirectionalMappingOutput = null;

            if (m_perDirectionMappings.ContainsKey(MappingDirectionEnum.TwoWay))
            {
                foreach (DomainMapping rule in m_perDirectionMappings[MappingDirectionEnum.TwoWay])
                {
                    RichIdentity mappingOutput = new RichIdentity();
                    if (TryApplyMappingRule(sourceUserIdentity, leftToRight, mappingOutput, rule))
                    {
                        if (appliedBidirectionalRule == null ||
                            m_mappingRuleComparer.Compare(rule, appliedBidirectionalRule) < 0)
                        {
                            appliedBidirectionalRule   = rule;
                            bidirectionalMappingOutput = mappingOutput;
                        }
                        mapped = true;
                    }
                }
            }

            if (mapped)
            {
                mappedUserIdentity.Domain = bidirectionalMappingOutput.Domain;
            }

            return(mapped);
        }
        private bool MapUser(
            RichIdentity sourceUserIdentity,
            bool leftToRight,
            RichIdentity mappedUserIdentity)
        {
            m_mappingRuleComparer.MapFromLeftToRight = leftToRight;

            NotifyingCollection <UserMapping> unidirectionalRules;

            if (leftToRight)
            {
                if (!m_perDirectionUserMappings.ContainsKey(MappingDirectionEnum.LeftToRight))
                {
                    return(false);
                }
                unidirectionalRules = m_perDirectionUserMappings[MappingDirectionEnum.LeftToRight];
            }
            else
            {
                if (!m_perDirectionUserMappings.ContainsKey(MappingDirectionEnum.RightToLeft))
                {
                    return(false);
                }
                unidirectionalRules = m_perDirectionUserMappings[MappingDirectionEnum.RightToLeft];
            }

            bool         mapped = false;
            UserMapping  appliedUnidirectionalRule   = null;
            RichIdentity unidirectionalMappingOutput = null;

            foreach (UserMapping rule in unidirectionalRules)
            {
                RichIdentity mappingOutput = new RichIdentity();
                if (TryApplyMappingRule(sourceUserIdentity, leftToRight, mappingOutput, rule))
                {
                    if (appliedUnidirectionalRule == null ||
                        m_mappingRuleComparer.Compare(rule, appliedUnidirectionalRule) < 0)
                    {
                        appliedUnidirectionalRule   = rule;
                        unidirectionalMappingOutput = mappingOutput;
                    }
                    mapped = true;
                }
            }

            #region teyang comment out this if clause if we do not want to give evaluation precedence to unidirectional mapping rules
            if (mapped)
            {
                mappedUserIdentity.Alias  = unidirectionalMappingOutput.Alias;
                mappedUserIdentity.Domain = unidirectionalMappingOutput.Domain;
                return(true);
            }
            #endregion


            UserMapping  appliedBidirectionalRule   = null;
            RichIdentity bidirectionalMappingOutput = null;
            if (m_perDirectionUserMappings.ContainsKey(MappingDirectionEnum.TwoWay))
            {
                foreach (UserMapping rule in m_perDirectionUserMappings[MappingDirectionEnum.TwoWay])
                {
                    RichIdentity mappingOutput = new RichIdentity();
                    if (TryApplyMappingRule(sourceUserIdentity, leftToRight, mappingOutput, rule))
                    {
                        if (appliedBidirectionalRule == null ||
                            m_mappingRuleComparer.Compare(rule, appliedUnidirectionalRule) < 0)
                        {
                            appliedUnidirectionalRule  = rule;
                            bidirectionalMappingOutput = mappingOutput;
                        }
                        mapped = true;
                    }
                }
            }

            #region teyang comment out this if clause if we do not want to give evaluation precedence to unidirectional mapping rules
            if (mapped)
            {
                mappedUserIdentity.Alias  = bidirectionalMappingOutput.Alias;
                mappedUserIdentity.Domain = bidirectionalMappingOutput.Domain;
            }
            #endregion


            #region teyang UNcomment this if clause out if we do not want to give evaluation precedence to unidirectional mapping rules
            //if (mapped)
            //{
            //    if (appliedBidirectionalRule != null && appliedUnidirectionalRule != null)
            //    {
            //        if (m_mappingRuleComparer.Compare(appliedBidirectionalRule, appliedUnidirectionalRule) < 0)
            //        {
            //            mappedUserIdentity = bidirectionalMappingOutput;
            //        }
            //        else
            //        {
            //            mappedUserIdentity = unidirectionalMappingOutput;
            //        }
            //    }
            //    else if (appliedBidirectionalRule != null)
            //    {
            //        mappedUserIdentity = bidirectionalMappingOutput;
            //    }
            //    else
            //    {
            //        Debug.Assert(null != unidirectionalMappingOutput, "null == unidirectionalMappingOutput");
            //        mappedUserIdentity = unidirectionalMappingOutput;
            //    }
            //}

            #endregion

            return(mapped);
        }