private string MapUserIdentity(
            FieldMap fieldMap,
            SourceSideTypeEnum fromSide,
            string srcFieldRefName,
            string tgtFieldRefName,
            string srcFieldValue)
        {
            RichIdentity srcUserId = new RichIdentity();

            UserIdFieldElement userIdField = fieldMap.GetUserIdField(fromSide, srcFieldRefName);

            srcUserId[userIdField.UserIdPropertyName] = srcFieldValue;

            RichIdentity mappedUserId;

            if (UserIdLookupService.TryLookup(srcUserId, m_contexts[fromSide], out mappedUserId))
            {
                SourceSideTypeEnum toSide       = (fromSide == SourceSideTypeEnum.Left) ? SourceSideTypeEnum.Right : SourceSideTypeEnum.Left;
                UserIdFieldElement tgtUserField = fieldMap.GetUserIdField(toSide, tgtFieldRefName);
                if (null != tgtUserField)
                {
                    return(mappedUserId[tgtUserField.UserIdPropertyName]);
                }
                else
                {
                    return(srcFieldValue);
                }
            }
            else
            {
                return(srcFieldValue);
            }
        }
        /// <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)
        {
            if (TryLookupInLocalUsers(richIdentity, context))
            {
                return(true);
            }

            return(TryLookupInLDAPUsers(richIdentity, context));
        }
        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));
        }
예제 #5
0
        internal static bool TryUpdateAccountDetails(
            string displayName,
            RichIdentity richIdentity)
        {
            DomainAlias?accName = GetAccountNameByDisplayName(displayName);

            if (accName.HasValue)
            {
                richIdentity.Domain = accName.Value.Domain;
                richIdentity.Alias  = accName.Value.Alias;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        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);
        }
예제 #7
0
        private void UpdateRichIdentity(RichIdentity richIdentity, TeamFoundationIdentity id)
        {
            const string Unknown = "";

            richIdentity.UniqueId          = id.Descriptor.Identifier;
            richIdentity.Alias             = id.GetAttribute(IdentityAttributeTags.AccountName, Unknown);
            richIdentity.DisplayName       = id.DisplayName;
            richIdentity.DistinguishedName = id.GetAttribute(IdentityAttributeTags.DistinguishedName, Unknown);
            richIdentity.Domain            = id.GetAttribute(IdentityAttributeTags.Domain, Unknown);

            if (id.GetAttribute(IdentityAttributeTags.MailAddress, "Unknown").Length != 0)
            {
                richIdentity.EmailAddress = id.GetAttribute(IdentityAttributeTags.MailAddress, Unknown);
            }

            TraceManager.TraceInformation(
                "Updated Identity (Unique Id: {0}; Alias: {1}; Display Name {2}; Distinguished Name: {3}; Domain: {4}",
                richIdentity.UniqueId ?? Unknown, richIdentity.Alias ?? Unknown, richIdentity.DisplayName ?? Unknown,
                richIdentity.DistinguishedName ?? Unknown, richIdentity.Domain ?? Unknown);
        }
        private void MapOwnerUserId(
            XmlElement rootNode,
            SourceSideTypeEnum fromSide)
        {
            if (!UserIdLookupService.IsConfigured)
            {
                return;
            }

            var authorAttr = rootNode.Attributes["Author"];

            if (authorAttr == null || string.IsNullOrEmpty(authorAttr.Value))
            {
                return;
            }

            RichIdentity srcUserId = new RichIdentity();
            var          authorUserIdPropertyAttr = rootNode.Attributes[Constants.WITAuthorUserIdPropertyName];
            string       authorUserIdProperty;

            if (authorUserIdPropertyAttr == null || string.IsNullOrEmpty(authorUserIdPropertyAttr.Value))
            {
                authorUserIdProperty = "DisplayName";
            }
            else
            {
                authorUserIdProperty = authorUserIdPropertyAttr.Value;
            }

            srcUserId[authorUserIdProperty] = authorAttr.Value;

            RichIdentity mappedUserId;

            if (UserIdLookupService.TryLookup(srcUserId, m_contexts[fromSide], out mappedUserId))
            {
                authorAttr.Value = mappedUserId.DisplayName;
            }
        }
        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);
            }
        }
예제 #11
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);
 }
        public bool TryLookup(RichIdentity richIdentity, string serverUrl)
        {
            TfsCore core = new TfsCore(serverUrl);

            return(core.TryLookup(richIdentity, null));
        }
예제 #14
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);
        }
예제 #15
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);
        }
예제 #16
0
        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);
            }
        }