KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > component > UserRoleUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.component;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * @author Manfred Geiler (latest modification by $Author: matze $)
24  * @version $Revision: 1.4 $ $Date: 2004/10/13 11:50:56 $
25  * $Log: UserRoleUtils.java,v $
26  * Revision 1.4 2004/10/13 11:50:56 matze
27  * renamed packages to org.apache
28  *
29  * Revision 1.3 2004/07/01 21:53:10 mwessendorf
30  * ASF switch
31  *
32  * Revision 1.2 2004/05/18 14:31:36 manolito
33  * user role support completely moved to components source tree
34  *
35  * Revision 1.1 2004/03/31 11:58:33 manolito
36  * custom component refactoring
37  *
38  */

39 public class UserRoleUtils
40 {
41     //private static final Log log = LogFactory.getLog(UserRoleUtils.class);
42

43     /**
44      * Gets the comma separated list of visibility user roles from the given component
45      * and checks if current user is in one of these roles.
46      * @param component a user role aware component
47      * @return true if no user roles are defined for this component or
48      * user is in one of these roles, false otherwise
49      */

50     public static boolean isVisibleOnUserRole(UIComponent component)
51     {
52         String JavaDoc userRole;
53         if (component instanceof UserRoleAware)
54         {
55             userRole = ((UserRoleAware)component).getVisibleOnUserRole();
56         }
57         else
58         {
59             userRole = (String JavaDoc)component.getAttributes().get(UserRoleAware.VISIBLE_ON_USER_ROLE_ATTR);
60         }
61
62         if (userRole == null)
63         {
64             // no restriction
65
return true;
66         }
67
68         FacesContext facesContext = FacesContext.getCurrentInstance();
69         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(userRole, ",");
70         while (st.hasMoreTokens())
71         {
72             if (facesContext.getExternalContext().isUserInRole(st.nextToken().trim()))
73             {
74                 return true;
75             }
76         }
77         return false;
78     }
79
80
81     /**
82      * Gets the comma separated list of enabling user roles from the given component
83      * and checks if current user is in one of these roles.
84      * @param component a user role aware component
85      * @return true if no user roles are defined for this component or
86      * user is in one of these roles, false otherwise
87      */

88     public static boolean isEnabledOnUserRole(UIComponent component)
89     {
90         String JavaDoc userRole;
91         if (component instanceof UserRoleAware)
92         {
93             userRole = ((UserRoleAware)component).getEnabledOnUserRole();
94         }
95         else
96         {
97             userRole = (String JavaDoc)component.getAttributes().get(UserRoleAware.ENABLED_ON_USER_ROLE_ATTR);
98         }
99
100         if (userRole == null)
101         {
102             // no restriction
103
return true;
104         }
105
106         FacesContext facesContext = FacesContext.getCurrentInstance();
107         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(userRole, ",");
108         while (st.hasMoreTokens())
109         {
110             if (facesContext.getExternalContext().isUserInRole(st.nextToken().trim()))
111             {
112                 return true;
113             }
114         }
115         return false;
116     }
117
118 }
119
Popular Tags