public class GroupManager extends Object implements Authorizer, WikiEventListener
Facade class for storing, retrieving and managing wiki groups on behalf of
AuthorizationManager, JSPs and other presentation-layer classes. GroupManager
works in collaboration with a back-end GroupDatabase
, which persists
groups to permanent storage.
Note: prior to JSPWiki 2.4.19, GroupManager was an interface; it is now a concrete, final class. The aspects of GroupManager which previously extracted group information from storage (e.g., wiki pages) have been refactored into the GroupDatabase interface.
Modifier and Type | Field and Description |
---|---|
protected WikiEngine |
m_engine |
protected WikiEventListener |
m_groupListener |
static String |
MESSAGES_KEY
Key used for adding UI messages to a user's WikiSession.
|
Constructor and Description |
---|
GroupManager() |
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(WikiEvent event)
Listens for
WikiSecurityEvent.PROFILE_NAME_CHANGED
events. |
void |
addWikiEventListener(WikiEventListener listener)
Registers a WikiEventListener with this instance.
|
protected void |
checkGroupName(WikiContext context,
String name)
Checks if a String is blank or a restricted Group name, and if it is,
appends an error to the WikiSession's message list.
|
protected String[] |
extractMembers(String memberLine)
Extracts carriage-return separated members into a Set of String objects.
|
Principal |
findRole(String name)
Returns a GroupPrincipal matching a given name.
|
protected void |
fireEvent(int type,
Object target)
Fires a WikiSecurityEvent of the provided type, Principal and target Object
to all registered listeners.
|
Group |
getGroup(String name)
Returns the Group matching a given name.
|
GroupDatabase |
getGroupDatabase()
Returns the current external
GroupDatabase in use. |
Principal[] |
getRoles()
Returns an array of GroupPrincipals this GroupManager knows about.
|
void |
initialize(WikiEngine engine,
Properties props)
Initializes the group cache by initializing the group database and
obtaining a list of all of the groups it stores.
|
boolean |
isUserInRole(WikiSession session,
Principal role)
Determines whether the Subject associated with a WikiSession is in a
particular role.
|
Group |
parseGroup(String name,
String memberLine,
boolean create)
Extracts group name and members from passed parameters and populates an
existing Group with them.
|
Group |
parseGroup(WikiContext context,
boolean create)
Extracts group name and members from the HTTP request and populates an
existing Group with them.
|
void |
removeGroup(String index)
Removes a named Group from the group database.
|
void |
removeWikiEventListener(WikiEventListener listener)
Un-registers a WikiEventListener with this instance.
|
void |
setGroup(WikiSession session,
Group group)
Saves the
Group created by a user in a wiki session. |
void |
validateGroup(WikiContext context,
Group group)
Validates a Group, and appends any errors to the session errors list.
|
public static final String MESSAGES_KEY
protected WikiEngine m_engine
protected WikiEventListener m_groupListener
public GroupManager()
public Principal findRole(String name)
Returns a GroupPrincipal matching a given name. If a group cannot be
found, return null
.
findRole
in interface Authorizer
name
- Name of the group. This is case-sensitive.public Group getGroup(String name) throws NoSuchPrincipalException
NoSuchPrincipalException
.name
- the name of the group to findNoSuchPrincipalException
- if the group cannot be foundpublic GroupDatabase getGroupDatabase() throws WikiSecurityException
GroupDatabase
in use. This method
is guaranteed to return a properly-initialized GroupDatabase, unless
it could not be initialized. In that case, this method throws
a WikiException
. The GroupDatabase
is lazily initialized.WikiSecurityException
- if the GroupDatabase could
not be initializedpublic Principal[] getRoles()
getRoles
in interface Authorizer
public void initialize(WikiEngine engine, Properties props) throws WikiSecurityException
initialize
in interface Authorizer
engine
- the wiki engineprops
- the properties used to initialize the wiki engineWikiSecurityException
- if GroupManager cannot be initializedGroupDatabase.initialize(org.apache.wiki.WikiEngine,
java.util.Properties)
,
GroupDatabase.groups()
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
, or if the user is
not authenticated, this method returns false
.
With respect to this implementation, the supplied Principal must be a
GroupPrincipal. The Subject posesses the "role" if it the session is
authenticated and a Subject's principal is a member of the
corresponding 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.
isUserInRole
in interface Authorizer
session
- the current WikiSessionrole
- the role to checktrue
if the user is considered to be in the role,
false
otherwisepublic Group parseGroup(String name, String memberLine, boolean create) throws WikiSecurityException
Extracts group name and members from passed parameters and populates an existing Group with them. The Group will either be a copy of an existing Group (if one can be found), or a new, unregistered Group (if not). Optionally, this method can throw a WikiSecurityException if the Group does not yet exist in the GroupManager cache.
The group
parameter in the HTTP request contains the Group
name to look up and populate. The members
parameter
contains the member list. If these differ from those in the existing
group, the passed values override the old values.
This method does not commit the new Group to the GroupManager cache. To
do that, use setGroup(WikiSession, Group)
.
name
- the name of the group to constructmemberLine
- the line of text containing the group membership listcreate
- whether this method should create a new, empty Group if one
with the requested name is not found. If false
,
groups that do not exist will cause a
NoSuchPrincipalException
to be thrownWikiSecurityException
- if the group name isn't allowed, or if
create
is false
and the Group named name
does not existGroup.RESTRICTED_GROUPNAMES
public Group parseGroup(WikiContext context, boolean create) throws WikiSecurityException
Extracts group name and members from the HTTP request and populates an existing Group with them. The Group will either be a copy of an existing Group (if one can be found), or a new, unregistered Group (if not). Optionally, this method can throw a WikiSecurityException if the Group does not yet exist in the GroupManager cache.
The group
parameter in the HTTP request contains the Group
name to look up and populate. The members
parameter
contains the member list. If these differ from those in the existing
group, the passed values override the old values.
This method does not commit the new Group to the GroupManager cache. To
do that, use setGroup(WikiSession, Group)
.
context
- the current wiki contextcreate
- whether this method should create a new, empty Group if one
with the requested name is not found. If false
,
groups that do not exist will cause a
NoSuchPrincipalException
to be thrownWikiSecurityException
- if the group name isn't allowed, or if
create
is false
and the Group does not existpublic void removeGroup(String index) throws WikiSecurityException
NoSuchPrincipalException
. After removal, this method will
commit the delete to the back-end group database. It will also fire a
WikiSecurityEvent.GROUP_REMOVE
event with
the GroupManager instance as the source and the Group as target.
If index
is null
, this method throws
an IllegalArgumentException
.index
- the group to removeWikiSecurityException
- if the Group cannot be removed by
the back-endGroupDatabase.delete(Group)
public void setGroup(WikiSession session, Group group) throws WikiSecurityException
Saves the Group
created by a user in a wiki session. This method
registers the Group with the GroupManager and saves it to the back-end
database. If an existing Group with the same name already exists, the new
group will overwrite it. After saving the Group, the group database
changes are committed.
This method fires the following events:
WikiSecurityEvent.GROUP_ADD
with the
GroupManager instance as its source and the new Group as the target.WikiSecurityEvent.GROUP_REMOVE
with this GroupManager instance as the source, and the new Group as the
target. It then fires a
WikiSecurityEvent.GROUP_ADD
event with the
same source and target.
In addition, if the save or commit actions fail, this method will attempt
to restore the older version of the wiki group if it exists. This will
result in a GROUP_REMOVE
event (for the new version of the
Group) followed by a GROUP_ADD
event (to indicate
restoration of the old version).
This method will register the new Group with the GroupManager. For example,
AuthenticationManager
attaches each
WikiSession as a GroupManager listener. Thus, the act of registering a
Group with setGroup
means that all WikiSessions will
automatically receive group add/change/delete events immediately.
session
- the wiki session, which may not be null
group
- the Group, which may not be null
WikiSecurityException
- if the Group cannot be saved by the back-endpublic void validateGroup(WikiContext context, Group group)
WikiSession.getMessages()
.context
- the current wiki contextgroup
- the supplied Groupprotected String[] extractMembers(String memberLine)
memberLine
- the list of membersprotected void checkGroupName(WikiContext context, String name) throws WikiSecurityException
context
- the wiki contextname
- the Group name to testWikiSecurityException
- if session
is
null
or the Group name is illegalGroup.RESTRICTED_GROUPNAMES
public void addWikiEventListener(WikiEventListener listener)
listener
- the event listenerpublic void removeWikiEventListener(WikiEventListener listener)
listener
- the event listenerprotected void fireEvent(int type, Object target)
type
- the event type to be firedtarget
- the changed Object, which may be null
WikiSecurityEvent
public void actionPerformed(WikiEvent event)
WikiSecurityEvent.PROFILE_NAME_CHANGED
events. If a user profile's name changes, each group is inspected. If an entry contains
a name that has changed, it is replaced with the new one. No group events are emitted
as a consequence of this method, because the group memberships are still the same; it is
only the representations of the names within that are changing.actionPerformed
in interface WikiEventListener
event
- the incoming eventCopyright © 2001-2019 The Apache Software Foundation. All rights reserved.