Commit 18cc4f4a authored by Matt Tucker's avatar Matt Tucker Committed by matt

Bug fixes from Greg Ferguson and Cameron Moore:

* All searches are done using the SUBTREE_SCOPE instead of the default, OBJECT_SCOPE. 
* The class now correctly uses the ldap.groupNameField section of the XML config file. 
* Correctly sorts groups and members within groups. 
* Correctly handle special characters, commas, within the CN (common name).


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1687 b35dd754-fafc-0310-a699-88a17e54d16e
parent 6505c538
...@@ -17,27 +17,24 @@ import org.jivesoftware.messenger.group.*; ...@@ -17,27 +17,24 @@ import org.jivesoftware.messenger.group.*;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.TreeMap;
import java.util.List; import java.util.TreeSet;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.StringTokenizer;
import java.text.MessageFormat; import java.text.MessageFormat;
import javax.naming.Context; import javax.naming.Name;
import javax.naming.NamingEnumeration; import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*; import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext; import javax.naming.ldap.LdapName;
import javax.naming.ldap.LdapContext;
/** /**
* LDAP implementation of the GroupProvider interface. All data in the directory is * LDAP implementation of the GroupProvider interface. All data in the directory is
* treated as read-only so any set operations will result in an exception. * treated as read-only so any set operations will result in an exception.
* *
* @author Greg Ferguson * @author Greg Ferguson and Cameron Moore
*/ */
public class LdapGroupProvider implements GroupProvider public class LdapGroupProvider implements GroupProvider
{ {
private LdapManager manager; private LdapManager manager;
...@@ -145,7 +142,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -145,7 +142,7 @@ public class LdapGroupProvider implements GroupProvider
} }
/** /**
* Will return the current number of groups in the * Will return the current number of groups in the
* LDAP server. * LDAP server.
* *
* @return the number of groups in the LDAP server. * @return the number of groups in the LDAP server.
...@@ -173,8 +170,10 @@ public class LdapGroupProvider implements GroupProvider ...@@ -173,8 +170,10 @@ public class LdapGroupProvider implements GroupProvider
} }
// Search for the dn based on the groupname. // Search for the dn based on the groupname.
answer = ctx.search("",searchFilter,new SearchControls()); SearchControls ctrls = new SearchControls();
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
answer = ctx.search("",searchFilter,ctrls);
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("... search finished"); Log.debug("... search finished");
} }
...@@ -184,7 +183,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -184,7 +183,7 @@ public class LdapGroupProvider implements GroupProvider
if (manager.isDebugEnabled()) if (manager.isDebugEnabled())
Log.debug("Error while searching for groups.",e); Log.debug("Error while searching for groups.",e);
} }
try try
{ {
while (answer.hasMoreElements()) while (answer.hasMoreElements())
{ {
...@@ -211,9 +210,9 @@ public class LdapGroupProvider implements GroupProvider ...@@ -211,9 +210,9 @@ public class LdapGroupProvider implements GroupProvider
} }
/** /**
* Will return a collecion of groups in the system * Will return a collecion of groups in the system
* based on the start index and end index. Useful when * based on the start index and end index. Useful when
* displaying a certain number of groups per page * displaying a certain number of groups per page
* on a webpage. * on a webpage.
* *
* @param start starting index * @param start starting index
...@@ -237,7 +236,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -237,7 +236,7 @@ public class LdapGroupProvider implements GroupProvider
} }
/** /**
* Will return a collection of groups in the * Will return a collection of groups in the
* system that the user provided belongs to. * system that the user provided belongs to.
* *
* @param user a user * @param user a user
...@@ -258,7 +257,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -258,7 +257,7 @@ public class LdapGroupProvider implements GroupProvider
return new ArrayList<Group>(); return new ArrayList<Group>();
} }
} }
String filter = MessageFormat.format(manager.getGroupSearchFilter(),username); String filter = MessageFormat.format(manager.getGroupSearchFilter(),username);
return getGroupBasedOnFilter(filter); return getGroupBasedOnFilter(filter);
} }
...@@ -301,7 +300,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -301,7 +300,7 @@ public class LdapGroupProvider implements GroupProvider
* @param username Name of a user. * @param username Name of a user.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void deleteMember(String groupName, String username) public void deleteMember(String groupName, String username)
throws UnsupportedOperationException throws UnsupportedOperationException
{ {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
...@@ -313,13 +312,13 @@ public class LdapGroupProvider implements GroupProvider ...@@ -313,13 +312,13 @@ public class LdapGroupProvider implements GroupProvider
* *
* @return true always because all LDAP functions are read only. * @return true always because all LDAP functions are read only.
*/ */
public boolean isReadOnly() public boolean isReadOnly()
{ {
return true; return true;
} }
/** /**
* An auxilary method used to perform LDAP queries based on a * An auxilary method used to perform LDAP queries based on a
* provided LDAP search filter. * provided LDAP search filter.
* *
* @return a collection of groups. * @return a collection of groups.
...@@ -327,7 +326,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -327,7 +326,7 @@ public class LdapGroupProvider implements GroupProvider
*/ */
public Collection<Group> getGroupBasedOnFilter (String searchFilter) public Collection<Group> getGroupBasedOnFilter (String searchFilter)
{ {
ArrayList<Group> groups = new ArrayList<Group>(); TreeMap<String,Group> groups = new TreeMap<String,Group>();
boolean debug = Log.isDebugEnabled(); boolean debug = Log.isDebugEnabled();
if (debug) { if (debug) {
Log.debug("Trying to find all groups in the system."); Log.debug("Trying to find all groups in the system.");
...@@ -344,10 +343,11 @@ public class LdapGroupProvider implements GroupProvider ...@@ -344,10 +343,11 @@ public class LdapGroupProvider implements GroupProvider
// Search for the dn based on the groupname. // Search for the dn based on the groupname.
SearchControls searchControls = new SearchControls(); SearchControls searchControls = new SearchControls();
String returnedAtts[]= { manager.getNameField(), String returnedAtts[]= { manager.getGroupNameField(),
manager.getGroupDescriptionField(), manager.getGroupDescriptionField(),
manager.getGroupMemberField() }; manager.getGroupMemberField() };
searchControls.setReturningAttributes(returnedAtts); searchControls.setReturningAttributes(returnedAtts);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
answer = ctx.search("",searchFilter,searchControls); answer = ctx.search("",searchFilter,searchControls);
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
...@@ -359,9 +359,13 @@ public class LdapGroupProvider implements GroupProvider ...@@ -359,9 +359,13 @@ public class LdapGroupProvider implements GroupProvider
{ {
if (manager.isDebugEnabled()) if (manager.isDebugEnabled())
Log.debug("Error while searching for groups.",e); Log.debug("Error while searching for groups.",e);
return groups; return groups.values();
} }
SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes( new String[]{manager.getUsernameField()} );
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
while (answer.hasMoreElements()) while (answer.hasMoreElements())
{ {
String name = ""; String name = "";
...@@ -371,14 +375,14 @@ public class LdapGroupProvider implements GroupProvider ...@@ -371,14 +375,14 @@ public class LdapGroupProvider implements GroupProvider
String description; String description;
try try
{ {
name = ((String)((a.get(manager.getNameField())).get())); name = ((String)((a.get(manager.getGroupNameField())).get()));
description = ((String)((a.get(manager.getGroupDescriptionField())).get())); description = ((String)((a.get(manager.getGroupDescriptionField())).get()));
} }
catch (Exception e) catch (Exception e)
{ {
description = ""; description = "";
} }
ArrayList<String> members = new ArrayList<String>(); TreeSet<String> members = new TreeSet<String>();
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())
...@@ -386,16 +390,30 @@ public class LdapGroupProvider implements GroupProvider ...@@ -386,16 +390,30 @@ public class LdapGroupProvider implements GroupProvider
String userName = (String)ne.next(); String userName = (String)ne.next();
if (!manager.getPosixEnabled()) if (!manager.getPosixEnabled())
{ //userName is full dn if not posix { //userName is full dn if not posix
StringTokenizer st = new StringTokenizer(userName,","); try
Attributes attrs = ctx.getAttributes(st.nextToken(), {
new String[]{manager.getUsernameField()}); // Get the CN using LDAP
Attribute a1 = attrs.get(manager.getUsernameField()); Name ldapname = new LdapName(userName);
if (a1 != null) String ldapcn = ldapname.get(ldapname.size()-1);
userName = (String)a1.get();
// We have to do a new search to find the username field
NamingEnumeration usrAnswer = ctx.search("",ldapcn,ctrls);
if (usrAnswer.hasMoreElements())
{
userName = (String)((SearchResult)usrAnswer.next()).getAttributes().get(
manager.getUsernameField()).get();
}
}
catch (Exception e)
{
if (manager.isDebugEnabled())
Log.debug("Error populating user with DN: "+userName,e);
}
} }
try try
{ {
User user = userManager.getUser(userName); User user = userManager.getUser(JID.escapeNode(userName));
members.add(user.getUsername()); members.add(user.getUsername());
} }
catch (UserNotFoundException e) catch (UserNotFoundException e)
...@@ -404,8 +422,10 @@ public class LdapGroupProvider implements GroupProvider ...@@ -404,8 +422,10 @@ public class LdapGroupProvider implements GroupProvider
Log.debug("User not found: "+userName); Log.debug("User not found: "+userName);
} }
} }
if (manager.isDebugEnabled())
Log.debug("Adding group \""+name+"\" with "+members.size()+" members.");
Group g = new Group(this,name,description,members,new ArrayList<String>()); Group g = new Group(this,name,description,members,new ArrayList<String>());
groups.add(g); groups.put(name,g);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -422,6 +442,6 @@ public class LdapGroupProvider implements GroupProvider ...@@ -422,6 +442,6 @@ public class LdapGroupProvider implements GroupProvider
} }
catch (Exception e) { } catch (Exception e) { }
return groups; return groups.values();
} }
} }
\ No newline at end of file
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