feat(staff): roles, users and role assignment API (#133 slice A) #135

Merged
amiryahaya merged 16 commits from feat/133-staff-access-api into main 2026-10-30 13:45:59 +01:00
Owner

What

Slice A of #133: the admin API for roles, users and role assignment (ADR 0029). Platform scope only; permissions admin_portal.administration.manage.

  • GET /v1/admin/permissions; roles CRUD /v1/admin/roles[/{id}] (caller must hold every permission they grant; create is idempotent; version on update/delete).
  • GET /v1/admin/users[/{id}], PUT /v1/admin/users/{id}/roles (admin_portal only; API-key service users excluded; last Super Admin guard under one advisory lock → 409 last_super_admin).
  • POST /v1/admin/users/{id}/deactivate|reactivate (idempotent). Migration 0033: users.deactivated_at/by; audit of users excludes email, display name and last login.
  • A deactivated user gets a fixed 403 on every authenticating route (not 401: the web client would loop).
    Not in this PR: staff invitations by email (slice B) and the admin pages (#134).

Reviews

Per-task reviews, then a whole-branch dual pass (Pass 1 ponytail, Pass 2 correctness) and a re-check of the fix wave. Pass 2 found two Important issues, fixed with failing tests first: a deactivated actor could reactivate themselves inside the cache window, and an org invitation from a deactivated inviter could still be accepted (also: last-administrator count now skips deactivated users). Ponytail Must fix list: clear.

Checks

  • make cover: 91.5% total (staff 99.1, staff/pgstore 88.5, orgs 93.3, orgs/pgstore 89.9, rbac/pgstore 85.1); go test ./..., make contract, make lint (0 issues) pass; scripts/generate-clients.sh ran (TS + Dart generate and check).

Rulings

  • Deactivated users: 403, not 401. Public routes (no authentication) still serve their token like an anonymous visitor.
  • Deactivating/reactivating a Super Admin needs a Super Admin caller; a deactivated actor cannot act at all.
  • Deactivate/reactivate carry no version (users.version changes on every sign-in); they are idempotent.

Known limits (ADR 0029)

  • Grants cache 30 s and provisioner cache 5 min on other nodes; orgs.Inviter/orgs.Members still use the plain role check inside that window.
  • Lock wait past the statement timeout gives 500, not 503; user detail returns any non-service user (customers too); deactivated tokens spend no rate budget on classed routes (#62); deactivation does not revoke API keys the user created; a role editor can narrow a custom role held by Super Admins (no escalation).
  • Not verified: real IdP, two real nodes, web e2e (no web changes).

🤖 Generated with Claude Code

## What Slice A of #133: the admin API for roles, users and role assignment (ADR 0029). Platform scope only; permissions `admin_portal.administration.manage`. - `GET /v1/admin/permissions`; roles CRUD `/v1/admin/roles[/{id}]` (caller must hold every permission they grant; create is idempotent; version on update/delete). - `GET /v1/admin/users[/{id}]`, `PUT /v1/admin/users/{id}/roles` (admin_portal only; API-key service users excluded; last Super Admin guard under one advisory lock → 409 `last_super_admin`). - `POST /v1/admin/users/{id}/deactivate|reactivate` (idempotent). Migration 0033: `users.deactivated_at/by`; audit of `users` excludes email, display name and last login. - A deactivated user gets a fixed 403 on every authenticating route (not 401: the web client would loop). Not in this PR: staff invitations by email (slice B) and the admin pages (#134). ## Reviews Per-task reviews, then a whole-branch dual pass (Pass 1 ponytail, Pass 2 correctness) and a re-check of the fix wave. Pass 2 found two Important issues, fixed with failing tests first: a deactivated actor could reactivate themselves inside the cache window, and an org invitation from a deactivated inviter could still be accepted (also: last-administrator count now skips deactivated users). Ponytail Must fix list: clear. ## Checks - `make cover`: **91.5%** total (staff 99.1, staff/pgstore 88.5, orgs 93.3, orgs/pgstore 89.9, rbac/pgstore 85.1); `go test ./...`, `make contract`, `make lint` (0 issues) pass; `scripts/generate-clients.sh` ran (TS + Dart generate and check). ## Rulings - Deactivated users: 403, not 401. Public routes (no authentication) still serve their token like an anonymous visitor. - Deactivating/reactivating a Super Admin needs a Super Admin caller; a deactivated actor cannot act at all. - Deactivate/reactivate carry no `version` (`users.version` changes on every sign-in); they are idempotent. ## Known limits (ADR 0029) - Grants cache 30 s and provisioner cache 5 min on other nodes; `orgs.Inviter`/`orgs.Members` still use the plain role check inside that window. - Lock wait past the statement timeout gives 500, not 503; user detail returns any non-service user (customers too); deactivated tokens spend no rate budget on classed routes (#62); deactivation does not revoke API keys the user created; a role editor can narrow a custom role held by Super Admins (no escalation). - Not verified: real IdP, two real nodes, web e2e (no web changes). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
GET /v1/admin/users (staff list: search, status, keyset paging), GET /v1/admin/users/{id}
(platform memberships with roles) and PUT /v1/admin/users/{id}/roles (replace one
application's platform roles; empty removes the membership and re-defaults another).
The caller must hold every permission of an added role, only a Super Admin touches the
Super Admin role, and rbac/pgstore.LockSuperAdmins (the one advisory lock, now also used
by GrantFirstSuperAdmin) serialises assignments so the last active Super Admin stays (409
last_super_admin). PUT is in the sensitive rate-limit class.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST /v1/admin/users/{id}/deactivate|reactivate (platform scope,
administration.manage, sensitive rate class): idempotent, no version,
no write on a repeat. Deactivation takes the Super Admin lock first and
refuses (409 last_super_admin) to leave no active Super Admin, including
a self-deactivation. Upsert reports Deactivated; the Authenticator
answers 403 for a deactivated user's valid token without spending the
failure budget; Grants is empty for them. The handler evicts the login
from the Provisioner and invalidates the authorizer after commit.
Roles are kept, so reactivation restores access.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Deactivate and Reactivate take the Super Admin lock, then refuse
(ErrForbidden, 403) to switch a user who holds Super Admin unless the
actor holds it too, before the no-op check, as SetRoles does for the
role. Reactivate now takes the actor. Comments note the two accepted
minors (provisioner re-cache race, no rate budget for deactivated tokens).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A deactivated user whose cached user and grants still pass on another node
could reactivate themselves, deactivate others or assign roles. Deactivate,
Reactivate and SetRoles now check the actor is active after taking the lock.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Accept checks the inviter with rbac/pgstore.HasActiveRoleTx (HasRoleTx is
unchanged), and CountAdministrators skips deactivated users so the last active
Organization Administrator cannot leave.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
rbac/pgstore.RemoveMembershipTx replaces the two copies in staff and orgs;
holdsAll answers through the Responder's ErrForbidden mapping; SetRoles
returns on the first write error.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs: deactivated inviter wording, active administrators comment (#133)
All checks were successful
ci / changes (pull_request) Successful in 13s
ci / clients (pull_request) Successful in 2m4s
ci / web (pull_request) Successful in 5m46s
ci / api (pull_request) Successful in 13m16s
ci / ci-gate (pull_request) Successful in 2s
33566139e2
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!135
No description provided.