Commit dc23a135 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Do not execute an LDAP query when not using posix mode and username can be obtained from DN. JM-607

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3620 b35dd754-fafc-0310-a699-88a17e54d16e
parent 0006621b
...@@ -27,6 +27,8 @@ import javax.naming.directory.*; ...@@ -27,6 +27,8 @@ import javax.naming.directory.*;
import javax.naming.ldap.LdapName; import javax.naming.ldap.LdapName;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* LDAP implementation of the GroupProvider interface. All data in the directory is treated as read-only so any set * LDAP implementation of the GroupProvider interface. All data in the directory is treated as read-only so any set
...@@ -336,6 +338,12 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -336,6 +338,12 @@ public class LdapGroupProvider implements GroupProvider {
String userSearchFilter = MessageFormat.format(manager.getSearchFilter(), "*"); String userSearchFilter = MessageFormat.format(manager.getSearchFilter(), "*");
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
String serverName = server.getServerInfo().getName(); String serverName = server.getServerInfo().getName();
// Build 3 groups.
// group 1: uid=
// group 2: rest of the text until first comma
// group 3: rest of the text
Pattern pattern =
Pattern.compile("(?i)(^" + manager.getUsernameField() + "=)([^,]+)(.+)");
while (answer.hasMoreElements()) { while (answer.hasMoreElements()) {
String name = ""; String name = "";
...@@ -354,25 +362,34 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -354,25 +362,34 @@ public class LdapGroupProvider implements GroupProvider {
Attribute member = a.get(manager.getGroupMemberField()); Attribute member = a.get(manager.getGroupMemberField());
NamingEnumeration ne = member.getAll(); NamingEnumeration ne = member.getAll();
while (ne.hasMore()) { while (ne.hasMore()) {
String username = (String)ne.next(); String username = (String) ne.next();
if (!manager.isPosixMode()) { //userName is full dn if not posix if (!manager.isPosixMode()) { //userName is full dn if not posix
try { try {
// Get the CN using LDAP // LdapName will not generate spaces around an '='
LdapName ldapname = new LdapName(username); // (according to the docs)
String ldapcn = ldapname.get(ldapname.size() - 1); Matcher matcher = pattern.matcher(username);
if (matcher.matches() && matcher.groupCount() == 3) {
// We have to do a new search to find the username field // The username is in the DN, no additional search needed
username = matcher.group(2);
String combinedFilter =
"(&(" + ldapcn + ")" + userSearchFilter + ")";
NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls);
if (usrAnswer.hasMoreElements()) {
username = (String)((SearchResult)usrAnswer.next())
.getAttributes().get(
manager.getUsernameField()).get();
} }
else { else {
throw new UserNotFoundException(); // We have to do a new search to find the username field
// Get the CN using LDAP
LdapName ldapname = new LdapName(username);
String ldapcn = ldapname.get(ldapname.size() - 1);
String combinedFilter =
"(&(" + ldapcn + ")" + userSearchFilter + ")";
NamingEnumeration usrAnswer =
ctx.search("", combinedFilter, ctrls);
if (usrAnswer.hasMoreElements()) {
username = (String) ((SearchResult) usrAnswer.next())
.getAttributes().get(
manager.getUsernameField()).get();
}
else {
throw new UserNotFoundException();
}
} }
} }
catch (Exception e) { catch (Exception e) {
......
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