Enforce read-only state for groups and projects pending deletion

What does this MR do and why?

A group marked for deletion, and everything under it, still accepts writes, and that work is lost when the group is deleted. This MR makes a group read-only once it is scheduled for deletion. The same applies to its subgroups and projects.

Related to #457722 (closed). It doesn't close the issue, because group name, path and settings changes are not blocked yet (see Not in this MR).

Changes

  • Policies: GroupPolicy and ProjectPolicy now prevent the group:archived and project:archived permissions when the resource or any ancestor group is scheduled for deletion (scheduled_for_deletion_in_hierarchy_chain?).
  • Permission lists: added the delete actions that were previously allowed while pending deletion (the archived & ~group_scheduled_for_deletion exception and its EE counterpart are removed), plus create_group_link, rename_project, admin_group_member, admin_project_member and invite_project_members.
  • Member removal still works: it now uses destroy_project_member (maintainer) and destroy_group_member (owner) instead of admin_*_member, so it isn't blocked. SAML and LDAP locks still apply.
  • Docs: the group and project deletion pages say that a resource pending deletion is read-only.

Also affects archived resources: archived and pending-deletion resources share one permission list, so archived groups and projects can no longer be shared or get new members, and archived projects can no longer be renamed. This matches #481743 (closed). The group that owns archiving should review it.

Not in this MR

  • Group name, path and settings: only admin_group covers them, and it also controls restore, unarchive and export, so it isn't blocked. Follow-up: #627530
  • Project rename in the UI: rename_project is only checked by the REST API. Follow-up: #628246
  • destroy_wiki: added to the list, but nothing checks it. Follow-up: #628245
  • Request and system spec coverage for the remaining permissions. Follow-up: #628244
  • Deleting or restoring a child of a pending-deletion group: #549074 (closed)

Database

The wider ancestor check adds one group_deletion_schedules lookup per namespace chain. It's cached per request. For lists, NamespaceSetting.with_ancestors_inherited_settings now also returns self_or_ancestors_deletion_scheduled, so preloaded callers make no extra query. Both queries use only index scans, and no new index is needed.

Query Cold Warm Plan
Ancestor lookup, 9-level chain 16.2 ms 0.26 ms https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/65450/commands/170246
Preload, 100 groups (new column adds 51 ms cold, 0.6 ms warm) 456 ms 3.95 ms https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/65450/commands/170247
Preload, 100 groups, master 3.35 ms https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/65450/commands/170248
Query budget changes

Single-resource endpoints make one extra query:

  • POST /projects/:id/members: 110 → 119
  • POST /namespaces/:id/-/work_items with a parent: new limit of 108
  • Groups::IssuesController#bulk_update: 432 → 433
  • Board issue move mutation: 189 → 191, and 111 → 113 for the nil-epic case

These go away with gitlab-org#21607, which stores namespace state on the record.

Testing notes
  • Specs schedule deletion with Groups::MarkForDeletionService, not the :deletion_scheduled trait. The trait doesn't create the group_deletion_schedules row that ancestor lookups read, so descendants wouldn't see the parent as scheduled.
  • The allows destroy actions case in the group archiving abilities shared example never asserted anything. It is replaced with a real prevents destroy actions check.
  • Request specs cover the endpoints for rename_project, create_group_link and adding members.

Screenshots or screen recordings

N/A. Backend policy changes only.

How to set up and validate locally

group = Group.find_by_full_path('your-group')
user  = group.owners.first
::Groups::MarkForDeletionService.new(group, user).execute

Ability.allowed?(user, :destroy_issue, group)                       # => false
Ability.allowed?(user, :admin_group_member, group)                  # => false
Ability.allowed?(user, :invite_group_members, group.children.first) # => false
Ability.allowed?(user, :create_issue, group.projects.first)         # => false

Ability.allowed?(user, :read_group, group)                          # => true
Ability.allowed?(user, :remove_group, group)                        # => true
Ability.allowed?(user, :admin_group, group)                         # => true (restore, export)

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.

Edited by Shubham Kumar

Merge request reports

Loading
Loading