feat(roles): roles remember the jobs they follow (re-appliable), every role edit is signed
Closes the permissions-matrix loose ends (venue-modules.md §Permissions matrix): - `role_jobs` (migration 0029): a role stores the manifest jobs it was composed from (chips on at save + any bundle fully present). `jobById` / `jobsBehind` in @parking/shared surface a followed job whose bundle grew past the role in a later release; the roles list shows a "behind <job>" badge with a one-click "Update to job" (the union, nothing removed) and the editor lints it. Never a runtime union: the grid stays the explicit enforcement layer and an update never widens a role without a click. - Every role create/update/delete appends a `config_change` (`role.<id>`, prev/value = name + sorted permissions + jobs, operator); a no-op resave signs nothing. roleRoutes now takes the ledger. - booth-supervisor already carries subscription:*; the stale open note is closed. Tests: routes/roles.test.ts. Wiki: venue-modules status, local-jwt-auth, log. Claude-Session: https://claude.ai/code/session_01FWncR69HgGPuei1dLrW3cU
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
type SessionUser,
|
||||
} from "./api.js";
|
||||
import { Modal } from "./ui/Modal.js";
|
||||
import { MODULES, tillsFor, type JobPreset, type ModuleId, type TillId } from "@parking/shared";
|
||||
import { MODULES, jobsBehind, tillsFor, type JobPreset, type ModuleId, type TillId } from "@parking/shared";
|
||||
|
||||
// Role management (admin). Compose a role from the permission grid (a checkbox
|
||||
// matrix of resource × action) and name it; users are then assigned a role. The
|
||||
@@ -27,6 +27,11 @@ import { MODULES, tillsFor, type JobPreset, type ModuleId, type TillId } from "@
|
||||
// fine-tune + enforcement layer. The editor LINTS the result (warnings, never blocks):
|
||||
// "mixes desks" (may open more than one till) and "partial job" (holds a module's read
|
||||
// permission but not the rest of its job — a desk that can look but not act).
|
||||
//
|
||||
// A role REMEMBERS the jobs it follows (chips on at save, or bundles fully present). When
|
||||
// a later release grows a job, the role shows as "behind" it — in the list (with a
|
||||
// one-click re-apply) and in the editor — instead of silently falling short the way the
|
||||
// wash operator's price list did (2026-09-06). Every save is signed on the ledger.
|
||||
|
||||
/** Group "resource:action" permissions by resource for the grid rows. */
|
||||
function groupByResource(perms: Permission[]): Record<string, Permission[]> {
|
||||
@@ -111,8 +116,17 @@ export function RolesManager({ user }: { user: SessionUser | null }) {
|
||||
<span className="text-[0.6875rem] text-term-muted">
|
||||
{t("roles.permCount", { count: r.permissions.length })} · {t("roles.userCount", { count: r.userCount })}
|
||||
</span>
|
||||
{behindOf(r).map((b) => (
|
||||
<span key={b.job} className="rounded-term border border-term-amber/60 px-1.5 py-0.5 text-[0.625rem] text-term-amber" title={b.missing.join(", ")}>
|
||||
{t("roles.behind", { job: t(`jobs.${b.job}`) })}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{canUpdate && !r.builtin && behindOf(r).length > 0 && (
|
||||
<button type="button" className="btn btn-primary btn-sm" title={behindOf(r).flatMap((b) => b.missing).join(", ")}
|
||||
onClick={() => reapply(r, invalidate, onError)}>{t("roles.reapply")}</button>
|
||||
)}
|
||||
{canUpdate && !r.builtin && (
|
||||
<button type="button" className="btn btn-ghost btn-sm" onClick={() => { setEditing(r); setError(null); }}>{t("roles.edit")}</button>
|
||||
)}
|
||||
@@ -133,15 +147,30 @@ async function deleteRoleSafe(id: string, ok: () => void, onError: (e: unknown)
|
||||
try { await deleteRole(id); ok(); } catch (e) { onError(e); }
|
||||
}
|
||||
|
||||
/** The jobs a role follows that have grown past it (this release's bundles). */
|
||||
function behindOf(r: ManagedRole): { job: string; missing: Permission[] }[] {
|
||||
const has = new Set(r.permissions);
|
||||
return jobsBehind(r.jobs ?? [], (p) => has.has(p));
|
||||
}
|
||||
|
||||
/** Re-apply = add what the followed jobs now carry. Nothing is removed; the save is
|
||||
* signed like any other role edit. */
|
||||
async function reapply(r: ManagedRole, ok: () => void, onError: (e: unknown) => void) {
|
||||
const missing = behindOf(r).flatMap((b) => b.missing);
|
||||
try { await updateRole(r.id, { permissions: [...new Set([...r.permissions, ...missing])] }); ok(); } catch (e) { onError(e); }
|
||||
}
|
||||
|
||||
/** The jobs the composer offers: every effective module's, in registry order. */
|
||||
function jobsFor(effective: readonly ModuleId[]): { module: ModuleId; job: JobPreset }[] {
|
||||
return MODULES.filter((m) => effective.includes(m.id)).flatMap((m) => m.jobs.map((job) => ({ module: m.id, job })));
|
||||
}
|
||||
|
||||
/** Composer lints — warnings about what the admin just composed. */
|
||||
function lintRole(perms: Set<Permission>, effective: readonly ModuleId[]): { key: string; vars?: Record<string, string> }[] {
|
||||
function lintRole(perms: Set<Permission>, jobs: Set<string>, effective: readonly ModuleId[]): { key: string; vars?: Record<string, string> }[] {
|
||||
const has = (p: Permission) => perms.has(p);
|
||||
const out: { key: string; vars?: Record<string, string> }[] = [];
|
||||
// Behind a job it follows: the bundle grew (a newer release) past what the role holds.
|
||||
for (const b of jobsBehind([...jobs], has)) out.push({ key: "roles.lintJobBehind", vars: { job: b.job, missing: b.missing.join(", ") } });
|
||||
// Mixes desks: may OPEN more than one till.
|
||||
const workable: TillId[] = tillsFor(effective, has, "shift");
|
||||
if (workable.length > 1) out.push({ key: "roles.lintMixedTills", vars: { tills: workable.join(", ") } });
|
||||
@@ -165,11 +194,14 @@ function RoleEditor({
|
||||
grouped: Record<string, Permission[]>;
|
||||
effective: readonly ModuleId[];
|
||||
onCancel: () => void;
|
||||
onSubmit: (v: { name: string; permissions: Permission[] }) => void;
|
||||
onSubmit: (v: { name: string; permissions: Permission[]; jobs: string[] }) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [name, setName] = useState(role?.name ?? "");
|
||||
const [perms, setPerms] = useState<Set<Permission>>(new Set(role?.permissions ?? []));
|
||||
// The jobs this role follows: what was remembered, plus (at save) any bundle that is
|
||||
// fully present — so a role composed before jobs were remembered picks them up.
|
||||
const [jobIds, setJobIds] = useState<Set<string>>(new Set(role?.jobs ?? []));
|
||||
const toggle = (p: Permission) =>
|
||||
setPerms((prev) => {
|
||||
const next = new Set(prev);
|
||||
@@ -177,15 +209,24 @@ function RoleEditor({
|
||||
return next;
|
||||
});
|
||||
const jobs = useMemo(() => jobsFor(effective), [effective]);
|
||||
const jobOn = (job: JobPreset) => job.permissions.every((p) => perms.has(p));
|
||||
const toggleJob = (job: JobPreset) =>
|
||||
const complete = (job: JobPreset) => job.permissions.every((p) => perms.has(p));
|
||||
const jobOn = (job: JobPreset) => jobIds.has(job.id) || complete(job);
|
||||
const toggleJob = (job: JobPreset) => {
|
||||
const on = jobOn(job);
|
||||
setJobIds((prev) => {
|
||||
const next = new Set(prev);
|
||||
on ? next.delete(job.id) : next.add(job.id);
|
||||
return next;
|
||||
});
|
||||
setPerms((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (job.permissions.every((p) => prev.has(p))) for (const p of job.permissions) next.delete(p);
|
||||
if (on) for (const p of job.permissions) next.delete(p);
|
||||
else for (const p of job.permissions) next.add(p);
|
||||
return next;
|
||||
});
|
||||
const lints = useMemo(() => lintRole(perms, effective), [perms, effective]);
|
||||
};
|
||||
const lints = useMemo(() => lintRole(perms, jobIds, effective), [perms, jobIds, effective]);
|
||||
const followed = () => jobs.filter(({ job }) => jobIds.has(job.id) || complete(job)).map(({ job }) => job.id);
|
||||
|
||||
const valid = name.trim().length > 0;
|
||||
|
||||
@@ -244,7 +285,7 @@ function RoleEditor({
|
||||
|
||||
<div className="mt-3 flex justify-end gap-2">
|
||||
<button type="button" className="btn btn-sm" onClick={onCancel}>{t("common.cancel")}</button>
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={!valid} onClick={() => onSubmit({ name: name.trim(), permissions: [...perms] })}>{t("common.save")}</button>
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={!valid} onClick={() => onSubmit({ name: name.trim(), permissions: [...perms], jobs: followed() })}>{t("common.save")}</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user