private static final int ALL = READ | UPDATE | DELETE | CREATE;
protected AuthenticationClientPermissions getClientPermissionsForCurrentUser(Request request)
throws ResourceException
{
AuthenticationClientPermissions perms = new AuthenticationClientPermissions();
Subject subject = SecurityUtils.getSubject();
if (getSecuritySystem().isAnonymousAccessEnabled()) {
perms.setLoggedIn(!getSecuritySystem().getAnonymousUsername().equals(subject.getPrincipal()));
}
else {
// anon access is disabled, simply ask JSecurity about this
perms.setLoggedIn(subject != null && subject.isAuthenticated());
}
if (perms.isLoggedIn()) {
// try to set the loggedInUsername
Object principal = subject.getPrincipal();
if (principal != null) {
perms.setLoggedInUsername(principal.toString());
}
}
// need to set the source of the logged in user
// The UI might need to show/hide something based on the user's source
// i.e. like the 'Change Password' link.
String username = perms.getLoggedInUsername();
if (StringUtils.isNotEmpty(username)) {
// look up the realm of the user
try {
User user = this.getSecuritySystem().getUser(username);
String source = (user != null) ? user.getSource() : null;
perms.setLoggedInUserSource(source);
}
catch (UserNotFoundException e) {
if (getLogger().isDebugEnabled()) {
getLogger().info("Failed to lookup user: {}", username, e);
}
else {
getLogger().info("Failed to lookup user: {}: {}/{}", username, e.getClass().getName(), e.getMessage());
}
}
}
Map<String, Integer> privilegeMap = new HashMap<String, Integer>();
for (Privilege priv : getSecuritySystem().listPrivileges()) {
if (priv.getType().equals("method")) {
String permission = priv.getPrivilegeProperty("permission");
privilegeMap.put(permission, NONE);
}
}
// this will update the privilegeMap
this.checkSubjectsPermissions(subject, privilegeMap);
for (Entry<String, Integer> privEntry : privilegeMap.entrySet()) {
ClientPermission cPermission = new ClientPermission();
cPermission.setId(privEntry.getKey());
cPermission.setValue(privEntry.getValue());
perms.addPermission(cPermission);
}
return perms;
}