Commit e8817be3 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Changes from Greg Ferguson

LdapGroupProvider:
 * LdapGroupProvider will now correctly show groups in the admin console.
 * LdapGroupProvider will be more efficient when showing groups in the admin console.
 * LdapGroupProvider will use the user search filter when trying to get the user name on a non-posix server.
 * LdapGroupProvider will display a better error message when it cannot find a user name.

 LdapManager:
 * LdapManager will now display group XML settings in the debug log on startup, previously only non-group settings were displayed.  Only if ldap debug is enabled.
 * LdapManager will no longer display javadoc warnings when building JM.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1732 b35dd754-fafc-0310-a699-88a17e54d16e
parent e1969a4e
...@@ -21,6 +21,8 @@ import java.util.TreeMap; ...@@ -21,6 +21,8 @@ import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Enumeration;
import java.util.Vector;
import java.text.MessageFormat; import java.text.MessageFormat;
import javax.naming.Name; import javax.naming.Name;
...@@ -41,6 +43,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -41,6 +43,7 @@ public class LdapGroupProvider implements GroupProvider
private UserManager userManager; private UserManager userManager;
private int groupCount; private int groupCount;
private long expiresStamp; private long expiresStamp;
private String[] standardAttributes;
/** /**
* Constructor of the LdapGroupProvider class. * Constructor of the LdapGroupProvider class.
...@@ -52,6 +55,10 @@ public class LdapGroupProvider implements GroupProvider ...@@ -52,6 +55,10 @@ public class LdapGroupProvider implements GroupProvider
userManager = UserManager.getInstance(); userManager = UserManager.getInstance();
groupCount = -1; groupCount = -1;
expiresStamp = System.currentTimeMillis(); expiresStamp = System.currentTimeMillis();
standardAttributes = new String[3];
standardAttributes[0] = manager.getGroupNameField();
standardAttributes[1] = manager.getGroupDescriptionField();
standardAttributes[2] = manager.getGroupMemberField();
} }
/** /**
...@@ -105,7 +112,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -105,7 +112,7 @@ public class LdapGroupProvider implements GroupProvider
String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
String searchFilter = "(&"+filter+"("+ String searchFilter = "(&"+filter+"("+
manager.getGroupNameField()+"="+group+"))"; manager.getGroupNameField()+"="+group+"))";
Collection<Group> groups = getGroupBasedOnFilter(searchFilter); Collection<Group> groups = populateGroups(searchForGroups(searchFilter,standardAttributes));
if (groups.size() > 1) if (groups.size() > 1)
return null; //if multiple groups found return null return null; //if multiple groups found return null
for (Group g : groups) for (Group g : groups)
...@@ -158,40 +165,18 @@ public class LdapGroupProvider implements GroupProvider ...@@ -158,40 +165,18 @@ public class LdapGroupProvider implements GroupProvider
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Trying to get the number of groups in the system."); Log.debug("Trying to get the number of groups in the system.");
} }
DirContext ctx = null;
NamingEnumeration answer = null;
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
try
{
ctx = manager.getContext();
if (manager.isDebugEnabled()) {
Log.debug("Starting LDAP search...");
Log.debug("Using groupSearchFilter: "+searchFilter);
}
// Search for the dn based on the groupname. String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
SearchControls ctrls = new SearchControls(); String returningAttributes[]= { manager.getGroupNameField() };
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter,returningAttributes);
answer = ctx.search("",searchFilter,ctrls); for (; answer.hasMoreElements(); count++)
if (manager.isDebugEnabled()) {
Log.debug("... search finished");
}
}
catch (Exception e)
{
if (manager.isDebugEnabled())
Log.debug("Error while searching for groups.",e);
}
try
{ {
while (answer.hasMoreElements()) try
{ {
count++;
answer.next(); answer.next();
} }
catch (Exception e) { }
} }
catch (Exception e){ }
this.groupCount = count; this.groupCount = count;
this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5; this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5;
...@@ -206,33 +191,43 @@ public class LdapGroupProvider implements GroupProvider ...@@ -206,33 +191,43 @@ public class LdapGroupProvider implements GroupProvider
public Collection<Group> getGroups() public Collection<Group> getGroups()
{ {
String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
return getGroupBasedOnFilter(filter); return populateGroups(searchForGroups(filter,standardAttributes));
} }
/** /**
* 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 number of groups desired.
* displaying a certain number of groups per page * Useful when displaying a certain number of groups
* on a webpage. * per page on a webpage.
* *
* @param start starting index * @param start starting index
* @param end ending index * @param num number of groups you want
* @return collection of groups. * @return collection of groups.
*/ */
public Collection<Group> getGroups(int start, int end) public Collection<Group> getGroups(int start, int num)
{ {
ArrayList<Group> returnCollection = new ArrayList<Group>(); ArrayList<Group> returnCollection = new ArrayList<Group>();
Collection<Group> groups = getGroups();
Iterator<Group> it = groups.iterator(); // get an enumeration of all groups in the system
for (int i = 0; i < groups.size(); i++)
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter,standardAttributes);
//place all groups that are wanted into an enumeration
Vector<SearchResult> v = new Vector<SearchResult>();
for (int i = 1; answer.hasMoreElements() && i <= (start+num); i++)
{ {
Group g = it.next(); try
if (i >= start && i <= end) {
returnCollection.add(g); SearchResult sr = answer.next();
if (i > end) if (i >= start)
break; v.add(sr);
}
catch (Exception e) { }
} }
return returnCollection;
return populateGroups(v.elements());
} }
/** /**
...@@ -259,7 +254,7 @@ public class LdapGroupProvider implements GroupProvider ...@@ -259,7 +254,7 @@ public class LdapGroupProvider implements GroupProvider
} }
String filter = MessageFormat.format(manager.getGroupSearchFilter(),username); String filter = MessageFormat.format(manager.getGroupSearchFilter(),username);
return getGroupBasedOnFilter(filter); return populateGroups(searchForGroups(filter,standardAttributes));
} }
/** /**
...@@ -317,22 +312,22 @@ public class LdapGroupProvider implements GroupProvider ...@@ -317,22 +312,22 @@ public class LdapGroupProvider implements GroupProvider
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 an enumeration of SearchResult.
* @param searchFilter LDAP search filter used to query. * @param searchFilter LDAP search filter used to query.
*/ */
public Collection<Group> getGroupBasedOnFilter (String searchFilter) private NamingEnumeration<SearchResult> searchForGroups (String searchFilter,
String[] returningAttributes)
{ {
TreeMap<String,Group> groups = new TreeMap<String,Group>(); if (manager.isDebugEnabled()) {
boolean debug = Log.isDebugEnabled();
if (debug) {
Log.debug("Trying to find all groups in the system."); Log.debug("Trying to find all groups in the system.");
} }
DirContext ctx = null; DirContext ctx = null;
NamingEnumeration answer = null; NamingEnumeration<SearchResult> answer = null;
try try
{ {
ctx = manager.getContext(); ctx = manager.getContext();
...@@ -343,35 +338,59 @@ public class LdapGroupProvider implements GroupProvider ...@@ -343,35 +338,59 @@ 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.getGroupNameField(), searchControls.setReturningAttributes(returningAttributes);
manager.getGroupDescriptionField(),
manager.getGroupMemberField() };
searchControls.setReturningAttributes(returnedAtts);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
answer = ctx.search("",searchFilter,searchControls); answer = ctx.search("",searchFilter,searchControls);
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("... search finished"); Log.debug("... search finished");
Log.debug("Starting to populate groups with users.");
} }
} }
catch (Exception e) catch (Exception e)
{ {
if (manager.isDebugEnabled()) if (manager.isDebugEnabled())
Log.debug("Error while searching for groups.",e); Log.debug("Error while searching for groups.",e);
return groups.values(); }
return answer;
}
/**
* An auxilary method used to populate LDAP groups based on a
* provided LDAP search result.
*
* @return a collection of groups.
* @param answer LDAP search result.
*/
private Collection<Group> populateGroups (Enumeration<SearchResult> answer)
{
if (manager.isDebugEnabled()) {
Log.debug("Starting to populate groups with users.");
}
TreeMap<String,Group> groups = new TreeMap<String,Group>();
DirContext ctx = null;
try
{
ctx = manager.getContext();
}
catch (Exception e)
{
return new ArrayList<Group>();
} }
SearchControls ctrls = new SearchControls(); SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes( new String[]{manager.getUsernameField()} ); ctrls.setReturningAttributes( new String[]{manager.getUsernameField()} );
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String userSearchFilter = MessageFormat.format(manager.getSearchFilter(),"*");
while (answer.hasMoreElements()) while (answer.hasMoreElements())
{ {
String name = ""; String name = "";
try try
{ {
Attributes a = (((SearchResult)answer.next()).getAttributes()); Attributes a = (((SearchResult)answer.nextElement()).getAttributes());
String description; String description;
try try
{ {
...@@ -393,17 +412,20 @@ public class LdapGroupProvider implements GroupProvider ...@@ -393,17 +412,20 @@ public class LdapGroupProvider implements GroupProvider
try try
{ {
// Get the CN using LDAP // Get the CN using LDAP
Name ldapname = new LdapName(userName); LdapName ldapname = new LdapName(userName);
String ldapcn = ldapname.get(ldapname.size()-1); String ldapcn = ldapname.get(ldapname.size()-1);
// We have to do a new search to find the username field // We have to do a new search to find the username field
NamingEnumeration usrAnswer = ctx.search("",ldapcn,ctrls); String combinedFilter = "(&("+ldapcn+")"+userSearchFilter+")";
NamingEnumeration usrAnswer = ctx.search("",combinedFilter,ctrls);
if (usrAnswer.hasMoreElements()) if (usrAnswer.hasMoreElements())
{ {
userName = (String)((SearchResult)usrAnswer.next()).getAttributes().get( userName = (String)((SearchResult)usrAnswer.next()).getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
} }
else
throw new UserNotFoundException();
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -194,6 +194,11 @@ public class LdapManager { ...@@ -194,6 +194,11 @@ public class LdapManager {
Log.debug("\t initialContextFactory: " + initialContextFactory); Log.debug("\t initialContextFactory: " + initialContextFactory);
Log.debug("\t connectionPoolEnabled: " + connectionPoolEnabled); Log.debug("\t connectionPoolEnabled: " + connectionPoolEnabled);
Log.debug("\t autoFollowReferrals: " + followReferrals); Log.debug("\t autoFollowReferrals: " + followReferrals);
Log.debug("\t groupNameField: " + groupNameField);
Log.debug("\t groupMemberField: " + groupMemberField);
Log.debug("\t groupDescriptionField: " + groupDescriptionField);
Log.debug("\t posixEnabled: " + posixEnabled);
Log.debug("\t groupSearchFilter: " + groupSearchFilter);
} }
} }
...@@ -830,7 +835,7 @@ public class LdapManager { ...@@ -830,7 +835,7 @@ public class LdapManager {
* Sets the field used to list members within a group. * Sets the field used to list members within a group.
* Value of groupMemberField defaults to "member". * Value of groupMemberField defaults to "member".
* *
* @param the field used to list members within a group. * @param groupMemberField the field used to list members within a group.
*/ */
public void setGroupmemberField(String groupMemberField) { public void setGroupmemberField(String groupMemberField) {
this.groupMemberField = groupMemberField; this.groupMemberField = groupMemberField;
...@@ -851,7 +856,7 @@ public class LdapManager { ...@@ -851,7 +856,7 @@ public class LdapManager {
* Sets the field used to describe a group. * Sets the field used to describe a group.
* Value of groupDescriptionField defaults to "description". * Value of groupDescriptionField defaults to "description".
* *
* @param the field used to describe a group. * @param groupDescriptionField the field used to describe a group.
*/ */
public void setGroupDescriptionField(String groupDescriptionField) { public void setGroupDescriptionField(String groupDescriptionField) {
this.groupDescriptionField = groupDescriptionField; this.groupDescriptionField = groupDescriptionField;
...@@ -872,7 +877,7 @@ public class LdapManager { ...@@ -872,7 +877,7 @@ public class LdapManager {
* Sets the field used to tell if ldap server is posix. * Sets the field used to tell if ldap server is posix.
* Value of posixEnabled defaults to false. * Value of posixEnabled defaults to false.
* *
* @param the field used to tell if ldap server is posix. * @param posixEnabled the field used to tell if ldap server is posix.
*/ */
public void setPosixEnabled(boolean posixEnabled) { public void setPosixEnabled(boolean posixEnabled) {
this.posixEnabled = posixEnabled; this.posixEnabled = posixEnabled;
...@@ -894,7 +899,7 @@ public class LdapManager { ...@@ -894,7 +899,7 @@ public class LdapManager {
* Sets the field used as the search filter when searching for groups. * Sets the field used as the search filter when searching for groups.
* Value of groupSearchFilter defaults "(groupMemberField=*)". * Value of groupSearchFilter defaults "(groupMemberField=*)".
* *
* @param the field used as the search filter when searching for groups. * @param groupSearchFilter the field used as the search filter when searching for groups.
*/ */
public void setGroupSearchFilter(String groupSearchFilter) { public void setGroupSearchFilter(String groupSearchFilter) {
this.groupSearchFilter = groupSearchFilter; this.groupSearchFilter = groupSearchFilter;
......
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