Add users to (and remove from) groups via API

(Adam Capriola) #1

Is the API at a point yet where you could add a user to (and remove them from) a group? This would be very useful for integration with other software where users need synchronized permissions across platforms. I tried researching couldn’t find any examples.

Sidenote: I think group mapping via SSO would be cool too, but doing it via API is probably more pertinent. As I understand, SSO would only add the groups (or whatever information) when the user logs in. They may stay logged in to Discourse indefinitely, but their permissions could change on the other side of a website more frequently. (I have membership-type sites in mind.) If an API was available for group management, accounts could be more accurately synced.

(Johan Jatko) #2

As Discourse is an Ember.js app for frontend, and a JSON API for backend, there should be an API endpoint for add users to group.
(You can try running the browser console with network-logging turned on, while adding a user to a group)

(Adam Capriola) #3

Damn, that’s pretty awesome! With that info in hand I was able to put something together for WordPress integration. This will need to be customized by anyone who wants to use it, but it is a relatively simple example that monitors for changes in a custom user_meta key to trigger an API call and synchronize the user permissions with Discourse. (This is more flexible than relying on WordPress “roles” and “capabilities.”) The API call could be adapted for WordPress SSO too.

(I’m in awe of how flawlessly this works, by the way. Amazing stuff!)

(David García-Navas) #4

Sorry for revive this old topic :innocent:

And thanks for sharing the code here. We’ve been testing it but we’ve not been able to make it work properly to sync groups with users, we have this error:

array(1) { ["basic_group"]=> array(8) { ["id"]=> int(41) ["automatic"]=> bool(false) ["name"]=> string(8) "Colombia" ["user_count"]=> int(2) ["alias_level"]=> int(99) ["visible"]=> bool(false) ["automatic_membership_email_domains"]=> NULL ["automatic_membership_retroactive"]=> bool(false) } }

(Colombia is the name of the group)

Some idea of what are we doing bad?

(Adam Capriola) #5

The groups API changed over the winter: