API: Is there a way to add a single user to a group without replacing all members?
-
Hi all,
We want to set Groups for Users via the API, as far as I can tell, there are two ways to change group membership:
- PUT /api/v1/groups/:groupId/members with { "userIds": }
- PUT /api/v1/users/:userId/groups with { "groupIds": [...] }
Looking at src/groups.js, both seem to replace rather than add:
- setMembers() first runs DELETE FROM groupMembers WHERE groupId = ? and then inserts only the userIds from the request.
- setLocalMembership() first removes the user from all local groups and then inserts only the groupIds from the request.
-> This means we cant just Add a new user to a Groupe via API correct? We woud always have to first get the Group then take all the listed useres Append the new users and run the Put groups API with the Appended list - or do the same with all current groups from a user?
-
Hello @n0ah
Correct, both calls replace the full list. There is no endpoint that adds or removes a single member.
PUT /api/v1/groups/:groupId/membersreplaces all members of that group.PUT /api/v1/users/:userId/groupsreplaces that user's memberships in local groups.
Memberships in external LDAP groups are left alone.
So it is read, modify, write.
I would use the per-user endpoint, since it only changes that one user.
With/groups/:groupId/members, two clients adding different users to the same group at the same time can overwrite each other.USER_ID=uid-xxx GROUP_ID=gid-yyy CURRENT=$(curl -s -H "Authorization: Bearer $TOKEN" "https://my.example.com/api/v1/users/$USER_ID" | yq -p json -o json -I0 '.groupIds') curl -X PUT -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \ "https://my.example.com/api/v1/users/$USER_ID/groups" \ -d "{\"groupIds\": $(echo "$CURRENT" | yq -p json -o json -I0 ". + [\"$GROUP_ID\"] | unique")}"
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login