# Groups API - manually syncing group membership?

**URL:** https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521
**Category:** Development
**Tags:** rest-api
**Created:** [June 11, 2020, 1:15pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521 "2020-06-11T13:15:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Judith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/judith/32/289903_2.png) [@Judith](https://meta.discourse.org/u/Judith)
#### Post date: [June 11, 2020, 1:15pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/1 "2020-06-11T13:15:25Z")

</div>

I seem to have boxed myself into a corner here..

I have a membership site (also in Rails) and I’m using the Discourse API in order to automatically create and populate restricted forum groups for all of my user groups. This works great; I’m even giving each group a wiki post to start with. (I’ve [posted my code](https://meta.discourse.org/t/providing-restricted-subforums-for-external-groups/139107).)

Now I’ve come across the issue that sometimes I need to manually sync up the group membership, for example when merging groups on my site or when the coordinator accidentally deleted/added people via the forum. For this purpose, I want to delete ALL Discourse group members and then re-add the ones that should be in it.

First problem: the Discourse API doesn’t seem to come with a DELETE ALL for group members, so I have to specify the names of everyone that should be deleted.

Second problem: I cannot actually retrieve the Discourse group’s current membership, because for whatever reason it’s asking me for the group NAME rather than ID. I’ve only stored the group ID on my site, because that’s what Discourse uses for the related PUT/POST/DELETE requests.

Third problem: It should be obvious, but I don’t see a way to retrieve the group’s name by supplying its ID, unless I want to retrieve all 200+ groups and iterate through them until I find one whose ID matches. This seems a ridiculous amount of trouble and also contravenes Rails conventions.

What am I missing?

EDIT: @codinghorror was really helpful last time - any ideas now?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 13, 2020, 3:50pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/2 "2020-06-13T15:50:31Z")

</div>

I’m not sure but maybe @justin or @blake could check in on this during their upcoming work week?

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [June 15, 2020, 2:08pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/4 "2020-06-15T14:08:31Z")

</div>

> [@Judith](#):
>
> Third problem: It should be obvious, but I don’t see a way to retrieve the group’s name by supplying its ID, unless I want to retrieve all 200+ groups and iterate through them until I find one whose ID matches. This seems a ridiculous amount of trouble and also contravenes Rails conventions.

You seem to be right on this – you cannot access group resources with the ID number, only the name.

> <https://github.com/discourse/discourse/blob/70e50a2255ebb30c78891a7f846e833ab85b0a8b/app/controllers/groups_controller.rb#L113>

The ID referenced here is not an ID, but the name, per

> <https://github.com/discourse/discourse/blob/70e50a2255ebb30c78891a7f846e833ab85b0a8b/app/controllers/groups_controller.rb#L569>

It probably makes sense to refactor the `/g/<group-name>` route to match how we now handle topics and categories. Something like `/g/<group-name>/<id>` or `/g/patrons/41` and allow `/g/41` to function the same way.

Until we fix that up, though, it seems the only way to accomplish what you’re looking for is:

1. Storing the group name alongside the id
2. Hitting `g/<group-name>/members.json` with a GET request to get the members
3. Loop through those to delete each so you can re-sync.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [June 15, 2020, 3:50pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/5 "2020-06-15T15:50:58Z")

</div>

This is an awesome problem to be looking into. Thanks for raising it, @Judith! Groups are so powerful and have so much promise! 🚀 I noticed this issue recently as well, when looking at maintaining group membership from wordpress. It would be pretty neat to be able to automagically add/remove people from groups for country and region of the world they say they are based in, languages they speak, themes they are interested in, etc, when they sign up and update their profiles in wordpress.

@justin’s suggestion of storing both group name and group ID is a good one and I’ll pass it on to the folks working on this integration now.

One feature I’d also love to see added to the UI is the ability for admins or group owners to bulk remove all members from a group, and re-add them. This would allow them to reset their group memberships manually from time to time based on exports from wordpress, even if they don’t have an automated systme in place to add/remove people from groups as they update their profiles in wordpress.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [June 15, 2020, 4:02pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/6 "2020-06-15T16:02:43Z")

</div>

> [@Judith](#):
>
> I have a membership site (also in Rails) and I’m using the Discourse API in order to automatically create and populate restricted forum groups for all of my user groups. This works great; I’m even giving each group a wiki post to start with. (I’ve [posted my code](https://meta.discourse.org/t/providing-restricted-subforums-for-external-groups/139107).)

I recommend using SSO between your main site and Discourse. That way you get group membership handling for free, and will have a `sync_sso` endpoint to update the membership in a straightforward way.

---

<div class="post-metadata">

### Author: ![Judith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/judith/32/289903_2.png) [@Judith](https://meta.discourse.org/u/Judith)
#### Post date: [June 15, 2020, 4:36pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/7 "2020-06-15T16:36:48Z")

</div>

At first I was only using SSO, but then I ran into the problem that it only updates group membership when people log in. In the case of people not being re-elected, or otherwise losing the right to access a group, or gaining the right to access a group (and should receive email notifications for messages in the restricted group subforum), I need to add/remove them via the API.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [June 15, 2020, 4:51pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/8 "2020-06-15T16:51:56Z")

</div>

> [@Judith](#):
>
> I ran into the problem that it only updates group membership when people log in

The `sync_sso` exists to address this exact problem! You can trigger what is effectively a “virtual” sso login anytime a membership change to fully sync up a user.

> [@Sync DiscourseConnect user data with the sync\_sso route](https://meta.discourse.org/t/sync-sso-user-data-with-the-sync-sso-route/84398):
>
> Single Sign On can be used to handle Discourse user authentication from a separate site. The [Setup DiscourseConnect - Official Single-Sign-On for Discourse (sso)](https://meta.discourse.org/t/official-single-sign-on-for-discourse-sso/13045) topic has details about how to implement [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) . The Problem With [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) , Discourse users will be created or updated when they login to Discourse from your external website. What it doesn’t handle is when you need to create or update Discourse users without having them login to your site. For sites that are using…

---

<div class="post-metadata">

### Author: ![gassim](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gassim/32/220910_2.png) [@gassim](https://meta.discourse.org/u/gassim)
#### Post date: [September 18, 2022, 8:52am UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/9 "2022-09-18T08:52:15Z")

</div>

> [@tobiaseigen](#):
>
> One feature I’d also love to see added to the UI is the ability for admins or group owners to bulk remove all members from a group, and re-add them. This would allow them to reset their group memberships manually from time to time based on exports from wordpress, even if they don’t have an automated systme in place to add/remove people from groups as they update their profiles in wordpress.

Hey!

Is this feature now available? Any updates on this?

Thanks!

---

<div class="post-metadata">

### Author: ![Judith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/judith/32/289903_2.png) [@Judith](https://meta.discourse.org/u/Judith)
#### Post date: [December 13, 2022, 1:14pm UTC](https://meta.discourse.org/t/groups-api-manually-syncing-group-membership/154521/10 "2022-12-13T13:14:26Z")

</div>

Any updates? @tobiaseigen
