8000 Allow searching for multiple users by their ids by maxhov · Pull Request #37901 · keycloak/keycloak · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow searching for multiple users by their ids #37901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.keycloak.services.resources.admin;

import java.util.Arrays;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.extensions.Extension;
Expand Down Expand Up @@ -299,13 +300,10 @@ public Stream<UserRepresentation> getUsers(
Stream<UserModel> userModels = Stream.empty();
if (search != null) {
if (search.startsWith(SEARCH_ID_PARAMETER)) {
UserModel userModel =
session.users().getUserById(realm, search.substring(SEARCH_ID_PARAMETER.length()).trim());
if (userModel != null) {
userModels = Stream.of(userModel);
if (AdminPermissionsSchema.SCHEMA.isAdminPermissionsEnabled(realm)) {
userModels = userModels.filter(userPermissionEvaluator::canView);
}
String[] userIds = search.substring(SEARCH_ID_PARAMETER.length()).trim().split("\\s+");
userModels = Arrays.stream(userIds).map(id -> session.users().getUserById(realm, id)).filter(Objects::nonNull);
if (AdminPermissionsSchema.SCHEMA.isAdminPermissionsEnabled(realm)) {
userModels = userModels.filter(userPermissionEvaluator::canView);
}
} else {
Map<String, String> attributes = new HashMap<>();
Expand Down
< 798B td id="diff-4ff205740d82fc72fd4347bb99928f4300ab664982cfd4d081193610c8fe53a9R1276" data-line-number="1276" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,8 @@ public void searchByIdpAndEnabled() {

@Test
public void searchById() {
String expectedUserId = createUsers().get(0);
List<String> userIds = createUsers();
String expectedUserId = userIds.get(0);
List<UserRepresentation> users = realm.users().search("id:" + expectedUserId, null, null);

assertEquals(1, users.size());
Expand All @@ -1274,6 +1275,19 @@ public void searchById() {

assertEquals(1, users.size());
assertEquals(expectedUserId, users.get(0).getId());

// Should allow searching for multiple users
String expectedUserId2 = userIds.get(1);
List<UserRepresentation> multipleUsers = realm.users().search(String.format("id:%s %s", expectedUserId, expectedUserId2), 0 , 10);;
assertThat(multipleUsers, hasSize(2));
assertThat(multipleUsers.get(0).getId(), is(expectedUserId));
assertThat(multipleUsers.get(1).getId(), is(expectedUserId2));

// Should take arbitrary amount of spaces in between ids
List<UserRepresentation> multipleUsers2 = realm.users().search(String.format("id: %s %s ", expectedUserId, expectedUserId2), 0 , 10);;
assertThat(multipleUsers2, hasSize(2));
assertThat(multipleUsers2.get(0).getId(), is(expectedUserId));
assertThat(multipleUsers2.get(1).getId(), is(expectedUserId2));
}

@Test
Expand Down
Loading
0