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.security.authentication.User;
34 import org.wamblee.security.authentication.UserAdministration;
37 * Utility base class for implementation of authentication rules based on the
39 * <li>The path of the resource. To obtain the path of a resource, subclasses
40 * must implement {@link #getResourcePath(Object)}. Whether a path is
41 * appropriate is determined by a
42 * {@link org.wamblee.security.authorization.AbstractPathCondition}.</li>
43 * <li>The user identity with which the resource is accessed. Whether a user is
44 * appropriate is determined by a
45 * {@link org.wamblee.security.authorization.AbstractUserCondition}.</li>
46 * <li>The operation that is requested. Whether the operation is appropriate is
47 * determined by a {@link org.wamblee.security.authorization.AbstractOperationCondition}
50 * In case all three conditions match, the condition returns the configured
51 * result passed at construction (GRANTED or DENIED). If the resource is not of
52 * the specified type, the result is UNSUPPORTED_RESOURCE, otherwise, the result
56 @Access(AccessType.PROPERTY)
57 public abstract class UrlAuthorizationRule extends AbstractAuthorizationRule {
58 private static final Logger LOGGER = Logger
59 .getLogger(UrlAuthorizationRule.class);
62 * Result that the rule will return in case there is a match.
64 private AuthorizationResult result;
67 * A condition which specifies which users the rule is for.
69 private UserCondition userCondition;
72 * Path the rule applies for.
74 private PathCondition pathCondition;
77 * Resource class that the rule applies for.
79 private Class resourceClass;
82 * Operation that this rule is for.
85 private OperationCondition operationCondition;
88 * Constructs an authorization rule. IF the group and path match, then the
89 * provided result will be returned.
92 * Result of the authorization when the path and group match.
93 * @param aUserCondition
94 * Condition to match users.
95 * @param aPathCondition
96 * Condition to match paths with.
97 * @param aResourceClass
98 * Supported resource class this is for.
99 * @param aOperationCondition
100 * Condition to match the operation with.
102 protected UrlAuthorizationRule(AuthorizationResult aResult,
103 UserCondition aUserCondition, PathCondition aPathCondition,
104 Class aResourceClass, OperationCondition aOperationCondition) {
105 if (!aResult.equals(GRANTED) && !aResult.equals(DENIED)) {
106 throw new IllegalArgumentException(
107 "Only GRANTED or DENIED may be used: " + aResult);
111 userCondition = aUserCondition;
112 pathCondition = aPathCondition;
113 resourceClass = aResourceClass;
114 operationCondition = aOperationCondition;
121 protected UrlAuthorizationRule(Class aResourceClass) {
123 userCondition = null;
124 pathCondition = null;
125 resourceClass = aResourceClass;
126 operationCondition = null;
133 protected UrlAuthorizationRule() {
135 userCondition = null;
136 pathCondition = null;
137 resourceClass = null;
138 operationCondition = null;
145 * org.wamblee.security.authorization.AuthorizationRule#getSupportedTypes()
148 public Class[] getSupportedTypes() {
149 return new Class[] { resourceClass };
156 * org.wamblee.security.authorization.AuthorizationRule#isAllowed(java.lang
157 * .Object, org.wamblee.security.authorization.Operation)
159 public AuthorizationResult isAllowed(Object aResource,
160 Operation aOperation, String aUser) {
161 if (!resourceClass.isInstance(aResource)) {
162 return UNSUPPORTED_RESOURCE;
165 String path = getResourcePath(aResource);
167 return isAllowedWithPath(path, aOperation, aUser);
171 * Determines if the operation is allowed on the resource.
174 * Path of the resource.
176 * Operation to be done.
178 * Currently logged in user or null if no user is logged in.
180 * @return Authorization result,
182 protected AuthorizationResult isAllowedWithPath(String aPath, Operation aOperation,
184 if (!pathCondition.matches(aPath)) {
188 if (!operationCondition.matches(aOperation)) {
192 if (!userCondition.matches(aUser)) {
200 * Gets the path of the resource.
203 * Resource, guaranteed to be an instance of
204 * {@link #resourceClass}.
206 * @return Path of the resource.
208 protected abstract String getResourcePath(Object aResource);
213 * @see java.lang.Object#toString()
216 public String toString() {
217 return "UrlAUthorizationRule(result = " + result +
218 ", pathCondition = " + pathCondition + ", userCondition = " +
219 userCondition + ", resourceClass = " + resourceClass + ")";
223 * Gets the authorization result for OR mapping.
227 @Column(name = "AUTH_RESULT", nullable = false)
228 protected String getAuthorizationResultString() {
229 if (result == null) {
233 return result.toString();
237 * Sets the authorization result, for OR mapping.
242 protected void setAuthorizationResultString(String aResult) {
243 result = AuthorizationResult.valueOf(aResult);
246 @Column(name = "RES_CLASSNAME", nullable = false)
247 protected String getResourceClassName() {
248 if (resourceClass == null) {
252 return resourceClass.getName();
255 protected void setResourceClassName(String aResourceClass) {
257 resourceClass = Class.forName(aResourceClass);
258 } catch (ClassNotFoundException e) {
259 LOGGER.error("Cannot find resource class '" + aResourceClass + "'",
261 throw new IllegalArgumentException(e.getMessage(), e);
267 * @return Returns the operationCondition.
269 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class, orphanRemoval = true)
270 @JoinColumn(name = "OPER_COND_PK")
271 public OperationCondition getOperationCondition() {
272 return operationCondition;
277 * @param aOperationCondition
278 * The operationCondition to set.
280 protected void setOperationCondition(OperationCondition aOperationCondition) {
281 operationCondition = aOperationCondition;
286 * @return Returns the pathCondition.
288 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class, orphanRemoval = true)
289 @JoinColumn(name = "PATH_COND_PK")
290 public PathCondition getPathCondition() {
291 return pathCondition;
296 * @param aPathCondition
297 * The pathCondition to set.
299 protected void setPathCondition(PathCondition aPathCondition) {
300 pathCondition = aPathCondition;
305 * @return Returns the userCondition.
307 @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class, orphanRemoval = true)
308 @JoinColumn(name = "USER_COND_PK")
309 public UserCondition getUserCondition() {
310 return userCondition;
315 * @param aUserCondition
316 * The userCondition to set.
318 protected void setUserCondition(UserCondition aUserCondition) {
319 userCondition = aUserCondition;
323 public void setUserAdministration(UserAdministration aAdmin) {
324 userCondition.setUserAdmin(aAdmin);