PDA

View Full Version : JSP: How to getAllAuthorities()?



colorfool
11-17-2008, 10:26 AM
I am trying to show different charts in a dashboard depending on which roles the logged in user belongs to. How can I get a list of the user's roles?
I have played around with the session (userSession.getAttribute("SECURITY_PRINCIPAL")) and tried my luck in many classes... I suppose I might need UserRoleListService? Anyhow, I am not used to Java and I am having a hard time finding my way. I would sure appreciate it if someone can give me the two lines of code that do the trick : )

All the best,

Axel

colorfool
11-18-2008, 07:50 AM
I am soooo sure that there is a more elegant way to solve this, here is my dirty version:


String dummy = userSession.getAttribute("SECURITY_PRINCIPAL").toString();
dummy = dummy.substring(dummy.indexOf("Granted Authorities:") + 21);
dummy = dummy.substring(0, dummy.indexOf(";"));
String[] roles = dummy.split(", ");
java.util.Arrays.sort(roles);
if (java.util.Arrays.binarySearch(roles, "MYROLE") >= 0) {
out.println("This is only seen by logged in users belonging to the MYROLE role.");
}Anybody up to showing me the error of my ways?

colorfool
11-18-2008, 10:53 AM
Now this is more like what I wanted:


UserDetailsRoleListService myRoleListService = PentahoSystem.getUserDetailsRoleListService();
if (myRoleListService.getRolesForUser(userSession.getName()).contains("MYROLE")) {
// here goes what is only seen by users belonging to MYROLE
}Cheers : )