public bool TryLookup(RichIdentity richIdentity, IdentityLookupContext context)
        {
            if (TryLookupInLocalUsers(richIdentity, context))
            {
                return(true);
            }

            return(TryLookupInLDAPUsers(richIdentity, context));
        }
        /// <summary>
        /// Basic user identity property, e.g. Alias and email address, mapping based on the mapping configuration.
        /// </summary>
        /// <param name="originalUserIdentity"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private bool TryMapUserIdentity(
            RichIdentity originalUserIdentity,
            IdentityLookupContext context,
            out RichIdentity mappingResult)
        {
            UserIdMappingAlgorithm algorithm = m_mappingAlgorithmPool.GetAlgorithm();

            return(algorithm.TryMapUserIdentity(originalUserIdentity, context, out mappingResult));
        }
        public bool TryLookup(RichIdentity richIdentity, IdentityLookupContext context)
        {
            TfsCore core = null;

            lock (m_perMigrationSourceTfsCoresLock)
            {
                if (!m_perMigrationSourceTfsCores.ContainsKey(context.SourceMigrationSourceId))
                {
                    return(false);
                }

                core = m_perMigrationSourceTfsCores[context.SourceMigrationSourceId];
            }

            return(core.TryLookup(richIdentity, context));
        }
        private bool LookupUserIdOnOneSide(RichIdentity userIdentity, IdentityLookupContext context, Guid ownerMigrationSourceId)
        {
            if (!m_lookupConfiguration.LookupIsEnabled)
            {
                return(true);
            }

            bool lookupSucceeded = false;

            Guid[] applicableProviderRefNames;
            lookupSucceeded = m_lookupConfiguration.TryGetConfiguredProviders(ownerMigrationSourceId, out applicableProviderRefNames);

            if (lookupSucceeded)
            {
                if (applicableProviderRefNames.Length == 0)
                {
                    // no specific set of lookup service providers is configured to be used for the subject Migration Source
                    // we default to use all of them
                    applicableProviderRefNames = m_lookupProviderRegistry.GetAllProviderReferenceNames();
                }

                foreach (Guid providerRefName in applicableProviderRefNames)
                {
                    IUserIdentityLookupServiceProvider provider = m_lookupProviderRegistry.GetProvider(providerRefName);
                    if (null != provider)
                    {
                        lookupSucceeded = provider.TryLookup(userIdentity, context);
                        if (lookupSucceeded)
                        {
                            break;
                        }
                    }
                    else
                    {
                        TraceManager.TraceWarning("Warning: cannot find User Id Lookup Service Provider (Reference name: '{0}')",
                                                  providerRefName.ToString());
                    }
                }
            }

            return(lookupSucceeded);
        }
        private bool TryLookupInLocalUsers(RichIdentity richIdentity, IdentityLookupContext context)
        {
            if (string.IsNullOrEmpty(richIdentity.Alias) && string.IsNullOrEmpty(richIdentity.DisplayName))
            {
                return(false);
            }

            ReadOnlyCollection <string> userNames = GetUserNames(context.SourceMigrationSourceId);

            if (userNames.Contains(richIdentity.Alias))
            {
                richIdentity.DisplayName = richIdentity.Alias;
            }
            else if (userNames.Contains(richIdentity.DisplayName))
            {
                richIdentity.Alias = richIdentity.DisplayName;
            }
            else
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Based on the context and the user Id mapping configuration, translate a source (original)
        /// user identity to a target (translated) user identity.
        /// </summary>
        /// <param name="originalUserIdentity">The original user identity</param>
        /// <param name="context"></param>
        /// <param name="translatedUserIdentity"></param>
        /// <returns>TRUE if lookup succeeded; FALSE otherwise.</returns>
        public bool TryLookup(
            RichIdentity originalUserIdentity,
            IdentityLookupContext context,
            out RichIdentity translatedUserIdentity)
        {
            if (null == originalUserIdentity)
            {
                throw new ArgumentNullException("originalUserIdentity");
            }

            if (null == context)
            {
                throw new ArgumentNullException("context");
            }

            translatedUserIdentity = originalUserIdentity;
            if (!m_serviceIsConfigured)
            {
                // when the service is not configured, i.e. <UserIdentityMappings> is missing or empty
                // we simply return the original user identity as the lookup result
                return(true);
            }

            // 1. [optional] look up on source side to find the original user id and refresh its properties
            if (!LookupUserIdOnOneSide(originalUserIdentity, context, context.SourceMigrationSourceId))
            {
                TraceManager.TraceError(@"User '{0}, {1}\{2}' cannot be found on source system. ",
                                        string.IsNullOrEmpty(originalUserIdentity.DisplayName) ? "Unknown DisplayName" : originalUserIdentity.DisplayName,
                                        string.IsNullOrEmpty(originalUserIdentity.Domain) ? "Unknown Domain" : originalUserIdentity.Domain,
                                        string.IsNullOrEmpty(originalUserIdentity.Alias) ? "Unknown Alias" : originalUserIdentity.Alias);
            }

            // 2. map original user id properties to target one's properties
            RichIdentity mappedUserId = null;

            if (TryMapUserIdentity(originalUserIdentity, context, out mappedUserId))
            {
                translatedUserIdentity = mappedUserId;
            }
            else
            {
                return(false);
            }

            // 3. [optional] look up on target side to find the mapped user id and refresh its properties
            IdentityLookupContext reversedContext = context.Reverse();

            if (LookupUserIdOnOneSide(mappedUserId, reversedContext, context.TargetMigrationSourceId))
            {
                translatedUserIdentity = mappedUserId;
                return(true);
            }
            else
            {
                TraceManager.TraceError(@"User '{0} {1}\{2}' cannot be found on target system. ",
                                        string.IsNullOrEmpty(translatedUserIdentity.DisplayName) ? "Unknown Display" : translatedUserIdentity.DisplayName,
                                        string.IsNullOrEmpty(translatedUserIdentity.Domain) ? "Unknown Domain" : translatedUserIdentity.Domain,
                                        string.IsNullOrEmpty(translatedUserIdentity.Alias) ? "Unknown Alias" : translatedUserIdentity.Alias);
                return(true);
            }
        }
Exemple #7
0
        public void UserMappingRuleEvaluator_TryMapUserIdentityTest()
        {
            NotifyingCollection <UserMappings> userMappings = new NotifyingCollection <UserMappings>();

            UserMappings mappings = new UserMappings();

            mappings.DirectionOfMapping = MappingDirectionEnum.LeftToRight;

            UserMapping mapping = new UserMapping();

            mapping.LeftUser         = new User();
            mapping.LeftUser.Alias   = "alias1";
            mapping.LeftUser.Domain  = "domain1";
            mapping.RightUser        = new User();
            mapping.RightUser.Alias  = "alias1_target";
            mapping.RightUser.Domain = "domain1_target";

            mappings.UserMapping.Add(mapping);

            userMappings.Add(mappings);


            UserMappingRuleEvaluator target             = new UserMappingRuleEvaluator(userMappings);
            RichIdentity             sourceUserIdentity = new RichIdentity();

            sourceUserIdentity.Alias       = "alias1";
            sourceUserIdentity.Domain      = "domain1";
            sourceUserIdentity.DisplayName = "random";

            IdentityLookupContext context = new IdentityLookupContext(Guid.Empty, Guid.Empty);

            context.MappingDirection = MappingDirectionEnum.LeftToRight;

            RichIdentity mappedUserIdentity = new RichIdentity();
            bool         expected           = true;
            bool         actual;

            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(mappedUserIdentity.Alias, "alias1_target");
            Assert.AreEqual(mappedUserIdentity.Domain, "domain1_target");
            Assert.AreEqual(mappedUserIdentity.DisplayName, string.Empty);


            sourceUserIdentity.Alias = "alias2";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreNotEqual(expected, actual);

            sourceUserIdentity.Alias  = "alias1";
            sourceUserIdentity.Domain = "different_domain";
            mappedUserIdentity        = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreNotEqual(expected, actual);


            // add another rule
            mappings = new UserMappings();
            mappings.DirectionOfMapping = MappingDirectionEnum.RightToLeft;

            mapping                  = new UserMapping();
            mapping.LeftUser         = new User();
            mapping.LeftUser.Alias   = "default_alias";
            mapping.LeftUser.Domain  = "default_domain";
            mapping.RightUser        = new User();
            mapping.RightUser.Alias  = "*";
            mapping.RightUser.Domain = "domain1";

            mappings.UserMapping.Add(mapping);
            userMappings.Add(mappings);

            target = new UserMappingRuleEvaluator(userMappings);

            // switch mapping direction
            context.MappingDirection = MappingDirectionEnum.RightToLeft;

            sourceUserIdentity.Alias  = "random alias";
            sourceUserIdentity.Domain = "domain1";
            mappedUserIdentity        = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(mappedUserIdentity.Alias, "default_alias");
            Assert.AreEqual(mappedUserIdentity.Domain, "default_domain");
            Assert.AreEqual(mappedUserIdentity.DisplayName, string.Empty);
        }
 private bool TryLookupInLDAPUsers(RichIdentity richIdentity, IdentityLookupContext context)
 {
     // throw new NotImplementedException();
     return(false);
 }
Exemple #9
0
        public void AliasMappingRuleEvaluator_TryMapUserIdentityTest()
        {
            AliasMappingRuleEvaluator target = new AliasMappingRuleEvaluator(m_aliasMappings);

            RichIdentity sourceUserIdentity = new RichIdentity();

            sourceUserIdentity.Alias  = "user2";
            sourceUserIdentity.Domain = "microsoft";

            IdentityLookupContext context = new IdentityLookupContext(Guid.Empty, Guid.Empty);

            context.MappingDirection = MappingDirectionEnum.LeftToRight;

            RichIdentity mappedUserIdentity = new RichIdentity();
            bool         expected           = true;
            bool         actual             = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);

            Assert.AreEqual(mappedUserIdentity.Alias, "user2_target");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "random user";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "user3";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "user3");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "user2_target_on_left";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "admin";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            // switch mapping direction
            context.MappingDirection = MappingDirectionEnum.RightToLeft;

            sourceUserIdentity.Alias = "user2";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "user2_target_on_left");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "random user";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, "random user");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.Alias = "ignore";
            mappedUserIdentity       = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.Alias, string.Empty);
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);
        }
