DefaultLdapUserRoleListService implements IUserRoleListService, InitializingBean { @Override public List<String> getRolesForUser( final ITenant tenant, final String username ) { if ( tenant != null && !tenant.equals( JcrTenantUtils.getDefaultTenant() ) ) { throw new UnsupportedOperationException( "only allowed to access to default tenant" ); } UserDetails user = userDetailsService.loadUserByUsername( userNameUtils.getPrincipleName( username ) ); Collection<? extends GrantedAuthority> results = user.getAuthorities(); Set<String> roles = ( roleComparator != null ) ? new TreeSet<String>( roleComparator ) : new LinkedHashSet<String>( results.size() ); for ( GrantedAuthority role : results ) { roles.add( role.getAuthority() ); } for ( String extraRole : extraRoles ) { roles.add( extraRole ); } return new ArrayList<String>( roles ); } DefaultLdapUserRoleListService();  DefaultLdapUserRoleListService( final Comparator<String> usernameComparator,
      final Comparator<String> roleComparator );  DefaultLdapUserRoleListService( final Comparator<String> usernameComparator,
      final Comparator<String> roleComparator, final IAuthenticationRoleMapper roleMapper ); @Override void afterPropertiesSet(); @Override List<String> getAllRoles(); @Override List<String> getAllUsers(); @Override List<String> getUsersInRole( final ITenant tenant, final String role ); @Override List<String> getRolesForUser( final ITenant tenant, final String username ); void setAllUsernamesSearch( final LdapSearch allUsernamesSearch ); void setAllAuthoritiesSearch( final LdapSearch allAuthoritiesSearch ); void setUsernamesInRoleSearch( final LdapSearch usernamesInRoleSearch ); void setUserDetailsService( final UserDetailsService userDetailsService ); void setRoleComparator( final Comparator<String> roleComparator ); void setUsernameComparator( final Comparator<String> usernameComparator ); ITenantedPrincipleNameResolver getUserNameUtils(); void setUserNameUtils( ITenantedPrincipleNameResolver userNameUtils ); ITenantedPrincipleNameResolver getRoleNameUtils(); void setRoleNameUtils( ITenantedPrincipleNameResolver roleNameUtils ); @Override List<String> getAllRoles( ITenant tenant ); @Override List<String> getAllUsers( ITenant tenant ); @Override List<String> getSystemRoles(); void setSystemRoles( List<String> systemRoles ); void setExtraRoles( List<String> extraRoles ); List<String> getExtraRoles();  }
@Test( expected = UnsupportedOperationException.class ) public void testGetRolesForUser_nonDefaultTenant() throws Exception { service.getRolesForUser( tenant, "admin" ); }
@Test public void testGetRolesForUser() throws Exception { service = new DefaultLdapUserRoleListService( usernameComparator, roleComparator, roleMapper ); service.setUserDetailsService( userDetailService ); service.setUserNameUtils( userNameUtils ); service.setExtraRoles( extraRoles ); assertEquals( userNameUtils, service.getUserNameUtils() ); when( userDetailService.loadUserByUsername( "joe" ) ).thenReturn( userDetails ); when( userNameUtils.getPrincipleName( anyString() ) ).thenReturn( "joe" ); auths.add( new SimpleGrantedAuthority( "test" ) ); when( userDetails.getAuthorities() ).thenReturn( auths ); List<String> foundRoles = service.getRolesForUser( JcrTenantUtils.getDefaultTenant(), "admin" ); assertNotNull( foundRoles ); assertEquals( 3, foundRoles.size() ); }