001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.auth.authorize; 020 021import org.apache.wiki.api.core.Context; 022import org.apache.wiki.api.core.Session; 023import org.apache.wiki.api.engine.Initializable; 024import org.apache.wiki.auth.AuthorizationManager; 025import org.apache.wiki.auth.Authorizer; 026import org.apache.wiki.auth.NoSuchPrincipalException; 027import org.apache.wiki.auth.WikiSecurityException; 028import org.apache.wiki.auth.permissions.AllPermission; 029import org.apache.wiki.event.WikiEventListener; 030import org.apache.wiki.event.WikiEventManager; 031import org.apache.wiki.event.WikiSecurityEvent; 032import org.apache.wiki.ui.Installer; 033 034import javax.servlet.http.HttpServletRequest; 035 036 037/** 038 * <p> 039 * Facade class for storing, retrieving and managing wiki groups on behalf of AuthorizationManager, JSPs and other presentation-layer 040 * classes. GroupManager works in collaboration with a back-end {@link GroupDatabase}, which persists groups to permanent storage. 041 * </p> 042 * <p> 043 * <em>Note: prior to JSPWiki 2.4.19, GroupManager was an interface; it is now a concrete, final class. The aspects of GroupManager 044 * which previously extracted group information from storage (e.g., wiki pages) have been refactored into the GroupDatabase interface.</em> 045 * </p> 046 * @since 2.4.19 047 */ 048public interface GroupManager extends Initializable, Authorizer, WikiEventListener { 049 050 /** Key used for adding UI messages to a user's Session. */ 051 String MESSAGES_KEY = "group"; 052 053 String PROP_GROUPDATABASE = "jspwiki.groupdatabase"; 054 055 /** 056 * Returns the Group matching a given name. If the group cannot be found, this method throws a <code>NoSuchPrincipalException</code>. 057 * 058 * @param name the name of the group to find 059 * @return the group 060 * @throws NoSuchPrincipalException if the group cannot be found 061 */ 062 Group getGroup( final String name ) throws NoSuchPrincipalException; 063 064 /** 065 * Returns the current external {@link GroupDatabase} in use. This method is guaranteed to return a properly-initialized GroupDatabase, 066 * unless it could not be initialized. In that case, this method throws a {@link org.apache.wiki.api.exceptions.WikiException}. The 067 * GroupDatabase is lazily initialized. 068 * 069 * @throws org.apache.wiki.auth.WikiSecurityException if the GroupDatabase could not be initialized 070 * @return the current GroupDatabase 071 * @since 2.3 072 */ 073 GroupDatabase getGroupDatabase() throws WikiSecurityException; 074 075 /** 076 * <p> 077 * Extracts group name and members from passed parameters and populates an existing Group with them. The Group will either be a copy of 078 * an existing Group (if one can be found), or a new, unregistered Group (if not). Optionally, this method can throw a 079 * WikiSecurityException if the Group does not yet exist in the GroupManager cache. 080 * </p> 081 * <p> 082 * The <code>group</code> parameter in the HTTP request contains the Group name to look up and populate. The <code>members</code> 083 * parameter contains the member list. If these differ from those in the existing group, the passed values override the old values. 084 * </p> 085 * <p> 086 * This method does not commit the new Group to the GroupManager cache. To do that, use {@link #setGroup(Session, Group)}. 087 * </p> 088 * @param name the name of the group to construct 089 * @param memberLine the line of text containing the group membership list 090 * @param create whether this method should create a new, empty Group if one with the requested name is not found. If <code>false</code>, 091 * groups that do not exist will cause a <code>NoSuchPrincipalException</code> to be thrown 092 * @return a new, populated group 093 * @see org.apache.wiki.auth.authorize.Group#RESTRICTED_GROUPNAMES 094 * @throws WikiSecurityException if the group name isn't allowed, or if <code>create</code> is <code>false</code> 095 * and the Group named <code>name</code> does not exist 096 */ 097 Group parseGroup( String name, String memberLine, boolean create ) throws WikiSecurityException; 098 099 /** 100 * <p> 101 * Extracts group name and members from the HTTP request and populates an existing Group with them. The Group will either be a copy of 102 * an existing Group (if one can be found), or a new, unregistered Group (if not). Optionally, this method can throw a 103 * WikiSecurityException if the Group does not yet exist in the GroupManager cache. 104 * </p> 105 * <p> 106 * The <code>group</code> parameter in the HTTP request contains the Group name to look up and populate. The <code>members</code> 107 * parameter contains the member list. If these differ from those in the existing group, the passed values override the old values. 108 * </p> 109 * <p> 110 * This method does not commit the new Group to the GroupManager cache. To do that, use {@link #setGroup(Session, Group)}. 111 * </p> 112 * @param context the current wiki context 113 * @param create whether this method should create a new, empty Group if one with the requested name is not found. If <code>false</code>, 114 * groups that do not exist will cause a <code>NoSuchPrincipalException</code> to be thrown 115 * @return a new, populated group 116 * @throws WikiSecurityException if the group name isn't allowed, if the group is the Admin group and the current session does not have 117 * AllPermission, or if <code>create</code> is <code>false</code> and the Group does not exist 118 */ 119 default Group parseGroup( final Context context, final boolean create ) throws WikiSecurityException { 120 // Extract parameters 121 final HttpServletRequest request = context.getHttpRequest(); 122 final String name = request.getParameter( "group" ); 123 final String memberLine = request.getParameter( "members" ); 124 125 // The default policy grants AllPermission to the Admin group, so a page creates it only for a session that has that permission. 126 // The installer creates the group with the parseGroup that takes the name, so it is not affected. 127 if( create && Installer.ADMIN_GROUP.equals( name ) && !context.getEngine().getManager( AuthorizationManager.class ) 128 .checkPermission( context.getWikiSession(), new AllPermission( context.getEngine().getApplicationName() ) ) ) { 129 throw new WikiSecurityException( "Illegal group name: " + name ); 130 } 131 132 // Create the named group; we pass on any NoSuchPrincipalExceptions 133 // that may be thrown if create == false, or WikiSecurityExceptions 134 final Group group = parseGroup( name, memberLine, create ); 135 136 // If no members, add the current user by default 137 if( group.members().length == 0 ) { 138 group.add( context.getWikiSession().getUserPrincipal() ); 139 } 140 141 return group; 142 } 143 144 /** 145 * Removes a named Group from the group database. If not found, throws a <code>NoSuchPrincipalException</code>. After removal, this 146 * method will commit the delete to the back-end group database. It will also fire a 147 * {@link org.apache.wiki.event.WikiSecurityEvent#GROUP_REMOVE} event with the GroupManager instance as the source and the Group as target. 148 * If <code>index</code> is <code>null</code>, this method throws an {@link IllegalArgumentException}. 149 * 150 * @param index the group to remove 151 * @throws WikiSecurityException if the Group cannot be removed by the back-end 152 * @see org.apache.wiki.auth.authorize.GroupDatabase#delete(Group) 153 */ 154 void removeGroup( final String index ) throws WikiSecurityException; 155 156 /** 157 * <p> 158 * Saves the {@link Group} created by a user in a wiki session. This method registers the Group with the GroupManager and saves it to 159 * the back-end database. If an existing Group with the same name already exists, the new group will overwrite it. After saving the 160 * Group, the group database changes are committed. 161 * </p> 162 * <p> 163 * This method fires the following events: 164 * </p> 165 * <ul> 166 * <li><strong>When creating a new Group</strong>, this method fires a {@link org.apache.wiki.event.WikiSecurityEvent#GROUP_ADD} with 167 * the GroupManager instance as its source and the new Group as the target.</li> 168 * <li><strong>When overwriting an existing Group</strong>, this method fires a new 169 * {@link org.apache.wiki.event.WikiSecurityEvent#GROUP_REMOVE} with this GroupManager instance as the source, and the new Group as the 170 * target. It then fires a {@link org.apache.wiki.event.WikiSecurityEvent#GROUP_ADD} event with the same source and target.</li> 171 * </ul> 172 * <p> 173 * In addition, if the save or commit actions fail, this method will attempt to restore the older version of the wiki group if it 174 * exists. This will result in a <code>GROUP_REMOVE</code> event (for the new version of the Group) followed by a <code>GROUP_ADD</code> 175 * event (to indicate restoration of the old version). 176 * </p> 177 * <p> 178 * This method will register the new Group with the GroupManager. For example, {@link org.apache.wiki.auth.AuthenticationManager} 179 * attaches each Session as a GroupManager listener. Thus, the act of registering a Group with <code>setGroup</code> means that 180 * all Sessions will automatically receive group add/change/delete events immediately. 181 * </p> 182 * 183 * @param session the wiki session, which may not be <code>null</code> 184 * @param group the Group, which may not be <code>null</code> 185 * @throws WikiSecurityException if the Group cannot be saved by the back-end 186 */ 187 void setGroup( final Session session, final Group group ) throws WikiSecurityException; 188 189 /** 190 * Validates a Group, and appends any errors to the session errors list. Any validation errors are added to the wiki session's messages 191 * collection (see {@link Session#getMessages()}. 192 * 193 * @param context the current wiki context 194 * @param group the supplied Group 195 */ 196 void validateGroup( final Context context, final Group group ); 197 198 /** 199 * Checks if a String is blank or a restricted Group name, and if it is, appends an error to the Session's message list. 200 * 201 * @param context the wiki context 202 * @param name the Group name to test 203 * @throws WikiSecurityException if <code>session</code> is <code>null</code> or the Group name is illegal 204 * @see Group#RESTRICTED_GROUPNAMES 205 */ 206 void checkGroupName( final Context context, final String name ) throws WikiSecurityException; 207 208 // events processing ....................................................... 209 210 /** 211 * Registers a WikiEventListener with this instance. This is a convenience method. 212 * 213 * @param listener the event listener 214 */ 215 void addWikiEventListener( WikiEventListener listener ); 216 217 /** 218 * Un-registers a WikiEventListener with this instance. This is a convenience method. 219 * 220 * @param listener the event listener 221 */ 222 void removeWikiEventListener( WikiEventListener listener ); 223 224 /** 225 * Fires a WikiSecurityEvent of the provided type, Principal and target Object to all registered listeners. 226 * 227 * @see org.apache.wiki.event.WikiSecurityEvent 228 * @param type the event type to be fired 229 * @param target the changed Object, which may be <code>null</code> 230 */ 231 default void fireEvent( final int type, final Object target ) { 232 if( WikiEventManager.isListening( this ) ) { 233 WikiEventManager.fireEvent( this, new WikiSecurityEvent( this, type, target ) ); 234 } 235 } 236 237}