package org.jboss.security;
import java.security.Principal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class RunAsIdentity extends CallerIdentity
{
private Set runAsRoles = new HashSet();
private static final String ANOYMOUS_PRINCIPAL = "anonymous";
public RunAsIdentity(String roleName, String principalName)
{
super(principalName != null ? principalName : ANOYMOUS_PRINCIPAL, null);
if (roleName == null)
throw new IllegalArgumentException("The run-as identity must have at least one role");
runAsRoles.add(new SimplePrincipal(roleName));
}
public RunAsIdentity(String roleName, String principalName, Set extraRoleNames)
{
this(roleName, principalName);
if (extraRoleNames != null)
{
Iterator it = extraRoleNames.iterator();
while (it.hasNext())
{
String extraRoleName = (String) it.next();
runAsRoles.add(new SimplePrincipal(extraRoleName));
}
}
}
public Set getRunAsRoles()
{
return new HashSet(runAsRoles);
}
public boolean doesUserHaveRole(Principal role)
{
return runAsRoles.contains(role);
}
public boolean doesUserHaveRole(Set methodRoles)
{
Iterator it = methodRoles.iterator();
while (it.hasNext())
{
Principal role = (Principal) it.next();
if (doesUserHaveRole(role))
return true;
}
return false;
}
public String toString()
{
return "[roles=" + runAsRoles + ",principal=" + getName() + "]";
}
}