com.ecyrd.jspwiki.auth.authorize
Class DefaultGroupManager

java.lang.Object
  extended by com.ecyrd.jspwiki.auth.authorize.DefaultGroupManager
All Implemented Interfaces:
GroupManager, Authorizer

public class DefaultGroupManager
extends Object
implements GroupManager

This default UserDatabase implementation provides groups to JSPWiki.

Groups are based on WikiPages. The name of the page determines the group name (as a convention, we suggest the name of the page ends in DefaultGroup, e.g. EditorGroup). By setting attribute 'members' on the page, the named members are added to the group:

 
  
   [{SET members fee fie foe foo}]
   
  
 

The list of members can be separated by commas or spaces.

TODO: are 'named members' supposed to be usernames, or are group names allowed? (Suggestion: both)

Since:
2.3
Version:
$Revision: 1.13 $ $Date: 2006/06/17 23:12:18 $
Author:
Janne Jalkanen, Andrew Jaquith

Nested Class Summary
static class DefaultGroupManager.GroupListener
          Tiny little listener that captures wiki events fired by Groups this GroupManager knows about.
 class DefaultGroupManager.SaveFilter
          This special filter class is used to refresh the database after a page has been changed.
 
Field Summary
static String ATTR_MEMBERLIST
          The attribute to set on a page - [{SET members ...}] - to define members of the group named by that page.
static String GROUP_PREFIX
           
protected  WikiEngine m_engine
           
protected  WikiEventListener m_groupListener
           
 
Fields inherited from interface com.ecyrd.jspwiki.auth.authorize.GroupManager
PROP_GROUPMANAGER
 
Constructor Summary
DefaultGroupManager()
           
 
Method Summary
 void add(Group group)
          Adds a Group to the group cache.
 void addWikiEventListener(WikiEventListener listener)
          Registers a WikiEventListener with this GroupManager.
 void commit()
          Commits the groups to disk.
 boolean exists(Group group)
          Returns true if a Group is known to the GroupManager (contained in the group cache), false otherwise.
 Principal findRole(String name)
           Returns a Group matching a given name.
protected  void fireEvent(WikiSecurityEvent event)
          Fires a wiki event to all registered listeners.
protected static String getGroupName(WikiPage p)
          Returns the name of a wiki group, based on a supplied wiki page.
 Principal[] getRoles()
          Returns an array of role Principals this GroupManager knows about.
 void initialize(WikiEngine engine, Properties props)
          Initializes the group cache by adding a DefaultGroupManager.SaveFilterto the page manager, so that groups are updated when pages are saved.
 boolean isUserInRole(WikiSession session, Principal role)
          Determines whether the Subject associated with a WikiSession is in a particular role.
protected  List parseMemberList(String memberLine)
          Protected method that parses through the group membership list on a wiki page, and returns a List containing the member names as Strings.
 void reload()
          Reloads the group cache by iterating through all wiki pages and updating groups whenever a [{SET members ...}] tag is found.
 void remove(Group group)
          Removes a Group from the group cache.
 void removeWikiEventListener(WikiEventListener listener)
          Un-registers a WikiEventListener with this GroupManager.
protected  void updateGroup(String groupName, List memberList)
          Updates a named group with a List of new members.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTR_MEMBERLIST

public static final String ATTR_MEMBERLIST
The attribute to set on a page - [{SET members ...}] - to define members of the group named by that page.

See Also:
Constant Field Values

m_engine

protected WikiEngine m_engine

m_groupListener

protected WikiEventListener m_groupListener

GROUP_PREFIX

public static final String GROUP_PREFIX
See Also:
Constant Field Values
Constructor Detail

DefaultGroupManager

public DefaultGroupManager()
Method Detail

add

public void add(Group group)
Adds a Group to the group cache. Note that this method fail, and will throw an IllegalArgumentException, if the proposed group is the same name as one of the built-in Roles: e.g., Admin, Authenticated, etc.

Specified by:
add in interface GroupManager
Parameters:
group - the Group to add
Throws:
IllegalArgumentException - if the group name isn't allowed
See Also:
GroupManager.add(Group)

addWikiEventListener

public void addWikiEventListener(WikiEventListener listener)
Registers a WikiEventListener with this GroupManager.

