Skip to content

Commit

Permalink
[KEYCLOAK-7206] - Search by user id on admin console
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor committed Apr 30, 2018
1 parent a3fb0a5 commit ddceaaf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Expand Up @@ -46,6 +46,19 @@ List<UserRepresentation> search(@QueryParam("username") String username,
@Produces(MediaType.APPLICATION_JSON)
List<UserRepresentation> 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:
*
* <ul>
* <li><i>id:</i> - Find users by identifier. For instance, <i>id:aa497859-bbf5-44ac-bf1a-74dbffcaf197</i></li>
* </ul>
*
* @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<UserRepresentation> search(@QueryParam("search") String search,
Expand Down
Expand Up @@ -49,6 +49,7 @@
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;
Expand All @@ -63,7 +64,9 @@
* @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;

Expand Down Expand Up @@ -187,9 +190,16 @@ public List<UserRepresentation> getUsers(@QueryParam("search") String search,
maxResults = maxResults != null ? maxResults : Constants.DEFAULT_MAX_RESULTS;

List<UserRepresentation> results = new ArrayList<UserRepresentation>();
List<UserModel> userModels;
List<UserModel> 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<String, String> attributes = new HashMap<String, String>();
if (last != null) {
Expand Down
Expand Up @@ -351,16 +351,20 @@ public void createUserWithFederationLink() {
assertEquals(user.getFederationLink(), createdUser.getFederationLink());
}

private void createUsers() {
private List<String> createUsers() {
List<String> ids = new ArrayList<>();

for (int i = 1; i < 10; i++) {
UserRepresentation user = new UserRepresentation();
user.setUsername("username" + i);
user.setEmail("user" + i + "@localhost");
user.setFirstName("First" + i);
user.setLastName("Last" + i);

createUser(user);
ids.add(createUser(user));
}

return ids;
}

@Test
Expand All @@ -385,6 +389,20 @@ public void searchByUsername() {
assertEquals(9, users.size());
}

@Test
public void searchById() {
String expectedUserId = createUsers().get(0);
List<UserRepresentation> 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();
Expand Down

0 comments on commit ddceaaf

Please sign in to comment.