UserAndRoleDirectoryServiceImpl implements UserDirectoryService, UserDetailsService, RoleDirectoryService { @Override public List<Role> findRoles(String query, Role.Target target, int offset, int limit) { if (query == null) throw new IllegalArgumentException("Query must be set"); Organization org = securityService.getOrganization(); if (org == null) throw new IllegalStateException("No organization is set"); final List<Role> roles = new ArrayList<>(); for (RoleProvider roleProvider : roleProviders) { final String providerOrgId = roleProvider.getOrganization(); if (ALL_ORGANIZATIONS.equals(providerOrgId) || org.getId().equals(providerOrgId)) { roleProvider.findRoles(query, target, 0, 0).forEachRemaining(roles::add); } } Stream<Role> stream = roles.stream().sorted(Comparator.comparing(Role::getName)).skip(offset); if (limit > 0) { return stream.limit(limit).collect(Collectors.toList()); } return stream.collect(Collectors.toList()); }  @Override Iterator<User> getUsers(); @Override User loadUser(String userName); @Override Iterator<User> loadUsers(Collection<String> userNames); @Override UserDetails loadUserByUsername(String userName); @Reference(name = "securityService") void setSecurityService(SecurityService securityService); @Override Iterator<User> findUsers(String query, int offset, int limit); @Override List<Role> findRoles(String query, Role.Target target, int offset, int limit); @Override long countUsers(); @Override void invalidate(String userName); static final String USER_CACHE_SIZE_KEY; static final String USER_CACHE_EXPIRY_KEY; }
@Test @SuppressWarnings("unchecked") public void testGetRoles() { List<Role> roles = directory.findRoles("%", Role.Target.ALL, 0, 0); Assert.assertEquals(4, roles.size()); }
@Test @SuppressWarnings("unchecked") public void testFindRoles() { List<Role> roles = directory.findRoles("%2012%", Role.Target.ALL, 0, 0); Assert.assertEquals(2, roles.size()); Assert.assertTrue("ROLE_MATH_2012".equals(roles.get(0).getName()) || "ROLE_MATH_2012".equals(roles.get(1).getName())); Assert.assertTrue("ROLE_ASTRO_2012".equals(roles.get(0).getName()) || "ROLE_ASTRO_2012".equals(roles.get(1).getName())); roles = directory.findRoles("%2012%", Role.Target.ALL, 1, 1); Assert.assertEquals(1, roles.size()); Assert.assertTrue("ROLE_ASTRO_2012".equals(roles.get(0).getName()) || "ROLE_MATH_2012".equals(roles.get(0).getName())); }