Skip to content

fix(deleter): user delete removes every policy the user holds - #2022

Open
AmanGIT07 wants to merge 1 commit into
mainfrom
fix/user-delete-removes-every-policy
Open

AmanGIT07 wants to merge 1 commit into
mainfrom
fix/user-delete-removes-every-policy

Conversation

@AmanGIT07

Copy link
Copy Markdown
Contributor

Summary

DeleteUser now deletes every live policy the user holds before it deletes the user. Before, it only removed the user from the orgs where it held an org-level policy, so these policies stayed live and kept naming a user that no longer exists:

  • a policy on a disabled project;
  • the group member policy on a disabled group;
  • a policy on a project in an org where the user has no org-level policy (CreatePolicy does not check org membership);
  • a policy on a resource inside a disabled project;
  • policies left by an earlier attempt that failed partway. The org policy is deleted first, so a retry no longer visits that org.

Changes

  • core/deleter/service.go: after the org removals, DeleteUser calls RemovePrincipalPolicies for the user, before it deletes the PATs and the user. A failure stops the delete and leaves the user in place; a retry deletes the rest.
  • core/deleter/mocks/membership_service.go: regenerated for the new interface method.

Technical Details

  • membership.Service.RemovePrincipalPolicies lists the live policies by principal, on any resource type and in any org, and deletes each one with policy.Service.Delete: the role binding relations first, then the row. The service user delete uses the same call.
  • The org removals stay, so each org still gets its member-removed audit record. ForceRemoveOrganizationMember is unchanged.
  • The leftover policies granted no access, because user.Service.Delete already removes every relation whose subject is the user, which includes each policy's bearer relation. They still showed in ListPolicies and GetPolicy.

Test Plan

  • core/deleter/service_test.go TestDeleteUser: the user is removed from each org, then its remaining policies are deleted, then its PATs, then the user. An org removal failure stops the delete before the policy step, and a policy step failure stops it before the PATs and the user. The cases fail without the policy step, with its error only logged, or with it after the PAT step.
  • e2e TestUserDeleteLeavesNoAccessAndNoLivePolicy: a user holds policies on an org, a disabled project, a disabled group, a project in another org, and an order inside the disabled project. After DeleteUser, no live policy names the user (checked per resource in the database and with ListPolicies), the user can get none of them, and ListOrganizationUsers names the user in neither users nor role_pairs. Without the policy step, the policies on the disabled project, the disabled group, the other org's project and the order stay live.
  • go test ./core/deleter/... passes.
  • go test ./test/e2e/... passes.
  • golangci-lint run ./core/deleter/... ./test/e2e/regression/... reports no issues.

DeleteUser deletes every live policy of the user after the org removals and before the PATs and the user row. Policies on disabled projects and groups, on projects in orgs where the user has no org-level policy, and on resources in disabled projects stayed live before.
@vercel

vercel Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
frontier Ready Ready Preview Oct 25, 2026 6:38am UTC

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 78c1825 and 08ae2bc.

Files selected for processing: 4
  • core/deleter/mocks/membership_service.go
  • core/deleter/service.go
  • core/deleter/service_test.go
  • test/e2e/regression/api_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Deleting a user now also removes their remaining access policies, including policies for resources outside their organization memberships.
    • If organization removal or policy cleanup fails, deletion stops before removing the user’s personal access tokens or account.

User deletion now removes the user’s principal policies after organization membership removal and before personal access token cleanup. Unit and end-to-end tests cover the deletion order, failure handling, remaining policies, and resource access.

Changes: User deletion policy cleanup

Layer / File(s) Summary
Policy cleanup contract and mock
core/deleter/service.go, core/deleter/mocks/membership_service.go
MembershipService adds RemovePrincipalPolicies. Its mock adds the method and typed expectation helpers.
Deletion flow and unit coverage
core/deleter/service.go, core/deleter/service_test.go
DeleteUser removes principal policies after organization membership removal and before personal access token cleanup. Unit tests cover this order and assert that errors stop later deletion steps.
End-to-end deletion regression
test/e2e/regression/api_test.go
The regression test verifies that user deletion removes live policies and resource access, and removes the user from organization users and role pairs.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: whoabhisheksah

Merge Risk: Minimal · up to 08ae2

User deletion is set up to remove remaining live policies before deleting the user. No actionable merge-blocking risk remains after normal checks.

Pre-merge checks: Passed 2
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 40169788070

Coverage increased (+0.01%) to 55.739%

Details

  • Coverage increased (+0.01%) from the base build.
  • Patch coverage: 3 of 3 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 41929
Covered Lines: 23371
Line Coverage: 55.74%
Coverage Strength: 19.39 hits per line

💛 - Coveralls

@AmanGIT07 AmanGIT07 mentioned this pull request Oct 25, 2026
10 tasks done

This branch was successfully deployed

1 active deployment
Preview — 08ae2bc7 Deployed Oct 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants