7686c334d7826c1907a1930a3dc39b5495658a4e
[utils] / security / impl / src / main / java / org / wamblee / security / authorization / DefaultAuthorizationService.java
1 /*
2  * Copyright 2005-2010 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 java.util.ArrayList;
19 import java.util.List;
20
21 import javax.persistence.CascadeType;
22 import javax.persistence.DiscriminatorValue;
23 import javax.persistence.Entity;
24 import javax.persistence.ManyToMany;
25 import javax.persistence.OneToMany;
26 import javax.persistence.OrderColumn;
27 import javax.persistence.Transient;
28
29 import org.wamblee.security.authentication.User;
30 import org.wamblee.security.authentication.UserAccessor;
31 import org.wamblee.security.authentication.UserAdministration;
32
33 /**
34  * Default implementation of an authorization service. To determine whether
35  * access to a resource is allowed, the service consults a number of
36  * authorization rules in a fixed order. The first rule that gives a result
37  * GRANTED or DENIED determines the result of the evaluation. Rules that return
38  * any other result are ignoed. If none of the rules match, than access is
39  * denied.
40  * 
41  * @author Erik Brakkee
42  */
43 @Entity
44 @DiscriminatorValue("DEFAULT")
45 public class DefaultAuthorizationService extends AbstractAuthorizationService {
46
47     /**
48      * List of ordered authorization rules.
49      */
50     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = AbstractAuthorizationRule.class)
51     @OrderColumn(name = "RULE_INDEX")
52     private List<AuthorizationRule> rules;
53
54     /**
55      * User accessor used to obtain the current user.
56      */
57     @Transient
58     private UserAccessor userAccessor;
59
60     @Transient
61     private UserAdministration userAdmin;
62
63     /**
64      * Constructs the service.
65      * 
66      * @param aAccessor
67      *            User accessor.
68      * @param aUserAdmin
69      *            User administration.
70      * @param aName
71      *            Name of this instance of the service.
72      */
73     public DefaultAuthorizationService(UserAccessor aAccessor,
74         UserAdministration aUserAdmin, String aName) {
75         super(aName);
76         rules = new ArrayList<AuthorizationRule>();
77         userAccessor = aAccessor;
78         userAdmin = aUserAdmin; 
79     }
80
81     /**
82      * Constructs the authorization service.
83      */
84     public DefaultAuthorizationService() {
85         rules = new ArrayList<AuthorizationRule>();
86         userAccessor = null;
87         userAdmin = null; 
88     }
89
90    
91     @Override
92     public void setUserAccessor(UserAccessor aUserAccessor) {
93         userAccessor = aUserAccessor;
94     }
95     
96     @Override
97     public void setUserAdministration(UserAdministration aUserAdmin) {
98         userAdmin = aUserAdmin;
99         System.out.println("Setting userAdmin");
100         for (AuthorizationRule rule: rules) { 
101             System.out.println("Setting useradmin on " + rule);
102             rule.setUserAdministration(userAdmin);
103         }
104     }
105
106     /*
107      * (non-Javadoc)
108      * 
109      * @see
110      * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
111      * .lang.Object, org.wamblee.security.authorization.Operation)
112      */
113     public boolean isAllowed(Object aResource, Operation aOperation) {
114         String user = userAccessor.getCurrentUser();
115
116         for (AuthorizationRule rule : rules) {
117             switch (rule.isAllowed(aResource, aOperation, user)) {
118             case DENIED:
119                 return false;
120
121             case GRANTED:
122                 return true;
123             }
124         }
125
126         return false;
127     }
128
129     /*
130      * (non-Javadoc)
131      * 
132      * @see org.wamblee.security.authorization.AuthorizationService#check(T,
133      * org.wamblee.security.authorization.Operation)
134      */
135     public <T> T check(T aResource, Operation aOperation) {
136         if (!isAllowed(aResource, aOperation)) {
137             throw new AuthorizationException(aResource, aOperation);
138         }
139
140         return aResource;
141     }
142
143     /*
144      * (non-Javadoc)
145      * 
146      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
147      */
148     public AuthorizationRule[] getRules() {
149         return rules.toArray(new AbstractAuthorizationRule[0]);
150     }
151
152     /*
153      * (non-Javadoc)
154      * 
155      * @see
156      * org.wamblee.security.authorization.AuthorizationService#appendRule(org
157      * .wamblee.security.authorization.AuthorizationRule)
158      */
159     public void appendRule(AuthorizationRule aRule) {
160         aRule.setUserAdministration(userAdmin);
161         rules.add(aRule);
162     }
163
164     /*
165      * (non-Javadoc)
166      * 
167      * @see
168      * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
169      * (int, org.wamblee.security.authorization.AuthorizationRule)
170      */
171     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
172         aRule.setUserAdministration(userAdmin);
173         rules.add(aIndex, aRule);
174     }
175
176     /*
177      * (non-Javadoc)
178      * 
179      * @see
180      * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
181      */
182     public void removeRule(int aIndex) {
183         rules.remove(aIndex);
184     }
185
186     /**
187      * For OR mapping.
188      * 
189      * @return The rules.
190      */
191     protected List<AuthorizationRule> getMappedRules() {
192         return rules;
193     }
194
195     /**
196      * For OR mapping.
197      * 
198      * @param aRules
199      *            The rules.
200      */
201     protected void setMappedRules(List<AuthorizationRule> aRules) {
202         rules = aRules;
203         for (AuthorizationRule rule: rules) { 
204             rule.setUserAdministration(userAdmin);
205         }
206     }
207 }