pike/glyph Public Public: anyone can read this; nobody can change it.
forked from apps/glyph
workbenchthis machine
Files e45e4989a79ef3f801c453528dd5b471f91bea4f
| 1 | /** |
| 2 | * Who may do what. `contract.json` is the table — the roles, and the least |
| 3 | * role each verb on each resource needs — and `src/can.ts` is the forge's |
| 4 | * evaluator, carried here verbatim. Actions ask `permit` and nothing else |
| 5 | * in the app decides who may do what, so the table *is* the policy: it can |
| 6 | * be read, compared and audited without running anything. |
| 7 | * |
| 8 | * A contract that does not read is not a warning. The app does not start. |
| 9 | */ |
| 10 | |
| 11 | import { readFileSync } from "node:fs"; |
| 12 | import { |
| 13 | decide, |
| 14 | readContract, |
| 15 | type Actor, |
| 16 | type Decision, |
| 17 | type Verb, |
| 18 | } from "./can.ts"; |
| 19 | import type { Who } from "./who.ts"; |
| 20 | |
| 21 | const read = readContract( |
| 22 | readFileSync(new URL("../contract.json", import.meta.url), "utf8"), |
| 23 | ); |
| 24 | if (!read.ok) throw new Error(`contract.json: ${read.problems.join("; ")}`); |
| 25 | |
| 26 | export const contract = read.contract; |
| 27 | |
| 28 | /** The forge's word for who is asking, as the evaluator takes it. */ |
| 29 | export const actorOf = (who: Who): Actor => |
| 30 | who.role === "anon" |
| 31 | ? { id: null, roles: [] } |
| 32 | : { id: who.id, roles: [who.role] }; |
| 33 | |
| 34 | /** May `who` do `verb` to `resource` — to `row`, when there is one? */ |
| 35 | export const permit = ( |
| 36 | who: Who, |
| 37 | resource: string, |
| 38 | verb: Verb, |
| 39 | row?: Readonly<Record<string, unknown>>, |
| 40 | ): Decision => decide(contract, resource, verb, actorOf(who), row); |