Plugin Development: List a user’s groups without making a request


(Philipp Rudloff) #1

As per “Getting user's custom groups problems”, the property Discourse.User.current().groups isn’t exposed to all pages.

As per “Discourse docs: Get a single user by username”, the response payload includes a groups array which would be sufficient.

However, I would prefer to not send a request for this information if I can get it without. Is there a way to list a user’s groups without sending a request?

I need to access a user’s group within my plugins Ember route.


(Vinoth Kannan) #2

You can add custom plugin code to achieve this. Something similar to below code (unverified). Then you can access it like Discourse.User.current().groups

  add_to_serializer(:current_user, :groups, false) {
    object.groups.pluck(:name)
  }

(Philipp Rudloff) #3

Great, that’s exactly what I was looking for. Logging Discourse.User.current().groups now:

[ "trust_level_0", "trust_level_1", "admins", "staff", "TestGroup" ]

(Rafael dos Santos Silva) #5

I was discussing with @eviltrout about adding this per default for the current user serializer since many themes can benefit from this.

Is this still in your plans @eviltrout ?


(Régis Hanol) #6

@maja, can you add that to your list?

We should always serialize all the group names of the current user (not only the primary group).


A way in client javascript to find if current user is member of private group?