From 7518692c0d97804b1af90720c8292d2356cf6239 Mon Sep 17 00:00:00 2001 From: Jeroen ter Voorde Date: Tue, 4 Jun 2019 17:30:33 +0200 Subject: [PATCH] [KEYCLOAK-10419] Added briefRepresentation parameter support to the admin client interface And added a aquillian test for it. --- .../admin/client/resource/GroupResource.java | 21 ++++++++++++ .../resources/admin/GroupResource.java | 3 +- .../testsuite/admin/group/GroupTest.java | 33 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/GroupResource.java b/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/GroupResource.java index 2c07b13bd6..ea0dbb9527 100755 --- a/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/GroupResource.java +++ b/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/GroupResource.java @@ -117,6 +117,7 @@ public interface GroupResource { @Path("/members") @Produces(MediaType.APPLICATION_JSON) public List members(); + /** * Get users *

@@ -132,4 +133,24 @@ public interface GroupResource { @Produces(MediaType.APPLICATION_JSON) public List members(@QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults); + + /** + * Get users + *

+ * Returns a list of users, filtered according to query parameters + * + * @param firstResult Pagination offset + * @param maxResults Pagination size + * @param briefRepresentation Only return basic information (only guaranteed to return id, username, created, first and last name, + * email, enabled state, email verification state, federation link, and access. + * Note that it means that namely user attributes, required actions, and not before are not returned.) + * @return + */ + @GET + @NoCache + @Path("/members") + @Produces(MediaType.APPLICATION_JSON) + public List members(@QueryParam("first") Integer firstResult, + @QueryParam("max") Integer maxResults, + @QueryParam("briefRepresentation") Boolean briefRepresentation); } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java b/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java index 123092df8b..208e30fdfb 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java @@ -207,8 +207,7 @@ public class GroupResource { @QueryParam("max") Integer maxResults, @QueryParam("briefRepresentation") Boolean briefRepresentation) { this.auth.groups().requireViewMembers(group); - - + firstResult = firstResult != null ? firstResult : 0; maxResults = maxResults != null ? maxResults : Constants.DEFAULT_MAX_RESULTS; boolean briefRepresentationB = briefRepresentation != null && briefRepresentation; diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java index 2192925472..13f5ff2d01 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java @@ -27,6 +27,8 @@ import org.keycloak.admin.client.resource.UsersResource; import org.keycloak.events.admin.OperationType; import org.keycloak.events.admin.ResourceType; import org.keycloak.models.Constants; +import org.keycloak.models.UserModel; +import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.AccessToken; import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.CredentialRepresentation; @@ -723,4 +725,35 @@ public class GroupTest extends AbstractGroupTest { assertEquals(new Long(allGroups.size()), realm.groups().count(true).get("count")); assertEquals(new Long(allGroups.size() + 1), realm.groups().count(false).get("count")); } + + @Test + public void testBriefRepresentationOnGroupMembers() { + String groupName = "group-" + UUID.randomUUID(); + + GroupsResource groups = adminClient.realms().realm("test").groups(); + try (Response response = groups.add(GroupBuilder.create().name(groupName).build())) { + String groupId = ApiUtil.getCreatedId(response); + + GroupResource group = groups.group(groupId); + + UsersResource users = adminClient.realms().realm("test").users(); + String userName = "user-" + UUID.randomUUID(); + + UserRepresentation userRepresentation = UserBuilder.create() + .username(userName) + .addAttribute("myattribute", "myvalue") + .build(); + + Response r = users.create(userRepresentation); + users.get(ApiUtil.getCreatedId(r)).joinGroup(groupId); + + UserRepresentation defaultRepresentation = group.members(null, null).get(0); + UserRepresentation fullRepresentation = group.members(null, null, false).get(0); + UserRepresentation briefRepresentation = group.members(null, null, true).get(0); + + assertEquals("full group member representation includes attributes", fullRepresentation.getAttributes(), userRepresentation.getAttributes()); + assertEquals("default group member representation is full", defaultRepresentation.getAttributes(), userRepresentation.getAttributes()); + assertNull("brief group member representation omits attributes", briefRepresentation.getAttributes()); + } + } }