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;
19 import org.wamblee.security.AbstractPersistent;
20 import org.wamblee.usermgt.User;
21 import org.wamblee.usermgt.UserAccessor;
23 import java.util.ArrayList;
24 import java.util.List;
27 * Default implementation of an authorization service. To determine whether
28 * access to a resource is allowed, the service consults a number of
29 * authorization rules in a fixed order. The first rule that gives a result
30 * GRANTED or DENIED determines the result of the evaluation. Rules that return
31 * any other result are ignoed. If none of the rules match, than access is
34 * @author Erik Brakkee
36 public class DefaultAuthorizationService extends AbstractPersistent implements
37 AuthorizationService {
39 * List of ordered authorization rules.
41 private List<AuthorizationRule> rules;
44 * User accessor used to obtain the current user.
46 private UserAccessor userAccessor;
49 * Name for this instance of the authorization service.
54 * Constructs the service.
59 * Name of this instance of the service.
61 public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
62 rules = new ArrayList<AuthorizationRule>();
63 userAccessor = aAccessor;
68 * Constructs the authorization service.
70 public DefaultAuthorizationService() {
71 rules = new ArrayList<AuthorizationRule>();
77 * Sets the user accessor.
79 * @param aUserAccessor
82 public void setUserAccessor(UserAccessor aUserAccessor) {
83 userAccessor = aUserAccessor;
90 * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
91 * .lang.Object, org.wamblee.security.authorization.Operation)
93 public boolean isAllowed(Object aResource, Operation aOperation) {
94 User user = userAccessor.getCurrentUser();
96 for (AuthorizationRule rule : rules) {
97 switch (rule.isAllowed(aResource, aOperation, user)) {
112 * @see org.wamblee.security.authorization.AuthorizationService#check(T,
113 * org.wamblee.security.authorization.Operation)
115 public <T> T check(T aResource, Operation aOperation) {
116 if (!isAllowed(aResource, aOperation)) {
117 throw new AuthorizationException(aResource, aOperation);
123 protected String getName() {
127 public void setName(String aName) {
134 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
136 public AuthorizationRule[] getRules() {
137 return rules.toArray(new AuthorizationRule[0]);
144 * org.wamblee.security.authorization.AuthorizationService#appendRule(org
145 * .wamblee.security.authorization.AuthorizationRule)
147 public void appendRule(AuthorizationRule aRule) {
155 * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
156 * (int, org.wamblee.security.authorization.AuthorizationRule)
158 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
159 rules.add(aIndex, aRule);
166 * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
168 public void removeRule(int aIndex) {
169 rules.remove(aIndex);
177 protected List<AuthorizationRule> getMappedRules() {
187 protected void setMappedRules(List<AuthorizationRule> aRules) {