(no commit message)
[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.JoinColumn;
25 import javax.persistence.JoinTable;
26 import javax.persistence.OneToMany;
27 import javax.persistence.OrderColumn;
28 import javax.persistence.Transient;
29
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     @JoinTable(name = "SEC_AUTH_SVC_RULE", joinColumns = { @JoinColumn(name = "SVC_ID") }, inverseJoinColumns = { @JoinColumn(name = "RULE_ID") })
52     @OrderColumn(name = "RULE_INDEX")
53     private List<AuthorizationRule> rules;
54
55     /**
56      * User accessor used to obtain the current user.
57      */
58     @Transient
59     private UserAccessor userAccessor;
60
61     @Transient
62     private UserAdministration userAdmin;
63
64     /**
65      * Constructs the service.
66      * 
67      * @param aAccessor
68      *            User accessor.
69      * @param aUserAdmin
70      *            User administration.
71      * @param aName
72      *            Name of this instance of the service.
73      */
74     public DefaultAuthorizationService(UserAccessor aAccessor,
75         UserAdministration aUserAdmin, String aName) {
76         super(aName);
77         rules = new ArrayList<AuthorizationRule>();
78         userAccessor = aAccessor;
79         userAdmin = aUserAdmin;
80     }
81
82     /**
83      * Constructs the authorization service.
84      */
85     public DefaultAuthorizationService() {
86         rules = new ArrayList<AuthorizationRule>();
87         userAccessor = null;
88         userAdmin = null;
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         for (AuthorizationRule rule : rules) {
100             rule.setUserAdministration(userAdmin);
101         }
102     }
103
104     /*
105      * (non-Javadoc)
106      * 
107      * @see
108      * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
109      * .lang.Object, org.wamblee.security.authorization.Operation)
110      */
111     public boolean isAllowed(Object aResource, Operation aOperation) {
112         String user = userAccessor.getCurrentUser();
113
114         for (AuthorizationRule rule : rules) {
115             switch (rule.isAllowed(aResource, aOperation, user)) {
116             case DENIED:
117                 return false;
118
119             case GRANTED:
120                 return true;
121             }
122         }
123
124         return false;
125     }
126
127     /*
128      * (non-Javadoc)
129      * 
130      * @see org.wamblee.security.authorization.AuthorizationService#check(T,
131      * org.wamblee.security.authorization.Operation)
132      */
133     public <T> T check(T aResource, Operation aOperation) {
134         if (!isAllowed(aResource, aOperation)) {
135             throw new AuthorizationException(aResource, aOperation);
136         }
137
138         return aResource;
139     }
140
141     /*
142      * (non-Javadoc)
143      * 
144      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
145      */
146     public AuthorizationRule[] getRules() {
147         return rules.toArray(new AbstractAuthorizationRule[0]);
148     }
149
150     /*
151      * (non-Javadoc)
152      * 
153      * @see
154      * org.wamblee.security.authorization.AuthorizationService#appendRule(org
155      * .wamblee.security.authorization.AuthorizationRule)
156      */
157     public void appendRule(AuthorizationRule aRule) {
158         aRule.setUserAdministration(userAdmin);
159         rules.add(aRule);
160     }
161
162     /*
163      * (non-Javadoc)
164      * 
165      * @see
166      * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
167      * (int, org.wamblee.security.authorization.AuthorizationRule)
168      */
169     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
170         aRule.setUserAdministration(userAdmin);
171         rules.add(aIndex, aRule);
172     }
173
174     /*
175      * (non-Javadoc)
176      * 
177      * @see
178      * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
179      */
180     public void removeRule(int aIndex) {
181         rules.remove(aIndex);
182     }
183
184     /**
185      * For OR mapping.
186      * 
187      * @return The rules.
188      */
189     protected List<AuthorizationRule> getMappedRules() {
190         return rules;
191     }
192
193     /**
194      * For OR mapping.
195      * 
196      * @param aRules
197      *            The rules.
198      */
199     protected void setMappedRules(List<AuthorizationRule> aRules) {
200         rules = aRules;
201         for (AuthorizationRule rule : rules) {
202             rule.setUserAdministration(userAdmin);
203         }
204     }
205 }