Skip to content

refactor: consolidate RBAC into dojo/authorization package#21

Closed
Maffooch wants to merge 1 commit intodevfrom
perm-cleanup
Closed

refactor: consolidate RBAC into dojo/authorization package#21
Maffooch wants to merge 1 commit intodevfrom
perm-cleanup

Conversation

@Maffooch
Copy link
Copy Markdown
Owner

@Maffooch Maffooch commented Mar 31, 2026

Description

Consolidate every RBAC / authorization concern into a single dojo/authorization/ package. Before this PR, authorization code lived in seven different places: dojo/models.py (RBAC models), 14 per-app queries.py files (get_authorized_*), every view file (@user_is_authorized decorators), dojo/api_v2/permissions.py, dojo/location/api/permissions.py, and dojo/templatetags/authorization_tags.py. After this PR, it all lives in dojo/authorization/.

Net diff: 85 files changed, +3015 / −2113.

What moves into dojo/authorization/

  • models.py — 7 RBAC models (Role, Global_Role, Dojo_Group_Member, Product_Member, Product_Group, Product_Type_Member, Product_Type_Group) extracted from dojo/models.py. app_label='dojo' is preserved so no migrations are needed; ~47 import sites are updated.
  • api_permissions.pydojo/api_v2/permissions.py and dojo/location/api/permissions.py merged into one module; originals deleted; consumers and tests rewritten.
  • template_filters.py — filter functions extracted from dojo/templatetags/authorization_tags.py. The templatetags module becomes a thin registration proxy.
  • query_filters.py + query_registrations.py — registry pattern (~1.9k lines of RBAC filter logic) extracted from 14 per-app queries.py files. Each get_authorized_* becomes a thin wrapper that defers to the registry and falls back to unfiltered querysets when no RBAC backend is registered.
  • url_permissions.py + middleware.py — map ~198 URL names to permission checks and enforce them via AuthorizationMiddleware.process_view. Removes @user_is_authorized, @user_has_global_permission, and @user_is_configuration_authorized from 26 view files.
  • __init__.py — exports the public surface and triggers query-filter registration at app startup.

Why

  • Single source of truth for "is this allowed?" — checks live next to the models, registry, and middleware that enforce them.
  • No more per-view decorators — one URL-permission map replaces ~200 decorator lines scattered across 26 view files.
  • Decouples dojo.models from authorization — RBAC models live in their own module, removing circular-import pressure.
  • Pluggableget_authorized_* falls back to unfiltered querysets when no RBAC backend is registered, so non-RBAC deployments keep working.

Behavioral changes

None intended. Authorization checks for the ~198 mapped URLs now run in middleware (process_view) instead of inside view bodies, but produce the same allow/deny outcome.

Test results

  • Full Django unit-test suite (Rest Framework × 4 platform combos) — green.
  • K8s deployment smoke (kubernetes 1.33.11, 1.35.4) — green.
  • Full Selenium UI matrix — green.
  • New unittests/test_permissions_audit.py exercises the URL-permission map for completeness.

Documentation

No external documentation impact — this is a structural / internal change.

Checklist

  • Rebased against the latest dev.
  • Submitted against dev.
  • Ruff compliant.
  • Python 3.13 compliant.
  • No model schema changes → no migrations needed (RBAC tables keep app_label='dojo').
  • Tests added / updated.
  • PR label: maintenance.

@Maffooch Maffooch changed the title Perm cleanup refactor: consolidate RBAC into dojo/authorization package Apr 27, 2026
Move every RBAC / authorization concern into a single dojo/authorization/
package. Before this change authorization code lived in seven different
places: dojo/models.py (RBAC models), 14 per-app queries.py files
(get_authorized_*), every view file (@user_is_authorized decorators),
dojo/api_v2/permissions.py, dojo/location/api/permissions.py, and
dojo/templatetags/authorization_tags.py.

Changes
- Move 7 RBAC models (Role, Global_Role, Dojo_Group_Member,
  Product_Member, Product_Group, Product_Type_Member,
  Product_Type_Group) from dojo/models.py to
  dojo/authorization/models.py. app_label='dojo' is preserved so no
  migrations are needed; ~47 import sites are updated.
- Merge dojo/api_v2/permissions.py and
  dojo/location/api/permissions.py into
  dojo/authorization/api_permissions.py.
- Extract template-tag logic from
  dojo/templatetags/authorization_tags.py into
  dojo/authorization/template_filters.py; the templatetags module
  becomes a thin registration proxy.
- Add dojo/authorization/query_filters.py (registry) and
  dojo/authorization/query_registrations.py (~1.9k lines of RBAC
  filter logic extracted from 14 per-app queries.py files). Each
  get_authorized_* becomes a thin wrapper that defers to the registry
  and falls back to unfiltered querysets when no RBAC backend is
  registered.
- Add dojo/authorization/url_permissions.py mapping ~198 URL names to
  permission checks plus dojo/authorization/middleware.py with
  AuthorizationMiddleware enforcing them via process_view. Removes
  @user_is_authorized, @user_has_global_permission, and
  @user_is_configuration_authorized from 26 view files.
- Update dojo/authorization/__init__.py exports and trigger
  query-filter registration at app startup.

Behavior is unchanged: authorization checks for the ~198 mapped URLs
now run in middleware (process_view) instead of view bodies, but
produce the same allow/deny outcome. Non-RBAC deployments keep working
because get_authorized_* falls back to unfiltered querysets.

Tests: unittests/test_permissions_audit.py exercises the URL-permission
map for completeness; existing API/UI suites pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant