Skip to content

Make Resource.delete() check user_can_delete_resource - #13036

Closed
illtellyouwhat wants to merge 2 commits into
archesproject:dev/8.1.xfrom
OpenTechStrategies:13035_scope_resource_delete_to_resource
Closed

illtellyouwhat wants to merge 2 commits into
archesproject:dev/8.1.xfrom
OpenTechStrategies:13035_scope_resource_delete_to_resource

Conversation

@illtellyouwhat

@illtellyouwhat illtellyouwhat commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Description of Change

A Resource Reviewer can delete any resource, in any lifecycle state, through Resource.delete(). The reviewer branch grants deletion without asking the permission framework about the resource. It now calls user_can_delete_resource, so per-resource permissions and the lifecycle state's can_delete_resource_instances flag apply.

The single-resource view already makes this check. Bulk Data Deletion does not, and has no permission check of its own, so Resource.delete() is its only gate.

The reasoning, and two alternatives considered but not chosen, are in the comment at the changed line.

Compatibility. If a site sets neither per-resource nor per-card permissions, the framework has no per-resource answer and falls back to card permissions. With none set, every card is open, so the reviewer passes. The per-resource part changes nothing on those sites.

The lifecycle part is the intended change. A reviewer without can_delete_all_resource_instance_lifecycle_states can no longer delete a resource whose lifecycle state forbids deletion. The default lifecycle marks Active and Retired that way.

Issues Solved

Closes #13035

Checklist

  • I targeted one of these branches:
    • dev/8.2.x (under development): features, bugfixes not covered below
    • dev/8.1.x (main support): regressions, crashing bugs, security issues, major bugs in new features
    • dev/7.6.x (extended support): major security issues, data loss issues
  • I added a changelog in arches/releases
  • I submitted a PR to arches-docs (if appropriate) — n/a, no user-facing change
  • Unit tests pass locally with my changes
  • I added tests that prove my fix is effective or that my feature works
  • My test fails on the target branch

Accessibility Checklist

No user-facing change, so no accessibility impact.

Ticket Background

Further comments

Found while building an instance-scoped permission framework, where delete was the one grant that could not be expressed. The framework already inherited user_can_delete_resource, but nothing called it on this path.

Targeting dev/8.1.x because this is a permission fix rather than a feature, and dev/8.1.x is contained in dev/8.2.x, so it reaches development on your next merge-up. Happy to rebase onto dev/8.2.x if you would rather.

Changelog. 8.1.5 is released, and releases/8.1.6.md does not exist yet. Should I create it with an entry for
#13036, or add the entry once you start that file?

Separate defect, not fixed here. If the user and their groups hold no per-resource permissions in Django Guardian, user_can_delete_resource looks for protected tiles with TileModel.objects.filter(resourceinstance_id=resourceid) (arches_permission_base.py:433). If the caller passes the resource object instead of its ID, resourceid is None. The query then finds no tiles, and the check passes even if protected tiles exist. Resource.delete() passes the object, so this PR adds the lifecycle and per-resource checks for reviewers, but not the per-card check. Two callers in views/api/resource.py also pass the object, so the defect predates this PR. The fix is probably result["resource"].pk, but it changes behavior for those API callers, so it needs its own issue.

Test evidence

Commit 1 adds two tests. Commit 2 applies the fix.

test_reviewer_cannot_delete_when_lifecycle_state_forbids_deletion fails on dev/8.1.x and passes with the fix. test_reviewer_can_still_delete_when_lifecycle_state_allows_deletion passes on both, which shows that reviewers still delete where the lifecycle state allows it.

At commit 1, a0eeeddd25:

$ python manage.py test tests.models.resource_test --settings="tests.test_settings"
FAIL: test_reviewer_cannot_delete_when_lifecycle_state_forbids_deletion
AssertionError: False is not true : the resource row was deleted even though deletion should be refused
Ran 23 tests in 9.661s
FAILED (failures=1)

At commit 2, 47b7e55b7f:

$ python manage.py test tests.models.resource_test --settings="tests.test_settings"
Ran 23 tests in 9.512s
OK

@CLAassistant

CLAassistant commented Oct 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

archesproject#13035

AI assistance: Claude Code (Claude Opus 5.5) wrote the tests. I read
them, approved them and ran them. Claude drafted the docstrings, and I
reworded them.
archesproject#13035

AI assistance: Claude Code (Claude Opus 5.5) helped trace the
permission call paths and proposed several fixes. I chose the final
fix. Claude drafted the code comment, and I reworded it. I read each
function myself and wrote or approved every line.
@illtellyouwhat
illtellyouwhat force-pushed the 13035_scope_resource_delete_to_resource branch from 39ddd2c to 47b7e55 Compare October 20, 2026 04:09
@chiatt
chiatt self-requested a review October 22, 2026 18:00

@chiatt chiatt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @illtellyouwhat, this is a nice catch. Could you rebase on dev/8.1.x? Once you do you'll have the 8.1.6.md file in the releases directory. You can add a changelog entry in there; just follow the pattern in 8.1.5.md

Comment on lines +698 to +721
# Ask the permission framework about this resource, so
# per-resource permissions and lifecycle rules apply to
# reviewers too. user_can_delete_resource refuses deletion
# when the resource's lifecycle state forbids it, unless the
# user holds can_delete_all_resource_instance_lifecycle_states.
#
# When a site sets neither per-resource nor per-card
# permissions, the framework answers "unknown" and falls back
# to a nodegroup check, which the reviewer passes. "unknown" is
# not itself a grant. In that case, a reviewer is refused only
# when the lifecycle state's can_delete_resource_instances flag
# forbids deletion.
#
# Alternative considered but not chosen: checking whether
# "delete_resourceinstance" is in get_perms(user, resource). On
# a site that sets no per-resource permissions, get_perms
# always returns an empty list, so the check always returns
# False. Every reviewer is refused, even though the
# default-allow framework treats "no permissions set" as
# allowed. user_can_delete_resource keeps that rule.
#
# Alternative considered but not chosen: adding a resource
# argument to user_is_resource_reviewer. It has 19 call sites
# in core, and 8 of them have no resource to pass.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this comment? The explanation of the reasoning is good in the PR and the code is otherwise clear.

@aarongundel aarongundel mentioned this pull request Oct 28, 2026
1 of 11 tasks
@chiatt

chiatt commented Oct 30, 2026

Copy link
Copy Markdown
Member

I wanted to make sure this got into the 8.1.6 release, so we cherrypicked this commit into this PR: #13079

@chiatt chiatt closed this Oct 30, 2026
@github-project-automation github-project-automation Bot moved this to ✅ Done in pipeline Oct 30, 2026
@illtellyouwhat

Copy link
Copy Markdown
Contributor Author

@chiatt , Sorry I missed this earlier, I've been traveling overseas and was unable to check here. Thank-you for moving it along to 8.1.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants