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.web.mvc.MVCContext;
34 import org.objectledge.web.mvc.security.Policy;
35 import org.objectledge.web.mvc.security.PolicySystem;
36 import org.objectledge.web.mvc.security.SecurityChecking;
37
38 /***
39 * A builder that must be protected by a specific policy.
40 *
41 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
42 * @version $Id: PolicyProtectedBuilder.java,v 1.3 2006/04/05 15:52:30 zwierzem Exp $
43 */
44 public abstract class PolicyProtectedBuilder
45 extends AbstractBuilder
46 implements SecurityChecking
47 {
48 private final PolicySystem policySystem;
49
50 /***
51 * Creates new PolicyProtectedBuider instance.
52 *
53 * @param context the request context.
54 * @param policySystemArg the PolicySystem component.
55 */
56 public PolicyProtectedBuilder(Context context, PolicySystem policySystemArg)
57 {
58 super(context);
59 this.policySystem = policySystemArg;
60 }
61
62 /***
63 * {@inheritDoc}
64 */
65 public boolean requiresSecureChannel(Context context)
66 throws Exception
67 {
68 Policy policy = getPolicy(context);
69 return policy.requiresSSL();
70 }
71
72 /***
73 * {@inheritDoc}
74 */
75 public boolean requiresAuthenticatedUser(Context context)
76 throws Exception
77 {
78 Policy policy = getPolicy(context);
79 return policy.requiresLogin();
80 }
81
82 /***
83 * {@inheritDoc}
84 */
85 public boolean checkAccessRights(Context context)
86 throws Exception
87 {
88 Policy policy = getPolicy(context);
89 AuthenticationContext authenticationContext =
90 AuthenticationContext.getAuthenticationContext(context);
91 return policySystem.checkPolicy(authenticationContext.getUserPrincipal(),
92 authenticationContext.isUserAuthenticated(), policy);
93 }
94
95 /***
96 * Retruns a policy matching the current request.
97 *
98 * @param context the request context.
99 * @return the matching policy.
100 * @throws ProcessingException if no policy matches the request.
101 */
102 private Policy getPolicy(Context context)
103 throws ProcessingException
104 {
105 MVCContext mvcContext = MVCContext.getMVCContext(context);
106 Policy policy = policySystem.getPolicy(mvcContext.getView(), mvcContext.getAction());
107 if(policy == null)
108 {
109 throw new ProcessingException(mvcContext.getView() +
110 " is not matched by any access policy");
111 }
112 return policy;
113 }
114 }