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

Javadoc, formatting, escape usernames (JM-378).

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2697 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2c4a7793
...@@ -20,12 +20,10 @@ import java.util.ArrayList; ...@@ -20,12 +20,10 @@ import java.util.ArrayList;
import java.util.TreeMap; 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.Enumeration; import java.util.Enumeration;
import java.util.Vector; import java.util.Vector;
import java.text.MessageFormat; import java.text.MessageFormat;
import javax.naming.Name;
import javax.naming.NamingEnumeration; import javax.naming.NamingEnumeration;
import javax.naming.directory.*; import javax.naming.directory.*;
import javax.naming.ldap.LdapName; import javax.naming.ldap.LdapName;
...@@ -36,8 +34,7 @@ import javax.naming.ldap.LdapName; ...@@ -36,8 +34,7 @@ import javax.naming.ldap.LdapName;
* *
* @author Greg Ferguson and Cameron Moore * @author Greg Ferguson and Cameron Moore
*/ */
public class LdapGroupProvider implements GroupProvider public class LdapGroupProvider implements GroupProvider {
{
private LdapManager manager; private LdapManager manager;
private UserManager userManager; private UserManager userManager;
...@@ -48,7 +45,6 @@ public class LdapGroupProvider implements GroupProvider ...@@ -48,7 +45,6 @@ public class LdapGroupProvider implements GroupProvider
/** /**
* Constructor of the LdapGroupProvider class. * Constructor of the LdapGroupProvider class.
* Gets an LdapManager instance from the LdapManager class. * Gets an LdapManager instance from the LdapManager class.
*
*/ */
public LdapGroupProvider() { public LdapGroupProvider() {
manager = LdapManager.getInstance(); manager = LdapManager.getInstance();
...@@ -62,100 +58,66 @@ public class LdapGroupProvider implements GroupProvider ...@@ -62,100 +58,66 @@ public class LdapGroupProvider implements GroupProvider
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
*
* @param group Name of the group to be created.
* @throws UnsupportedOperationException when called.
*/
public Group createGroup (String group)
throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
/**
* Always throws UnsupportedOperationException because
* LDAP operations are treated as read only.
* *
* @param group Group that should be deleted. * @param name the name of the group to create.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void deleteGroup (Group group) public Group createGroup(String name) throws UnsupportedOperationException {
throws UnsupportedOperationException throw new UnsupportedOperationException();
{
throw new UnsupportedOperationException();
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param group Group that should be deleted. * @param name the name of the group to delete
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void deleteGroup (String group) public void deleteGroup(String name) throws UnsupportedOperationException {
throws UnsupportedOperationException throw new UnsupportedOperationException();
{
throw new UnsupportedOperationException();
} }
/** public Group getGroup(String group) {
* Will populate a group object based on the group name String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
* that is given. String searchFilter = "(&" + filter + "(" +
* manager.getGroupNameField() + "=" + group + "))";
* @param group Name of group that should be retrieved. Collection<Group> groups = populateGroups(searchForGroups(searchFilter, standardAttributes));
* @return a populated group based on the name provided. if (groups.size() > 1) {
*/
public Group getGroup (String group)
{
String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
String searchFilter = "(&"+filter+"("+
manager.getGroupNameField()+"="+group+"))";
Collection<Group> groups = populateGroups(searchForGroups(searchFilter,standardAttributes));
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) {
return g; //returns the first group found return g; //returns the first group found
}
return null; return null;
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param name Current name of group. * @param oldName the current name of the group.
* @param s Desired name of group. * @param newName the desired new name of the group.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void setName(String name, String s) public void setName(String oldName, String newName) throws UnsupportedOperationException {
throws UnsupportedOperationException throw new UnsupportedOperationException();
{
throw new UnsupportedOperationException();
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param description Current description of group. * @param name the group name.
* @param s Desired description of group. * @param description the group description.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void setDescription(String description,String s) public void setDescription(String name, String description) throws UnsupportedOperationException {
throws UnsupportedOperationException throw new UnsupportedOperationException();
{
throw new UnsupportedOperationException();
} }
/** public int getGroupCount() {
* Will return the current number of groups in the
* LDAP server.
*
* @return the number of groups in the LDAP server.
*/
public int getGroupCount()
{
// Cache group count for 5 minutes. // Cache group count for 5 minutes.
if (groupCount != -1 && System.currentTimeMillis() < expiresStamp) { if (groupCount != -1 && System.currentTimeMillis() < expiresStamp) {
return groupCount; return groupCount;
...@@ -166,304 +128,265 @@ public class LdapGroupProvider implements GroupProvider ...@@ -166,304 +128,265 @@ public class LdapGroupProvider implements GroupProvider
Log.debug("Trying to get the number of groups in the system."); Log.debug("Trying to get the number of groups in the system.");
} }
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
String returningAttributes[]= { manager.getGroupNameField() }; String returningAttributes[] = {manager.getGroupNameField()};
NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter,returningAttributes); NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter, returningAttributes);
for (; answer.hasMoreElements(); count++) for (; answer.hasMoreElements(); count++) {
{ try {
try 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;
return count; return count;
} }
/** public Collection<Group> getGroups() {
* Will return a collecion of all groups in the system. String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
* return populateGroups(searchForGroups(filter, standardAttributes));
* @return collection of all groups in the system.
*/
public Collection<Group> getGroups()
{
String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
return populateGroups(searchForGroups(filter,standardAttributes));
} }
/** public Collection<Group> getGroups(int start, int num) {
* Will return a collecion of groups in the system // Get an enumeration of all groups in the system
* based on the start index and number of groups desired. String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
* Useful when displaying a certain number of groups NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter, standardAttributes);
* per page on a webpage.
*
* @param start starting index
* @param num number of groups you want
* @return collection of groups.
*/
public Collection<Group> getGroups(int start, int num)
{
ArrayList<Group> returnCollection = new ArrayList<Group>();
// get an enumeration of all groups in the system
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*");
NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter,standardAttributes);
//place all groups that are wanted into an enumeration
// Place all groups that are wanted into an enumeration
Vector<SearchResult> v = new Vector<SearchResult>(); Vector<SearchResult> v = new Vector<SearchResult>();
for (int i = 1; answer.hasMoreElements() && i <= (start+num); i++) for (int i = 1; answer.hasMoreElements() && i <= (start + num); i++) {
{ try {
try SearchResult sr = answer.next();
{ if (i >= start) {
SearchResult sr = answer.next(); v.add(sr);
if (i >= start) }
v.add(sr); }
} catch (Exception e) {
catch (Exception e) { } // Ignore.
}
} }
return populateGroups(v.elements()); return populateGroups(v.elements());
} }
/** public Collection<Group> getGroups(User user) {
* Will return a collection of groups in the
* system that the user provided belongs to.
*
* @param user a user
* @return collection of groups.
*/
public Collection<Group> getGroups(User user)
{
String username = JID.unescapeNode(user.getUsername()); String username = JID.unescapeNode(user.getUsername());
if (!manager.getPosixEnabled()) if (!manager.isPosixMode()) {
{ try {
try username = manager.findUserDN(username) + "," +
{ manager.getBaseDN();
username = manager.findUserDN(username) + "," + }
manager.getBaseDN(); catch (Exception e) {
} return new ArrayList<Group>();
catch (Exception e) }
{
return new ArrayList<Group>();
}
} }
String filter = MessageFormat.format(manager.getGroupSearchFilter(),username); String filter = MessageFormat.format(manager.getGroupSearchFilter(), username);
return populateGroups(searchForGroups(filter,standardAttributes)); return populateGroups(searchForGroups(filter, standardAttributes));
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param groupName Name of a group. * @param groupName name of a group.
* @param username Name of a user. * @param username name of a user.
* @param administrator True if is an administrator. * @param administrator true if is an administrator.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void addMember(String groupName, String username, boolean administrator) public void addMember(String groupName, String username, boolean administrator)
throws UnsupportedOperationException throws UnsupportedOperationException
{ {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param groupName Name of a group. * @param groupName the naame of a group.
* @param username Name of a user. * @param username the name of a user.
* @param administrator True if is an administrator. * @param administrator true if is an administrator.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void updateMember(String groupName, String username, boolean administrator) public void updateMember(String groupName, String username, boolean administrator)
throws UnsupportedOperationException throws UnsupportedOperationException
{ {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** /**
* Always throws UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP operations are treated as read only. * LDAP groups are read-only.
* *
* @param groupName Name of a group. * @param groupName the name of a group.
* @param username Name of a user. * @param username the ame 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();
} }
/** /**
* Always returns true because all LDAP operations * Always throws an UnsupportedOperationException because
* are treated as read only. * LDAP groups are read-only.
* *
* @return true always because all LDAP functions are read only. * @return true 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 an enumeration of SearchResult.
* @param searchFilter LDAP search filter used to query. * @param searchFilter LDAP search filter used to query.
* @return an enumeration of SearchResult.
*/ */
private NamingEnumeration<SearchResult> searchForGroups (String searchFilter, private NamingEnumeration<SearchResult> searchForGroups(String searchFilter,
String[] returningAttributes) String[] returningAttributes) {
{ if (manager.isDebugEnabled()) {
if (manager.isDebugEnabled()) { 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<SearchResult> answer = null;
NamingEnumeration<SearchResult> answer = null; try {
try ctx = manager.getContext();
{ if (manager.isDebugEnabled()) {
ctx = manager.getContext(); Log.debug("Starting LDAP search...");
if (manager.isDebugEnabled()) { Log.debug("Using groupSearchFilter: " + searchFilter);
Log.debug("Starting LDAP search..."); }
Log.debug("Using groupSearchFilter: "+searchFilter);
} // Search for the dn based on the groupname.
SearchControls searchControls = new SearchControls();
// Search for the dn based on the groupname. searchControls.setReturningAttributes(returningAttributes);
SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(returningAttributes); answer = ctx.search("", searchFilter, searchControls);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
answer = ctx.search("",searchFilter,searchControls); if (manager.isDebugEnabled()) {
Log.debug("... search finished");
if (manager.isDebugEnabled()) { }
Log.debug("... search finished"); }
} catch (Exception e) {
} if (manager.isDebugEnabled()) {
catch (Exception e) Log.debug("Error while searching for groups.", e);
{ }
if (manager.isDebugEnabled()) }
Log.debug("Error while searching for groups.",e); return answer;
}
return answer;
} }
/** /**
* An auxilary method used to populate LDAP groups based on a * An auxilary method used to populate LDAP groups based on a
* provided LDAP search result. * provided LDAP search result.
* *
* @return a collection of groups.
* @param answer LDAP search result. * @param answer LDAP search result.
* @return a collection of groups.
*/ */
private Collection<Group> populateGroups (Enumeration<SearchResult> answer) private Collection<Group> populateGroups(Enumeration<SearchResult> answer) {
{ if (manager.isDebugEnabled()) {
if (manager.isDebugEnabled()) { Log.debug("Starting to populate groups with users.");
Log.debug("Starting to populate groups with users."); }
}
TreeMap<String, Group> groups = new TreeMap<String, Group>();
TreeMap<String,Group> groups = new TreeMap<String,Group>();
DirContext ctx = null;
DirContext ctx = null; try {
try ctx = manager.getContext();
{ }
ctx = manager.getContext(); catch (Exception e) {
} return new ArrayList<Group>();
catch (Exception e) }
{
return new ArrayList<Group>(); SearchControls ctrls = new SearchControls();
} ctrls.setReturningAttributes(new String[]{manager.getUsernameField()});
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes( new String[]{manager.getUsernameField()} ); String userSearchFilter = MessageFormat.format(manager.getSearchFilter(), "*");
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
while (answer.hasMoreElements()) {
String userSearchFilter = MessageFormat.format(manager.getSearchFilter(),"*"); String name = "";
try {
while (answer.hasMoreElements()) Attributes a = (((SearchResult) answer.nextElement()).getAttributes());
{ String description;
String name = ""; try {
try name = ((String) ((a.get(manager.getGroupNameField())).get()));
{ description = ((String) ((a.get(manager.getGroupDescriptionField())).get()));
Attributes a = (((SearchResult)answer.nextElement()).getAttributes()); }
String description; catch (Exception e) {
try description = "";
{ }
name = ((String)((a.get(manager.getGroupNameField())).get())); TreeSet<String> members = new TreeSet<String>();
description = ((String)((a.get(manager.getGroupDescriptionField())).get())); Attribute member = a.get(manager.getGroupMemberField());
} NamingEnumeration ne = member.getAll();
catch (Exception e) while (ne.hasMore()) {
{ String username = (String) ne.next();
description = ""; if (!manager.isPosixMode()) { //userName is full dn if not posix
} try {
TreeSet<String> members = new TreeSet<String>(); // Get the CN using LDAP
Attribute member = a.get(manager.getGroupMemberField()); LdapName ldapname = new LdapName(username);
NamingEnumeration ne = member.getAll(); String ldapcn = ldapname.get(ldapname.size() - 1);
while (ne.hasMore())
{ // We have to do a new search to find the username field
String userName = (String)ne.next();
if (!manager.getPosixEnabled()) String combinedFilter = "(&(" + ldapcn + ")" + userSearchFilter + ")";
{ //userName is full dn if not posix NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls);
try if (usrAnswer.hasMoreElements()) {
{ username = (String) ((SearchResult) usrAnswer.next()).getAttributes().get(
// Get the CN using LDAP manager.getUsernameField()).get();
LdapName ldapname = new LdapName(userName); }
String ldapcn = ldapname.get(ldapname.size()-1); else {
throw new UserNotFoundException();
// We have to do a new search to find the username field }
}
String combinedFilter = "(&("+ldapcn+")"+userSearchFilter+")"; catch (Exception e) {
NamingEnumeration usrAnswer = ctx.search("",combinedFilter,ctrls); if (manager.isDebugEnabled()) {
if (usrAnswer.hasMoreElements()) Log.debug("Error populating user with DN: " + username, e);
{ }
userName = (String)((SearchResult)usrAnswer.next()).getAttributes().get( }
manager.getUsernameField()).get(); }
} // A search filter may have been defined in the LdapUserProvider.
else // Therefore, we have to try to load each user we found to see if
throw new UserNotFoundException(); // it passes the filter.
try {
// In order to lookup a username from the manager, the username
// must be a properly escaped JID node.
String escapedUsername = JID.escapeNode(username);
userManager.getUser(escapedUsername);
// No exception, so the user must exist. Add the user as a group
// member using the escaped username.
members.add(escapedUsername);
} }
catch (Exception e) catch (UserNotFoundException e) {
{ if (manager.isDebugEnabled()) {
if (manager.isDebugEnabled()) Log.debug("User not found: " + username);
Log.debug("Error populating user with DN: "+userName,e); }
} }
} }
try if (manager.isDebugEnabled()) {
{ Log.debug("Adding group \"" + name + "\" with " + members.size() + " members.");
User user = userManager.getUser(JID.escapeNode(userName));
members.add(user.getUsername());
} }
catch (UserNotFoundException e) Group g = new Group(this, name, description, members, new ArrayList<String>());
{ groups.put(name, g);
if (manager.isDebugEnabled()) }
Log.debug("User not found: "+userName); catch (Exception e) {
if (manager.isDebugEnabled()) {
Log.debug("Error while populating group, " + name + ".", e);
} }
} }
if (manager.isDebugEnabled()) }
Log.debug("Adding group \""+name+"\" with "+members.size()+" members."); if (manager.isDebugEnabled()) {
Group g = new Group(this,name,description,members,new ArrayList<String>()); Log.debug("Finished populating group(s) with users.");
groups.put(name,g); }
} try {
catch (Exception e) ctx.close();
{ }
if (manager.isDebugEnabled()) catch (Exception e) {
Log.debug("Error while populating group, "+name+".",e); }
}
} return groups.values();
if (manager.isDebugEnabled())
Log.debug("Finished populating group(s) with users.");
try
{
ctx.close();
}
catch (Exception e) { }
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