1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.web.mvc.builders;
29
30 import org.objectledge.authentication.AuthenticationContext;
31 import org.objectledge.context.Context;
32 import org.objectledge.pipeline.ProcessingException;
33 import org.objectledge.pipeline.Valve;
34 import org.objectledge.web.mvc.MVCContext;
35 import org.objectledge.web.mvc.security.Policy;
36 import org.objectledge.web.mvc.security.PolicySystem;
37 import org.objectledge.web.mvc.security.SecurityChecking;
38
39 /***
40 * An action that must be protected by a specific policy.
41 *
42 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
43 * @version $Id: PolicyProtectedAction.java,v 1.1 2005/05/06 09:38:09 rafal Exp $
44 */
45 public abstract class PolicyProtectedAction
46 implements SecurityChecking, Valve
47 {
48 private final PolicySystem policySystem;
49
50 /***
51 * Creates new PolicyProtectedBuider instance.
52 *
53 * @param policySystemArg the PolicySystem component.
54 */
55 public PolicyProtectedAction(PolicySystem policySystemArg)
56 {
57 this.policySystem = policySystemArg;
58 }
59
60 /***
61 * {@inheritDoc}
62 */
63 public boolean requiresSecureChannel(Context context)
64 throws Exception
65 {
66 return false;
67 }
68
69 /***
70 * {@inheritDoc}
71 */
72 public boolean requiresAuthenticatedUser(Context context)
73 throws Exception
74 {
75 Policy policy = getPolicy(context);
76 AuthenticationContext authenticationContext =
77 AuthenticationContext.getAuthenticationContext(context);
78 return !policy.requiresLogin() || authenticationContext.isUserAuthenticated();
79 }
80
81 /***
82 * {@inheritDoc}
83 */
84 public boolean checkAccessRights(Context context)
85 throws Exception
86 {
87 Policy policy = getPolicy(context);
88 AuthenticationContext authenticationContext =
89 AuthenticationContext.getAuthenticationContext(context);
90 return policySystem.checkPolicy(authenticationContext.getUserPrincipal(),
91 authenticationContext.isUserAuthenticated(), policy);
92 }
93
94 /***
95 * Retruns a policy matching the current request.
96 *
97 * @param context the request context.
98 * @return the matching policy.
99 * @throws ProcessingException if no policy matches the request.
100 */
101 private Policy getPolicy(Context context)
102 throws ProcessingException
103 {
104 MVCContext mvcContext = MVCContext.getMVCContext(context);
105 Policy policy = policySystem.getPolicy(mvcContext.getView(), mvcContext.getAction());
106 if(policy == null)
107 {
108 throw new ProcessingException(mvcContext.getAction() +
109 " is not matched by any access policy");
110 }
111 return policy;
112 }
113 }