E. Karim
E. Karim

Reputation: 759

Is there a way to add a user to several groups (in bulk) in Keycloak using Keycloak Admin REST API?

At the moment I am adding the user to one group at a time using this endpoint:

PUT /{realm}/users/{id}/groups/{groupId}

In my use case it would be beneficial to perform the affectations in bulk, so far I haven't found a documented way of doing so, is there a way to do it? Thanks

Upvotes: 4

Views: 4742

Answers (3)

user3079834
user3079834

Reputation: 2214

The following way still works as of Keycloak v21.0.2 but is super uncomfortable, especially in case when one group should be joined and another should be left:

PUT /{realm}/users/{id}/groups/{groupId}

In documentation for UserRepresentation the entry groups is listed as < string > array, however fetching user with

GET /{realm}/users/{id}

returns a UserRepresentation that is lacking the entry groups (in my case the user was part of a group, so it was not empty).

When updating a user with

PUT /{realm}/users/{id}

with a minimal UserRepresentation with only groups defined like this:

{"groups":["cdcb76bd-ebb1-48ef-bec2-a81aad370746"]}

the SuccessCode 204 is returned, but nothing changed (no removing unmentioned group or adding new group).

This is still an active issue as of May 2023, see here: https://github.com/keycloak/keycloak/issues/9354.

Upvotes: 1

Ramil Minnigaliev
Ramil Minnigaliev

Reputation: 49

PUT /{realm}/users/{id}

It's in the documentation, but it doesn't work.

I started discussion on github

Upvotes: 4

TacheDeChoco
TacheDeChoco

Reputation: 3903

You could try to update the full user data

PUT /{realm}/users/{id}

with a partial UserRepresentation containing a minimal json with "groups" array only ?

I see that nearly all fields are marked as optional: cfr https://www.keycloak.org/docs-api/12.0/rest-api/index.html#_userrepresentation

Upvotes: 2

Related Questions