3

I'm working on a project using Angular - Spring boot - Keycloak 16.0 and one of the requirements is for the user to be able to fetch the user's list from Keycloak paginated, filtered and sorted by a user attribute (ex. email, username, etc.).

Based on Keycloak's documentation and by reading the source code on Github filtering and pagination is possible, but there's no sorting feature (the result seems always sorted by username):

/**
 * Get users
 *
 * Returns a stream of users, filtered according to query parameters.
 *
 * @param search A String contained in username, first or last name, or email
 * @param last A String contained in lastName, or the complete lastName, if param "exact" is true
 * @param first A String contained in firstName, or the complete firstName, if param "exact" is true
 * @param email A String contained in email, or the complete email, if param "exact" is true
 * @param username A String contained in username, or the complete username, if param "exact" is true
 * @param emailVerified whether the email has been verified
 * @param idpAlias The alias of an Identity Provider linked to the user
 * @param idpUserId The userId at an Identity Provider linked to the user
 * @param firstResult Pagination offset
 * @param maxResults Maximum results size (defaults to 100)
 * @param enabled Boolean representing if user is enabled or not
 * @param briefRepresentation Boolean which defines whether brief representations are returned (default: false)
 * @param exact Boolean which defines whether the params "last", "first", "email" and "username" must match exactly
 * @param searchQuery A query to search for custom attributes, in the format 'key1:value2 key2:value2'
 * @return a non-null {@code Stream} of users
 */
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<UserRepresentation> getUsers(@QueryParam("search") String search,
                                           @QueryParam("lastName") String last,
                                           @QueryParam("firstName") String first,
                                           @QueryParam("email") String email,
                                           @QueryParam("username") String username,
                                           @QueryParam("emailVerified") Boolean emailVerified,
                                           @QueryParam("idpAlias") String idpAlias,
                                           @QueryParam("idpUserId") String idpUserId,
                                           @QueryParam("first") Integer firstResult,
                                           @QueryParam("max") Integer maxResults,
                                           @QueryParam("enabled") Boolean enabled,
                                           @QueryParam("briefRepresentation") Boolean briefRepresentation,
                                           @QueryParam("exact") Boolean exact,
                                           @QueryParam("q") String searchQuery)

My question is, is there something that I'm missing? Has anyone run into the same problem who can suggest me a possible solution?

Chris K
  • 347
  • 1
  • 3
  • 16
  • Currently, there is no support to sort users by email, users are by default sorted by username. I would suggest you to pitch this enhancement in keycloak community or create Rest API in front of Keycloak API that will sort the users for you according to email – Abhijeet Jul 05 '22 at 08:37

0 Answers0