private int InsertUsersToTableByFilter(string tableCode, string filter)
        {
            // TODO ########### ########### ######### ############# ####### GetUsersByLdapFilter
            var result = 0;

            string[] userAttributes         = GetUserAttributes();
            string[] userRequiredAttributes = GetUserRequiredAttributes();
            var      request = new SearchRequest(_ldapUsersEntry, filter, SearchScope.Subtree, userAttributes);
            PageResultRequestControl paging = CreatePageResultRequestControl();

            request.Controls.Add(paging);
            SortRequestControl sortControl = CreateSortRequestControl(_ldapUserLoginAttribute);

            request.Controls.Add(sortControl);
            do
            {
                SearchResponse response;
                try {
                    response = _ldapConnection.SendRequest(request, RequestTimeout) as SearchResponse;
                } catch (Exception exception) {
                    LogLdapRequestError(exception, request);
                    throw;
                }
                if (response.ResultCode == ResultCode.Success)
                {
                    foreach (SearchResultEntry entry in response.Entries)
                    {
                        CheckEntryAttributes(entry, userRequiredAttributes);
                        LdapUser ldapUser   = CreateLdapUser(entry, string.Empty);
                        var      insertUser = new Insert(_userConnection).Into(tableCode)
                                              .Set("Id", Column.Parameter(ldapUser.Id))
                                              .Set("Name", Column.Parameter(ldapUser.Name))
                                              .Set("ModifiedOn", Column.Parameter(ldapUser.ModifiedOn))
                                              .Set("FullName", Column.Parameter(ldapUser.FullName))
                                              .Set("Company", Column.Parameter(ldapUser.Company))
                                              .Set("Email", Column.Parameter(ldapUser.Email))
                                              .Set("Phone", Column.Parameter(ldapUser.Phone))
                                              .Set("JobTitle", Column.Parameter(ldapUser.JobTitle))
                                              .Set("IsActive", Column.Parameter(ldapUser.IsActive))
                                              .Set("Dn", Column.Parameter(ldapUser.Dn));
                        try {
                            result += insertUser.Execute();
                        } catch (Exception e) {
                            _log.ErrorFormat("An error occurred while adding the record to the \"{0}\" table: {1}", e,
                                             tableCode, e.Message);
                            throw;
                        }
                    }
                }
                else
                {
                    _log.DebugFormat("Unable to obtain a list of users with the \"{0}\" filter. Result code: {1}.",
                                     filter, response.ResultCode);
                }
                var responseControl = (PageResultResponseControl)Array.Find(response.Controls,
                                                                            item => item is PageResultResponseControl);
                paging.Cookie = responseControl.Cookie;
            } while (paging.Cookie.Length != 0);
            return(result);
        }
        public virtual List <LdapUser> GetUsersByLdapFilter(string filter)
        {
            var usersList = new List <LdapUser>();

            string[] userAttributes         = GetUserAttributes();
            string[] userRequiredAttributes = GetUserRequiredAttributes();
            var      request = new SearchRequest(_ldapUsersEntry, filter, SearchScope.Subtree, userAttributes);
            PageResultRequestControl paging = CreatePageResultRequestControl();

            request.Controls.Add(paging);
            SortRequestControl sortControl = CreateSortRequestControl(_ldapUserLoginAttribute);

            request.Controls.Add(sortControl);
            do
            {
                SearchResponse response;
                try {
                    response = _ldapConnection.SendRequest(request, RequestTimeout) as SearchResponse;
                } catch (Exception exception) {
                    LogLdapRequestError(exception, request);
                    throw;
                }
                if (response.ResultCode == ResultCode.Success)
                {
                    foreach (SearchResultEntry entry in response.Entries)
                    {
                        CheckEntryAttributes(entry, userRequiredAttributes);
                        LdapUser ldapUser = CreateLdapUser(entry, null);
                        usersList.Add(ldapUser);
                    }
                }
                else
                {
                    _log.DebugFormat("Unable to obtain a list of users with the \"{0}\" filter. Result code: {1}. Error: {2}",
                                     filter, response.ResultCode, response.ErrorMessage ?? string.Empty);
                }
                var responseControl = (PageResultResponseControl)Array.Find(response.Controls,
                                                                            item => item is PageResultResponseControl);
                paging.Cookie = responseControl.Cookie;
            } while (paging.Cookie.Length != 0);
            return(usersList);
        }
        private LdapUser CreateLdapUser(SearchResultEntry entry, string attributeDefValue)
        {
            LdapUser ldapUser = new LdapUser();

            ldapUser.Id       = GetEntryIdentityAttribute(entry, _ldapUserIdentityAttribute);
            ldapUser.Name     = GetEntryRequiredAttributeStringValue(entry, _ldapUserLoginAttribute);
            ldapUser.FullName = GetEntryAttributeStringValue(entry, _ldapUserFullNameAttribute, attributeDefValue);
            ldapUser.Company  = GetEntryAttributeStringValue(entry, _ldapUserCompanyAttribute, attributeDefValue);
            ldapUser.Email    = GetEntryAttributeStringValue(entry, _ldapUserEmailAttribute, attributeDefValue);
            ldapUser.Phone    = GetEntryAttributeStringValue(entry, _ldapUserPhoneAttribute, attributeDefValue);
            ldapUser.JobTitle = GetEntryAttributeStringValue(entry, _ldapUserJobTitleAttribute, attributeDefValue);
            string accountControlAttributeValue =
                GetEntryAttributeStringValue(entry, LdapUserAccountControlAttributeName, string.Empty);

            ldapUser.IsActive = string.IsNullOrEmpty(accountControlAttributeValue) ||
                                IsActive(accountControlAttributeValue);
            bool useLoginUserLDAPEntryDN = _userConnection.AppConnection.UseLoginUserLDAPEntryDN;

            ldapUser.Dn         = useLoginUserLDAPEntryDN ? entry.DistinguishedName : attributeDefValue;
            ldapUser.ModifiedOn = GetEntryDateTimeAttributeValue(entry, _ldapEntryModifiedOnAttribute);
            return(ldapUser);
        }