Commit 14ac1e1f authored by Jay Kline's avatar Jay Kline Committed by jay

usernameSuffix support (JM-1078)



git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8580 b35dd754-fafc-0310-a699-88a17e54d16e
parent 8da314d2
...@@ -41,6 +41,7 @@ import java.util.regex.Pattern; ...@@ -41,6 +41,7 @@ import java.util.regex.Pattern;
* <li>ldap.adminDN</li> * <li>ldap.adminDN</li>
* <li>ldap.adminPassword</li> * <li>ldap.adminPassword</li>
* <li>ldap.usernameField -- default value is "uid".</li> * <li>ldap.usernameField -- default value is "uid".</li>
* <li>ldap.usernameSuffix -- default value is "".</li>
* <li>ldap.nameField -- default value is "cn".</li> * <li>ldap.nameField -- default value is "cn".</li>
* <li>ldap.emailField -- default value is "mail".</li> * <li>ldap.emailField -- default value is "mail".</li>
* <li>ldap.searchFilter -- the filter used to load the list of users. When defined, it * <li>ldap.searchFilter -- the filter used to load the list of users. When defined, it
...@@ -130,6 +131,7 @@ public class LdapManager { ...@@ -130,6 +131,7 @@ public class LdapManager {
private int port; private int port;
private int readTimeout = -1; private int readTimeout = -1;
private String usernameField; private String usernameField;
private String usernameSuffix;
private String nameField; private String nameField;
private String emailField; private String emailField;
private String baseDN; private String baseDN;
...@@ -208,6 +210,10 @@ public class LdapManager { ...@@ -208,6 +210,10 @@ public class LdapManager {
if (usernameField == null) { if (usernameField == null) {
usernameField = "uid"; usernameField = "uid";
} }
usernameSuffix = properties.get("ldap.usernameSuffix");
if (usernameSuffix == null) {
usernameSuffix = "";
}
baseDN = properties.get("ldap.baseDN"); baseDN = properties.get("ldap.baseDN");
if (baseDN == null) { if (baseDN == null) {
baseDN = ""; baseDN = "";
...@@ -299,6 +305,7 @@ public class LdapManager { ...@@ -299,6 +305,7 @@ public class LdapManager {
buf.append("\t host: ").append(hosts).append("\n"); buf.append("\t host: ").append(hosts).append("\n");
buf.append("\t port: ").append(port).append("\n"); buf.append("\t port: ").append(port).append("\n");
buf.append("\t usernamefield: ").append(usernameField).append("\n"); buf.append("\t usernamefield: ").append(usernameField).append("\n");
buf.append("\t usernameSuffix: ").append(usernameSuffix).append("\n");
buf.append("\t baseDN: ").append(baseDN).append("\n"); buf.append("\t baseDN: ").append(baseDN).append("\n");
buf.append("\t alternateBaseDN: ").append(alternateBaseDN).append("\n"); buf.append("\t alternateBaseDN: ").append(alternateBaseDN).append("\n");
buf.append("\t nameField: ").append(nameField).append("\n"); buf.append("\t nameField: ").append(nameField).append("\n");
...@@ -590,6 +597,8 @@ public class LdapManager { ...@@ -590,6 +597,8 @@ public class LdapManager {
*/ */
public String findUserDN(String username, String baseDN) throws Exception { public String findUserDN(String username, String baseDN) throws Exception {
boolean debug = Log.isDebugEnabled(); boolean debug = Log.isDebugEnabled();
//Support for usernameSuffix
username = username + usernameSuffix;
if (debug) { if (debug) {
Log.debug("Trying to find a user's DN based on their username. " + usernameField + ": " + username Log.debug("Trying to find a user's DN based on their username. " + usernameField + ": " + username
+ ", Base DN: " + baseDN + "..."); + ", Base DN: " + baseDN + "...");
...@@ -810,6 +819,14 @@ public class LdapManager { ...@@ -810,6 +819,14 @@ public class LdapManager {
return usernameField; return usernameField;
} }
/**
* Returns the suffix appended to the username when LDAP lookups are performed.
* By default this is "".
*/
public String getUsernameSuffix() {
return usernameSuffix;
}
/** /**
* Sets the LDAP field name that the username lookup will be performed on. * Sets the LDAP field name that the username lookup will be performed on.
* By default this is "uid". * By default this is "uid".
...@@ -828,6 +845,22 @@ public class LdapManager { ...@@ -828,6 +845,22 @@ public class LdapManager {
} }
} }
/**
* Set the suffix appended to the username whenever LDAP lookups are performed.
*
* @param usernameSuffix the String to append to usernames for lookups
*/
public void setUsernameSuffix(String usernameSuffix) {
this.usernameSuffix = usernameSuffix;
if (usernameSuffix == null) {
properties.remove("ldap.usernameSuffix");
this.usernameSuffix = "";
}
else {
properties.put("ldap.usernameSuffix", usernameSuffix);
}
}
/** /**
* Returns the LDAP field name that the user's name is stored in. By default * Returns the LDAP field name that the user's name is stored in. By default
* this is "cn". Another common value is "displayName". * this is "cn". Another common value is "displayName".
......
...@@ -321,6 +321,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -321,6 +321,11 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().get( String username = (String)((SearchResult)answer.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
...@@ -329,6 +334,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -329,6 +334,11 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID. // Get the next userID.
String username = (String) ((SearchResult) answer2.next()).getAttributes().get( String username = (String) ((SearchResult) answer2.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
...@@ -354,12 +364,22 @@ public class LdapUserProvider implements UserProvider { ...@@ -354,12 +364,22 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().get( String username = (String)((SearchResult)answer.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} else if (alternateBaseDN != null && answer2.hasMoreElements()) { } else if (alternateBaseDN != null && answer2.hasMoreElements()) {
// Get the next userID. // Get the next userID.
String username = (String) ((SearchResult) answer2.next()).getAttributes().get( String username = (String) ((SearchResult) answer2.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} else { } else {
...@@ -473,7 +493,13 @@ public class LdapUserProvider implements UserProvider { ...@@ -473,7 +493,13 @@ public class LdapUserProvider implements UserProvider {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE); searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} }
searchControls.setReturningAttributes(new String[] { manager.getUsernameField() }); searchControls.setReturningAttributes(new String[] { manager.getUsernameField() });
String searchFilter = MessageFormat.format(manager.getSearchFilter(),"*");
StringBuilder filter = new StringBuilder(); StringBuilder filter = new StringBuilder();
//Add the global search filter so only those users the directory administrator wants to include
//are returned from the directory
filter.append("(&(");
filter.append(searchFilter);
filter.append(")");
if (fields.size() > 1) { if (fields.size() > 1) {
filter.append("(|"); filter.append("(|");
} }
...@@ -484,6 +510,7 @@ public class LdapUserProvider implements UserProvider { ...@@ -484,6 +510,7 @@ public class LdapUserProvider implements UserProvider {
if (fields.size() > 1) { if (fields.size() > 1) {
filter.append(")"); filter.append(")");
} }
filter.append(")");
NamingEnumeration answer = ctx.search("", filter.toString(), searchControls); NamingEnumeration answer = ctx.search("", filter.toString(), searchControls);
while (answer.hasMoreElements()) { while (answer.hasMoreElements()) {
// Get the next userID. // Get the next userID.
...@@ -500,6 +527,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -500,6 +527,11 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().get( String username = (String)((SearchResult)answer.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
...@@ -567,7 +599,13 @@ public class LdapUserProvider implements UserProvider { ...@@ -567,7 +599,13 @@ public class LdapUserProvider implements UserProvider {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE); searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} }
searchControls.setReturningAttributes(new String[] { manager.getUsernameField() }); searchControls.setReturningAttributes(new String[] { manager.getUsernameField() });
String searchFilter = MessageFormat.format(manager.getSearchFilter(),"*");
StringBuilder filter = new StringBuilder(); StringBuilder filter = new StringBuilder();
//Add the global search filter so only those users the directory administrator wants to include
//are returned from the directory
filter.append("(&(");
filter.append(searchFilter);
filter.append(")");
if (fields.size() > 1) { if (fields.size() > 1) {
filter.append("(|"); filter.append("(|");
} }
...@@ -578,6 +616,7 @@ public class LdapUserProvider implements UserProvider { ...@@ -578,6 +616,7 @@ public class LdapUserProvider implements UserProvider {
if (fields.size() > 1) { if (fields.size() > 1) {
filter.append(")"); filter.append(")");
} }
filter.append(")");
// TODO: used paged results if supported by LDAP server. // TODO: used paged results if supported by LDAP server.
NamingEnumeration answer = ctx.search("", filter.toString(), searchControls); NamingEnumeration answer = ctx.search("", filter.toString(), searchControls);
NamingEnumeration answer2 = null; NamingEnumeration answer2 = null;
...@@ -590,8 +629,7 @@ public class LdapUserProvider implements UserProvider { ...@@ -590,8 +629,7 @@ public class LdapUserProvider implements UserProvider {
if (answer.hasMoreElements()) { if (answer.hasMoreElements()) {
answer.next(); answer.next();
} }
else if (alternateBaseDN != null && answer2.hasMoreElements()) else if (alternateBaseDN != null && answer2.hasMoreElements()) {
{
answer2.next(); answer2.next();
} }
else { else {
...@@ -604,14 +642,23 @@ public class LdapUserProvider implements UserProvider { ...@@ -604,14 +642,23 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().get( String username = (String)((SearchResult)answer.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
else if (alternateBaseDN != null && answer2.hasMoreElements()) else if (alternateBaseDN != null && answer2.hasMoreElements()) {
{
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer2.next()).getAttributes().get( String username = (String)((SearchResult)answer2.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
// Remove usernameSuffix if set
String suffix = manager.getUsernameSuffix();
if(suffix.length() > 0 && username.endsWith(suffix)) {
username = username.substring(0,username.length()-suffix.length());
}
// Escape username and add to results. // Escape username and add to results.
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment