Author: norman Date: Thu Oct 6 06:12:54 2011 New Revision: 1179514 URL: http://svn.apache.org/viewvc?rev=1179514&view=rev Log: more effective getUserByName(String name) in org.apache.james.user.ldap.ReadOnlyUsersLDAPRepository. Thanks to Peter Kvokacka for the patch. See JAMES-1313
Modified: james/server/trunk/ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java Modified: james/server/trunk/ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java URL: http://svn.apache.org/viewvc/james/server/trunk/ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java?rev=1179514&r1=1179513&r2=1179514&view=diff ============================================================================== --- james/server/trunk/ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java (original) +++ james/server/trunk/ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java Thu Oct 6 06:12:54 2011 @@ -351,14 +351,23 @@ public class ReadOnlyUsersLDAPRepository * Propagated by the underlying LDAP communication layer. */ private ReadOnlyLDAPUser buildUser(String userDN) throws NamingException { - ReadOnlyLDAPUser result; - - Attributes userAttributes = ldapConnection.getLdapContext().getAttributes(userDN); - Attribute userName = userAttributes.get(userIdAttribute); - - result = new ReadOnlyLDAPUser(userName.get().toString(), userDN, ldapHost); - - return result; + SearchControls sc = new SearchControls(); + sc.setSearchScope(SearchControls.OBJECT_SCOPE); + sc.setReturningAttributes(new String[] {userIdAttribute}); + sc.setCountLimit(1); + + NamingEnumeration<SearchResult> sr = ldapConnection.getLdapContext().search(userDN, "(objectClass=" + userObjectClass + ")", sc); + + if (!sr.hasMore()) + return null; + + Attributes userAttributes = sr.next().getAttributes(); + Attribute userName = userAttributes.get(userIdAttribute); + + if (!restriction.isActivated() || userInGroupsMembershipList(userDN, restriction.getGroupMembershipLists(ldapConnection))) + return new ReadOnlyLDAPUser(userName.get().toString(), userDN, ldapHost); + + return null; } /* @@ -424,22 +433,13 @@ public class ReadOnlyUsersLDAPRepository * org.apache.james.api.user.UsersRepository#getUserByName(java.lang.String) */ public User getUserByName(String name) throws UsersRepositoryException { - try { - Iterator<ReadOnlyLDAPUser> userIt = buildUserCollection(getValidUsers()).iterator(); - while (userIt.hasNext()) { - ReadOnlyLDAPUser u = userIt.next(); - if (u.getUserName().equals(name)) { - return u; - } - } - - } catch (NamingException e) { - log.error("Unable to retrieve user from ldap", e); - throw new UsersRepositoryException("Unable to retrieve user from ldap", e); - - } - return null; - + try { + return buildUser(userIdAttribute + "=" + name + "," + userBase); + } catch (NamingException e) { + log.error("Unable to retrieve user from ldap", e); + throw new UsersRepositoryException("Unable to retrieve user from ldap", e); + + } } /* --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org