Files
HealthLog/prisma/migrations/0296_account_grant_scope_and_manage/migration.sql
T
Marc-André Bombeck da3cabb55f Give a grant a section set and a third level
A grant has meant one thing since it shipped: the whole record, to read or to
add to. This lays the words for the two shapes the rest of the sharing work
needs — part of a record, and management of one — while changing what no
existing grant does.

`ShareDomain` is a closed set of eight sections in `src/lib/sharing/scope.ts`,
clustered from the delegable read surfaces rather than invented, plus the
distinguished `record` for the routes that read across them. It never converts
to a `ModuleKey` and never imports one: two masks, two questions, no shared
spelling, because the day they share a name is the day somebody makes one
imply the other, and a module the owner enabled is not a section the owner
shared.

`account_grants.scope_json` is NULL on every row that exists, and NULL is the
entire record — the answer those rows were consented as, so no backfill and no
re-consent. A non-NULL value is read through one fail-closed normaliser: a
blob it cannot parse opens nothing, including a section the blob happened to
name. That is the deliberate inverse of the module preference normaliser,
which fails open on purpose; both do the safe thing with a value nobody can
read, and the safe thing differs because one answers an authorization question
and the other a presentation question.

`MANAGE` joins the level enum, ordered above WRITE so a write grant can never
satisfy it, and carries no scope at all: management of part of a record
promises a boundary that cannot survive an edit, so the invite path refuses to
write one.

`requireRecordAuth` takes the section as a second required argument. The
requirement is the lever — a delegable route without a classification does not
typecheck, so the set of classified routes cannot fall behind the set of
delegable ones. All fifty-seven modules carry theirs. The structural guard
freezes each against the production vocabulary, fails when a module's own arms
disagree, and pins a non-zero count of resolved sections so a matcher that
stopped reading the argument fails instead of agreeing with everything.

`requireGuardianAuth` is exported and reaches nothing yet. It gates on the
managed-profile marker rather than on the grant, so an adult who granted
management of their own record cannot arrive at that record's settings by any
argument about what MANAGE means: widening MANAGE would widen the record
resolver and would not touch this one. Its frozen list lands empty, with the
declaration site as the positive control that keeps "nothing reaches it" a
measurement rather than a renamed symbol.

`users.managed_profile_at` says since when a record is one somebody else runs.
A timestamp and not a boolean, for a reason the schema has to keep possible:
clearing it is the handover, and the grants and the activity trail span it
untouched.
2026-08-04 17:11:40 +02:00

41 lines
2.3 KiB
SQL

-- v1.37.0 — a grant can open part of a record, and a grant can hand it over.
--
-- Two additive changes, no backfill, no re-consent, and no existing row
-- changes meaning:
--
-- * `scope_json` is NULL on every row that exists, and NULL means the entire
-- record. That is not a default standing in for a missing answer — it is
-- the answer every one of those rows was actually consented as, before
-- narrowing was a thing anybody could ask for. A non-NULL value is a
-- non-empty array of section keys from the closed vocabulary in
-- `src/lib/sharing/scope.ts`.
-- * `MANAGE` joins the level enum. No row becomes MANAGE here; the value
-- exists so a row can state the level instead of implying it, the same
-- reason `account_grant_access` was a type rather than a boolean from day
-- one (migration 0292).
--
-- Json rather than a text[] or a join table. A join table would be the
-- normalised answer and it would be the wrong one: the set is read on every
-- delegated request as part of deciding an authorization, so it belongs on the
-- row the resolver already loads, and it is written exactly once, at
-- invitation, by a machine — never edited, never queried across grants. There
-- is no in-place widening anywhere in this feature, so the set has no update
-- path to keep consistent. A text[] would carry the same shape with a
-- Postgres-specific type Prisma models less directly, for no read this
-- product performs.
--
-- No CHECK constraint asserting the array is non-empty, well-formed, or absent
-- on a MANAGE row. Those three conditions are enforced in
-- `src/lib/sharing/grants.ts`, at the one function that writes the column, and
-- the reason is the same one migrations 0290 and 0292 gave for declining their
-- own CHECKs: the delete-all-data guard seeds a row into every table by
-- walking `information_schema`, and a constraint it cannot satisfy breaks a
-- fixture whose whole value is that nobody hand-writes it. The READ side is
-- what actually protects the record anyway — `normaliseScope` resolves a
-- malformed blob to the empty set and refuses every section, so a bad value
-- that reached the column by any route grants nothing rather than everything.
ALTER TYPE "account_grant_access" ADD VALUE 'MANAGE';
ALTER TABLE "account_grants" ADD COLUMN "scope_json" JSONB;