private LDAPObject FindUserByMember(string userAttributeValue)
        {
            if (!AllDomainUsers.Any() && !TryLoadLDAPUsers())
            {
                return(null);
            }

            return(AllDomainUsers.FirstOrDefault(u =>
                                                 Convert.ToString(u.InvokeGet(_settings.UserAttribute))
                                                 .Equals(userAttributeValue,
                                                         StringComparison.InvariantCultureIgnoreCase)));
        }
Example #2
0
        private LdapObject FindUserByMember(string userAttributeValue)
        {
            if (!AllDomainUsers.Any() && !TryLoadLDAPUsers())
            {
                return(null);
            }

            return(AllDomainUsers.FirstOrDefault(u =>
                                                 u.DistinguishedName.Equals(userAttributeValue, StringComparison.InvariantCultureIgnoreCase) ||
                                                 Convert.ToString(u.GetValue(Settings.UserAttribute)).Equals(userAttributeValue,
                                                                                                             StringComparison.InvariantCultureIgnoreCase)));
        }
        public void SyncUserGroupMembership(UserInfo user)
        {
            if (user == null ||
                !_settings.GroupMembership ||
                AllDomainGroups == null ||
                !AllDomainGroups.Any() && !TryLoadLDAPGroups() ||
                !AllDomainUsers.Any() && !TryLoadLDAPUsers())
            {
                return;
            }

            var domainUser = AllDomainUsers.FirstOrDefault(u => u.Sid.Equals(user.Sid));

            if (domainUser == null)
            {
                return;
            }

            var userAttributeValue = _ldapHelper.GetUserAttribute(domainUser, _settings.UserAttribute);

            foreach (var domainGroup in AllDomainGroups)
            {
                var sid = domainGroup.Sid;

                var members = _ldapHelper.GetGroupAttribute(domainGroup, _settings.GroupAttribute);

                if (members == null)
                {
                    continue;
                }

                foreach (var member in members)
                {
                    var ldapUser = FindUserByMember(member);

                    if (ldapUser == null)
                    {
                        continue;
                    }

                    if (!userAttributeValue.Equals(member, StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    var groupInfo = CoreContext.UserManager.GetGroupInfoBySid(sid);

                    if (!Equals(groupInfo, Core.Users.Constants.LostGroupInfo))
                    {
                        CoreContext.UserManager.AddUserIntoGroup(user.ID, groupInfo.ID);
                    }
                }
            }

            var primaryGroup = AllDomainGroups.FirstOrDefault(g => g.Sid.EndsWith("-513"));

            if (primaryGroup == null)
            {
                return;
            }

            var getPrimaryGroup = CoreContext.UserManager.GetGroupInfoBySid(primaryGroup.Sid);

            if (!Equals(getPrimaryGroup, Core.Users.Constants.LostGroupInfo))
            {
                CoreContext.UserManager.AddUserIntoGroup(user.ID, getPrimaryGroup.ID);
            }
        }