private UserInfo CreateUserInfo(LDAPUser domainUser, LDAPSupportSettings settings)
        {
            var user = new UserInfo
            {
                ID               = Guid.NewGuid(),
                UserName         = domainUser.AccountName,
                Sid              = domainUser.Sid.Value,
                ActivationStatus = (!string.IsNullOrEmpty(domainUser.Mail) ? EmployeeActivationStatus.Activated : EmployeeActivationStatus.NotActivated),
                Email            = (!string.IsNullOrEmpty(domainUser.Mail) ? domainUser.Mail : string.Empty),
                MobilePhone      = (!string.IsNullOrEmpty(domainUser.Mobile) ? domainUser.Mobile : string.Empty),
                Title            = (!string.IsNullOrEmpty(domainUser.Title) ? domainUser.Title : string.Empty),
                Location         = (!string.IsNullOrEmpty(domainUser.Street) ? domainUser.Street : string.Empty),
                WorkFromDate     = TenantUtil.DateTimeNow()
            };

            if (!string.IsNullOrEmpty(domainUser.FirstName))
            {
                if (domainUser.FirstName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    user.FirstName = domainUser.FirstName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                else
                {
                    user.FirstName = domainUser.FirstName;
                }
            }
            else
            {
                user.FirstName = string.Empty;
            }

            if (!string.IsNullOrEmpty(domainUser.SecondName))
            {
                if (domainUser.SecondName.Length > MAX_NUMBER_OF_SYMBOLS)
                {
                    user.LastName = domainUser.SecondName.Substring(0, MAX_NUMBER_OF_SYMBOLS);
                }
                else
                {
                    user.LastName = domainUser.SecondName;
                }
            }
            else
            {
                user.LastName = string.Empty;
            }

            if (user.Email == string.Empty)
            {
                var    domain     = ADDomain.GetDomain(settings);
                var    domainName = domain != null ? domain.Name : UNKNOWN_DOMAIN;
                string loginName  = domainUser.InvokeGet(settings.LoginAttribute).ToString();
                user.Email = loginName.Contains("@") ? loginName : loginName + "@" + domainName;
            }
            return(user);
        }
        public string GetSidOfCurrentUser(string login, LDAPSupportSettings settings)
        {
            var users = ADDomain.GetUsersByAttributesAndFilter(settings, "(" + settings.LoginAttribute + "=" + login + ")");

            if (users.Count != 0 && users[0] != null && users[0].Sid != null)
            {
                return(users[0].Sid.Value);
            }
            return(null);
        }
        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 dn = ADDomain.GetUserAttribute(domainUser, settings.UserAttribute);

            foreach (var domainGroup in DomainGroups)
            {
                var members = ADDomain.GetGroupAttribute(domainGroup, settings.GroupAttribute);
                if (members != null)
                {
                    if (members.Value != null)
                    {
                        foreach (var member in members)
                        {
                            var memberString = member.ToString();
                            if (IsUser(memberString))
                            {
                                if (dn.Equals(memberString, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    var group = CoreContext.GroupManager.GetGroupInfoBySid(domainGroup.Sid.Value);
                                    if (group != Core.Users.Constants.LostGroupInfo)
                                    {
                                        CoreContext.UserManager.AddUserIntoGroup(user.ID, group.ID);
                                    }
                                }
                            }
                            else if (!_cache.Exists(memberString, domainGroup.Sid.Value) && IsGroup(memberString))
                            {
                                _cache.Add(memberString, domainGroup.Sid.Value);
                            }
                        }
                    }
                }
            }
            if (_primaryGroup != null)
            {
                var primaryGroup = CoreContext.GroupManager.GetGroupInfoBySid(_primaryGroup.Sid);
                if (primaryGroup != Core.Users.Constants.LostGroupInfo)
                {
                    CoreContext.UserManager.AddUserIntoGroup(user.ID, primaryGroup.ID);
                }
            }
        }
        public UserInfo GetDiscoveredUser(string sid)
        {
            var domainUser = ADDomain.GetUserBySid(sid);

            _domainUsers = new List <LDAPUser>(ONE_USER);
            _domainUsers.Add(domainUser);
            if (domainUser != null)
            {
                return(CreateUserInfo(domainUser));
            }
            return(ASC.Core.Users.Constants.LostUser);
        }
 public List <GroupInfo> GetDiscoveredGroupsByAttributes(LDAPSupportSettings settings)
 {
     if (settings.GroupMembership)
     {
         if (DomainGroups == null)
         {
             DomainGroups = ADDomain.GetGroupsByParameter(settings);
         }
         if (DomainGroups != null)
         {
             var groups        = new List <GroupInfo>(DomainGroups.Count);
             var removedGroups = new List <LDAPGroup>();
             foreach (var domainGroup in DomainGroups)
             {
                 var lastId = domainGroup.Sid.Value.Split(LAST_HYPHEN).Last();
                 if (lastId != PRIMARY_GROUP)
                 {
                     var members = ADDomain.GetGroupAttribute(domainGroup, settings.GroupAttribute);
                     if (members == null || members.Value == null)
                     {
                         removedGroups.Add(domainGroup);
                         continue;
                     }
                 }
                 var groupInfo = new GroupInfo
                 {
                     Name = domainGroup.Name,
                     Sid  = domainGroup.Sid.Value
                 };
                 // Domain Users - primary group
                 if (domainGroup.Sid.Value.Split(LAST_HYPHEN).Last() == PRIMARY_GROUP)
                 {
                     _primaryGroup = groupInfo;
                 }
                 if (CoreContext.GroupManager.GetGroupInfoBySid(groupInfo.Sid).ID == Core.Users.Constants.LostGroupInfo.ID)
                 {
                     groups.Add(groupInfo);
                 }
             }
             foreach (var domainGroup in removedGroups)
             {
                 if (DomainGroups.Contains(domainGroup))
                 {
                     DomainGroups.Remove(domainGroup);
                 }
             }
             return(groups);
         }
     }
     return(null);
 }
        public IEnumerable <GroupInfo> GetDiscoveredGroups()
        {
            var domainGroups = ADDomain.GetGroups();
            var groups       = new List <GroupInfo>(domainGroups.Count);

            foreach (var domainGroup in domainGroups)
            {
                groups.Add(new GroupInfo
                {
                    Name = domainGroup.Name,
                    Sid  = domainGroup.Sid.Value
                });
            }
            return(groups);
        }
        public IEnumerable <UserInfo> GetDiscoveredUsers()
        {
            _domainUsers = ADDomain.GetUsers();
            IList <UserInfo> users = new List <UserInfo>();

            foreach (var domainUser in _domainUsers)
            {
                if (domainUser.IsDisabled && domainUser.AccountName != string.Empty)
                {
                    continue;
                }
                users.Add(CreateUserInfo(domainUser));
            }
            return(users);
        }
        public UserInfo GetDiscoveredUser(LDAPSupportSettings settings, string sid)
        {
            var domainUser = ADDomain.GetUserBySid(settings, sid);

            if (domainUser != null && !domainUser.IsDisabled && IsUserExistsInGroup(domainUser, settings))
            {
                _domainUsers = new List <LDAPUser>();
                _domainUsers.Add(domainUser);
                var userInfo = CreateUserInfo(domainUser, settings);
                if (CheckEmail(userInfo))
                {
                    return(userInfo);
                }
            }
            return(Core.Users.Constants.LostUser);
        }
        private bool IsUserExistsInGroup(LDAPUser domainUser, LDAPSupportSettings settings)
        {
            if (!settings.GroupMembership || DomainGroups == null || DomainGroups.Count == 0 || _primaryGroup != null)
            {
                return(true);
            }

            var dn = ADDomain.GetUserAttribute(domainUser, settings.UserAttribute);

            foreach (var domainGroup in DomainGroups)
            {
                if (ADDomain.UserExistsInGroup(domainGroup, dn, settings.GroupAttribute))
                {
                    return(true);
                }
            }

            return(false);
        }