1 22 package org.jboss.aspects.security; 23 24 import org.jboss.security.RealmMapping; 25 import org.jboss.security.RunAsIdentity; 26 import org.jboss.security.SimplePrincipal; 27 28 import java.security.Principal ; 29 import java.util.HashSet ; 30 31 38 public class SecurityContext 39 { 40 protected static ThreadLocal currentDomain = new ThreadLocal (); 41 42 public static ThreadLocal getCurrentDomain() 43 { 44 return currentDomain; 45 } 46 47 51 public static Principal getCallerPrincipal() 52 { 53 return SecurityActions.getCallerPrincipal(); 54 } 55 56 60 public static Principal getCurrentPrincipal() 61 { 62 return SecurityActions.getPrincipal(); 63 } 64 65 72 public static boolean isCallerInRole(String roleName) 73 { 74 return isInRole(getCallerPrincipal(), roleName); 75 } 76 77 85 public static boolean isCurrentInRole(String roleName) 86 { 87 return isInRole(getCurrentPrincipal(), roleName); 88 } 89 90 private static boolean isInRole(Principal principal, String roleName) 91 { 92 RealmMapping rm = (RealmMapping)currentDomain.get(); 93 if (rm == null) return false; 94 95 HashSet set = new HashSet (); 96 set.add(new SimplePrincipal(roleName)); 97 98 if (principal instanceof RunAsIdentity) 99 { 100 return ((RunAsIdentity)principal).doesUserHaveRole(set); 101 } 102 else 103 { 104 return rm.doesUserHaveRole(principal, set); 105 } 106 } 107 108 109 } 110 | Popular Tags |