KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > security > AccessController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.security;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.riotfamily.riot.security.auth.RiotUser;
30 import org.riotfamily.riot.security.policy.AuthorizationPolicy;
31 import org.riotfamily.riot.security.session.AccessControlFilterPlugin;
32 import org.riotfamily.riot.security.session.AccessControlInterceptor;
33 import org.riotfamily.riot.security.session.SecurityContext;
34
35
36
37 /**
38  * Provides static methods to check permissions and associate a user
39  * with the current Thread.
40  * <p>
41  * This class is only usable if an {@link AccessControlFilterPlugin} or
42  * {@link AccessControlInterceptor} is configured.
43  */

44 public final class AccessController {
45
46     private AccessController() {
47     }
48     
49     private static List JavaDoc policies;
50
51     /**
52      * The {@link AccessControlInitializer} sets a list of
53      * {@link AuthorizationPolicy policies} so that they can be accessed
54      * from a static context.
55      */

56     static void setPolicies(List JavaDoc policies) {
57         AccessController.policies = policies;
58     }
59         
60     public static RiotUser getCurrentUser() {
61         return SecurityContext.getCurrentUser();
62     }
63     
64     public static boolean isAuthenticatedUser() {
65         return getCurrentUser() != null;
66     }
67     
68     public static boolean isGranted(String JavaDoc action, Object JavaDoc object) {
69         return isGranted(getCurrentUser(), action, object);
70     }
71     
72     public static void checkPermission(String JavaDoc action, Object JavaDoc object) {
73         RiotUser subject = getCurrentUser();
74         if (subject != null) {
75             Iterator JavaDoc it = policies.iterator();
76             while (it.hasNext()) {
77                 AuthorizationPolicy policy = (AuthorizationPolicy) it.next();
78                 int access = policy.checkPermission(subject, action, object);
79                 if (access == AuthorizationPolicy.ACCESS_GRANTED) {
80                     return;
81                 }
82                 else if (access == AuthorizationPolicy.ACCESS_DENIED) {
83                     throw new AccessDeniedException(subject, action, object, policy);
84                 }
85             }
86         }
87         throw new AccessDeniedException(subject, action, object, null);
88     }
89
90     public static boolean isGranted(RiotUser user, String JavaDoc action, Object JavaDoc object) {
91         if (user != null) {
92             Iterator JavaDoc it = policies.iterator();
93             while (it.hasNext()) {
94                 AuthorizationPolicy policy = (AuthorizationPolicy) it.next();
95                 int access = policy.checkPermission(user, action, object);
96                 if (access == AuthorizationPolicy.ACCESS_GRANTED) {
97                     return true;
98                 }
99                 else if (access == AuthorizationPolicy.ACCESS_DENIED) {
100                     return false;
101                 }
102             }
103         }
104         return false;
105     }
106     
107 }
108
Popular Tags