Dashboard > Community Wiki > ... > Template Examples > List users with role
List users with role Log In View a printable version of the current page.

Added by GrĂ©gory Joseph , last edited by Boris Kraft on Jun 23, 2008  (view change)
Labels: 

We have quite a number of sites being managed in our Magnolia installation now, all with a variety of users assigned to them. Our administrators wanted to be able to determine what users have access to a particular site. While it's easy to get a list of Roles that one particular user has access to through the default UI, it's less obvious how to find what Users are assigned to a particular Role.

To address this need, we wrote a little JSP servlet that will iterate through the Roles in the system and list each user that's assigned to each of them.

First off, we have a bit of code that grabs a list of users, sends each off for processing, and then sends the results off for rendering. Results are stored in a TreeMap, where the key is the role name and the value is a TreeSet that contains the usernames that have been given that Role. (TreeMap and TreeSet are used to give us free sorting.)

<jsp:scriptlet>
	<![^CDATA[
	
	UserManager userManager = Security.getUserManager();
	TreeMap results = new TreeMap();

	Iterator users = userManager.getAllUsers().iterator();
	while ( users.hasNext() ) {
		User user = (User)users.next();
		processUser( user, results );
	}
			
	printResults( results, response );
	]]>
</jsp:scriptlet>

Next, the method to look at each user, iterating through the roles that are assigned it, and populating the appropriate bit of the results. (Note that getRoles() returns a collection of Strings, rather than a collection of Roles as any right-thinking person might expect.)

public void processUser( User user, TreeMap results ) throws Exception {
	Iterator roles = user.getRoles().iterator();
	 while ( roles.hasNext() ) {
		String roleName = (String)roles.next();
		
		if ( results.containsKey( roleName ) ) {
			TreeSet users = (TreeSet)results.get( roleName );
			users.add( user.getName() );
		} else {
			TreeSet users = new TreeSet();
			users.add( user.getName() );
			results.put( roleName, users ); 
		} 
		
	}
}

And finally, the method that takes our data structures and turns them into HTML:

public void printResults( TreeMap results, HttpServletResponse response ) throws Exception {
	response.getWriter().println("<h1>Users Assigned to Roles</h1>" );

	response.getWriter().println("<ul>" );
	Iterator roles = results.keySet().iterator();
	while ( roles.hasNext() ) {
		String roleName = (String)roles.next();
		TreeSet roleUsers = (TreeSet)results.get( roleName );
		
		response.getWriter().println("<li><div class=\"roleName\">" +  roleName + "</div><ul>");
		Iterator users = roleUsers.iterator();
		while ( users.hasNext() ) {
			String user = (String)users.next();
			response.getWriter().println("<li class=\"userName\">" + user + "</li>" );
		}
		response.getWriter().println("</ul></li>");
	}
	response.getWriter().println("</ul>" );
}

I've attached the complete JSP for use as well – it should run unaltered on any Magnolia 3.x installation.

In its previous incarnation on JspWiki, this page was last edited on Mar 20, 2007 5:52:27 PM by SeanMcMains

It might be worth adding this to the Tools section in AdminCentral, or even to the Security menu

Powered by a free Atlassian Confluence Open Source Project License granted to Magnolia International. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators