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.ui; 020 021import org.apache.wiki.api.core.Command; 022import org.apache.wiki.api.core.ContextEnum; 023import org.apache.wiki.auth.GroupPrincipal; 024import org.apache.wiki.auth.permissions.GroupPermission; 025 026import java.security.Permission; 027 028/** 029 * <p>Defines Commands for viewing, editing and deleting wiki groups. GroupCommands can be combined with GroupPrincipals to produce 030 * targeted Commands.</p> 031 * <p>This class is not <code>final</code>; it may be extended in the future.</p> 032 * @since 2.4.22 033 */ 034public final class GroupCommand extends AbstractCommand { 035 036 /** GroupCommand for deleting a group. */ 037 public static final Command DELETE_GROUP = new GroupCommand( ContextEnum.GROUP_DELETE,null, GroupPermission.DELETE_ACTION ); 038 039 /** GroupCommand for editing a group. */ 040 public static final Command EDIT_GROUP = new GroupCommand( ContextEnum.GROUP_EDIT, null, GroupPermission.EDIT_ACTION ); 041 042 /** GroupCommand for viewing a group. */ 043 public static final Command VIEW_GROUP = new GroupCommand( ContextEnum.GROUP_VIEW, null, GroupPermission.VIEW_ACTION ); 044 045 private final String m_action; 046 047 private final Permission m_permission; 048 049 /** 050 * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The WikiPage for this command is 051 * initialized to <code>null</code>. 052 * 053 * @param currentContext the current context. 054 * @param target the target of this command (a GroupPrincipal representing a Group); may be <code>null</code> 055 * @param action the action used to construct a suitable GroupPermission 056 * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code> 057 */ 058 private GroupCommand( final ContextEnum currentContext, final GroupPrincipal target, final String action ) { 059 this( currentContext.getRequestContext(), currentContext.getUrlPattern(), currentContext.getContentTemplate(), target, action ); 060 } 061 062 /** 063 * Constructs a new Command with a specified wiki context, URL pattern, type, and content template. The WikiPage for this command is 064 * initialized to <code>null</code>. 065 * 066 * @param requestContext the request context 067 * @param urlPattern the URL pattern 068 * @param target the target of this command (a GroupPrincipal representing a Group); may be <code>null</code> 069 * @param action the action used to construct a suitable GroupPermission 070 * @param contentTemplate the content template; may be <code>null</code> 071 * @throws IllegalArgumentException if the request content, URL pattern, or type is <code>null</code> 072 */ 073 private GroupCommand( final String requestContext, 074 final String urlPattern, 075 final String contentTemplate, 076 final GroupPrincipal target, 077 final String action ) { 078 super( requestContext, urlPattern, contentTemplate, target ); 079 m_action = action; 080 if ( target == null || m_action == null ) { 081 m_permission = null; 082 } else { 083 m_permission = new GroupPermission( target.getName(), action ); 084 } 085 } 086 087 /** 088 * Creates and returns a targeted Command by combining a GroupPrincipal with this Command. The supplied <code>target</code> object 089 * must be non-<code>null</code> and of type GroupPrincipal. If the target is not of the correct type, this method throws an 090 * {@link IllegalArgumentException}. 091 * 092 * @param target the GroupPrincipal to combine into the current Command 093 * @return the new, targeted command 094 */ 095 @Override 096 public Command targetedCommand(final Object target ) { 097 if( !( target instanceof GroupPrincipal ) ) { 098 throw new IllegalArgumentException( "Target must non-null and of type GroupPrincipal." ); 099 } 100 return new GroupCommand( getRequestContext(), getURLPattern(), getContentTemplate(), ( GroupPrincipal )target, m_action ); 101 } 102 103 /** 104 * Returns the name of the command, which will either be the target (if specified), or the "friendly name" for the JSP. 105 * 106 * @return the name 107 * @see org.apache.wiki.api.core.Command#getName() 108 */ 109 @Override 110 public String getName() { 111 final Object target = getTarget(); 112 if ( target == null ) { 113 return getJSPFriendlyName(); 114 } 115 return ( ( GroupPrincipal ) target ).getName(); 116 } 117 118 /** 119 * Returns the permission required to execute this GroupCommand. 120 * 121 * @return the permission 122 * @see org.apache.wiki.api.core.Command#requiredPermission() 123 */ 124 @Override 125 public Permission requiredPermission() { 126 return m_permission; 127 } 128 129}