Files
PacketStation/frontend/src/auth/roles.ts
T
spencermandClaude Fable 5.1 97b0973130 Phase 1: identity, sessions, audit, frontend shell
AD sign-in over LDAPS (search with the service account, bind as the user,
nested groups through LDAP_MATCHING_RULE_IN_CHAIN, disabled accounts refused),
roles as the union of PS-* groups mapped by a table derived from the seeded
departments. Server-side sessions with idle, lock and absolute timeouts, Origin
and X-Requested-With check, step-up unlock. Optional Argon2id PIN set with the
domain password; PIN sign-in by employee ID on registered devices only; five
wrong attempts disable the PIN, three wrong at unlock or signature end the
session. Rate limits per account and per address before any bind. Devices,
signatures table and sign helper, audit rows for every identity event, SSE over
LISTEN/NOTIFY with presence. Account settings and Admin APIs and screens
(AD view, devices, PIN clearing, group map with missing-group flags, refresh
from AD now). CLI ad-groups --list and ad-refresh. Migration 0002.

Frontend: real sign-in (password tab, PIN keypad on registered devices), lock
screen, role switcher, Account settings, Admin shell, screen placeholders.

CI: Samba AD DC container (PSTEST.LOCAL) with a generated CA, 49 of the 50
groups plus one extra, 17 test users; wired into the CI compose overlay and
the Gitea workflow; Playwright sign-in scenario.

Verified on docker-1: 171 server tests green inside the CI stack against
PostgreSQL and the Samba DC, 48 frontend tests, 8 Playwright scenarios against
the live stack (password and PIN sign-in, lock and unlock, Admin group map).
Findings recorded in the docs: the DC needs CAP_SYS_ADMIN, a hidden tab locks
only after a minute as the old build did,
and the app uses the alias ps-app for Playwright because "app" is on
Chromium's HSTS preload list.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-16 07:40:29 -06:00

138 lines
4.9 KiB
TypeScript

// Role strings, the static role-to-screen map and the labels.
// The server decides access on every request. This file only shapes the screens.
import type { Department } from "./types";
/** Screens in the order the role switcher shows them (the old build's order). */
export const SCREENS = ["clerical", "warehouse", "supervisor", "stall", "qc", "pm", "admin"] as const;
export type Screen = (typeof SCREENS)[number];
export const SCREEN_LABELS: Record<Screen, string> = {
clerical: "Clerical",
warehouse: "Warehouse",
supervisor: "Supervisor",
stall: "Weld Stall",
qc: "Q.C.",
pm: "Project Mgmt",
admin: "Admin",
};
/** The phase in which each placeholder screen gets its real content. */
export const SCREEN_PHASE: Record<Exclude<Screen, "admin">, number> = {
clerical: 2,
warehouse: 4,
supervisor: 5,
stall: 5,
qc: 6,
pm: 8,
};
export function isScreen(s: string | undefined): s is Screen {
return s !== undefined && (SCREENS as readonly string[]).includes(s);
}
export type ShopLevel = "worker" | "lead" | "supervisor";
export type ParsedRole =
| { shop: false; kind: string }
| { shop: true; dept: string; level: ShopLevel };
const SHOP_LEVELS: readonly ShopLevel[] = ["worker", "lead", "supervisor"];
/** "shop:03:supervisor" -> shop role; anything else is an office role kind. */
export function parseRole(role: string): ParsedRole {
const m = /^shop:([^:]+):(worker|lead|supervisor)$/.exec(role);
if (m && m[1] !== undefined && m[2] !== undefined) {
return { shop: true, dept: m[1], level: m[2] as ShopLevel };
}
return { shop: false, kind: role };
}
/** Turns a role kind and department ("shop_lead", "03") into the role string ("shop:03:lead"). */
export function roleFromKind(kind: string, dept: string | null): string {
if (kind.includes(":")) return kind;
const m = /^shop_(worker|lead|supervisor)$/.exec(kind);
if (m && m[1] && dept) return `shop:${dept}:${m[1]}`;
return kind;
}
/** The static map. Same rule as the server's screens_for(roles). */
export function screensFor(roles: readonly string[]): Screen[] {
const out = new Set<Screen>();
for (const r of roles) {
const p = parseRole(r);
if (p.shop) {
out.add("stall");
if (p.level !== "worker") out.add("supervisor");
continue;
}
if (p.kind === "clerical" || p.kind === "clerical_supervisor") out.add("clerical");
else if (p.kind.startsWith("warehouse")) out.add("warehouse");
else if (p.kind.startsWith("qc")) out.add("qc");
else if (p.kind === "pm") out.add("pm");
else if (p.kind === "admin") SCREENS.forEach((s) => out.add(s));
}
return SCREENS.filter((s) => out.has(s));
}
/** Known screens from a server list, in switcher order. */
export function orderScreens(list: readonly string[]): Screen[] {
return SCREENS.filter((s) => list.includes(s));
}
/** The screen a user lands on: the first non-admin screen, else admin, else null. */
export function firstScreen(list: readonly string[]): Screen | null {
const ordered = orderScreens(list);
return ordered.find((s) => s !== "admin") ?? ordered[0] ?? null;
}
export function homePath(list: readonly string[]): string {
const s = firstScreen(list);
return s ? `/${s}` : "/account";
}
const OFFICE_LABELS: Record<string, string> = {
admin: "Admin",
clerical: "Clerical",
clerical_supervisor: "Clerical supervisor",
warehouse: "Warehouse",
warehouse_supervisor: "Warehouse supervisor",
qc: "Q.C.",
qc_supervisor: "Q.C. supervisor",
pm: "Project Management",
};
const LEVEL_LABELS: Record<ShopLevel, string> = { worker: "Worker", lead: "Lead", supervisor: "Supervisor" };
/** "shop:03:supervisor" -> "03 Weld Parts · Supervisor". Office roles get their plain name. */
export function roleLabel(role: string, departments: readonly Department[] = []): string {
const p = parseRole(role);
if (!p.shop) return OFFICE_LABELS[p.kind] ?? p.kind;
const dept = departments.find((d) => d.code === p.dept);
const where = dept ? `${p.dept} ${dept.name}` : p.dept;
return `${where} · ${LEVEL_LABELS[p.level]}`;
}
/** The .rchip variant for a role: supervisors pink, leads green, admin amber. */
export function roleChipClass(role: string): string {
const p = parseRole(role);
if (p.shop) return p.level === "worker" ? "rchip" : `rchip ${p.level}`;
if (p.kind === "admin") return "rchip admin";
if (p.kind.endsWith("_supervisor")) return "rchip supervisor";
return "rchip";
}
/** Office roles first in a fixed order, then shop roles by department and level. */
export function sortRoles(roles: readonly string[]): string[] {
const office = Object.keys(OFFICE_LABELS);
const rank = (r: string): [number, string, number] => {
const p = parseRole(r);
if (!p.shop) return [0, "", office.includes(p.kind) ? office.indexOf(p.kind) : office.length];
return [1, p.dept, SHOP_LEVELS.indexOf(p.level)];
};
return [...roles].sort((a, b) => {
const [a0, a1, a2] = rank(a);
const [b0, b1, b2] = rank(b);
return a0 - b0 || a1.localeCompare(b1) || a2 - b2;
});
}