Change search object to use constructor injection (SEC-165) .

This commit is contained in:
Luke Taylor 2006-02-03 19:53:08 +00:00
parent 436fcde10b
commit 842ad929a4
3 changed files with 51 additions and 40 deletions

View File

@ -83,6 +83,25 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
//~ Methods ================================================================ //~ Methods ================================================================
public FilterBasedLdapUserSearch(String searchBase,
String searchFilter,
InitialDirContextFactory initialDirContextFactory) {
Assert.notNull(initialDirContextFactory, "initialDirContextFactory must not be null");
Assert.notNull(searchFilter, "searchFilter must not be null.");
Assert.notNull(searchBase, "searchBase must not be null (an empty string is acceptable).");
this.searchFilter = searchFilter;
this.initialDirContextFactory = initialDirContextFactory;
this.searchBase = searchBase;
if(searchBase.length() == 0) {
logger.info("SearchBase not set. Searches will be performed from the root: " +
initialDirContextFactory.getRootDn());
}
}
//~ Methods ================================================================
/** /**
* Return the LdapUserInfo containing the user's information, or null if * Return the LdapUserInfo containing the user's information, or null if
* no SearchResult is found. * no SearchResult is found.
@ -95,6 +114,11 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
ctls.setTimeLimit( searchTimeLimit ); ctls.setTimeLimit( searchTimeLimit );
ctls.setSearchScope( searchScope ); ctls.setSearchScope( searchScope );
if (logger.isDebugEnabled()) {
logger.debug("Searching for user '" + username + "', in context " + ctx +
", with user search " + this.toString());
}
try { try {
String[] args = new String[] { LdapUtils.escapeNameForFilter(username) }; String[] args = new String[] { LdapUtils.escapeNameForFilter(username) };
@ -106,13 +130,13 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
SearchResult searchResult = (SearchResult)results.next(); SearchResult searchResult = (SearchResult)results.next();
if(results.hasMore()) { if (results.hasMore()) {
throw new BadCredentialsException("Expected a single user but search returned multiple results"); throw new BadCredentialsException("Expected a single user but search returned multiple results");
} }
StringBuffer userDn = new StringBuffer(searchResult.getName()); StringBuffer userDn = new StringBuffer(searchResult.getName());
if(searchBase.length() > 0) { if (searchBase.length() > 0) {
userDn.append(","); userDn.append(",");
userDn.append(searchBase); userDn.append(searchBase);
} }
@ -129,24 +153,6 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
} }
} }
public void afterPropertiesSet() throws Exception {
Assert.notNull(initialDirContextFactory, "initialDirContextFactory must be set");
Assert.notNull(searchFilter, "searchFilter must be set.");
if(searchBase.equals("")) {
logger.info("No search base DN supplied. Search will be performed from the root: " +
initialDirContextFactory.getRootDn());
}
}
public void setInitialDirContextFactory(InitialDirContextFactory initialDirContextFactory) {
this.initialDirContextFactory = initialDirContextFactory;
}
public void setSearchFilter(String searchFilter) {
this.searchFilter = searchFilter;
}
public void setSearchSubtree(boolean searchSubtree) { public void setSearchSubtree(boolean searchSubtree) {
// this.searchSubtree = searchSubtree; // this.searchSubtree = searchSubtree;
this.searchScope = searchSubtree ? this.searchScope = searchSubtree ?
@ -157,7 +163,15 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
this.searchTimeLimit = searchTimeLimit; this.searchTimeLimit = searchTimeLimit;
} }
public void setSearchBase(String searchBase) { public String toString() {
this.searchBase = searchBase; StringBuffer sb = new StringBuffer();
sb.append("[ searchFilter: '").append(searchFilter).append("', ");
sb.append("searchBase: '").append(searchBase).append("'");
sb.append(", scope: ").append(searchScope ==
SearchControls.SUBTREE_SCOPE ? "subtree" : "single-level, ");
sb.append("searchTimeLimit: ").append(searchTimeLimit).append(" ]");
return sb.toString();
} }
} }

View File

@ -45,7 +45,6 @@ public class DefaultInitialDirContextFactoryTests extends AbstractLdapServerTest
assertEquals("dc=acegisecurity,dc=org", idf.getRootDn()); assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
} }
public void testConnectionFailure() throws Exception { public void testConnectionFailure() throws Exception {
// Use the wrong port // Use the wrong port
idf = new DefaultInitialDirContextFactory("ldap://localhost:60389"); idf = new DefaultInitialDirContextFactory("ldap://localhost:60389");

View File

@ -3,7 +3,6 @@ package org.acegisecurity.providers.ldap.search;
import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase; import org.acegisecurity.providers.ldap.AbstractLdapServerTestCase;
import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory; import org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory;
import org.acegisecurity.providers.ldap.LdapUserInfo; import org.acegisecurity.providers.ldap.LdapUserInfo;
import org.acegisecurity.providers.ldap.search.FilterBasedLdapUserSearch;
import org.acegisecurity.userdetails.UsernameNotFoundException; import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.BadCredentialsException; import org.acegisecurity.BadCredentialsException;
@ -15,7 +14,6 @@ import org.acegisecurity.BadCredentialsException;
*/ */
public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase { public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
private DefaultInitialDirContextFactory dirCtxFactory; private DefaultInitialDirContextFactory dirCtxFactory;
private FilterBasedLdapUserSearch locator;
public void setUp() throws Exception { public void setUp() throws Exception {
dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL); dirCtxFactory = new DefaultInitialDirContextFactory(PROVIDER_URL);
@ -23,10 +21,6 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
dirCtxFactory.setExtraEnvVars(EXTRA_ENV); dirCtxFactory.setExtraEnvVars(EXTRA_ENV);
dirCtxFactory.setManagerDn(MANAGER_USER); dirCtxFactory.setManagerDn(MANAGER_USER);
dirCtxFactory.setManagerPassword(MANAGER_PASSWORD); dirCtxFactory.setManagerPassword(MANAGER_PASSWORD);
locator = new FilterBasedLdapUserSearch();
locator.setSearchSubtree(false);
locator.setSearchTimeLimit(0);
locator.setInitialDirContextFactory(dirCtxFactory);
} }
public FilterBasedLdapUserSearchTests(String string) { public FilterBasedLdapUserSearchTests(String string) {
@ -38,26 +32,28 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
} }
public void testBasicSearch() throws Exception { public void testBasicSearch() throws Exception {
locator.setSearchBase("ou=people"); FilterBasedLdapUserSearch locator =
locator.setSearchFilter("(uid={0})"); new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
locator.afterPropertiesSet();
LdapUserInfo bob = locator.searchForUser("bob"); LdapUserInfo bob = locator.searchForUser("bob");
locator.setSearchSubtree(false);
locator.setSearchTimeLimit(0);
// name is wrong with embedded apacheDS // name is wrong with embedded apacheDS
// assertEquals("uid=bob,ou=people,"+ROOT_DN, bob.getDn()); // assertEquals("uid=bob,ou=people,"+ROOT_DN, bob.getDn());
} }
public void testSubTreeSearchSucceeds() throws Exception { public void testSubTreeSearchSucceeds() throws Exception {
// Don't set the searchBase, so search from the root. // Don't set the searchBase, so search from the root.
locator.setSearchFilter("(cn={0})"); FilterBasedLdapUserSearch locator =
new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory);
locator.setSearchSubtree(true); locator.setSearchSubtree(true);
locator.afterPropertiesSet();
LdapUserInfo ben = locator.searchForUser("Ben Alex"); LdapUserInfo ben = locator.searchForUser("Ben Alex");
// assertEquals("uid=ben,ou=people,"+ROOT_DN, bob.getDn()); // assertEquals("uid=ben,ou=people,"+ROOT_DN, bob.getDn());
} }
public void testSearchForInvalidUserFails() { public void testSearchForInvalidUserFails() {
locator.setSearchBase("ou=people"); FilterBasedLdapUserSearch locator =
locator.setSearchFilter("(uid={0})"); new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory);
try { try {
locator.searchForUser("Joe"); locator.searchForUser("Joe");
@ -67,8 +63,8 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
} }
public void testFailsOnMultipleMatches() { public void testFailsOnMultipleMatches() {
locator.setSearchBase("ou=people"); FilterBasedLdapUserSearch locator =
locator.setSearchFilter("(cn=*)"); new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory);
try { try {
locator.searchForUser("Ignored"); locator.searchForUser("Ignored");
@ -80,8 +76,10 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
// Try some funny business with filters. // Try some funny business with filters.
public void testExtraFilterPartToExcludeBob() throws Exception { public void testExtraFilterPartToExcludeBob() throws Exception {
locator.setSearchBase("ou=people"); FilterBasedLdapUserSearch locator =
locator.setSearchFilter("(&(cn=*)(!(|(uid={0})(uid=marissa))))"); new FilterBasedLdapUserSearch("ou=people",
"(&(cn=*)(!(|(uid={0})(uid=marissa))))",
dirCtxFactory);
// Search for bob, get back ben... // Search for bob, get back ben...
LdapUserInfo ben = locator.searchForUser("bob"); LdapUserInfo ben = locator.searchForUser("bob");