Specified by:
addWikiEventListener in interface GroupManager
Parameters:
listener - the event listener

commit

public void commit()
            throws WikiException
Commits the groups to disk. This method is a no-op, since the wiki's page SaveFilter does this.

Specified by:
commit in interface GroupManager
Throws:
WikiException
See Also:
GroupManager.commit()

exists

public boolean exists(Group group)
Returns true if a Group is known to the GroupManager (contained in the group cache), false otherwise.

Specified by:
exists in interface GroupManager
See Also:
GroupManager.exists(Group)

findRole

public Principal findRole(String name)

Returns a Group matching a given name. If a group cannot be found, return null.

Specified by:
findRole in interface Authorizer
Parameters:
name - Name of the group. This is case-sensitive.
Returns:
A DefaultGroup instance.
See Also:
Authorizer.findRole(java.lang.String)

getRoles

public Principal[] getRoles()
Returns an array of role Principals this GroupManager knows about. This method will return an array of Group objects corresponding to the wiki groups managed by this class. This method actually returns a defensive copy of an internally stored hashmap.

Specified by:
getRoles in interface Authorizer
Returns:
an array of Principals representing the roles

initialize

public void initialize(WikiEngine engine,
                       Properties props)
Initializes the group cache by adding a DefaultGroupManager.SaveFilterto the page manager, so that groups are updated when pages are saved. This method also calls reload().

Specified by:
initialize in interface GroupManager
Specified by:
initialize in interface Authorizer
Parameters:
engine - the current wiki engine
props - the wiki engine initialization properties
See Also:
GroupManager.initialize(com.ecyrd.jspwiki.WikiEngine, java.util.Properties)

isUserInRole

public boolean isUserInRole(WikiSession session,
                            Principal role)

Determines whether the Subject associated with a WikiSession is in a particular role. This method takes two parameters: the WikiSession containing the subject and the desired role ( which may be a Role or a Group). If either parameter is null, this method must return false.

With respect to this implementation, the supplied Principal must be a Group. The Subject posesses the "role" if it is a member of that Group. This method simply finds the Group in question, then delegates to Group.isMember(Principal) for each of the principals in the Subject's principal set.

Specified by:
isUserInRole in interface Authorizer
Parameters:
session - the current WikiSession
role - the role to check
Returns:
true if the user is considered to be in the role, false otherwise

reload

public void reload()
Reloads the group cache by iterating through all wiki pages and updating groups whenever a [{SET members ...}] tag is found. For each group definition found, the protected method updateGroup(String, List) is called.

Specified by:
reload in interface GroupManager
See Also:
GroupManager.reload()

remove

public void remove(Group group)
Removes a Group from the group cache.

Specified by:
remove in interface GroupManager
Parameters:
group - the group to remove
See Also:
GroupManager.remove(Group)

removeWikiEventListener

public void removeWikiEventListener(WikiEventListener listener)
Un-registers a WikiEventListener with this GroupManager.

Specified by:
removeWikiEventListener in interface GroupManager
Parameters:
listener - the event listener

parseMemberList

protected List parseMemberList(String memberLine)
Protected method that parses through the group membership list on a wiki page, and returns a List containing the member names as Strings.

Parameters:
memberLine - the line of text containing the group membership list
Returns:
the member names, as a List of Strings

fireEvent

protected void fireEvent(WikiSecurityEvent event)
Fires a wiki event to all registered listeners.

Parameters:
event - the event

getGroupName

protected static String getGroupName(WikiPage p)
Returns the name of a wiki group, based on a supplied wiki page. If the page name begins with prefix GROUP_PREFIX, the group name will be all of the characters following the prefix. If the wiki page does not begin with the default group prefix, this method returns null.

Parameters:
p - the wiki page
Returns:
the group name, or null

updateGroup

protected void updateGroup(String groupName,
                           List memberList)
Updates a named group with a List of new members. The List is a collection of Strings that denotes members of this group. Each member is added to the group as a WikiPrincipal. If the group already exists in the cache, the List contents are added to the existing membership. If the group doesn't exist, it is created. If the List contains no members, the group is removed from the cache.

Parameters:
groupName - the name of the group to update
memberList - the members to add to the group definition