01: package org.acegisecurity.config;
02:
03: import org.acegisecurity.AccessDecisionManager;
04: import org.acegisecurity.vote.AffirmativeBased;
05: import org.acegisecurity.vote.AuthenticatedVoter;
06: import org.acegisecurity.vote.ConsensusBased;
07: import org.acegisecurity.vote.RoleVoter;
08: import org.acegisecurity.vote.UnanimousBased;
09: import org.springframework.beans.factory.support.AbstractBeanDefinition;
10: import org.springframework.beans.factory.support.ManagedList;
11: import org.springframework.beans.factory.support.RootBeanDefinition;
12: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
13: import org.springframework.beans.factory.xml.BeanDefinitionParser;
14: import org.springframework.beans.factory.xml.ParserContext;
15: import org.springframework.util.StringUtils;
16: import org.springframework.util.xml.DomUtils;
17: import org.w3c.dom.Element;
18: import org.w3c.dom.Node;
19: import org.w3c.dom.NodeList;
20:
21: public class AuthorizationManagerBeanDefinitionParser extends
22: AbstractBeanDefinitionParser implements BeanDefinitionParser {
23: // ~ static initializers
24: // ================================================================================================
25:
26: public static final String ROLE_VOTER_ELE = "role-voter";
27:
28: public static final String AUTHENTICATED_VOTER_ELE = "authenticated-voter";
29:
30: public static final String STRATEGY_ATTRIBUTE = "strategy";
31:
32: // ~ Method
33: // ================================================================================================
34:
35: protected AbstractBeanDefinition parseInternal(Element element,
36: ParserContext parserContext) {
37: return createBeanDefinition(element, parserContext);
38: }
39:
40: private RootBeanDefinition createBeanDefinition(Element element,
41: ParserContext parserContext) {
42: ManagedList decisionVoters = new ManagedList();
43:
44: Element roleVoterEle = DomUtils.getChildElementByTagName(
45: element, ROLE_VOTER_ELE);
46: Element authVoterEle = DomUtils.getChildElementByTagName(
47: element, AUTHENTICATED_VOTER_ELE);
48:
49: if (roleVoterEle != null
50: && roleVoterEle.getLocalName().equals(ROLE_VOTER_ELE)) {
51: decisionVoters.add(new RootBeanDefinition(RoleVoter.class));
52: }
53: if (authVoterEle != null
54: && authVoterEle.getLocalName().equals(
55: AUTHENTICATED_VOTER_ELE)) {
56: decisionVoters.add(new RootBeanDefinition(
57: AuthenticatedVoter.class));
58: }
59:
60: String strategy = element.getAttribute(STRATEGY_ATTRIBUTE);
61: if (StringUtils.hasLength(strategy)) {
62: if (strategy.equals("affirmative")) {
63: return createAccessDecisionManager(
64: AffirmativeBased.class, decisionVoters);
65: } else if (strategy.equals("consensus")) {
66: return createAccessDecisionManager(
67: ConsensusBased.class, decisionVoters);
68: } else if (strategy.equals("unanimous")) {
69: return createAccessDecisionManager(
70: UnanimousBased.class, decisionVoters);
71: }
72: } else {
73: return createAccessDecisionManagerAffirmativeBased();
74: }
75: return null;
76: }
77:
78: protected static RootBeanDefinition createAccessDecisionManagerAffirmativeBased() {
79: ManagedList decisionVoters = new ManagedList();
80: decisionVoters.add(new RootBeanDefinition(
81: AuthenticatedVoter.class));
82: decisionVoters.add(new RootBeanDefinition(RoleVoter.class));
83: return createAccessDecisionManager(AffirmativeBased.class,
84: decisionVoters);
85: }
86:
87: protected static RootBeanDefinition createAccessDecisionManager(
88: Class clazz, ManagedList decisionVoters) {
89: RootBeanDefinition accessDecisionManager = new RootBeanDefinition(
90: clazz);
91: accessDecisionManager.getPropertyValues().addPropertyValue(
92: "allowIfAllAbstainDecisions", Boolean.FALSE);
93: accessDecisionManager.getPropertyValues().addPropertyValue(
94: "decisionVoters", decisionVoters);
95: return accessDecisionManager;
96: }
97:
98: }
|