Skip to content

Commit

Permalink
Fix user search on keycloak instances without machine role
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro committed Jul 11, 2024
1 parent effa13b commit cc658ea
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ private static boolean isTeam(String role) {

@Override public List<UserService.UserData> searchUsers(String query) {
try {
Set<String> machineIds = keycloak.realm(realm).roles().get(Roles.MACHINE).getUserMembers(0, Integer.MAX_VALUE).stream().map(UserRepresentation::getId).collect(Collectors.toSet());
Set<String> machineIds = safeMachineIds();
return keycloak.realm(realm).users().search(query, null, null).stream().filter(rep -> !machineIds.contains(rep.getId())).map(KeycloakUserBackend::toUserInfo).toList();
} catch (Throwable t) {
throw ServiceException.serverError("Unable to search for users");
}
}

private Set<String> safeMachineIds() {
try {
return keycloak.realm(realm).roles().get(Roles.MACHINE).getUserMembers(0, Integer.MAX_VALUE).stream().map(UserRepresentation::getId).collect(Collectors.toSet());
} catch (Exception e) {
// ignore exception
return Set.of();
}
}

@Override public List<UserService.UserData> info(List<String> usernames) {
List<UserService.UserData> users = new ArrayList<>();
for (String username : usernames) {
Expand Down

0 comments on commit cc658ea

Please sign in to comment.