2 * Copyright 2005-2010 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.
16 package org.wamblee.security.authorization;
18 import static org.wamblee.security.authorization.AuthorizationResult.*;
20 import javax.enterprise.inject.Typed;
21 import javax.persistence.Access;
22 import javax.persistence.AccessType;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.DiscriminatorValue;
26 import javax.persistence.Entity;
27 import javax.persistence.JoinColumn;
28 import javax.persistence.ManyToOne;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Transient;
32 import org.apache.log4j.Logger;
33 import org.wamblee.usermgt.User;
36 * Utility base class for implementation of authentication rules based on the
38 * <li>The path of the resource. To obtain the path of a resource, subclasses
39 * must implement {@link #getResourcePath(Object)}. Whether a path is
40 * appropriate is determined by a
41 * {@link org.wamblee.security.authorization.AbstractPathCondition}.</li>
42 * <li>The user identity with which the resource is accessed. Whether a user is
43 * appropriate is determined by a
44 * {@link org.wamblee.security.authorization.AbstractUserCondition}.</li>
45 * <li>The operation that is requested. Whether the operation is appropriate is
46 * determined by a {@link org.wamblee.security.authorization.AbstractOperationCondition}
49 * In case all three conditions match, the condition returns the configured
50 * result passed at construction (GRANTED or DENIED). If the resource is not of
51 * the specified type, the result is UNSUPPORTED_RESOURCE, otherwise, the result
55 @Access(AccessType.PROPERTY)
56 public abstract class UrlAuthorizationRule extends AbstractAuthorizationRule {
57 private static final Logger LOGGER = Logger
58 .getLogger(UrlAuthorizationRule.class);
61 * Result that the rule will return in case there is a match.
63 private AuthorizationResult result;
66 * A condition which specifies which users the rule is for.
68 private UserCondition userCondition;
71 * Path the rule applies for.
73 private PathCondition pathCondition;
76 * Resource class that the rule applies for.
78 private Class resourceClass;
81 * Operation that this rule is for.
84 private OperationCondition operationCondition;
87 * Constructs an authorization rule. IF the group and path match, then the
88 * provided result will be returned.
91 * Result of the authorization when the path and group match.
92 * @param aUserCondition
93 * Condition to match users.
94 * @param aPathCondition
95 * Condition to match paths with.
96 * @param aResourceClass
97 * Supported resource class this is for.
98 * @param aOperationCondition
99 * Condition to match the operation with.
101 protected UrlAuthorizationRule(AuthorizationResult aResult,
102 UserCondition aUserCondition, PathCondition aPathCondition,
103 Class aResourceClass, OperationCondition aOperationCondition) {
104 if (!aResult.equals(GRANTED) && !aResult.equals(DENIED)) {
105 throw new IllegalArgumentException(
106 "Only GRANTED or DENIED may be used: " + aResult);
110 userCondition = aUserCondition;
111 pathCondition = aPathCondition;
112 resourceClass = aResourceClass;
113 operationCondition = aOperationCondition;
120 protected UrlAuthorizationRule(Class aResourceClass) {
122 userCondition = null;
123 pathCondition = null;
124 resourceClass = aResourceClass;
125 operationCondition = null;
132 protected UrlAuthorizationRule() {
134 userCondition = null;
135 pathCondition = null;
136 resourceClass = null;
137 operationCondition = null;
144 * org.wamblee.security.authorization.AuthorizationRule#getSupportedTypes()
147 public Class[] getSupportedTypes() {
148 return new Class[] { resourceClass };
155 * org.wamblee.security.authorization.AuthorizationRule#isAllowed(java.lang
156 * .Object, org.wamblee.security.authorization.Operation)
158 public AuthorizationResult isAllowed(Object aResource,
159 Operation aOperation, User aUser) {
160 if (!resourceClass.isInstance(aResource)) {
161 return UNSUPPORTED_RESOURCE;
164 String path = getResourcePath(aResource);
166 return isAllowed(path, aOperation, aUser);
170 * Determines if the operation is allowed on the resource.
173 * Path of the resource.
175 * Operation to be done.
177 * Currently logged in user or null if no user is logged in.
179 * @return Authorization result,
181 protected AuthorizationResult isAllowed(String aPath, Operation aOperation,
183 if (!pathCondition.matches(aPath)) {
187 if (!operationCondition.matches(aOperation)) {
191 if (!userCondition.matches(aUser)) {
199 * Gets the path of the resource.
202 * Resource, guaranteed to be an instance of
203 * {@link #resourceClass}.
205 * @return Path of the resource.
207 protected abstract String getResourcePath(Object aResource);
212 * @see java.lang.Object#toString()
215 public String toString() {
216 return "UrlAUthorizationRule(result = " + result +
217 ", pathCondition = " + pathCondition + ", userCondition = " +
218 userCondition + ", resourceClass = " + resourceClass + ")";
222 * Gets the authorization result for OR mapping.
226 @Column(name = "AUTH_RESULT", nullable = false)
227 protected String getAuthorizationResultString() {
228 if (result == null) {
232 return result.toString();
236 * Sets the authorization result, for OR mapping.
241 protected void setAuthorizationResultString(String aResult) {
242 result = AuthorizationResult.valueOf(aResult);
245 @Column(name = "RES_CLASSNAME", nullable = false)
246 protected String getResourceClassName() {
247 if (resourceClass == null) {
251 return resourceClass.getName();
254 protected void setResourceClassName(String aResourceClass) {
256 resourceClass = Class.forName(aResourceClass);
257 } catch (ClassNotFoundException e) {
258 LOGGER.error("Cannot find resource class '" + aResourceClass + "'",
260 throw new IllegalArgumentException(e.getMessage(), e);
266 * @return Returns the operationCondition.
268 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class, orphanRemoval = true)
269 @JoinColumn(name = "OPER_COND_PK")
270 public OperationCondition getOperationCondition() {
271 return operationCondition;
276 * @param aOperationCondition
277 * The operationCondition to set.
279 protected void setOperationCondition(OperationCondition aOperationCondition) {
280 operationCondition = aOperationCondition;
285 * @return Returns the pathCondition.
287 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class, orphanRemoval = true)
288 @JoinColumn(name = "PATH_COND_PK")
289 public PathCondition getPathCondition() {
290 return pathCondition;
295 * @param aPathCondition
296 * The pathCondition to set.
298 protected void setPathCondition(PathCondition aPathCondition) {
299 pathCondition = aPathCondition;
304 * @return Returns the userCondition.
306 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class, orphanRemoval = true)
307 @JoinColumn(name = "USER_COND_PK")
308 public UserCondition getUserCondition() {
309 return userCondition;
314 * @param aUserCondition
315 * The userCondition to set.
317 protected void setUserCondition(UserCondition aUserCondition) {
318 userCondition = aUserCondition;