feat(permissions): per-desk till guards, jobs in the role composer, permission-scoped live feed; role reassignment applies without re-login
Permissions matrix rethink (wiki/decisions/venue-modules.md §"Permissions matrix", open-questions #16) — the grid stays the enforcement layer: - Move 1: each desk's money is guarded by that desk's own permissions. Manifest tillGuards {read, shift, cash}: booth = shift:read / shift:create / drawer:create (unchanged), carwash = carwash:read / carwash:cash (new). Shift + drawer routes resolve the guard FROM THE TILL (requireTill); a wash role holds no shift:* and cannot touch the booth by construction. Replaces the session:read borrowing (tillPermission). /api/shift/tills lists the role's readable tills with canWork; history/movements without a till filter return the union of readable tills. - Move 2: jobs — manifest permission bundles (booth-operator, booth-supervisor, merchant, wash-operator) as one-click chips in Setup → Roles, with "mixes desks" and "partial job" lints (warnings, never blocks). - Move 3: the live WebSocket admits any watch permission (event/session/device read or a module's feedPermission) and filters every push per role; report:read is the reports screen only. Auth: the token's roleId is only a hint — refreshRole() after every jwtVerify resolves the user's CURRENT role (cached, bumped on role/user writes), so reassigning a user's role applies on the next request and a deleted user's session ends with 401. Tests: till guards + look-only role, feed rules, every job's permissions exist, role reassignment without re-login. 353/353. Claude-Session: https://claude.ai/code/session_01FWncR69HgGPuei1dLrW3cU
This commit is contained in:
@@ -5,12 +5,21 @@ import {
|
||||
isModuleId,
|
||||
isTillId,
|
||||
parseEntitledModules,
|
||||
tillGuards,
|
||||
tillsFor,
|
||||
tillsOf,
|
||||
type ModuleId,
|
||||
type TillGuards,
|
||||
type TillId,
|
||||
} from "@parking/shared";
|
||||
import { roleHasPermissions } from "./auth.js";
|
||||
import { requireAuth, roleHasPermissions } from "./auth.js";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyRequest {
|
||||
/** Set by requireTill(): the till this request addresses (already authorized). */
|
||||
till?: TillId;
|
||||
}
|
||||
}
|
||||
|
||||
// Venue modules — the server side of "entitled ∩ activated" (registry + rules live in
|
||||
// @parking/shared; design in wiki/decisions/venue-modules.md).
|
||||
@@ -57,10 +66,36 @@ export function effectiveTillsFor(db: Db): TillId[] {
|
||||
return tillsOf(effectiveModulesFor(db));
|
||||
}
|
||||
|
||||
/** The tills a role may WORK here (open/close its shift, move its cash): effective
|
||||
* tills whose module permission the role holds. */
|
||||
export function accessibleTillsFor(db: Db, roleId: string): TillId[] {
|
||||
return tillsFor(effectiveModulesFor(db), (p) => roleHasPermissions(roleId, [p]));
|
||||
/** The tills a role may SEE (default) or WORK (`shift` / `cash`) here: the effective
|
||||
* tills whose module guard the role holds (each desk's money is guarded by that desk's
|
||||
* own permissions — venue-modules.md §"Permissions matrix"). */
|
||||
export function tillsReadableBy(db: Db, roleId: string, kind: keyof TillGuards = "read"): TillId[] {
|
||||
return tillsFor(effectiveModulesFor(db), (p) => roleHasPermissions(roleId, [p]), kind);
|
||||
}
|
||||
|
||||
/** preHandler factory for the shift/drawer routes: authenticate, parse the `till`
|
||||
* (query on GET, body on POST; absent = booth; 400 `bad_till` when unknown or its
|
||||
* module is off), then require the role to hold THAT TILL's guard for `kind` (403
|
||||
* `till_forbidden`). The authorized till lands on `req.till`. The permission is thus
|
||||
* resolved from the till, never fixed: the booth checks `shift:read`/`shift:create`/
|
||||
* `drawer:create`, the wash `carwash:read`/`carwash:cash`. */
|
||||
export function requireTill(db: Db, kind: keyof TillGuards, from: "query" | "body") {
|
||||
return async (req: FastifyRequest, reply: FastifyReply): Promise<void | FastifyReply> => {
|
||||
await requireAuth(req, reply);
|
||||
const raw = from === "query" ? (req.query as { till?: unknown } | undefined)?.till : (req.body as { till?: unknown } | undefined)?.till;
|
||||
const till = parseTill(db, raw);
|
||||
if (!till) {
|
||||
await reply.code(400).send({ error: "unknown till", code: "bad_till" });
|
||||
return reply;
|
||||
}
|
||||
if (!roleHasPermissions(req.user.roleId, [tillGuards(till)[kind]])) {
|
||||
await reply
|
||||
.code(403)
|
||||
.send({ error: `your role cannot ${kind === "read" ? "see" : "work"} the ${till} till`, code: "till_forbidden", till });
|
||||
return reply;
|
||||
}
|
||||
req.till = till;
|
||||
};
|
||||
}
|
||||
|
||||
/** Parse a till from a query/body value. Absent/blank = the booth. Unknown, or a till
|
||||
|
||||
Reference in New Issue
Block a user