Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-013 — PermissionAction as a first-class entity with IsConsole/IsSystem

Date: 2026-07-23 Status: Accepted Extends: ADR-004 (role-based permissions), ADR-006 (local Identity). Relates to: the SA-only tenant-administration model (review finding todos/001) and the role-assignment authority guard (todos/002). Reference: ITLab.WorkGuardian (PermissionAction/RolePermission entities, IsConsole), adapted to the canon and extended with IsSystem. Spec / plan: docs/specs/permission-action-entity.md, docs/plans/2026-07-23-feat-permission-action-entity-plano.md.

Context​

A permission used to be only a const string in Permissions, discovered by reflection in PermissionCatalog; RolePermissions stored the raw string with no referential integrity and no metadata. There was no way to express "this permission is privileged and may only be granted to a certain kind of role". The anti-escalation that existed (SetGroupPermissions restricting a caller to permissions they hold) was behavioural — it depended on every code path being correct — not structural.

The team decided to bring the PermissionAction mechanic from ITLab.WorkGuardian, making permission a persisted entity with metadata, and to add IsSystem (new; WorkGuardian only has IsConsole) to govern permission↔role binding.

Decision​

1. PermissionAction is a global reference entity​

Infrastructure/Authentication/PermissionAction.cs — { Id (string PK), IsConsole, IsSystem }, table PermissionActions in the default schema, not IMultiTenant (the catalogue is the same for every tenant). The PK is the permission string itself (Tenants.Create), so it matches the value stored in RolePermissions. There is no Name column — the SPA localizes labels by id (i18n), unlike WorkGuardian.

2. Metadata declared by attributes, discovered by reflection​

[SystemPermission] / [ConsolePermission] annotate the const fields of Permissions; PermissionCatalog.AllWithMetadata() reads them via reflection into PermissionMetadata { Id, IsConsole, IsSystem }. The code catalogue stays the single source of truth. Current mapping: Tenants.* + Tenants.AuthSettings.* = system + console; Users.*/Groups.* = console; Orders.* = neither.

3. RolePermission references PermissionAction by FK​

RolePermission.Permission → PermissionActionId, with an FK to PermissionActions (OnDelete Restrict — avoids the multiple-cascade-paths conflict with the role FK and prevents deleting a permission still bound to a role). Migration AddPermissionActions does it backfill-before-constraint: rename the column, create the table, seed it from DISTINCT RolePermissions.PermissionActionId, then add the FK (valid on in-place upgrades; a no-op on a fresh DB where roles are runtime-provisioned).

4. Idempotent startup seed​

PermissionActionSeeder upserts the catalogue from PermissionCatalog.AllWithMetadata() — adds missing, reconciles flags, never deletes (a row may be FK-referenced). It runs before IdentitySeeder (role provisioning writes RolePermissions).

5. Binding rule — IsSystem permission ↔ IsSystem role only​

GroupManagement.SetPermissionsAsync rejects any requested IsSystem permission on a non-IsSystem group with Permission.SystemOnly (409), independent of and stricter than the caller-scope guard, and before any write.

Resulting invariant (runtime): an IsSystem permission reaches a group only via RoleProvisioner — because system groups are already read-only in SetGroupPermissions and regular groups are blocked by this rule. This makes anti-escalation structural, complementing todos/001 (host-only Tenants.* reserved for the SA) and todos/002 (role-assignment authority guard).

6. IsConsole is metadata only (this phase)​

IsConsole marks the management/console surface; GET /v1/permissions exposes it and the SPA matrix uses it for presentation. No runtime gating and no AppRole.IsConsole yet — deferred.

7. G-01 — this lives in the template, not the DevPack​

The entity, storage, metadata, and binding rule are template-local; no DevPack change, no bilateral release.

Evidence: the DevPack is deliberately source-agnostic about permissions — DevPack ADR-001 (no framework wrappers); IPermissionEvaluator/IRolePermissionResolver are consumer-implemented; permissions are strings defined by the consumer; BaseIdentityRole has no IsSystem/IsConsole; there is no PermissionAction/RolePermission entity in the DevPack. Putting an EF entity + assignment rules there would violate that philosophy. The template consumes the DevPack unchanged (IRolePermissionResolver/RoleBasedPermissionEvaluator still operate on strings + role names). This reverses the spec's tentative §7 lean toward the DevPack. A generic permission-metadata mechanism for other DevPack consumers is possible future bilateral work, explicitly out of scope.

Consequences​

  • Referential integrity: a role can no longer bind a permission that does not exist (FK, in addition to the catalogue validator).
  • Structural anti-escalation via IsSystem — a regular, admin-defined group can never receive a privileged permission, regardless of code path.
  • GET /v1/permissions returns {id, isConsole, isSystem} (breaking change; only the SPA consumes it) — the SPA matrix disables IsSystem permissions for non-system groups and badges them; OpenAPI regen tracked by todos/004.
  • Single-PR delivery in the template; no DevPack version bump.
  • InMemory does not run migrations, so a relational apply is the real gate for the data migration (the emitted T-SQL was inspected: rename → create → backfill → index → FK).

Alternatives considered​

  • Mechanic in the DevPack (spec §7 tentative) — rejected; violates DevPack ADR-001 / source-agnostic philosophy and would force a bilateral release.
  • IsSystem as a seed convention only (no entity) — rejected; no referential integrity, no structural guard (that was the prior state).
  • DB-level enforcement (trigger/constraint) — optional defense-in-depth; the application rule already covers it. Deferred.
  • PK int + code — rejected; string id matches the current value and makes the migration a rename.