Skip to content
Open
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 @@ -303,73 +303,66 @@ public Stream<UserRepresentation> getUsers(
? Collections.emptyMap()
: SearchQueryUtils.getFields(searchQuery);

Stream<UserModel> userModels = Stream.empty();
if (search != null) {
SearchQueryUtils.UserSearchPrefix prefix = SearchQueryUtils.UserSearchPrefix.matching(search);
if (prefix != null) {
userModels = Arrays.stream(prefix.splitTerms(search))
Stream<UserModel> userModels = Arrays.stream(prefix.splitTerms(search))
.map(term -> prefix.lookup(session.users(), realm, term))
.filter(Objects::nonNull);
if (AdminPermissionsSchema.SCHEMA.isAdminPermissionsEnabled(realm)) {
userModels = userModels.filter(userPermissionEvaluator::canView);
}
} else {
Map<String, String> attributes = new HashMap<>();
attributes.put(UserModel.SEARCH, search.trim());
if (enabled != null) {
attributes.put(UserModel.ENABLED, enabled.toString());
}
if (emailVerified != null) {
attributes.put(UserModel.EMAIL_VERIFIED, emailVerified.toString());
}
addCreatedTimestampConditions(attributes, createdAfter, createdBefore);

return searchForUser(attributes, realm, userPermissionEvaluator, briefRepresentation, firstResult,
maxResults, false);
return toRepresentation(realm, userPermissionEvaluator, briefRepresentation, userModels);
}
} else if (last != null || first != null || email != null || username != null || emailVerified != null
}

if (search != null || last != null || first != null || email != null || username != null || emailVerified != null
|| idpAlias != null || idpUserId != null || enabled != null || exact != null || !searchAttributes.isEmpty()
|| createdAfter != null || createdBefore != null) {
Map<String, String> attributes = new HashMap<>();
if (last != null) {
attributes.put(UserModel.LAST_NAME, last);
}
if (first != null) {
attributes.put(UserModel.FIRST_NAME, first);
}
if (email != null) {
attributes.put(UserModel.EMAIL, email);
}
if (username != null) {
attributes.put(UserModel.USERNAME, username);
}
if (emailVerified != null) {
attributes.put(UserModel.EMAIL_VERIFIED, emailVerified.toString());
}
if (idpAlias != null) {
attributes.put(UserModel.IDP_ALIAS, idpAlias);
}
if (idpUserId != null) {
attributes.put(UserModel.IDP_USER_ID, idpUserId);
}
if (enabled != null) {
attributes.put(UserModel.ENABLED, enabled.toString());
}
if (exact != null) {
attributes.put(UserModel.EXACT, exact.toString());
}
addCreatedTimestampConditions(attributes, createdAfter, createdBefore);

attributes.putAll(searchAttributes);

return searchForUser(attributes, realm, userPermissionEvaluator, briefRepresentation, firstResult,
maxResults, true);
} else {
return searchForUser(new HashMap<>(), realm, userPermissionEvaluator, briefRepresentation,
firstResult, maxResults, false);
}
Map<String, String> attributes = new HashMap<>();
if (search != null) {
attributes.put(UserModel.SEARCH, search.trim());
Comment on lines +323 to +324

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accurate observation, but this is pre-existing behaviour in the LDAP provider rather than something introduced here, and it matches the current SPI contract documented in UserQueryMethodsProvider#searchForUserStream ("when SEARCH is set all other params are ignored"). Changing it means changing that contract and reworking the LDAP query building, so I have raised the scoping question with the maintainers in the conversation below rather than expanding this PR unilaterally.

}
if (last != null) {
attributes.put(UserModel.LAST_NAME, last);
}
if (first != null) {
attributes.put(UserModel.FIRST_NAME, first);
}
if (email != null) {
attributes.put(UserModel.EMAIL, email);
}
if (username != null) {
attributes.put(UserModel.USERNAME, username);
}
if (emailVerified != null) {
attributes.put(UserModel.EMAIL_VERIFIED, emailVerified.toString());
}
if (idpAlias != null) {
attributes.put(UserModel.IDP_ALIAS, idpAlias);
}
if (idpUserId != null) {
attributes.put(UserModel.IDP_USER_ID, idpUserId);
}
if (enabled != null) {
attributes.put(UserModel.ENABLED, enabled.toString());
}
if (exact != null) {
attributes.put(UserModel.EXACT, exact.toString());
}
addCreatedTimestampConditions(attributes, createdAfter, createdBefore);

return toRepresentation(realm, userPermissionEvaluator, briefRepresentation, userModels);
attributes.putAll(searchAttributes);

// Service accounts are excluded from full-text search, but included when filtering by explicit attributes.
boolean includeServiceAccounts = search == null;

return searchForUser(attributes, realm, userPermissionEvaluator, briefRepresentation, firstResult,
maxResults, includeServiceAccounts);
} else {
return searchForUser(new HashMap<>(), realm, userPermissionEvaluator, briefRepresentation,
firstResult, maxResults, false);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,46 @@ public void searchWithFilters() {
assertEquals(0, searchInvalidSizeAndDisabled.size());
}

@Test
@DatabaseTest
public void searchStacksWithOtherFilters() {
// The 'search' query param must combine with the other filters instead of overriding them, see
// https://github.com/keycloak/keycloak/issues/49995
UserRepresentation alice = new UserRepresentation();
alice.setUsername("stackalice");
alice.setEmail("alice@stackbug.test");
alice.setFirstName("Alice");
alice.setLastName("Stackbugcommon");
alice.setEmailVerified(true);
alice.setRequiredActions(Collections.emptyList());
alice.setEnabled(true);
createUser(alice);

UserRepresentation bob = new UserRepresentation();
bob.setUsername("stackbob");
bob.setEmail("bob@stackbug.test");
bob.setFirstName("Bob");
bob.setLastName("Stackbugcommon");
bob.setEmailVerified(true);
bob.setRequiredActions(Collections.emptyList());
bob.setEnabled(true);
createUser(bob);

// sanity check: the search term on its own matches both users via their last name
List<UserRepresentation> bySearchOnly = managedRealm.admin().users().search("Stackbugcommon", null, null, null, true, null, null, null);
assertThat(bySearchOnly, hasSize(2));

// search combined with username must narrow down to a single user, not ignore the username filter
List<UserRepresentation> bySearchAndUsername = managedRealm.admin().users().search("Stackbugcommon", null, null, null, true, "stackalice", null, null);
assertThat(bySearchAndUsername, hasSize(1));
assertEquals("stackalice", bySearchAndUsername.get(0).getUsername());

// search combined with email must narrow down as well
List<UserRepresentation> bySearchAndEmail = managedRealm.admin().users().search("Stackbugcommon", null, null, "bob@stackbug.test", true, null, null, null);
assertThat(bySearchAndEmail, hasSize(1));
assertEquals("stackbob", bySearchAndEmail.get(0).getUsername());
}

@Test
public void searchWithFilterAndEnabledAttribute() {
createUser();
Expand Down
Loading