Exemple #10
0
        public void DisplayNameMappingRuleEvaluator_TryMapUserIdentityTest()
        {
            DisplayNameMappingRuleEvaluator target = new DisplayNameMappingRuleEvaluator(m_mappings);

            RichIdentity sourceUserIdentity = new RichIdentity();

            sourceUserIdentity.DisplayName = "user2";
            sourceUserIdentity.Domain      = "microsoft";

            IdentityLookupContext context = new IdentityLookupContext(Guid.Empty, Guid.Empty); // TODO: Initialize to an appropriate value

            context.MappingDirection = MappingDirectionEnum.LeftToRight;

            RichIdentity mappedUserIdentity = new RichIdentity();
            bool         expected           = true;
            bool         actual             = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);

            Assert.AreEqual(mappedUserIdentity.DisplayName, "user2 target");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.DisplayName = "random user";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.DisplayName = "user3";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "user3");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.DisplayName = "user2_target_on left";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.DisplayName = "admin";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "default");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            // switch mapping direction
            context.MappingDirection = MappingDirectionEnum.RightToLeft;

            sourceUserIdentity.DisplayName = "user2";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "user2_target_on left");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);

            sourceUserIdentity.DisplayName = "random user";
            mappedUserIdentity             = new RichIdentity();
            actual = target.TryMapUserIdentity(sourceUserIdentity, context, mappedUserIdentity);
            Assert.AreEqual(mappedUserIdentity.DisplayName, "random user");
            Assert.AreEqual(mappedUserIdentity.Domain, string.Empty);
            Assert.AreEqual(expected, actual);
        }
        internal bool TryLookup(
            RichIdentity richIdentity,
            IdentityLookupContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(richIdentity.DisplayName))
                {
                    if (!m_adSearchOnly)
                    {
                        // "Jeff Smith"
                        TeamFoundationIdentity id = ResolveIdentity(richIdentity.DisplayName,
                                                                    MembershipQuery.None,
                                                                    ReadIdentityOptions.None,
                                                                    IdentitySearchFactor.DisplayName);

                        UpdateRichIdentity(richIdentity, id);
                        return(true);
                    }
                    else
                    {
                        return(ADUserSearcher.TryUpdateAccountDetails(richIdentity.DisplayName, richIdentity));
                    }
                }
                else if (!string.IsNullOrEmpty(richIdentity.Alias) && !string.IsNullOrEmpty(richIdentity.Domain))
                {
                    if (!m_adSearchOnly)
                    {
                        // "Fabrikam\jeffsmith"
                        string accountName        = richIdentity.Domain + @"\" + richIdentity.Alias;
                        TeamFoundationIdentity id = ResolveIdentity(accountName,
                                                                    MembershipQuery.None,
                                                                    ReadIdentityOptions.None,
                                                                    IdentitySearchFactor.AccountName);
                        UpdateRichIdentity(richIdentity, id);
                        return(true);
                    }
                }
                else if (!string.IsNullOrEmpty(richIdentity.Alias))
                {
                    if (!m_adSearchOnly)
                    {
                        TeamFoundationIdentity id = ResolveIdentity(richIdentity.Alias,
                                                                    MembershipQuery.None,
                                                                    ReadIdentityOptions.None,
                                                                    IdentitySearchFactor.AccountName);
                        UpdateRichIdentity(richIdentity, id);
                        return(true);
                    }
                }

                return(false);
            }
            catch (IdentityUnresolvedException unrslvEx)
            {
                TraceManager.TraceInformation(unrslvEx.Message);
                return(false);
            }
            catch (IllegalIdentityException illglIdEx)
            {
                TraceManager.TraceInformation(illglIdEx.Message);
                return(false);
            }
        }