Fix project Members page missing users with access via group-to-group share
What does this MR do and why?
When a project is shared with a group, and that group (or one of its parent groups) has itself invited another group via a group-to-group share, the members of that invited group have real access to the project but did not appear on the project's Members page. This MR makes those users show up, with the same role they actually have on the project.
The same fix applies to the REST API (GET /projects/:id/members/all, see Members API) and the GraphQL Project.projectMembers field, because they run the same query as the Members page.
Example:
- Group Y has a subgroup B.
- Group X is invited into Group Y (Y > Manage > Members > Invite a group).
- A project (outside Y) is shared with Group B.
- User A is a direct member of Group X.
User A can access the project (they have a ProjectAuthorization), but before this change the project's Members page did not list them. After this change they are listed, with their role capped to the lowest of: their role in X, the role X was invited into Y with, and the role B was invited into the project with.
This access path is documented: Member access and roles lists "Members of other groups that are shared with the invited group" among the members who get access when a group is invited to a project. The Members page now reflects that.
Users who cannot see a private group are still prevented from seeing that group's members through this path, matching the existing behavior for invited groups.
Important: only direct members of the shared-into group (X in the example) gain access. Inherited members of X (members of X's parent groups) do not gain access via this path. This matches how ProjectAuthorization is computed and is documented in the Sharing groups table ("Inherited member of the group that is invited: No").
Technical details
Root cause: MembersFinder#project_invited_groups built its set of source groups from project.invited_groups.self_and_ancestors and matched GroupMember rows only against those IDs. It never followed GroupGroupLink, so members whose GroupMember.source_id is a group shared into the invited groups' ancestor chain were never returned, even though ProjectAuthorization (via Members::MembersWithParents#members_from_self_and_ancestor_group_shares) already accounts for them.
Fix: In project_invited_groups, also include the groups shared into the invited groups' ancestor chain via GroupGroupLink.groups_having_access_to(invited_groups_ids_including_ancestors) (one hop of GroupGroupLink; shares are not transitive in GitLab), and union those with the original set. Only direct members of those shared-into groups are included; .self_and_ancestors is intentionally not chained, because inherited members of the shared-into group do not gain access. The same public_or_visible_to_user filter that already applies to the invited-groups chain when the current user is not a project member is applied to the new groups as well. Existing filtering (non_minimal_access, non_request, share_with_group_enabled? / coerce_to_no_access) is unchanged. The effective role still comes from project_authorizations.access_level via the existing COALESCE in MembersFinder#distinct_on. Everything is done with subqueries; no IDs are loaded into memory.
No feature flag: the change only affects which already-authorized users the Members page query returns. It does not grant or change any access; ProjectAuthorization is untouched.
Lint baseline: GroupGroupLink.groups_having_access_to was listed as an unused scope in scripts/lint/keela_baseline.yml. It is now used, so its entry is removed from the baseline.
References
Closes #592067
Related #545196 (the membership source label shown for these users is a separate display issue and is not addressed here).
Screenshots or screen recordings
No layout or component changes. The only visible effect is that the affected users now appear in the project Members list (see "How to set up and validate locally").
Database review
This MR changes the query built in MembersFinder#project_invited_groups (app/finders/members_finder.rb). No migrations or schema changes.
The SQL below is the real output of .to_sql, captured in CI by running spec/finders/members_finder_spec.rb against this branch (job log, search for SQL_BEFORE / SQL_AFTER). IDs are the test fixtures' (project_group_links.project_id = 10). Only whitespace was added for readability.
The project_invited_groups relation is one UNION ALL branch of the full Members query (MembersFinder#distinct_union_of_members, a SELECT DISTINCT ON (user_id, invite_email) ... LEFT JOIN project_authorizations). That outer query is unchanged; the before/after of the full query differs only in this branch (both full versions are in the job log under SQL_BEFORE full members query / SQL_AFTER full members query).
The author is a community contributor without postgres.ai / Database Lab access, so the database reviewer is asked to:
- Produce query plans against production-scale data via Database Lab, substituting a real
project_idfor a project shared with several groups that have deep ancestor chains and manygroup_group_links. - Confirm execution time stays within the query timing guidelines.
Note for the reviewer: the invited-groups ancestor subquery (base_ancestors_cte) appears twice in the new SQL (once as union branch 1, once inside the IN (...) of branch 2), so it is evaluated twice. If the plan shows this is costly, it can be hoisted into a single CTE.
Before (master): project_invited_groups, current user is a project member
SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", NULL AS member_role_id, "members"."expiry_notified_at", "members"."request_accepted_at"
FROM "members"
WHERE "members"."type" = 'GroupMember'
AND "members"."source_type" = 'Namespace'
AND "members"."source_id" IN (
WITH "base_ancestors_cte" AS MATERIALIZED (
SELECT "namespaces"."traversal_ids"
FROM "namespaces"
INNER JOIN "project_group_links" ON "namespaces"."id" = "project_group_links"."group_id"
WHERE "namespaces"."type" = 'Group' AND "project_group_links"."project_id" = 10
)
SELECT "id"
FROM "namespaces"
INNER JOIN (SELECT DISTINCT unnest("base_ancestors_cte"."traversal_ids") FROM base_ancestors_cte) AS ancestors(ancestor_id) ON namespaces.id = ancestors.ancestor_id
WHERE "namespaces"."type" = 'Group'
)
AND (members.access_level > 5)
AND "members"."requested_at" IS NULLAfter (this MR): project_invited_groups, current user is a project member
SELECT "members"."id", "members"."access_level", "members"."source_id", "members"."source_type", "members"."user_id", "members"."notification_level", "members"."type", "members"."created_at", "members"."updated_at", "members"."created_by_id", "members"."invite_email", "members"."invite_token", "members"."invite_accepted_at", "members"."requested_at", "members"."expires_at", "members"."ldap", "members"."override", "members"."state", "members"."invite_email_success", "members"."member_namespace_id", NULL AS member_role_id, "members"."expiry_notified_at", "members"."request_accepted_at"
FROM "members"
WHERE "members"."type" = 'GroupMember'
AND "members"."source_type" = 'Namespace'
AND "members"."source_id" IN (
SELECT "namespaces"."id" FROM (
(
WITH "base_ancestors_cte" AS MATERIALIZED (
SELECT "namespaces"."traversal_ids"
FROM "namespaces"
INNER JOIN "project_group_links" ON "namespaces"."id" = "project_group_links"."group_id"
WHERE "namespaces"."type" = 'Group' AND "project_group_links"."project_id" = 10
)
SELECT "namespaces"."id", "namespaces"."name", "namespaces"."path", "namespaces"."owner_id", "namespaces"."created_at", "namespaces"."updated_at", "namespaces"."type", "namespaces"."avatar", "namespaces"."membership_lock", "namespaces"."share_with_group_lock", "namespaces"."visibility_level", "namespaces"."request_access_enabled", "namespaces"."ldap_sync_status", "namespaces"."ldap_sync_error", "namespaces"."ldap_sync_last_update_at", "namespaces"."ldap_sync_last_successful_update_at", "namespaces"."ldap_sync_last_sync_at", "namespaces"."lfs_enabled", "namespaces"."parent_id", "namespaces"."shared_runners_minutes_limit", "namespaces"."repository_size_limit", "namespaces"."require_two_factor_authentication", "namespaces"."two_factor_grace_period", "namespaces"."project_creation_level", "namespaces"."runners_token", "namespaces"."saml_discovery_token", "namespaces"."runners_token_encrypted", "namespaces"."auto_devops_enabled", "namespaces"."extra_shared_runners_minutes_limit", "namespaces"."last_ci_minutes_notification_at", "namespaces"."last_ci_minutes_usage_notification_level", "namespaces"."subgroup_creation_level", "namespaces"."max_pages_size", "namespaces"."max_artifacts_size", "namespaces"."mentions_disabled", "namespaces"."default_branch_protection", "namespaces"."max_personal_access_token_lifetime", "namespaces"."shared_runners_enabled", "namespaces"."allow_descendants_override_disabled_shared_runners", "namespaces"."traversal_ids", "namespaces"."organization_id", "namespaces"."state"
FROM "namespaces"
INNER JOIN (SELECT DISTINCT unnest("base_ancestors_cte"."traversal_ids") FROM base_ancestors_cte) AS ancestors(ancestor_id) ON namespaces.id = ancestors.ancestor_id
WHERE "namespaces"."type" = 'Group'
)
UNION ALL
(
SELECT "namespaces"."id", "namespaces"."name", "namespaces"."path", "namespaces"."owner_id", "namespaces"."created_at", "namespaces"."updated_at", "namespaces"."type", "namespaces"."avatar", "namespaces"."membership_lock", "namespaces"."share_with_group_lock", "namespaces"."visibility_level", "namespaces"."request_access_enabled", "namespaces"."ldap_sync_status", "namespaces"."ldap_sync_error", "namespaces"."ldap_sync_last_update_at", "namespaces"."ldap_sync_last_successful_update_at", "namespaces"."ldap_sync_last_sync_at", "namespaces"."lfs_enabled", "namespaces"."parent_id", "namespaces"."shared_runners_minutes_limit", "namespaces"."repository_size_limit", "namespaces"."require_two_factor_authentication", "namespaces"."two_factor_grace_period", "namespaces"."project_creation_level", "namespaces"."runners_token", "namespaces"."saml_discovery_token", "namespaces"."runners_token_encrypted", "namespaces"."auto_devops_enabled", "namespaces"."extra_shared_runners_minutes_limit", "namespaces"."last_ci_minutes_notification_at", "namespaces"."last_ci_minutes_usage_notification_level", "namespaces"."subgroup_creation_level", "namespaces"."max_pages_size", "namespaces"."max_artifacts_size", "namespaces"."mentions_disabled", "namespaces"."default_branch_protection", "namespaces"."max_personal_access_token_lifetime", "namespaces"."shared_runners_enabled", "namespaces"."allow_descendants_override_disabled_shared_runners", "namespaces"."traversal_ids", "namespaces"."organization_id", "namespaces"."state"
FROM "namespaces"
WHERE "namespaces"."type" = 'Group'
AND "namespaces"."id" IN (
SELECT "group_group_links"."shared_with_group_id"
FROM "group_group_links"
WHERE "group_group_links"."shared_group_id" IN (
WITH "base_ancestors_cte" AS MATERIALIZED (
SELECT "namespaces"."traversal_ids"
FROM "namespaces"
INNER JOIN "project_group_links" ON "namespaces"."id" = "project_group_links"."group_id"
WHERE "namespaces"."type" = 'Group' AND "project_group_links"."project_id" = 10
)
SELECT "id"
FROM "namespaces"
INNER JOIN (SELECT DISTINCT unnest("base_ancestors_cte"."traversal_ids") FROM base_ancestors_cte) AS ancestors(ancestor_id) ON namespaces.id = ancestors.ancestor_id
WHERE "namespaces"."type" = 'Group'
)
)
)
) namespaces
WHERE "namespaces"."type" = 'Group'
)
AND (members.access_level > 5)
AND "members"."requested_at" IS NULLUNION ALL (not UNION) is used because duplicate IDs in the IN (...) predicate are harmless and remove_duplicates: false avoids the extra DISTINCT pass.
When the current user is not a project member
Before and after, branch 1 additionally carries the standard Group.public_or_visible_to_user predicate ("namespaces"."visibility_level" IN (10, 20) OR EXISTS (... authorized groups of the user ...)). After this MR the same predicate is also appended to branch 2. The full generated statement for this case is in the job log under SQL_AFTER project_invited_groups (non-member) (it is long because public_or_visible_to_user expands the user's authorized-groups subquery inline, exactly as it already does for branch 1 on master).
How to set up and validate locally
- Create a top-level group Y with a subgroup B, and another top-level group X.
- Add a user A to X as Owner (direct membership only; do not add A to any parent of X).
- In Y, go to Manage > Members > Invite a group and invite X with Maintainer role.
- Create a project outside Y, then Manage > Members > Invite a group and invite B with Developer role.
- Open the project's Manage > Members page and search for user A.
Before this change user A is missing. After this change user A is listed with Developer role (the lowest of Owner in X, Maintainer on the X→Y link, Developer on the B→project link).
Optional Rails console check after the UI setup:
project = Project.find_by_full_path('<path/to/project>')
user_a = User.find_by_username('<username>')
ProjectAuthorization.exists?(user: user_a, project: project)
# => true (both before and after the fix)
MembersFinder.new(project, project.first_owner)
.execute(include_relations: MembersFinder::RELATIONS)
.map(&:user).include?(user_a)
# => false before the fix, true afterTo verify that inherited members of X are NOT shown (expected behavior):
x_group = Group.find_by_full_path('<path/to/X>')
parent_of_x = Group.create!(name: 'Parent of X', path: 'parent-of-x')
x_group.update!(parent: parent_of_x)
inherited_user = User.create!(...)
parent_of_x.add_developer(inherited_user)
MembersFinder.new(project, project.first_owner)
.execute(include_relations: MembersFinder::RELATIONS)
.map(&:user).include?(inherited_user)
# => false (inherited members of the shared-into group do not gain access)MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.