Issue
Is there any way to get a String[] with the roles a user has in the JSP or Servlet?
I know about request.isUserInRole("role1") but I also want to know all the roles of the user.
I searched the servlet source and it seems this is not possible, but this seems odd to me.
So... any ideas?
Solution
Read in all the possible roles, or hardcode a list. Then iterate over it running the isUserInRole and build a list of roles the user is in and then convert the list to an array.
String[] allRoles = {"1","2","3"};
HttpServletRequest request = ... (or from method argument)
List userRoles = new ArrayList(allRoles.length);
for(String role : allRoles) {
if(request.isUserInRole(role)) {
userRoles.add(role);
}
}
// I forgot the exact syntax for list.toArray so this is prob wrong here
return userRoles.toArray(String[].class);
Answered By - Josh
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)