1 7 8 package org.jboss.security; 9 10 import java.security.Principal ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 15 22 public class RunAsIdentity extends CallerIdentity 23 { 24 25 private Set runAsRoles = new HashSet (); 26 27 private static final String ANOYMOUS_PRINCIPAL = "anonymous"; 28 29 32 public RunAsIdentity(String roleName, String principalName) 33 { 34 super(principalName != null ? principalName : ANOYMOUS_PRINCIPAL, null); 36 37 if (roleName == null) 38 throw new IllegalArgumentException ("The run-as identity must have at least one role"); 39 40 runAsRoles.add(new SimplePrincipal(roleName)); 41 } 42 43 46 public RunAsIdentity(String roleName, String principalName, Set extraRoleNames) 47 { 48 this(roleName, principalName); 49 50 if (extraRoleNames != null) 52 { 53 Iterator it = extraRoleNames.iterator(); 54 while (it.hasNext()) 55 { 56 String extraRoleName = (String ) it.next(); 57 runAsRoles.add(new SimplePrincipal(extraRoleName)); 58 } 59 } 60 } 61 62 public Set getRunAsRoles() 63 { 64 return new HashSet (runAsRoles); 65 } 66 67 public boolean doesUserHaveRole(Principal role) 68 { 69 return runAsRoles.contains(role); 70 } 71 72 75 public boolean doesUserHaveRole(Set methodRoles) 76 { 77 Iterator it = methodRoles.iterator(); 78 while (it.hasNext()) 79 { 80 Principal role = (Principal ) it.next(); 81 if (doesUserHaveRole(role)) 82 return true; 83 } 84 return false; 85 } 86 87 91 public String toString() 92 { 93 return "[roles=" + runAsRoles + ",principal=" + getName() + "]"; 94 } 95 } 96 | Popular Tags |