1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.component;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import java.util.StringTokenizer;
24
25 /**
26 * @author Manfred Geiler (latest modification by $Author: skitching $)
27 * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
28 */
29 public final class UserRoleUtils
30 {
31 //private static final Log log = LogFactory.getLog(UserRoleUtils.class);
32
33 /**
34 * Constructor (private one)
35 */
36 private UserRoleUtils(){
37
38 }
39
40 /**
41 * Gets the comma separated list of visibility user roles from the given component
42 * and checks if current user is in one of these roles.
43 * @param component a user role aware component
44 * @return true if no user roles are defined for this component or
45 * user is in one of these roles, false otherwise
46 */
47 public static boolean isVisibleOnUserRole(UIComponent component)
48 {
49 String userRole;
50 if (component instanceof UserRoleAware)
51 {
52 userRole = ((UserRoleAware)component).getVisibleOnUserRole();
53 }
54 else
55 {
56 userRole = (String)component.getAttributes().get(UserRoleAware.VISIBLE_ON_USER_ROLE_ATTR);
57 }
58
59 if (userRole == null)
60 {
61 // no restriction
62 return true;
63 }
64
65 FacesContext facesContext = FacesContext.getCurrentInstance();
66 StringTokenizer st = new StringTokenizer(userRole, ",");
67 while (st.hasMoreTokens())
68 {
69 if (facesContext.getExternalContext().isUserInRole(st.nextToken().trim()))
70 {
71 return true;
72 }
73 }
74 return false;
75 }
76
77
78 /**
79 * Gets the comma separated list of enabling user roles from the given component
80 * and checks if current user is in one of these roles.
81 * @param component a user role aware component
82 * @return true if no user roles are defined for this component or
83 * user is in one of these roles, false otherwise
84 */
85 public static boolean isEnabledOnUserRole(UIComponent component)
86 {
87 String userRole;
88 if (component instanceof UserRoleAware)
89 {
90 userRole = ((UserRoleAware)component).getEnabledOnUserRole();
91 }
92 else
93 {
94 userRole = (String)component.getAttributes().get(UserRoleAware.ENABLED_ON_USER_ROLE_ATTR);
95 }
96
97 if (userRole == null)
98 {
99 // no restriction
100 return true;
101 }
102
103 FacesContext facesContext = FacesContext.getCurrentInstance();
104 StringTokenizer st = new StringTokenizer(userRole, ",");
105 while (st.hasMoreTokens())
106 {
107 if (facesContext.getExternalContext().isUserInRole(st.nextToken().trim()))
108 {
109 return true;
110 }
111 }
112 return false;
113 }
114
115 }