[KEYCLOAK-7206] - Search by user id on admin console

This commit is contained in:
pedroigor 2018-04-28 07:02:10 -03:00
parent a3fb0a5ac5
commit ddceaaf3d5
3 changed files with 45 additions and 4 deletions

View file

@ -46,6 +46,19 @@ public interface UsersResource {
@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,

View file

@ -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<UserRepresentation> results = new ArrayList<UserRepresentation>();
List<UserModel> userModels;
List<UserModel> userModels = Collections.emptyList();
if (search != null) {
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) {

View file

@ -351,7 +351,9 @@ public class UserTest extends AbstractAdminTest {
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);
@ -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<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();