# 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:** 1\
**Showing post:** 9

<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
}

```

---

_[View the full topic](https://meta.discourse.org/t/plugin-to-show-full-name-only-to-users-of-the-same-group/203813)._
