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 0af05751acbf015c44d937c749a0c90bffce056e Author: Benoit Tellier <[email protected]> AuthorDate: Wed Jun 9 09:11:09 2021 +0700 JAMES-3594 Use Filter instead of search templates --- server/data/data-ldap/pom.xml | 4 ---- .../apache/james/user/ldap/ReadOnlyLDAPUsersDAO.java | 20 +++++++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/server/data/data-ldap/pom.xml b/server/data/data-ldap/pom.xml index aec06a1..a2e2228 100644 --- a/server/data/data-ldap/pom.xml +++ b/server/data/data-ldap/pom.xml @@ -82,10 +82,6 @@ <artifactId>commons-configuration2</artifactId> </dependency> <dependency> - <groupId>org.apache.directory.api</groupId> - <artifactId>api-ldap-model</artifactId> - </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> 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 d400aed..772d410 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 @@ -61,7 +61,6 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { private static final Logger LOGGER = LoggerFactory.getLogger(ReadOnlyLDAPUsersDAO.class); private LdapRepositoryConfiguration ldapConfiguration; - private String filterTemplate; private LDAPConnectionPool ldapConnectionPool; @Inject @@ -103,7 +102,6 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { + ldapConfiguration.getConnectionTimeout() + '\n' + "readTimeout: " + ldapConfiguration.getReadTimeout() + '\n' + "maxRetries: " + ldapConfiguration.getMaxRetries() + '\n'); } - filterTemplate = "(&({0}={1})(objectClass={2})" + StringUtils.defaultString(ldapConfiguration.getFilter(), "") + ")"; LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions(); connectionOptions.setConnectTimeoutMillis(ldapConfiguration.getConnectionTimeout()); @@ -120,6 +118,16 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { ldapConnectionPool.close(); } + private Filter createFilter(String username) { + return Optional.ofNullable(ldapConfiguration.getFilter()) + .map(Throwing.function(userFilter -> Filter.createANDFilter( + Filter.createEqualityFilter("objectClass", ldapConfiguration.getUserObjectClass()), + Filter.createEqualityFilter(ldapConfiguration.getUserIdAttribute(), username), + Filter.create(userFilter)))) + .orElseGet(() -> Filter.createANDFilter( + Filter.createEqualityFilter("objectClass", ldapConfiguration.getUserObjectClass()), + Filter.createEqualityFilter(ldapConfiguration.getUserIdAttribute(), username))); + } /** * Indicates if the user with the specified DN can be found in the group @@ -192,15 +200,9 @@ public class ReadOnlyLDAPUsersDAO implements UsersDAO, Configurable { private ReadOnlyLDAPUser searchAndBuildUser(Username name) throws LDAPException { LDAPConnection connection = ldapConnectionPool.getConnection(); try { - String sanitizedFilter = FilterEncoder.format( - filterTemplate, - ldapConfiguration.getUserIdAttribute(), - name.asString(), - ldapConfiguration.getUserObjectClass()); - SearchResult searchResult = connection.search(ldapConfiguration.getUserBase(), SearchScope.SUB, - sanitizedFilter, + createFilter(name.asString()), ldapConfiguration.getUserIdAttribute()); SearchResultEntry result = searchResult.getSearchEntries() --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
