From ddceaaf3d5808e309eb4b7e6dd2e39a0714e2300 Mon Sep 17 00:00:00 2001 From: pedroigor Date: Sat, 28 Apr 2018 07:02:10 -0300 Subject: [PATCH] [KEYCLOAK-7206] - Search by user id on admin console --- .../admin/client/resource/UsersResource.java | 13 +++++++++++ .../resources/admin/UsersResource.java | 14 ++++++++++-- .../keycloak/testsuite/admin/UserTest.java | 22 +++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/UsersResource.java b/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/UsersResource.java index f478e0b392..ca80245e47 100755 --- a/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/UsersResource.java +++ b/integration/admin-client/src/main/java/org/keycloak/admin/client/resource/UsersResource.java @@ -46,6 +46,19 @@ public interface UsersResource { @Produces(MediaType.APPLICATION_JSON) List search(@QueryParam("username") String username); + /** + * Search for users whose username or email matches the value provided by {@code search}. The {@code search} + * argument also allows finding users by specific attributes as follows: + * + * + * + * @param search the value to search. It can be the username, email or any of the supported options to query based on user attributes + * @param firstResult the position of the first result to retrieve + * @param maxResults the maximum number of results to retreive + * @return a list of {@link UserRepresentation} + */ @GET @Produces(MediaType.APPLICATION_JSON) List search(@QueryParam("search") String search, diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java index e4a7c08149..d591581c8b 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java @@ -49,6 +49,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -63,7 +64,9 @@ import java.util.Set; * @version $Revision: 1 $ */ public class UsersResource { + private static final Logger logger = Logger.getLogger(UsersResource.class); + private static final String SEARCH_ID_PARAMETER = "id:"; protected RealmModel realm; @@ -187,9 +190,16 @@ public class UsersResource { maxResults = maxResults != null ? maxResults : Constants.DEFAULT_MAX_RESULTS; List results = new ArrayList(); - List userModels; + List userModels = Collections.emptyList(); if (search != null) { - userModels = session.users().searchForUser(search.trim(), realm, firstResult, maxResults); + if (search.startsWith(SEARCH_ID_PARAMETER)) { + UserModel userModel = session.users().getUserById(search.substring(SEARCH_ID_PARAMETER.length()).trim(), realm); + if (userModel != null) { + userModels = Arrays.asList(userModel); + } + } else { + userModels = session.users().searchForUser(search.trim(), realm, firstResult, maxResults); + } } else if (last != null || first != null || email != null || username != null) { Map attributes = new HashMap(); if (last != null) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java index c08ecfd8b3..078b603b98 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -351,7 +351,9 @@ public class UserTest extends AbstractAdminTest { assertEquals(user.getFederationLink(), createdUser.getFederationLink()); } - private void createUsers() { + private List createUsers() { + List ids = new ArrayList<>(); + for (int i = 1; i < 10; i++) { UserRepresentation user = new UserRepresentation(); user.setUsername("username" + i); @@ -359,8 +361,10 @@ public class UserTest extends AbstractAdminTest { user.setFirstName("First" + i); user.setLastName("Last" + i); - createUser(user); + ids.add(createUser(user)); } + + return ids; } @Test @@ -385,6 +389,20 @@ public class UserTest extends AbstractAdminTest { assertEquals(9, users.size()); } + @Test + public void searchById() { + String expectedUserId = createUsers().get(0); + List users = realm.users().search("id:" + expectedUserId, null, null); + + assertEquals(1, users.size()); + assertEquals(expectedUserId, users.get(0).getId()); + + users = realm.users().search("id: " + expectedUserId + " ", null, null); + + assertEquals(1, users.size()); + assertEquals(expectedUserId, users.get(0).getId()); + } + @Test public void search() { createUsers();