Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / main / java / org / wamblee / security / authorization / DefaultAuthorizationService.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.wamblee.security.authorization;
17
18 import org.wamblee.persistence.AbstractPersistent;
19
20 import org.wamblee.usermgt.User;
21 import org.wamblee.usermgt.UserAccessor;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
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
32  * denied.
33  * 
34  * @author Erik Brakkee
35  */
36 public class DefaultAuthorizationService extends AbstractPersistent implements
37     AuthorizationService {
38     /**
39      * List of ordered authorization rules.
40      */
41     private List<AuthorizationRule> rules;
42
43     /**
44      * User accessor used to obtain the current user.
45      */
46     private UserAccessor userAccessor;
47
48     /**
49      * Name for this instance of the authorization service.
50      */
51     private String name;
52
53     /**
54      * Constructs the service.
55      * 
56      * @param aAccessor
57      *            User accessor.
58      * @param aName
59      *            Name of this instance of the service.
60      */
61     public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
62         rules = new ArrayList<AuthorizationRule>();
63         userAccessor = aAccessor;
64         name = aName;
65     }
66
67     /**
68      * Constructs the authorization service.
69      */
70     public DefaultAuthorizationService() {
71         rules = new ArrayList<AuthorizationRule>();
72         userAccessor = null;
73         name = null;
74     }
75
76     /**
77      * Sets the user accessor.
78      * 
79      * @param aUserAccessor
80      *            User accessor.
81      */
82     public void setUserAccessor(UserAccessor aUserAccessor) {
83         userAccessor = aUserAccessor;
84     }
85
86     /*
87      * (non-Javadoc)
88      * 
89      * @see
90      * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
91      * .lang.Object, org.wamblee.security.authorization.Operation)
92      */
93     public boolean isAllowed(Object aResource, Operation aOperation) {
94         User user = userAccessor.getCurrentUser();
95
96         for (AuthorizationRule rule : rules) {
97             switch (rule.isAllowed(aResource, aOperation, user)) {
98             case DENIED:
99                 return false;
100
101             case GRANTED:
102                 return true;
103             }
104         }
105
106         return false;
107     }
108
109     /*
110      * (non-Javadoc)
111      * 
112      * @see org.wamblee.security.authorization.AuthorizationService#check(T,
113      * org.wamblee.security.authorization.Operation)
114      */
115     public <T> T check(T aResource, Operation aOperation) {
116         if (!isAllowed(aResource, aOperation)) {
117             throw new AuthorizationException(aResource, aOperation);
118         }
119
120         return aResource;
121     }
122
123     protected String getName() {
124         return name;
125     }
126
127     public void setName(String aName) {
128         name = aName;
129     }
130
131     /*
132      * (non-Javadoc)
133      * 
134      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
135      */
136     public AuthorizationRule[] getRules() {
137         return rules.toArray(new AuthorizationRule[0]);
138     }
139
140     /*
141      * (non-Javadoc)
142      * 
143      * @see
144      * org.wamblee.security.authorization.AuthorizationService#appendRule(org
145      * .wamblee.security.authorization.AuthorizationRule)
146      */
147     public void appendRule(AuthorizationRule aRule) {
148         rules.add(aRule);
149     }
150
151     /*
152      * (non-Javadoc)
153      * 
154      * @see
155      * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
156      * (int, org.wamblee.security.authorization.AuthorizationRule)
157      */
158     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
159         rules.add(aIndex, aRule);
160     }
161
162     /*
163      * (non-Javadoc)
164      * 
165      * @see
166      * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
167      */
168     public void removeRule(int aIndex) {
169         rules.remove(aIndex);
170     }
171
172     /**
173      * For OR mapping.
174      * 
175      * @return The rules.
176      */
177     protected List<AuthorizationRule> getMappedRules() {
178         return rules;
179     }
180
181     /**
182      * For OR mapping.
183      * 
184      * @param aRules
185      *            The rules.
186      */
187     protected void setMappedRules(List<AuthorizationRule> aRules) {
188         rules = aRules;
189     }
190 }