2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.security.authorization;
19 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
20 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
21 import static org.wamblee.security.authorization.AuthorizationResult.UNDECIDED;
22 import static org.wamblee.security.authorization.AuthorizationResult.UNSUPPORTED_RESOURCE;
24 import org.apache.log4j.Logger;
25 import org.wamblee.persistence.AbstractPersistent;
26 import org.wamblee.usermgt.User;
29 * Utility base class for implementation of authentication rules based on the
31 * <li> The path of the resource. To obtain the path of a resource, subclasses
32 * must implement {@link #getResourcePath(Object)}.
33 * Whether a path is appropriate is determined by a
34 * {@link org.wamblee.security.authorization.PathCondition}.
36 * <li> The user identity with which the resource is accessed.
37 * Whether a user is appropriate is determined by
38 * a {@link org.wamblee.security.authorization.UserCondition}.
40 * <li> The operation that is requested.
41 * Whether the operation is appropriate is determined by a
42 * {@link org.wamblee.security.authorization.OperationCondition}.
46 * In case all three conditions match, the condition returns the configured
47 * result passed at construction (GRANTED or DENIED). If the resource is not
48 * of the specified type, the result is UNSUPPORTED_RESOURCE, otherwise, the
49 * result is UNDECIDED.
51 public abstract class UrlAuthorizationRule extends AbstractPersistent implements AuthorizationRule {
53 private static final Logger LOGGER = Logger.getLogger(UrlAuthorizationRule.class);
56 * Result that the rule will return in case there is a match.
58 private AuthorizationResult _result;
61 * A condition which specifies which users the rule is for.
63 private UserCondition _userCondition;
66 * Path the rule applies for.
68 private PathCondition _pathCondition;
71 * Resource class that the rule applies for.
73 private Class _resourceClass;
76 * Operation that this rule is for.
78 private OperationCondition _operationCondition;
81 * Constructs an authorization rule.
82 * IF the group and path match, then the provided result will be returned.
83 * @param aResult Result of the authorization when the path and group match.
84 * @param aUserCondition Condition to match users.
85 * @param aPathCondition Condition to match paths with.
86 * @param aResourceClass Supported resource class this is for.
87 * @param aOperationCondition Condition to match the operation with.
89 protected UrlAuthorizationRule(AuthorizationResult aResult, UserCondition aUserCondition,
90 PathCondition aPathCondition, Class aResourceClass, OperationCondition aOperationCondition) {
91 if ( !aResult.equals(GRANTED) && !aResult.equals(DENIED)) {
92 throw new IllegalArgumentException("Only GRANTED or DENIED may be used: " + aResult);
95 _userCondition = aUserCondition;
96 _pathCondition = aPathCondition;
97 _resourceClass = aResourceClass;
98 _operationCondition = aOperationCondition;
105 protected UrlAuthorizationRule(Class aResourceClass) {
107 _userCondition = null;
108 _pathCondition = null;
109 _resourceClass = aResourceClass;
110 _operationCondition = null;
117 protected UrlAuthorizationRule() {
119 _userCondition = null;
120 _pathCondition = null;
121 _resourceClass = null;
122 _operationCondition = null;
129 * @see org.wamblee.security.authorization.AuthorizationRule#getSupportedTypes()
131 public Class[] getSupportedTypes() {
132 return new Class[] { _resourceClass };
138 * @see org.wamblee.security.authorization.AuthorizationRule#isAllowed(java.lang.Object,
139 * org.wamblee.security.authorization.Operation)
141 public AuthorizationResult isAllowed(Object aResource, Operation anOperation, User aUser) {
142 if ( ! _resourceClass.isInstance(aResource)) {
143 return UNSUPPORTED_RESOURCE;
145 String path = getResourcePath(aResource);
146 return isAllowed(path, anOperation, aUser);
150 * Determines if the operation is allowed on the resource.
151 * @param aPath Path of the resource.
152 * @param aOperation Operation to be done.
153 * @param aUser Currently logged in user or null if no user is logged in.
154 * @return Authorization result,
156 protected AuthorizationResult isAllowed(String aPath, Operation aOperation, User aUser) {
157 if ( ! _pathCondition.matches(aPath) ) {
160 if ( !_operationCondition.matches(aOperation) ) {
163 if ( !_userCondition.matches(aUser)) {
170 * Gets the path of the resource.
171 * @param aResource Resource, guaranteed to be an instance of
172 * {@link #_resourceClass}.
173 * @return Path of the resource.
175 protected abstract String getResourcePath(Object aResource);
178 * @see java.lang.Object#toString()
181 public String toString() {
182 return "UrlAUthorizationRule(result = " + _result +
183 ", pathCondition = " + _pathCondition +
184 ", userCondition = " + _userCondition +
185 ", resourceClass = " + _resourceClass + ")";
189 * Gets the authorization result for OR mapping.
192 protected String getAuthorizationResultString() {
193 if ( _result == null ) {
196 return _result.toString();
200 * Sets the authorization result, for OR mapping.
201 * @param aResult Result.
203 protected void setAuthorizationResultString(String aResult) {
204 _result = AuthorizationResult.valueOf(aResult);
207 protected String getResourceClassName() {
208 if ( _resourceClass == null ) {
211 return _resourceClass.getName();
214 protected void setResourceClassName(String aResourceClass) {
216 _resourceClass = Class.forName(aResourceClass);
217 } catch (ClassNotFoundException e) {
218 LOGGER.error("Cannot find resource class '" + aResourceClass + "'", e);
219 throw new IllegalArgumentException(e.getMessage(), e);
224 * @return Returns the _operationCondition.
226 public OperationCondition getOperationCondition() {
227 return _operationCondition;
231 * @param aOperationCondition The _operationCondition to set.
233 protected void setOperationCondition(OperationCondition aOperationCondition) {
234 _operationCondition = aOperationCondition;
238 * @return Returns the _pathCondition.
240 public PathCondition getPathCondition() {
241 return _pathCondition;
245 * @param aPathCondition The _pathCondition to set.
247 protected void setPathCondition(PathCondition aPathCondition) {
248 _pathCondition = aPathCondition;
252 * @return Returns the _userCondition.
254 public UserCondition getUserCondition() {
255 return _userCondition;
259 * @param aUserCondition The _userCondition to set.
261 protected void setUserCondition(UserCondition aUserCondition) {
262 _userCondition = aUserCondition;