This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit bfd78ca918f98f613452d948ba672f724ad4ee67 Author: Benoit Tellier <[email protected]> AuthorDate: Wed Jun 9 11:21:11 2021 +0700 JAMES-3594 LDAP user listing: avoid extra requests for each users Because we needed a conversion DN <-> username. --- .../james/user/ldap/ReadOnlyLDAPUsersDAO.java | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyLDAPUsersDAO.java b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyLDAPUsersDAO.java index 9a4d707..f1c1819 100644 --- a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyLDAPUsersDAO.java +++ b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyLDAPUsersDAO.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; import javax.annotation.PreDestroy; import javax.inject.Inject; @@ -45,6 +46,7 @@ import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; import com.github.steveash.guavate.Guavate; +import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.Entry; import com.unboundid.ldap.sdk.Filter; import com.unboundid.ldap.sdk.LDAPConnection; @@ -169,7 +171,7 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { return result; } - private Set<String> getAllUsersFromLDAP() throws LDAPException { + private Set<String> getAllUsersDNFromLDAP() throws LDAPException { SearchRequest searchRequest = new SearchRequest(ldapConfiguration.getUserBase(), SearchScope.SUB, createFilter(), @@ -183,6 +185,21 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { .collect(Guavate.toImmutableSet()); } + private Stream<Username> getAllUsernamesFromLDAP() throws LDAPException { + SearchRequest searchRequest = new SearchRequest(ldapConfiguration.getUserBase(), + SearchScope.SUB, + createFilter(), + ldapConfiguration.getUserIdAttribute()); + + SearchResult searchResult = ldapConnectionPool.search(searchRequest); + + return searchResult.getSearchEntries() + .stream() + .flatMap(entry -> Optional.ofNullable(entry.getAttribute(ldapConfiguration.getUserIdAttribute())).stream()) + .map(Attribute::getValue) + .map(Username::of); + } + /** * Extract the user attributes for the given collection of userDNs, and * encapsulates the user list as a collection of {@link ReadOnlyLDAPUser}s. @@ -315,6 +332,10 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { } private Iterator<Username> doList() throws LDAPException { + if (!ldapConfiguration.getRestriction().isActivated()) { + return getAllUsernamesFromLDAP().iterator(); + } + return buildUserCollection(getValidUsers()) .stream() .map(ReadOnlyLDAPUser::getUserName) @@ -322,7 +343,7 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { } private Collection<String> getValidUsers() throws LDAPException { - Set<String> userDNs = getAllUsersFromLDAP(); + Set<String> userDNs = getAllUsersDNFromLDAP(); Collection<String> validUserDNs; if (ldapConfiguration.getRestriction().isActivated()) { final LDAPConnection connection = ldapConnectionPool.getConnection(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
