CustomRoleProvider implements RoleProvider { @Override public Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit) { if (query == null) throw new IllegalArgumentException("Query must be set"); Organization organization = securityService.getOrganization(); if ((target == Role.Target.ACL) && (rolematch != null)) { String exactQuery = StringUtils.removeEnd(query, "%"); Matcher m = rolematch.matcher(exactQuery); if (m.matches()) { List<Role> roles = new LinkedList<Role>(); JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization); roles.add(new JaxbRole(exactQuery, jaxbOrganization, "Custom Role", Role.Type.EXTERNAL)); return roles.iterator(); } } return Stream.$(roles).filter(filterByName._2(query)).drop(offset) .apply(limit > 0 ? StreamOp.<String> id().take(limit) : StreamOp.<String> id()).map(toRole._2(organization)) .iterator(); }  @Reference(name = "security-service") void setSecurityService(SecurityService securityService); @Override String getOrganization(); @Override List<Role> getRolesForUser(String userName); @Override Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit); static final String CUSTOM_ROLES_KEY; static final String CUSTOM_ROLES_PATTERN_KEY; }
@Test public void testFindRoles() throws Exception { assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("ROLE_TWO%", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("ROLE_TWO", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("ROLE_ONE", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("ROLE_THREE", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("123_Learner", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("123_Instructor", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("456_Learner%", Role.Target.ACL, 0, 2)).size()); assertEquals(1, IteratorUtils.toList(customRoleProvider.findRoles("1b1b6e5d-34ac-4370-9b20-8d649df9878c_Instructor", Role.Target.ACL, 0, 2)).size()); assertEquals(0, IteratorUtils.toList(customRoleProvider.findRoles("unknown%", Role.Target.ACL, 0, 1)).size()); assertEquals(0, IteratorUtils.toList(customRoleProvider.findRoles("987_Admin", Role.Target.ACL, 0, 1)).size()); assertEquals(0, IteratorUtils.toList(customRoleProvider.findRoles("ROLE_ONE_", Role.Target.ACL, 0, 2)).size()); }