* @param roleNames The role names for which the caller is being checked for
* @return true if the user is in <b>any</b> one of the <code>roleNames</code>. Else returns false
*/
public boolean isCallerInRole(final Object incommingMappedRoles, final Map<String, Collection<String>> roleLinks,
final String... roleNames) {
final SecurityRolesMetaData mappedRoles = (SecurityRolesMetaData) incommingMappedRoles;
final SecurityContext securityContext = doPrivileged(securityContext());
if (securityContext == null) {
return false;
}
RoleGroup roleGroup = null;
RunAs runAs = securityContext.getIncomingRunAs();
if (runAs != null && runAs instanceof RunAsIdentity) {
RunAsIdentity runAsIdentity = (RunAsIdentity) runAs;
roleGroup = runAsIdentity.getRunAsRolesAsRoleGroup();
} else {
AuthorizationManager am = securityContext.getAuthorizationManager();
SecurityContextCallbackHandler scb = new SecurityContextCallbackHandler(securityContext);
roleGroup = am.getSubjectRoles(securityContext.getSubjectInfo().getAuthenticatedSubject(), scb);
}
List<Role> roles = roleGroup.getRoles();
// TODO - Review most performant way.
Set<String> requiredRoles = new HashSet<String>();
for (String current : roleNames) {
requiredRoles.add(current);
}
Set<String> actualRoles = new HashSet<String>();
for (Role current : roles) {
actualRoles.add(current.getRoleName());
}
// add mapped roles
if (mappedRoles != null) {
Principal callerPrincipal = getCallerPrincipal();
Set<String> mapped = mappedRoles.getSecurityRoleNamesByPrincipal(callerPrincipal.getName());
if (mapped != null) {
actualRoles.addAll(mapped);
}
}
// if the actual roles matches any of the required roles, then return true