currentUser.groups doesn't match group_users table

AI-generated summary

currentUser.groups doesn’t match group_users table

Nacho_Caballero reported an issue where currentUser.groups does not match the groups listed in the group_users table. To reproduce, they created a theme to log the user’s ID and group IDs, then compared it to the actual groups in the database using a Data Explorer query.

The results showed discrepancies between the API output and the DB output, with some groups missing from currentUser.groups. david suggested that the missing groups might be hidden from users, which would explain why they are not included in the currentUser object.

Nacho_Caballero confirmed that this is the case, and the issue is no longer considered a bug.

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

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.

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