Search code examples
javakeycloakkeycloak-rest-api

How to search users inside group in Keycloak?


Using Keycloak 11.0.3. I trying to search users inside group using Keycloak API:

List<UserRepresentation>  users = realmResource.users().search(username, firstname, lastname, email,
    0, 100);

But when I try to get groups of found users I get null even if user have group:

List<String> groups = users.get(0).getGroups(); //It's null

So how to search users inside group?


Solution

  • To get the groups that a user belongs to try with the following:

    realm.users().get(userId).groups();
    

    Use the userID instead.

    To get the users of a given group do the following:

    realm.groups().group(groupId).members();