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;
* <li>ldap.adminDN</li>
* <li>ldap.adminPassword</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.emailField -- default value is "mail".</li>
* <li>ldap.searchFilter -- the filter used to load the list of users. When defined, it
......@@ -130,6 +131,7 @@ public class LdapManager {
private int port;
private int readTimeout = -1;
private String usernameField;
private String usernameSuffix;
private String nameField;
private String emailField;
private String baseDN;
......@@ -208,6 +210,10 @@ public class LdapManager {
if (usernameField == null) {
usernameField = "uid";
}
usernameSuffix = properties.get("ldap.usernameSuffix");
if (usernameSuffix == null) {
usernameSuffix = "";
}
baseDN = properties.get("ldap.baseDN");
if (baseDN == null) {
baseDN = "";
......@@ -299,6 +305,7 @@ public class LdapManager {
buf.append("\t host: ").append(hosts).append("\n");
buf.append("\t port: ").append(port).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 alternateBaseDN: ").append(alternateBaseDN).append("\n");
buf.append("\t nameField: ").append(nameField).append("\n");
......@@ -590,6 +597,8 @@ public class LdapManager {
*/
public String findUserDN(String username, String baseDN) throws Exception {
boolean debug = Log.isDebugEnabled();
//Support for usernameSuffix
username = username + usernameSuffix;
if (debug) {
Log.debug("Trying to find a user's DN based on their username. " + usernameField + ": " + username
+ ", Base DN: " + baseDN + "...");
......@@ -810,6 +819,14 @@ public class LdapManager {
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.
* By default this is "uid".
......@@ -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
* this is "cn". Another common value is "displayName".
......
......@@ -321,6 +321,11 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
}
......@@ -329,6 +334,11 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID.
String username = (String) ((SearchResult) answer2.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
}
......@@ -354,12 +364,22 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
} else if (alternateBaseDN != null && answer2.hasMoreElements()) {
// Get the next userID.
String username = (String) ((SearchResult) answer2.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
} else {
......@@ -454,7 +474,7 @@ public class LdapUserProvider implements UserProvider {
}
List<String> usernames = new ArrayList<String>();
LdapContext ctx = null;
LdapContext ctx2 = null;
LdapContext ctx2 = null;
try {
ctx = manager.getContext(baseDN);
// Sort on username field.
......@@ -473,7 +493,13 @@ public class LdapUserProvider implements UserProvider {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
searchControls.setReturningAttributes(new String[] { manager.getUsernameField() });
String searchFilter = MessageFormat.format(manager.getSearchFilter(),"*");
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) {
filter.append("(|");
}
......@@ -484,6 +510,7 @@ public class LdapUserProvider implements UserProvider {
if (fields.size() > 1) {
filter.append(")");
}
filter.append(")");
NamingEnumeration answer = ctx.search("", filter.toString(), searchControls);
while (answer.hasMoreElements()) {
// Get the next userID.
......@@ -492,18 +519,23 @@ public class LdapUserProvider implements UserProvider {
// Escape username and add to results.
usernames.add(JID.escapeNode(username));
}
if (alternateBaseDN != null) {
ctx2 = manager.getContext(alternateBaseDN);
ctx2.setRequestControls(searchControl);
answer = ctx2.search("", filter.toString(), searchControls);
if (alternateBaseDN != null) {
ctx2 = manager.getContext(alternateBaseDN);
ctx2.setRequestControls(searchControl);
answer = ctx2.search("", filter.toString(), searchControls);
while (answer.hasMoreElements()) {
// Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
}
}
}
// Close the enumeration.
answer.close();
// If client-side sorting is enabled, sort.
......@@ -567,7 +599,13 @@ public class LdapUserProvider implements UserProvider {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
searchControls.setReturningAttributes(new String[] { manager.getUsernameField() });
String searchFilter = MessageFormat.format(manager.getSearchFilter(),"*");
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) {
filter.append("(|");
}
......@@ -578,22 +616,22 @@ public class LdapUserProvider implements UserProvider {
if (fields.size() > 1) {
filter.append(")");
}
filter.append(")");
// TODO: used paged results if supported by LDAP server.
NamingEnumeration answer = ctx.search("", filter.toString(), searchControls);
NamingEnumeration answer2 = null;
if(alternateBaseDN != null) {
NamingEnumeration answer2 = null;
if(alternateBaseDN != null) {
ctx2 = manager.getContext(alternateBaseDN);
ctx2.setRequestControls(searchControl);
answer2 = ctx2.search("", filter.toString(), searchControls);
}
}
for (int i=0; i < startIndex; i++) {
if (answer.hasMoreElements()) {
answer.next();
}
else if (alternateBaseDN != null && answer2.hasMoreElements())
{
answer2.next();
}
else if (alternateBaseDN != null && answer2.hasMoreElements()) {
answer2.next();
}
else {
return Collections.emptyList();
}
......@@ -604,24 +642,33 @@ public class LdapUserProvider implements UserProvider {
// Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
}
else if (alternateBaseDN != null && answer2.hasMoreElements())
{
else if (alternateBaseDN != null && answer2.hasMoreElements()) {
// Get the next userID.
String username = (String)((SearchResult)answer2.next()).getAttributes().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.
usernames.add(JID.escapeNode(username));
}
}
else {
break;
}
}
// Close the enumeration.
answer.close();
// If client-side sorting is enabled, sort.
if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting"))) {
Collections.sort(usernames);
......
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