Repository navigation
fix: delete invitations with a user's roles - #355
Conversation
Revoking a user's roles on a service also deletes the invitations to it that the user created and nobody accepted, also those that do not depend on their role, so that their links stop working. Accepted ones hold another user's acceptance and stay until they expire. Closes #354 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Log each deleted invitation, correct what the docs say about downgrades and admins, and test creators by admin and group. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideSuccessful service-role revocation now removes all unaccepted invitations created by the revoked user on that service—including admin- and group-created invitations—while preserving accepted invitations and logging each deletion; tests and documentation define the behavior and edge cases. Sequence diagram for invitation cleanup after role revocationsequenceDiagram
actor Admin
participant API as permissions API
participant Roles as _delete_roles
participant Invitations as invitations collection
participant Logger as logger
Admin->>API: deleteServicePermission
API->>Roles: _delete_roles
alt role revocation succeeds
Roles->>Invitations: update accepted invitations
Roles->>Invitations: _delete_created_invitations
Invitations-->>Roles: unaccepted invitations created by user
loop each invitation
Roles->>Invitations: delete_one
Invitations-->>Roles: deleted_count
alt invitation deleted
Roles->>Logger: logger.info
end
end
else last owner or user has no role
API-->>Admin: 409 or 404
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #355 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 2572 2579 +7
=========================================
+ Hits 2572 2579 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="cloud_registry/ga4gh/registry/permissions.py" line_range="593" />
<code_context>
+ query = {
+ "serviceId": serviceId,
+ "createdBy": user,
+ "accepted": {"$exists": False},
+ }
+ for invitation in list(collection.find(query, {"_id": True})):
</code_context>
<issue_to_address>
**Revoked invitation becomes usable again**
When an invitation is claimed before cleanup, its role grant fails, and `acceptInvitation` resets the claim afterward, `_delete_created_invitations` skips the claimed invitation, then `acceptInvitation` unsets `accepted` after `put_role` fails, leaving an admin- or still-owner-group-created link usable after revocation.
Ensure a failed acceptance cannot restore an invitation that revocation has already skipped.
Also at `cloud_registry/ga4gh/registry/permissions.py:596`.
</issue_to_address>
### Comment 2
<location path="cloud_registry/ga4gh/registry/permissions.py" line_range="595" />
<code_context>
+ "createdBy": user,
+ "accepted": {"$exists": False},
+ }
+ for invitation in list(collection.find(query, {"_id": True})):
+ if collection.delete_one({**query, "_id": invitation["_id"]}).deleted_count:
+ logger.info(
</code_context>
<issue_to_address>
**New invitations survive revocation**
When an invitation is inserted after the cleanup scan and its creator still qualifies as an admin or through an owner group, `_delete_created_invitations` misses the new invitation, while `postServiceInvitation`'s final `_created_as` check still passes, leaving the link usable after revocation.
Coordinate invitation creation with revocation so an invitation cannot be inserted after cleanup and pass its final authorization check.
Also at `cloud_registry/ga4gh/registry/permissions.py:652`.
</issue_to_address>
### Comment 3
<location path="cloud_registry/ga4gh/registry/permissions.py" line_range="652" />
<code_context>
{"serviceId": serviceId, "accepted.userId": subject},
{"$unset": {"accepted.name": "", "accepted.email": "", "label": ""}},
)
+ _delete_created_invitations(serviceId, subject)
</code_context>
<issue_to_address>
**Failed cleanup cannot be retried**
When a database operation after `revoke_roles` succeeds fails before invitation cleanup completes, `_forget_names`, the accepted-invitation update, or `_delete_created_invitations` propagates the error after the role is removed; a retry returns `404` before cleanup, leaving invitations and possibly creator metadata behind, with links still usable.
Make invitation cleanup complete or remain retryable when a database operation fails after role revocation.
</issue_to_address>
### Comment 4
<location path="cloud_registry/ga4gh/registry/permissions.py" line_range="590-595" />
<code_context>
+ user: User identifier.
+ """
+ collection = get_collection(INVITATIONS)
+ query = {
+ "serviceId": serviceId,
+ "createdBy": user,
+ "accepted": {"$exists": False},
+ }
+ for invitation in list(collection.find(query, {"_id": True})):
+ if collection.delete_one({**query, "_id": invitation["_id"]}).deleted_count:
+ logger.info(
</code_context>
<issue_to_address>
**Revocations scan invitation history**
When the invitations collection or a service's retained invitation history is large, `collection.find` must scan invitations to match `createdBy`, then materializes matching IDs before deleting them individually, making role revocations increasingly expensive and prone to timing out or failing partway through cleanup.
Add an index that supports the cleanup query on `serviceId`, `createdBy`, and acceptance status.
</issue_to_address>Linked issues
#354 Clear invitations a user created when revoking · 5 of 5 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Delete invitations on the service that the user created and nobody accepted. | ✅ Met | The cleanup filters by service, creator, and absence of an acceptance; it covers open and expired invitations, including those created as an admin or through a group. |
| 2 | Keep accepted invitations until they expire. | ✅ Met | Accepted invitations are excluded from the deletion query; the existing cleanup removes acceptance details without deleting the invitation. |
| 3 | Only clean up after successful revocation; 409 and 404 clear nothing. |
✅ Met | The invitation cleanup runs only after revoke_roles succeeds and after the no-role check; the tests cover last-owner 409 and no-role 404. |
| 4 | Add tests for the cleanup behavior. | ✅ Met | Tests cover accepted, open, expired, cross-service, admin-created, group-created, unsuccessful-revocation, and concurrent-acceptance cases. |
| 5 | Document the behavior and the retained creator fields in docs/access-control.md and docs/upgrading.md. |
✅ Met | Both documents describe which invitations are removed or retained and name createdBy, createdAs, and createdAt. |
Sourcery assessment
Needs a human reviewer. 4 findings to address first, and revoking a user's roles now permanently deletes every unaccepted invitation they created for that service, including invitations created as an admin or through a group that would otherwise remain valid. If that deletion policy is wrong, the invitation records and working links are not restored by reverting; affected invitations must be created again.
Blocking findings: cloud_registry/ga4gh/registry/permissions.py:593, cloud_registry/ga4gh/registry/permissions.py:595, cloud_registry/ga4gh/registry/permissions.py:652, cloud_registry/ga4gh/registry/permissions.py:595
Summary
Revoking a user's roles on a service now also deletes the invitations to that service that the user created and nobody accepted, open or expired, also those created as an admin or through a group that stays owner, which stayed valid until now. So a removed user's open links stop working, and their identifier and times go with them. Accepted invitations stay until they expire, as they hold an acceptance.
The cleanup runs only after the revocation succeeded:
409for the last owner and404for a user without a role delete nothing. Each deleted invitation is logged.The docs now say which invitations go and which stay, and name what they keep (
createdBy,createdAs,createdAt), indocs/access-control.mdanddocs/upgrading.md, as the pre-release review asked (finding L4). Alsodocs/operations.md, the OpenAPI description and the release notes.Closes #354
How it was verified
tests/test_invitations.pyfailed before the change. File result now: 107 passed, 4 skipped.pre-commit run --all-files(without gitleaks locally),mypy cloud_registry tests,mkdocs build --strict.Declined review findings
404): needs a database failure between two writes; the request fails with an error, and the docs say how to find such invitations bycreatedBy.Known limit
404), so their invitations on such services stay;docs/access-control.mdkeeps the advice to delete them bycreatedBy.🤖 Generated with Claude Code
Summary by Sourcery
Remove unaccepted service invitations created by users whose roles are successfully revoked, while preserving accepted invitations and documenting the behavior.
Bug Fixes:
Enhancements:
Documentation:
Tests: