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 java.util.ArrayList;
20 import java.util.List;
22 import org.wamblee.persistence.AbstractPersistent;
23 import org.wamblee.usermgt.User;
24 import org.wamblee.usermgt.UserAccessor;
27 * Default implementation of an authorization service.
28 * To determine whether access to a resource is allowed, the service consults a number
29 * of authorization rules in a fixed order. The first rule that gives a result GRANTED or
30 * DENIED determines the result of the evaluation. Rules that return any other result are
31 * ignoed. If none of the rules match, than access is denied.
33 * @author Erik Brakkee
35 public class DefaultAuthorizationService extends AbstractPersistent implements AuthorizationService {
38 * List of ordered authorization rules.
40 private List<AuthorizationRule> _rules;
43 * User accessor used to obtain the current user.
45 private UserAccessor _userAccessor;
48 * Name for this instance of the authorization service.
53 * Constructs the service.
54 * @param aAccessor User accessor.
55 * @param aName Name of this instance of the service.
57 public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
58 _rules = new ArrayList<AuthorizationRule>();
59 _userAccessor = aAccessor;
64 * Constructs the authorization service.
66 public DefaultAuthorizationService() {
67 _rules = new ArrayList<AuthorizationRule>();
73 * Sets the user accessor.
74 * @param aUserAccessor User accessor.
76 public void setUserAccessor(UserAccessor aUserAccessor) {
77 _userAccessor = aUserAccessor;
81 * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation)
83 public boolean isAllowed(Object aResource, Operation aOperation) {
84 User user = _userAccessor.getCurrentUser();
85 for (AuthorizationRule rule: _rules) {
86 switch ( rule.isAllowed(aResource, aOperation, user)) {
87 case DENIED: { return false; }
88 case GRANTED: { return true; }
95 * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
97 public <T> T check(T aResource, Operation aOperation) {
98 if ( !isAllowed(aResource, aOperation)) {
99 throw new AuthorizationException(aResource, aOperation);
104 protected String getName() {
108 public void setName(String aName) {
113 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
115 public AuthorizationRule[] getRules() {
116 return _rules.toArray(new AuthorizationRule[0]);
120 * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
122 public void appendRule(AuthorizationRule aRule) {
127 * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int, org.wamblee.security.authorization.AuthorizationRule)
129 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
130 _rules.add(aIndex, aRule);
134 * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
136 public void removeRule(int aIndex) {
137 _rules.remove(aIndex);
144 protected List<AuthorizationRule> getMappedRules() {
150 * @param aRules The rules.
152 protected void setMappedRules(List<AuthorizationRule> aRules) {