/users/count endpoint with search field has different behavior than /users query endpoint #17620

closes #17620
This commit is contained in:
Pedro Hos 2023-03-14 19:24:55 -03:00 committed by Michal Hajas
parent 24ecfcb5aa
commit bd0a23a865
2 changed files with 21 additions and 3 deletions

View file

@ -619,8 +619,11 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore {
List<Predicate> predicates = new ArrayList<>();
predicates.add(builder.equal(root.get("realmId"), realm.getId()));
predicates.add(builder.or(getSearchOptionPredicateArray(search, builder, root)));
for (String stringToSearch : search.trim().split("\\s+")) {
predicates.add(builder.or(getSearchOptionPredicateArray(stringToSearch, builder, root)));
}
queryBuilder.where(predicates.toArray(new Predicate[0]));
return em.createQuery(queryBuilder).getSingleResult().intValue();
@ -643,7 +646,11 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore {
List<Predicate> predicates = new ArrayList<>();
predicates.add(builder.equal(userJoin.get("realmId"), realm.getId()));
predicates.add(builder.or(getSearchOptionPredicateArray(search, builder, userJoin)));
for (String stringToSearch : search.trim().split("\\s+")) {
predicates.add(builder.or(getSearchOptionPredicateArray(stringToSearch, builder, userJoin)));
}
predicates.add(groupMembership.get("groupId").in(groupIds));
queryBuilder.where(predicates.toArray(new Predicate[0]));

View file

@ -74,6 +74,17 @@ public class UsersTest extends AbstractAdminTest {
assertCaseInsensitiveSearch();
}
@Test
public void searchUserMatchUsersCount() {
createUser(REALM_NAME, "john.doe", "password", "John", "Doe Smith", "john.doe@keycloak.org");
String search = "jo do";
assertThat(adminClient.realm(REALM_NAME).users().count(search), is(1));
List<UserRepresentation> users = adminClient.realm(REALM_NAME).users().search(search, null, null);
assertThat(users, hasSize(1));
assertThat(users.get(0).getUsername(), is("john.doe"));
}
@Test
public void searchUserCaseSensitiveFirst() throws Exception {