Admin API: roles, users and role assignment (#44 follow-up) #133

Closed
opened 2026-10-30 08:14:21 +01:00 by amiryahaya · 1 comment
Owner

Follow-up to #44. The admin portal can review organizations (PR #131) but there is no API to manage roles, users or role assignment: roles are built-in data (rbac.SystemRoles(), re-applied by KWS_MODE=migrate) and memberships are only created by the organization flows, the bootstrap admin (KWS_BOOTSTRAP_ADMIN_EMAIL) and API keys. Staff cannot be given or lose a role without SQL.

Scope

Admin routes under /v1/admin, platform scope only (rbac.PlatformOnly), permission admin_portal.administration.manage ([verify] whether a separate admin_portal.users.manage key is wanted):

  • GET /v1/admin/permissions: the permission catalog (key, application, description) so a role editor can list choices.
  • Roles: list, get, create, update (name, description, permissions, version), delete (custom roles only; system roles read-only; a role in use is a 409). Tables exist: roles, role_permissions.
  • Users: list (search by email/name, keyset paging) and get (display name, email, verified flag, memberships with roles); deactivate/reactivate; invitations (create, list, delete) for staff.
  • Role assignment: set the roles of a user's platform membership (create the membership when needed), remove it. Tables: memberships, membership_roles.

Rules to carry over

  • The caller must hold every permission of any role they grant or put in a custom role (same rule as API keys, ADR 0027); no one can grant Super Admin unless they are Super Admin.
  • Never remove the last Super Admin (409 last_administrator-style, locked like members in ADR 0021).
  • Audit through postgres.InTx and audit_attach on the touched tables; edit conflicts by version (409); Idempotency-Key on create; call Authorizer.Invalidate after a change (other nodes catch up within the 30 s TTL).
  • RFC 7807 fixed texts; never log emails; rate-limit class in ratelimit.Patterns and TooManyRequests in the OpenAPI operations.
  • OpenAPI operations with operationId/tag/security/statuses and live contract test calls (make contract); TDD; make cover >= 80%; regenerate clients.

Decisions (owner, 2026-10-30)

  • Deactivate or remove: only an admin (admin_portal.administration.manage) can deactivate a user or remove their membership. Deactivating is a real state on the user (a deactivated user is refused at sign-in/authorizer with the same 401/403 as no access); removing the platform membership drops their roles. Decided: a real state on the user (new migration: users.deactivated_at timestamptz + deactivated_by uuid REFERENCES users(id), audited, never blocks reading history). A deactivated user is refused by the authenticator/authorizer like a user with no access (same fixed 401/403), loses nothing else (roles stay, so reactivating restores access), and cannot be the last active Super Admin (409). Deactivate/reactivate are admin-only routes with version.
  • Scope: this API manages platform-scope roles and memberships only. Organization-scoped roles and members stay with the partner portal (#49; orgs.Inviter/orgs.Members already exist).
  • Staff are invited by email only: no "sign in first, then get a role" path. Staff are created through an invitation (reuse the invitation machinery from ADR 0021: 32 random bytes, only the hash stored, emailed by the worker, token never in a response/log/path/query, accept by a signed-in user with a verified email equal to the invited one). Needs a platform-scope variant (no organization), admin_portal.administration.manage to invite, Idempotency-Key on create, expiry job, and the same "inviter must still hold the permission at accept time" rule. Deleting an unaccepted invitation is allowed.

Blocks the web pages in the issue "Admin: roles, users and role assignment pages".

Follow-up to #44. The admin portal can review organizations (PR #131) but there is **no API to manage roles, users or role assignment**: roles are built-in data (`rbac.SystemRoles()`, re-applied by `KWS_MODE=migrate`) and memberships are only created by the organization flows, the bootstrap admin (`KWS_BOOTSTRAP_ADMIN_EMAIL`) and API keys. Staff cannot be given or lose a role without SQL. ## Scope Admin routes under `/v1/admin`, platform scope only (`rbac.PlatformOnly`), permission `admin_portal.administration.manage` (**[verify]** whether a separate `admin_portal.users.manage` key is wanted): - `GET /v1/admin/permissions`: the permission catalog (key, application, description) so a role editor can list choices. - Roles: list, get, create, update (name, description, permissions, `version`), delete (custom roles only; system roles read-only; a role in use is a 409). Tables exist: `roles`, `role_permissions`. - Users: list (search by email/name, keyset paging) and get (display name, email, verified flag, memberships with roles); deactivate/reactivate; invitations (create, list, delete) for staff. - Role assignment: set the roles of a user's platform membership (create the membership when needed), remove it. Tables: `memberships`, `membership_roles`. ## Rules to carry over - The caller must hold every permission of any role they grant or put in a custom role (same rule as API keys, ADR 0027); no one can grant Super Admin unless they are Super Admin. - Never remove the last Super Admin (409 `last_administrator`-style, locked like members in ADR 0021). - Audit through `postgres.InTx` and `audit_attach` on the touched tables; edit conflicts by `version` (409); `Idempotency-Key` on create; call `Authorizer.Invalidate` after a change (other nodes catch up within the 30 s TTL). - RFC 7807 fixed texts; never log emails; rate-limit class in `ratelimit.Patterns` and `TooManyRequests` in the OpenAPI operations. - OpenAPI operations with `operationId`/tag/security/statuses and live contract test calls (`make contract`); TDD; `make cover` >= 80%; regenerate clients. ## Decisions (owner, 2026-10-30) - **Deactivate or remove:** only an admin (`admin_portal.administration.manage`) can deactivate a user or remove their membership. Deactivating is a real state on the user (a deactivated user is refused at sign-in/authorizer with the same 401/403 as no access); removing the platform membership drops their roles. **Decided: a real state on the user** (new migration: `users.deactivated_at timestamptz` + `deactivated_by uuid REFERENCES users(id)`, audited, never blocks reading history). A deactivated user is refused by the authenticator/authorizer like a user with no access (same fixed 401/403), loses nothing else (roles stay, so reactivating restores access), and cannot be the last active Super Admin (409). Deactivate/reactivate are admin-only routes with `version`. - **Scope:** this API manages **platform-scope** roles and memberships only. Organization-scoped roles and members stay with the partner portal (#49; `orgs.Inviter`/`orgs.Members` already exist). - **Staff are invited by email only:** no "sign in first, then get a role" path. Staff are created through an invitation (reuse the invitation machinery from ADR 0021: 32 random bytes, only the hash stored, emailed by the worker, token never in a response/log/path/query, accept by a signed-in user with a verified email equal to the invited one). Needs a platform-scope variant (no organization), `admin_portal.administration.manage` to invite, `Idempotency-Key` on create, expiry job, and the same "inviter must still hold the permission at accept time" rule. Deleting an unaccepted invitation is allowed. Blocks the web pages in the issue "Admin: roles, users and role assignment pages".
amiryahaya added this to the P4 Web.Admin milestone 2026-10-30 08:14:21 +01:00
Author
Owner

Status 2026-10-31: slice A (PR #135) and slice B staff invitations (PR #136) are merged. Against a real local Zitadel the first admin flow found that Zitadel JWT access tokens carry no email, so bootstrap and invitation matching never fired; fixed in PR #139 (email read from the userinfo endpoint). Customers who sign in to the admin portal now see a no-access page (PR #140). Still untried against a real IdP: the staff invitation email and accept, and the prompt=create sign-up (needs a local mail catcher). Left open: the owner decides when to close. Deployment checklist (not yet a PR) is on branch docs/deployment-checklist.

Status 2026-10-31: slice A (PR #135) and slice B staff invitations (PR #136) are merged. Against a real local Zitadel the first admin flow found that Zitadel JWT access tokens carry no email, so bootstrap and invitation matching never fired; fixed in PR #139 (email read from the userinfo endpoint). Customers who sign in to the admin portal now see a no-access page (PR #140). Still untried against a real IdP: the staff invitation email and accept, and the prompt=create sign-up (needs a local mail catcher). Left open: the owner decides when to close. Deployment checklist (not yet a PR) is on branch docs/deployment-checklist.
Sign in to join this conversation.
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#133
No description provided.