Skip to content

feat(perm): manage.py explain_permission — grant-model authority debugger - #2454

Open
vecchp wants to merge 2 commits into
test/perm-org-mutation-gatingfrom
feat/perm-explain-permission
Open

vecchp wants to merge 2 commits into
test/perm-org-mutation-gatingfrom
feat/perm-explain-permission

Conversation

@vecchp

@vecchp vecchp commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

What

The DX gap flagged in the grant-model audit: nothing answered "why can/can't user X do P at org O / on object R?" — you had to read predicate code and run ad-hoc shell queries. This adds manage.py explain_permission plus its pure core common.permissions.explain.explain().

Design — the verdict can never disagree with enforcement

The explanation re-asks the canonical predicate for the mode — can() at an org, can_obj() on a row, can_anywhere() with no scope — then walks the arms behind it:

arm what it shows
global tier superuser / global Role names / user_permissions
direct grant role(s) + org; "held roles at org that don't carry the perm" when close-but-denied
delegated permission-matched inheritance, and the "delegation exists but the user doesn't act at the delegator" near-miss
object grant staged rows; not wired until the clients cutover
legacy rows PermissionGroup membership with the domain's live/inert posture (LEGACY_INERT_APPS)

The legacy arm doubles as a cutover-progress view: for notes/clients it shows the arm that actually authorizes today and the grant-model verdict (what enforcement becomes after cutover); for cut-over domains it proves leftover rows are inert.

user        Jane Doe <jane@example.org> (pk=42)
permission  shelters.change_shelter  [grant-only domain — legacy rows inert]
org         7 "Acme Housing"
verdict     ALLOWED  (org predicate)
arms
  global tier   no   superuser: no; global roles: none; user_permissions: no
  direct grant  YES  role(s) Shelter Operator at org 7
  delegated     no   no org-principal delegations scope this org for this permission
  object grant  no   no object grants — the arm is not wired yet (clients cutover)
  legacy rows   no   no PermissionGroup rows carry this permission — inert for this domain

Exits 0 on ALLOWED, 1 on DENIED — script-friendly.

Verified

  • 13 new tests, all asserting the load-bearing property verdict == can/can_obj/can_anywhere per scenario, plus arm attribution (direct, delegated matched/unmatched, global role, superuser, legacy live vs inert, ORG vs SHARED write tier, command output + exit code).
  • common/tests full suite: 245 passed; ruff check/format clean; mypy clean on the new files (only the pre-existing accounts/managers.py no-any-return pair remains, untouched).
  • Docs: apps/betterangels-backend/docs/permissions.md — new Debugging authority section + Key Files rows.

Stacked on #2453 (the ungated-mutation tripwire).

Summary by Sourcery

Add an authority debugger that explains canonical permission decisions and identifies the grant or legacy sources behind them.

New Features:

  • Add a pure permission-explanation API and manage.py explain_permission command for diagnosing a user's authority at an organization, on an object, or globally.
  • Report authorization sources across global, direct, delegated, object, and legacy permission arms, including legacy live/inert status and near-miss details.

Enhancements:

  • Ensure explanations always derive their verdict from the canonical permission predicates and provide script-friendly allowed/denied exit codes.

Documentation:

  • Document the authority debugger, usage examples, and new permission explanation files.

Tests:

  • Add coverage for predicate parity, authority attribution, delegation behavior, legacy-domain posture, object write tiers, validation, and command output/exit codes.

…debugger

Nothing answered 'why can/can't user X do P at org O / on object R?'.
The new command re-asks the canonical predicate for the mode (can at an
org, can_obj on a row, can_anywhere with no scope) so the verdict can
never disagree with enforcement, then walks the arms behind it: global
tier (superuser / global Role / user_permissions), direct grant,
delegated grant (permission-matched), object grant (not wired until the
clients cutover), and legacy PermissionGroup rows with the domain's
live/inert posture — the last one doubles as a cutover-progress view:
notes/clients still authorize through it, and the verdict shown is what
enforcement becomes after cutover.

Exits 0 on ALLOWED, 1 on DENIED — script-friendly.

Tests pin the load-bearing property (verdict == can/can_obj/can_anywhere
in every scenario) plus arm attribution per arm.  Docs: permissions.md
'Debugging authority' + Key Files.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry @vecchp, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 days by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduces a canonical-predicate-backed permission authority debugger as both a reusable explanation API and manage.py command, with arm-level diagnostics for global, direct, delegated, object, and legacy authorization paths, comprehensive parity-focused tests, and documentation.

Sequence diagram for permission authority explanation

sequenceDiagram
    actor Operator
    participant Command as explain_permission
    participant Explain as permissions.explain.explain
    participant Predicate as Canonical selectors
    participant Database as Authorization data

    Operator->>Command: provide user, perm, org or object
    Command->>Explain: explain(user, perm, org, obj)
    Explain->>Predicate: can(), can_obj(), or can_anywhere()
    Predicate->>Database: evaluate effective authority
    Database-->>Predicate: verdict
    Explain->>Database: inspect global, direct, delegated, object, legacy arms
    Database-->>Explain: arm diagnostics and cutover posture
    Explain-->>Command: Explanation
    Command-->>Operator: ALLOWED or DENIED, arms, notes
    alt verdict is denied
        Command-->>Operator: exit code 1
    else verdict is allowed
        Command-->>Operator: exit code 0
    end
Loading

File-Level Changes

Change Details Files
Add a pure permission-explanation service that derives its verdict from the canonical authorization selectors and reports each authorization arm plus cutover context.
  • Validate permission and scope inputs and select org, object, or anywhere predicate modes.
  • Re-query canonical can(), can_obj(), or can_anywhere() for the verdict.
  • Inspect global, direct, delegated, object, and legacy PermissionGroup authority arms.
  • Report delegation near-misses, held-but-insufficient roles, object-arm wiring status, write tiers, and legacy live/inert posture.
apps/betterangels-backend/common/permissions/explain.py
Expose the explanation service through a script-friendly Django management command.
  • Resolve users by primary key, username, or case-insensitive email; organizations by primary key or slug; and objects by model label and primary key.
  • Render the verdict, scope, domain posture, arms, and notes in a human-readable format.
  • Reject conflicting scopes and malformed or missing references with CommandError.
  • Exit with status 0 for allowed results and status 1 for denied results.
apps/betterangels-backend/common/management/commands/explain_permission.py
Add coverage for verdict parity, arm attribution, cutover behavior, scope tiers, validation, and command behavior.
  • Assert explanation verdicts match can(), can_obj(), and can_anywhere() across grant, global, delegated, legacy, and object scenarios.
  • Cover delegated permission matching and the missing-delegator-authority near-miss.
  • Verify legacy rows are reported as live for legacy domains and inert for cut-over domains.
  • Verify command output and denied exit status.
apps/betterangels-backend/common/tests/test_explain_permission.py
Document the new authority debugger and its implementation entry points.
  • Add usage examples and explain the canonical-verdict and exit-code guarantees.
  • Add the explanation module and management command to the permissions key-files table.
apps/betterangels-backend/docs/permissions.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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