Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Offical apps | Community apps | Demo | Docs | Install
  1. Cloudron Forum
  2. Discuss
  3. API: Is there a way to add a single user to a group without replacing all members?

API: Is there a way to add a single user to a group without replacing all members?

Scheduled Pinned Locked Moved Discuss
2 Posts 2 Posters 69 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N
    N
    N0ah
    wrote last edited by
    #1

    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:

    1. PUT /api/v1/groups/:groupId/members with { "userIds": }
    2. 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?

    1 Reply Last reply
    2
    • jamesJ
      jamesJ
      james
      Staff
      wrote last edited by
      #2

      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/members replaces all members of that group.
      • PUT /api/v1/users/:userId/groups replaces 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")}"
      1 Reply Last reply
      1

      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
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Bookmarks
      • Search