1 package org.codehaus.redback.integration.reports;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.codehaus.plexus.redback.rbac.RBACManager;
23 import org.codehaus.plexus.redback.rbac.RbacManagerException;
24 import org.codehaus.plexus.redback.rbac.Role;
25 import org.codehaus.plexus.redback.rbac.UserAssignment;
26 import org.codehaus.plexus.redback.system.SecuritySystem;
27 import org.codehaus.plexus.redback.users.User;
28 import org.codehaus.plexus.redback.users.UserManager;
29 import org.codehaus.redback.integration.util.RoleSorter;
30 import org.codehaus.redback.integration.util.UserComparator;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.io.OutputStream;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
46 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
49 @Service( "report#rolesmatrix-csv" )
50 public class CsvRolesMatrix
54 private SecuritySystem securitySystem;
57 @Named( value = "rBACManager#jdo" )
58 private RBACManager rbacManager;
60 public String getName()
62 return "Roles Matrix";
65 public String getType()
70 public void writeReport( OutputStream os )
71 throws ReportException
73 UserManager userManager = securitySystem.getUserManager();
75 List<User> allUsers = userManager.getUsers();
77 Map<String, List<String>> assignmentsMap;
81 allRoles = rbacManager.getAllRoles();
82 Collections.sort( allRoles, new RoleSorter() );
84 assignmentsMap = new HashMap<String, List<String>>();
86 for ( UserAssignment assignment : rbacManager.getAllUserAssignments() )
88 assignmentsMap.put( assignment.getPrincipal(), assignment.getRoleNames() );
91 catch ( RbacManagerException e )
93 throw new ReportException( "Unable to obtain list of all roles.", e );
96 Collections.sort( allUsers, new UserComparator( "username", true ) );
98 PrintWriter out = new PrintWriter( os );
100 writeCsvHeader( out, allRoles );
102 for ( User user : allUsers )
104 writeCsvRow( out, user, assignmentsMap, allRoles );
110 private void writeCsvHeader( PrintWriter out, List<Role> allRoles )
112 out.print( "Username,Full Name,Email Address" );
113 for ( Role role : allRoles )
115 out.print( "," + escapeCell( role.getName() ) );
120 private void writeCsvRow( PrintWriter out, User user, Map<String, List<String>> assignmentsMap,
121 List<Role> allRoles )
123 out.print( escapeCell( user.getUsername() ) );
124 out.print( "," + escapeCell( user.getFullName() ) );
125 out.print( "," + escapeCell( user.getEmail() ) );
127 List<String> assignedRoleNames = assignmentsMap.get( user.getPrincipal().toString() );
128 if ( assignedRoleNames == null )
130 assignedRoleNames = new ArrayList<String>();
133 for ( Role role : allRoles )
136 if ( assignedRoleNames.contains( role.getName() ) )
144 private String escapeCell( String cell )
146 return "\"" + StringEscapeUtils.escapeJava( cell ) + "\"";
149 public String getId()
151 return "rolesmatrix";
154 public String getMimeType()