1 16 package org.apache.myfaces.component; 17 18 import javax.faces.component.UIComponent; 19 import javax.faces.context.FacesContext; 20 import java.util.StringTokenizer ; 21 22 39 public class UserRoleUtils 40 { 41 43 50 public static boolean isVisibleOnUserRole(UIComponent component) 51 { 52 String userRole; 53 if (component instanceof UserRoleAware) 54 { 55 userRole = ((UserRoleAware)component).getVisibleOnUserRole(); 56 } 57 else 58 { 59 userRole = (String )component.getAttributes().get(UserRoleAware.VISIBLE_ON_USER_ROLE_ATTR); 60 } 61 62 if (userRole == null) 63 { 64 return true; 66 } 67 68 FacesContext facesContext = FacesContext.getCurrentInstance(); 69 StringTokenizer st = new StringTokenizer (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 88 public static boolean isEnabledOnUserRole(UIComponent component) 89 { 90 String userRole; 91 if (component instanceof UserRoleAware) 92 { 93 userRole = ((UserRoleAware)component).getEnabledOnUserRole(); 94 } 95 else 96 { 97 userRole = (String )component.getAttributes().get(UserRoleAware.ENABLED_ON_USER_ROLE_ATTR); 98 } 99 100 if (userRole == null) 101 { 102 return true; 104 } 105 106 FacesContext facesContext = FacesContext.getCurrentInstance(); 107 StringTokenizer st = new StringTokenizer (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 |