01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.vote;
17:
18: import org.acegisecurity.AccessDeniedException;
19: import org.acegisecurity.Authentication;
20: import org.acegisecurity.ConfigAttributeDefinition;
21:
22: import java.util.Iterator;
23:
24: /**
25: * Simple concrete implementation of {@link org.acegisecurity.AccessDecisionManager} that grants access if any
26: * <code>AccessDecisionVoter</code> returns an affirmative response.
27: */
28: public class AffirmativeBased extends AbstractAccessDecisionManager {
29: //~ Methods ========================================================================================================
30:
31: /**
32: * This concrete implementation simply polls all configured {@link AccessDecisionVoter}s and grants access
33: * if any <code>AccessDecisionVoter</code> voted affirmatively. Denies access only if there was a deny vote AND no
34: * affirmative votes.<p>If every <code>AccessDecisionVoter</code> abstained from voting, the decision will
35: * be based on the {@link #isAllowIfAllAbstainDecisions()} property (defaults to false).</p>
36: *
37: * @param authentication the caller invoking the method
38: * @param object the secured object
39: * @param config the configuration attributes associated with the method being invoked
40: *
41: * @throws AccessDeniedException if access is denied
42: */
43: public void decide(Authentication authentication, Object object,
44: ConfigAttributeDefinition config)
45: throws AccessDeniedException {
46: Iterator iter = this .getDecisionVoters().iterator();
47: int deny = 0;
48:
49: while (iter.hasNext()) {
50: AccessDecisionVoter voter = (AccessDecisionVoter) iter
51: .next();
52: int result = voter.vote(authentication, object, config);
53:
54: switch (result) {
55: case AccessDecisionVoter.ACCESS_GRANTED:
56: return;
57:
58: case AccessDecisionVoter.ACCESS_DENIED:
59: deny++;
60:
61: break;
62:
63: default:
64: break;
65: }
66: }
67:
68: if (deny > 0) {
69: throw new AccessDeniedException(messages.getMessage(
70: "AbstractAccessDecisionManager.accessDenied",
71: "Access is denied"));
72: }
73:
74: // To get this far, every AccessDecisionVoter abstained
75: checkAllowIfAllAbstainDecisions();
76: }
77: }
|