feat(api): RBAC - permission catalog, system roles, scoped grants, first-admin bootstrap #74

Merged
amiryahaya merged 1 commit from feat/rbac into main 2026-10-22 21:15:05 +02:00
Owner

Summary

Implements #12 - roles and permissions (RBAC), ported from what the legacy code actually did.

What the legacy system really did (read from the code, not assumed):

  • Admin portal: access was four coarse ASP.NET policies built from role names (RequireBackendRole, RequireAdministratorRole, RequireFinanceRole, RequireReportingRole). The 19 fine-grained admin permissions existed but were never enforced.
  • Operator portal: real permission checks (HasPermissions) with roles stored as data, created per organization and assigned to an organization membership.
  • Approving an organization hard-coded role id 2 for its first user.

What this PR does

  • Permission catalog (56): 52 legacy permissions + 4 new gates that replace the policies (admin_portal.access|administration.manage|finance.manage|reports.view). String keys replace numeric ids; the legacy id is kept for audit.
  • System roles: the 11 AdminRoles reproduce the four legacy policies exactly (a test pins the role lists), plus Organization Administrator, Usher, Checker.
  • Roles are data (roles, role_permissions, memberships, membership_roles); grants are scoped - platform scope applies everywhere, organization scope only to its own organization (HasInOrganization).
  • Authorizer with a per-user 30 s cache + Invalidate; Guard.Require(permission): 401 anonymous, 403 without (never names the permission), 500 on failure (nothing leaked).
  • KWS_MODE=migrate also refreshes the system roles. New endpoints GET /v1/me/access, GET /v1/permissions (needs administration.manage).
  • First administrator: KWS_BOOTSTRAP_ADMIN_EMAIL. The first user to sign in with that verified email becomes Super Admin, once, serialised by a DB advisory lock.

Decisions to review ([verify] with the business)

  1. Customer permissions: legacy enum names were shifted vs their UI label (...CustomerUpdate was labelled "Create New Customer"). I ported by the label admins saw: view/create/update/delete (no "export"). Legacy controllers only ever checked view.
  2. Non-Super admin roles hold only their policy gates, not the fine-grained admin permissions (which legacy never enforced). Super Admin holds everything.
  3. Warden / Rangers exist but hold nothing (they were in no policy).
  4. Organization Administrator (all business permissions) is new - the legacy role id 2 was not seeded in the code I could see.
  5. organization_id has no FK yet; #37 (organizations) adds it.

Test plan

  • Tests first. Catalog integrity, the four-policy contract, scope rules, store (real DB), concurrency (8 users race for first admin -> exactly 1 wins), authorizer cache/TTL/invalidate, guard 401/403/500
  • Full stack through the running server: stranger 403, unverified bootstrap email 403, real first admin 200, a second owner of the same address 403
  • Coverage 91.4% (gate 80%); lint 0 issues; govulncheck clean
  • CI green on Forgejo

Closes #12

🤖 Generated with Claude Code

## Summary Implements #12 - roles and permissions (RBAC), ported from what the legacy code actually did. **What the legacy system really did** (read from the code, not assumed): - Admin portal: access was **four coarse ASP.NET policies built from role names** (`RequireBackendRole`, `RequireAdministratorRole`, `RequireFinanceRole`, `RequireReportingRole`). The 19 fine-grained admin permissions existed but were never enforced. - Operator portal: real permission checks (`HasPermissions`) with **roles stored as data**, created per organization and assigned to an *organization membership*. - Approving an organization hard-coded role id 2 for its first user. **What this PR does** - Permission **catalog** (56): 52 legacy permissions + 4 new gates that replace the policies (`admin_portal.access|administration.manage|finance.manage|reports.view`). String keys replace numeric ids; the legacy id is kept for audit. - **System roles**: the 11 `AdminRole`s reproduce the four legacy policies exactly (a test pins the role lists), plus *Organization Administrator*, *Usher*, *Checker*. - Roles are **data** (`roles`, `role_permissions`, `memberships`, `membership_roles`); grants are **scoped** - platform scope applies everywhere, organization scope only to its own organization (`HasInOrganization`). - `Authorizer` with a per-user 30 s cache + `Invalidate`; `Guard.Require(permission)`: 401 anonymous, 403 without (never names the permission), 500 on failure (nothing leaked). - `KWS_MODE=migrate` also refreshes the system roles. New endpoints `GET /v1/me/access`, `GET /v1/permissions` (needs `administration.manage`). - **First administrator**: `KWS_BOOTSTRAP_ADMIN_EMAIL`. The first user to sign in with that **verified** email becomes Super Admin, once, serialised by a DB advisory lock. ## Decisions to review ([verify] with the business) 1. **Customer permissions**: legacy enum names were shifted vs their UI label (`...CustomerUpdate` was labelled "Create New Customer"). I ported by the label admins saw: view/create/update/delete (no "export"). Legacy controllers only ever checked `view`. 2. **Non-Super admin roles hold only their policy gates**, not the fine-grained admin permissions (which legacy never enforced). Super Admin holds everything. 3. **Warden / Rangers** exist but hold nothing (they were in no policy). 4. **Organization Administrator** (all business permissions) is new - the legacy role id 2 was not seeded in the code I could see. 5. `organization_id` has no FK yet; #37 (organizations) adds it. ## Test plan - [x] Tests first. Catalog integrity, the four-policy contract, scope rules, store (real DB), concurrency (8 users race for first admin -> exactly 1 wins), authorizer cache/TTL/invalidate, guard 401/403/500 - [x] Full stack through the running server: stranger 403, **unverified** bootstrap email 403, real first admin 200, a second owner of the same address 403 - [x] Coverage 91.4% (gate 80%); lint 0 issues; govulncheck clean - [ ] CI green on Forgejo Closes #12 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(api): RBAC - permission catalog, system roles, scoped grants, first-admin bootstrap
All checks were successful
ci / changes (pull_request) Successful in 8s
ci / api (pull_request) Successful in 6m42s
ci / ci-gate (pull_request) Successful in 3s
32e4b609a4
- rbac: catalog of 56 permissions (52 legacy ported by their UI label + 4 new gates replacing the legacy ASP.NET policies)
- system roles: 11 AdminRole roles reproduce the four legacy admin policies (pinned by a test), plus Organization Administrator, Usher, Checker
- roles are data: roles, role_permissions, memberships (user x application x optional organization), membership_roles
- grants are scoped: platform scope applies everywhere, organization scope only to that organization (HasInOrganization)
- Authorizer with per-user TTL cache (30s) + Invalidate; Guard.Require(permission): 401 anonymous, 403 without (names nothing), 500 on failure
- first Super Admin: KWS_BOOTSTRAP_ADMIN_EMAIL, VERIFIED email only, once, serialised by an advisory lock (concurrency tested)
- KWS_MODE=migrate now also refreshes system roles; GET /v1/me/access, GET /v1/permissions
- coverage 91.4%

Closes #12

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!74
No description provided.