コード例 #1
0
        public void AddUserIntoGroups(UserInfo user, LDAPSupportSettings settings)
        {
            if (user == null || !settings.GroupMembership || DomainGroups == null)
            {
                return;
            }

            var domainUser = FindDomainUser(user.Sid);

            if (domainUser == null)
            {
                return;
            }

            var userAttributeValue = ldapHelper.GetUserAttribute(domainUser, settings.UserAttribute);

            foreach (var domainGroup in DomainGroups)
            {
                string sid     = domainGroup.Sid;
                var    members = ldapHelper.GetGroupAttribute(domainGroup, settings.GroupAttribute);
                if (members != null)
                {
                    foreach (string member in members)
                    {
                        if (IsUser(member, settings.UserAttribute))
                        {
                            if (userAttributeValue.Equals(member, StringComparison.InvariantCultureIgnoreCase))
                            {
                                var group = CoreContext.UserManager.GetGroupInfoBySid(sid);
                                if (group != Core.Users.Constants.LostGroupInfo)
                                {
                                    CoreContext.UserManager.AddUserIntoGroup(user.ID, group.ID);
                                }
                            }
                        }
                        else if (!relationGroupCache.Exists(member, sid) && IsGroup(member, settings.UserAttribute))
                        {
                            relationGroupCache.Add(member, sid);
                        }
                    }
                }
            }
            if (primaryGroup != null)
            {
                var getPrimaryGroup = CoreContext.UserManager.GetGroupInfoBySid(primaryGroup.Sid);
                if (getPrimaryGroup != Core.Users.Constants.LostGroupInfo)
                {
                    CoreContext.UserManager.AddUserIntoGroup(user.ID, getPrimaryGroup.ID);
                }
            }
        }
コード例 #2
0
        public bool IsUserExistsInGroups(UserInfo ldapUser)
        {
            try
            {
                if (!_settings.GroupMembership)
                {
                    return(false);
                }

                if (ldapUser == null ||
                    Equals(ldapUser, Core.Users.Constants.LostUser) ||
                    string.IsNullOrEmpty(ldapUser.Sid))
                {
                    return(false);
                }

                if (!AllDomainGroups.Any() && !TryLoadLDAPGroups())
                {
                    return(false);
                }

                var domainUser = _ldapHelper.GetUserBySid(_settings, ldapUser.Sid);

                if (domainUser == null)
                {
                    return(false);
                }

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

                foreach (var domainGroup in AllDomainGroups)
                {
                    if (_ldapHelper.UserExistsInGroup(_settings, domainGroup, distinguishedName, _settings.GroupAttribute))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ldapUser != null)
                {
                    _log.ErrorFormat("IsUserExistInGroups(login: '******' sid: '{1}') error {2}", ldapUser.UserName, ldapUser.Sid, ex);
                }
            }

            return(false);
        }