p2-inc / keycloak-orgs

Single realm, multi-tenancy for SaaS apps
Other
362 stars 65 forks source link

Search for list of users in `/{realm}/orgs/{orgId}/members` #132

Closed matheushent closed 3 months ago

matheushent commented 9 months ago

I'd like to request the option to search for a list of input members instead of single searching. Currently, we can do something similar to GET /realms/{realm}/orgs/{orgId}/members?search=matheus. I mean, it'd be very cool if we can do something like GET /realms/{realm}/orgs/{orgId}/members?search=matheus,john.doe,jane.

I believe it wouldn't be that big of a deal, just need to work on this function.

matheushent commented 9 months ago

my first draft is

public Stream<UserModel> searchForMembersStream(
      String search, Integer firstResult, Integer maxResults) {
    // Split the search string into individual search terms
    String[] searchTerms = search.split(",");

    // TODO: This could be optimized for large member lists with a query
    return getMembersStream()
        .filter(
            (m) -> {
              if (searchTerms.length == 0 || searchTerms[0].isEmpty()) return true;

              // Check if any of the search terms match any user attribute
              for (String term : searchTerms) {
                term = term.trim().toLowerCase(); // Remove leading/trailing spaces and convert to lowercase
                if ((m.getEmail() != null && m.getEmail().toLowerCase().contains(term))
                    || (m.getUsername() != null && m.getUsername().toLowerCase().contains(term))
                    || (m.getFirstName() != null && m.getFirstName().toLowerCase().contains(term))
                    || (m.getLastName() != null && m.getLastName().toLowerCase().contains(term))) {
                  return true; // User matches at least one search term
                }
              }
              return false; // User does not match any search terms
            })
        .skip(firstResult)
        .limit(maxResults);
}

I just wonder it can be inefficient for many users in the string

xgp commented 9 months ago

@matheushent Is there an example of multi-user search anywhere in the Keycloak Admin API? In general, we try to follow the conventions established there, but I'm willing to add if there is a compelling use case.

matheushent commented 9 months ago

indeed, there's no such a thing in the Keycloak admin api in general, which I think is a miss TBH. In my case, the use case is simply to speed up a general search I do on Keycloak to know if a list of users belong to an organization. Right now I'm implementing the parallelism at my application level.

xgp commented 9 months ago

@matheushent Thanks. We'll look at a PR. Please make sure it includes a test.

injae-kim commented 3 months ago

Awesome feature request! can't wait to use this bulk search API :)

millwheel commented 3 months ago

I created PR for this issue

xgp commented 3 months ago

fixed in #208