feat(auth): dynamic RBAC — composable roles + resource×CRUD permissions
Replace the hardcoded role enum (admin/operator/cashier/readonly, checked
literally as requireRole("admin",...) across ~15 routes) with dynamic RBAC:
roles are DATA, route guards check a PERMISSION.
@parking/shared defines a code-defined grid: RESOURCES (user/role/tariff/
subscription/site/device/shift/payment/session/event/report) × Action
(create/read/update/delete + domain verbs void/cash) -> PERMISSIONS
(resource:action, e.g. tariff:update, payment:create, event:void).
DB: new roles + role_permissions tables; users.role enum -> role_id FK;
migration 0007_rbac (create tables, seed the builtin admin role + all 26
perms, seed operator/cashier/readonly composable roles matching old
behaviour, rebuild users to swap the column copying all rows).
auth.ts: JWT payload role -> roleId; permissionsFor(roleId) with an
in-memory cache + bumpPermsCache(); requirePermission(...perms) preHandler;
requireAuth for /me & /language; initAuth(db) wires the resolver once. Every
route guard mapped to a permission; device ingress (devices/qr-reader) stays
auth-free by design. New routes/users.ts (user:* CRUD, bcrypt 12, last-admin
guard) + routes/roles.ts (role:* CRUD, builtin-protected, perms validated
against the grid, cache bump on write). auth/me + /login return
{roleId, roleName, permissions, language}. seed-admin -> roleId:'admin'.
Frontend: SessionUser carries permissions + can() helper; router nav/route
guards gate by permission (requirePerm replaces adminOnly); SiteSettings
edit gated by site:update; new UsersManager + RolesManager (permission
checkbox grid; admin role locked); i18n nav.users/roles + blocks (sq+en).
Decisions: one role per user; protected built-in admin (no-lockout: the last
admin can't be deleted/downgraded); JWT carries roleId, perms resolved
per-request so role edits apply immediately (no re-login).
Verified: full build green; 20-assertion inject test passes (cashier 403s on
tariff publish + user list, admin passes, granting a perm applies on the next
request, last-admin + builtin-role protections return 409); migration 0007
applied to a copy of the live DB (incl WAL/shm) — existing admin maps to
role_id='admin', all rows preserved. Append-only event chain untouched
(event:void gates appending a void, not a delete).
Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
@@ -5,7 +5,61 @@
|
||||
// and exit events are never edited or deleted — a "void" is itself an appended
|
||||
// event. See wiki/concepts/append-only-event-chain.md.
|
||||
|
||||
export type Role = "admin" | "operator" | "cashier" | "readonly";
|
||||
// --- Authorization: dynamic RBAC (resource × CRUD permissions) ---------------
|
||||
// Roles are DATA (admin-composable rows in the DB), not a hardcoded enum. A role
|
||||
// is a named bundle of PERMISSIONS; a permission is a `resource:action` pair drawn
|
||||
// from the code-defined grid below. Route guards check a permission, never a role
|
||||
// name. A built-in, locked `admin` role (id ADMIN_ROLE_ID) always holds every
|
||||
// permission, so administration can never be locked out. See
|
||||
// wiki/entities/local-jwt-auth.md and the RBAC plan.
|
||||
|
||||
/** The resources permissions are scoped to (code-defined; roles/assignments are data). */
|
||||
export const RESOURCES = [
|
||||
"user", // manage operators/cashiers + reset password
|
||||
"role", // compose roles + assign permissions
|
||||
"tariff", // read / publish a new version
|
||||
"subscription", // the subscription registry
|
||||
"site", // site_config + device setup/assign
|
||||
"device", // device status / printers / snapshots / catalog
|
||||
"shift", // open/close own shift; move the drawer float
|
||||
"payment", // take payment, quote, voucher/receipt, exit, reopen
|
||||
"session", // active sessions, lookup
|
||||
"event", // the signed ledger feed + void
|
||||
"report", // events feed, occupancy, future reports
|
||||
] as const;
|
||||
export type Resource = (typeof RESOURCES)[number];
|
||||
|
||||
/** CRUD plus two domain verbs where CRUD doesn't fit: `void` (append a void event,
|
||||
* NOT a delete) and `cash` (move the drawer float — an admin-grade shift action). */
|
||||
export type Action = "create" | "read" | "update" | "delete" | "void" | "cash";
|
||||
|
||||
/** A single permission, e.g. "tariff:update". The route guard checks one of these. */
|
||||
export type Permission = `${Resource}:${Action}`;
|
||||
|
||||
/** The complete, code-defined permission grid. Only these strings are checkable by
|
||||
* a guard — an admin composes roles by selecting from this set. Preserves today's
|
||||
* exact authz semantics (e.g. void split from read; shift cash split from open). */
|
||||
export const PERMISSIONS: readonly Permission[] = [
|
||||
"user:create", "user:read", "user:update", "user:delete",
|
||||
"role:create", "role:read", "role:update", "role:delete",
|
||||
"tariff:read", "tariff:update",
|
||||
"subscription:read", "subscription:create", "subscription:update", "subscription:delete",
|
||||
"site:read", "site:update",
|
||||
"device:read",
|
||||
"shift:read", "shift:create", "shift:cash",
|
||||
"payment:read", "payment:create",
|
||||
"session:read",
|
||||
"event:read", "event:void",
|
||||
"report:read",
|
||||
] as const;
|
||||
|
||||
/** The protected built-in role: non-deletable, non-editable, always = ALL
|
||||
* permissions. At least one user must always hold it (no-lockout invariant). */
|
||||
export const ADMIN_ROLE_ID = "admin";
|
||||
|
||||
/** Transitional alias. Roles are now DB rows keyed by a string id; `Role` is kept
|
||||
* as `string` so any not-yet-migrated reference still compiles. */
|
||||
export type Role = string;
|
||||
|
||||
export type Direction = "entry" | "exit";
|
||||
|
||||
@@ -637,13 +691,6 @@ function compareCard(
|
||||
return 0;
|
||||
}
|
||||
|
||||
export const ROLES: readonly Role[] = [
|
||||
"admin",
|
||||
"operator",
|
||||
"cashier",
|
||||
"readonly",
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Signs the canonical bytes of an event for the append-only chain. This is the
|
||||
* abstraction over the [[atecc608]] secure element: the real, non-extractable
|
||||
|
||||
Reference in New Issue
Block a user