currentUser.groups doesn't match group_users table

I believe this is a bug, but feel free to recategorize it to #dev

Steps to reproduce

  1. Create a theme by going to Admin > Themes > Common > </head> and paste this

    <script type="text/discourse-plugin" version="0.8.42">
      const currentUser = api.getCurrentUser()
      const groupIds = currentUser.groups.map(x => x.id)
      console.log(`User id: ${currentUser.id} Group ids: ${groupIds} (${groupIds.length})`)
    </script>
    
  2. Impersonate different users to see what groups you get

  3. Go to the Data Explorer (or the DB) and run this query

    SELECT
        u.id,
        array_agg(group_id),
        count(group_id)
    FROM
        group_users gu
        LEFT JOIN users u ON u.id = gu.user_id
        LEFT JOIN GROUPS g ON g.id = gu.group_id
    WHERE
        user_id IN (2, 3, 6) -- or whatever user ids you impersonated
    GROUP BY
        u.id
    ORDER BY
        u.id
    

DB output

- 2	{1,3,10,11,12,13,14,50,63,64,68}	11

- 3	{2,3,10,11,12,13,14,53,54,59,61,75,77,79,88}	15 

- 6	{10,11,54,75,77,87}	6

API output

- User id: 2 Group ids: 1, 3, 10, 11, 12, 13, 14, 50, 63, 64, 68 (11)

- User id: 3 Group ids: 10, 53, 54, 59, 61, 75, 79, 88 (8) <= 7 missing groups

- User id: 6 Group ids: 10, 54 (2) <= 4 missing groups

The DB output is correct (I can see those users do have those groups assigned in the UI), so there seems to be a problem in the API. What could be causing some groups to not get sent to currentUser.groups?

Is it possible that the missing groups have been configured to be hidden from the users? If so, then they won’t be included in the currentUser object on the client.

4 Likes

Ah, good point. No longer a bug then. Thanks!

2 Likes