# Plugin to show full name only to users of the same group

**URL:** https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813
**Category:** Development
**Created:** [September 17, 2021, 12:58pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813 "2021-09-17T12:58:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![matewka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matewka/32/163418_2.png) [@matewka](https://meta.discourse.org/u/matewka)
#### Post date: [September 17, 2021, 12:58pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/1 "2021-09-17T12:58:14Z")

</div>

Hi everyone,

I’m googling my problem for a few weeks already and I’m pretty close to giving up.

The background:  
I develop and maintain a multi-tenant admin panel for sport clubs.  
My self-hosted discourse forum is available only to members of those clubs. I would like it to be open for public. My own SSO integration automatically assigns users’ full name from my database to the discourse user once logged in.

The problem:  
Right now I cannot make my forum public because the full name of a user should be only visible to users of the same club.

My experience as a Ruby developer is zero (I’m a JS dev) but so far I’ve figured out this part of code:

```plaintext
# plugin.rb

after_initialize {
  require_dependency 'basic_user_serializer'
  require_dependency 'current_user'

  class ::BasicUserSerializer
    attributes :name

    def name
      # fixed set of group names that indicate belonging to a club
      clubGroups = Array['foo', 'bar', 'baz']
      
      # PART THAT I CAN'T FIGURE OUT:
      # Show the name of the user ONLY if they share one of the same of the currently logged user's `clubGroups`,
      # e.g. I'm logged in as a user who belongs to 'bar' group, meaning I belong to the Bar club. I should be able to see names of only those users who also belong to the 'bar' group
      ???

    end
  end
}

```

I can’t figure out the following things:

1. how to fetch current user’s groups
2. how to fetch featured user’s groups

Once I have the above, I could compare the 2 arrays with the `clubGroups` and decide wether to show the user’s name or not.

Another challenge for me will be turning off the possibility to edit your own name, but that’s a different story.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 17, 2021, 1:37pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/2 "2021-09-17T13:37:01Z")

</div>

The `GroupUser` model contains the Group membership by User.

You want something like: `GroupUser.where(user_id: current_user.id)` to return the Group ids for the current user.

That will give you an Active Record Relation.

Then you might want to `map` it into an array:

```plaintext
GroupUser.where(user_id: myuser.id).map {|gu| gu.group_id}

```

Follow the same approach for your featured user.

---

<div class="post-metadata">

### Author: ![matewka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matewka/32/163418_2.png) [@matewka](https://meta.discourse.org/u/matewka)
#### Post date: [September 17, 2021, 1:57pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/3 "2021-09-17T13:57:16Z")

</div>

Perfect! Thanks 🙂

I don’t know yet how to turn it into a working solution but that’s already a major hint.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 17, 2021, 1:57pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/4 "2021-09-17T13:57:40Z")

</div>

Never give up. Just keep going. 🚀

---

<div class="post-metadata">

### Author: ![matewka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matewka/32/163418_2.png) [@matewka](https://meta.discourse.org/u/matewka)
#### Post date: [September 17, 2021, 2:11pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/5 "2021-09-17T14:11:52Z")

</div>

Another obstacle:

When trying to fetch current user’s ID to pass to the query, I don’t really know how I can fetch it.  
When I navigate to the CurrentUser model in the code, the only method that looks relevant to my requirements is `current_user` but when I do this:

```plaintext
pp CurrentUser.current_user

```

it throws me:

```plaintext
undefined method `current_user' for CurrentUser:Module

```

I think I’m definitely missing some basic knowledge about Ruby here but maybe you can easily solve it for me?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 17, 2021, 2:19pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/6 "2021-09-17T14:19:49Z")

</div>

You might want to look at the code of this plugin. It adds avatar flair for people in the same groups so they are able to recognize each other as group members. That’s a different thing but it does contain the code that checks if the viewing user is in the same group as the user that posted.

> <https://github.com/communiteq/discourse-membership-indicator/blob/master/plugin.rb>

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 17, 2021, 2:23pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/7 "2021-09-17T14:23:33Z")

</div>

The clue here is this method:

> <https://github.com/discourse/discourse/blob/e7b8e755833c56aafd8b33be5487c3228d872a43/app/serializers/basic_user_serializer.rb#L26>

```plaintext
  def user_is_current_user
    object.id == scope.user&.id
  end

```

The user being serialized is `object`

The current user is actually in `scope.user`, so:

```plaintext
GroupUser.where(user_id: scope.user&.id).map {|gu| gu.group_id}

```

probably works for current user.

etc. etc.

Good tips:

1. Read the Discourse source code
2. As per @RGJ look at a few open source plugins to see how they do things
3. Prototype stuff in the rails console `rails c --sandbox`

---

<div class="post-metadata">

### Author: ![matewka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matewka/32/163418_2.png) [@matewka](https://meta.discourse.org/u/matewka)
#### Post date: [September 17, 2021, 2:31pm UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/8 "2021-09-17T14:31:32Z")

</div>

Amazing! Lots of great knowledge. Thanks guys 😍

I’m diving into it :dealwithit:

---

<div class="post-metadata">

### Author: ![matewka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matewka/32/163418_2.png) [@matewka](https://meta.discourse.org/u/matewka)
#### Post date: [September 18, 2021, 10:12am UTC](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813/9 "2021-09-18T10:12:21Z")

</div>

For the sake of future visitors of this topic, here’s the final solution that I came up with.

It’s not perfect, nor optimised but working! And that’s the first and most important step 😉

```plaintext
after_initialize {
  require_dependency 'basic_user_serializer'

  class ::BasicUserSerializer
    attributes :name

    def name
      if user_is_current_user
        return _old_name_method
      else
        # TODO: make this list editable
        clubGroups = Array['...']

        if scope.user
          ownGroups = GroupUser.where(user_id: scope.user.id)
                               .map { |gu| gu.group_id }
                               .map { |group_id| Group.find_by(id: group_id) }
                               .filter { |group| clubGroups.include? group.name }

          if ownGroups.length > 0
            ownGroupsIds = ownGroups.map { |g| g.id }

            if GroupUser.exists?(user_id: user.id, group_id: ownGroupsIds)
              return _old_name_method
            end
          end
        end
      end

      return ''
    end

    private def _old_name_method
      return Hash === user ? user[:name] : user.try(:name)
    end
  end
}

```
