@simetrajs/designer 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,13 @@ Contents:
7
7
  - Users, authorship and membership
8
8
  - Rights declarations
9
9
  - Rights sets and roles
10
+ - Adopted membership and assignment sources
11
+ - Who reads what
12
+ - Who writes what
13
+ - Generated privileges
14
+ - Platform roles
15
+ - Reading past row-level security
16
+ - Carrier copies
10
17
 
11
18
  ## Users, authorship and membership
12
19
 
@@ -31,7 +38,12 @@ People are platform data, not tables you write yourself:
31
38
  the provider's account table.
32
39
  - **Authorship.** `trackAuthor: true` on a Catalog or a Document adds the
33
40
  standard `createdBy` and `updatedBy` references to the users catalog;
34
- without a users catalog it is an error (`users.catalog-missing`).
41
+ without a users catalog it is an error (`users.catalog-missing`). Under a
42
+ command the platform forces `createdBy`/`updatedBy` to the current user;
43
+ `createdBy` never changes; a `beforeWrite` handler may not assign them,
44
+ and a subscription may not list them in `whenChanged`.
45
+ Outside a command (an import, a migration) whoever writes the row sets
46
+ them.
35
47
  - **Membership.** A scoped catalog with `membership: { "user": "<attribute>" }`
36
48
  is the membership of its scope kind: the attribute is a scalar `Ref` to the
37
49
  users catalog, nullable for an invited member without an account. The
@@ -40,11 +52,26 @@ People are platform data, not tables you write yourself:
40
52
  its scope set function from that catalog. Use it instead of a hand-written
41
53
  member table and set function. One membership catalog per scope kind.
42
54
 
55
+ The platform layer (identities, the current user, provisioning) lives in the
56
+ PG schema `simetra`, generated when a users catalog exists, or when the
57
+ project has a membership source and its provider's user id is the `sub` claim
58
+ (then only the current-user function, no identities table). The schema is
59
+ reserved: no object, `defaultSchema`, set function, external reference,
60
+ event handler or SQL unit may use it, and there is no `metadata/sql/simetra/`
61
+ folder (`schema.reserved`). In SQL, the
62
+ current user is `(select simetra.current_user_id())`, written exactly so in a
63
+ policy (`sql.bare-current-user` rejects a bare call there); it is `NULL` for
64
+ an anonymous session or an anonymous sign-in of the provider, a service
65
+ session and an invalid user.
66
+
43
67
  ## Rights declarations
44
68
 
45
69
  A **carrier** is an attribute of the object, or a two-step path
46
70
  `["<ref attribute>", "<attribute of its target>"]`; every step stays inside
47
- the tenant (`rights.carrier-cross-tenant`).
71
+ the tenant (`rights.carrier-cross-tenant`), and the first step points to an
72
+ object of a kind, not to an adopted `CustomTable`
73
+ (`rights.carrier-path-adopted`: the platform keeps a copy of the path's value
74
+ with a trigger on the target table, see "Carrier copies").
48
75
 
49
76
  - **`customActions`** — actions beyond the standard ones of the kind; each
50
77
  gets an id, `requires` names standard action words or other custom actions
@@ -52,9 +79,15 @@ the tenant (`rights.carrier-cross-tenant`).
52
79
  object's attributes, tabular sections and custom actions.
53
80
  `{ "name": "<action>", "requires": ["update"], "effects": { "header": ["<attribute>"] } }`
54
81
  Effects only on a kind with a writing standard action (not an accumulation
55
- register or a recorder-subordinate information register), and
56
- `tabularSections: true` only on a kind with tabular sections
57
- (`rights.custom-action-effects`).
82
+ register or a recorder-subordinate information register),
83
+ `tabularSections: true` only on a kind with tabular sections, and never a
84
+ transition column (`posted`, the deletion mark) or an immutable column in
85
+ `header` (`rights.custom-action-effects`, reasons `transitionColumn` and
86
+ `immutableColumn`). On a catalog an immutable column is any column the
87
+ command role cannot update (the `ref` key, `createdBy`, `updatedBy`,
88
+ `createdAt`, `updatedAt`, `version`, `predefinedName`, the platform
89
+ attributes of the users catalog); on other kinds it is the `ref` key,
90
+ `createdBy` and `updatedBy`.
58
91
  - **`ownership`** — carriers whose `Ref` names the owner of a row (a member
59
92
  or a user). `"ownership": ["<attribute>", ["<ref attribute>", "<attribute>"]]`
60
93
  The target is the users catalog, the membership catalog of the object's own
@@ -69,11 +102,17 @@ the tenant (`rights.carrier-cross-tenant`).
69
102
  - **`access`** — the project access dimensions that narrow the rows, each with
70
103
  its carrier to the dimension's value catalog.
71
104
  `"access": [{ "attribute": "<attribute>", "dimension": "<dimension>" }]` or
72
- `{ "ref": ["<ref attribute>", "<attribute>"], "dimension": "<dimension>" }`
105
+ `{ "ref": ["<ref attribute>", "<attribute>"], "dimension": "<dimension>" }`.
106
+ A register maps only a dimension of its own (`"attribute"`), never a
107
+ resource, an attribute or a path (`rights.register-access-dimension`): its
108
+ movements and totals are read by the same dimensions.
73
109
  - **`accessDimensions`** (project) — a value catalog plus where the current
74
110
  user's values come from: a relation object or a verbatim SQL function.
75
111
  `{ "name": "<dimension>", "target": <catalog ref>, "values": { "relation": { "object": <ref>, "subject": "<field>", "value": "<field>" } } }`
76
112
  or `"values": { "function": { "name": "<function>" } }`.
113
+ A read policy is named after the dimension, so a name that is `any` or
114
+ `own`, or starts with `own_` or `in_` (in snake case), is
115
+ `rights.dimension-name-reserved`.
77
116
  The function is verbatim SQL of a closed form: no arguments,
78
117
  `RETURNS SETOF uuid LANGUAGE sql STABLE SECURITY DEFINER SET search_path = ''`
79
118
  with an `AS $$ … $$` body,
@@ -81,13 +120,23 @@ the tenant (`rights.carrier-cross-tenant`).
81
120
  functions it calls; a `plpgsql` function in that chain is refused), and no
82
121
  `simetra.*` object except `simetra.current_user_id()`
83
122
  (`rights.dimension-function-*`).
123
+ With system rights catalogs, every relation that this function or a
124
+ verbatim scope set function reads must be a table of a kind, an adopted
125
+ table (`CustomTable`) or a model view over them. Anything else fails with
126
+ `rights.snapshot-source-unknown`, which names the relation and the function
127
+ that reads it: the platform watches only those tables for the actor
128
+ snapshot ("Rights inside a transaction" below). Read the data from such a
129
+ table instead, or remove the read.
84
130
  - **`membershipSources`** (project) — an adopted table that holds the
85
131
  membership of a scope kind instead of a membership catalog.
86
- `{ "name": "<source>", "scopeKind": "<scope kind>", "table": <CustomTable ref>, "columns": { "scope": "<column>", "user": "<column>" } }`
132
+ `{ "name": "<source>", "scopeKind": "<scope kind>", "table": <CustomTable ref>, "columns": { "scope": "<column>", "user": "<column>", "active": "<column>" }, "where": [<condition>] }`
133
+ (`active` and `where` are optional). See "Adopted membership and assignment sources".
87
134
  - **`assignmentSources`** (project) — an adopted table that holds role
88
135
  assignments of one level: `"global"`, `{ "scopeKind": "<scope kind>" }` or
89
136
  `{ "container": <ref> }`.
90
- `{ "name": "<source>", "assignedOn": "global", "table": <CustomTable ref>, "columns": { "member": "<column>", "role": "<column>" } }`
137
+ `{ "name": "<source>", "assignedOn": "global", "table": <CustomTable ref>, "columns": { "member": "<column>", "role": "<column>" }, "where": [<condition>] }`
138
+ (`columns` also takes `scope`, `container` and `active`). See "Adopted
139
+ membership and assignment sources".
91
140
 
92
141
  Dimensions and sources are project elements: add them to the project root
93
142
  (`"target": { "kind": "Project" }`) and rename or delete them by
@@ -154,3 +203,555 @@ The compiler reports these as `rights.*` (for example `rights.grant-level`,
154
203
  the compiler's rule catalog. Changing a role's level is an
155
204
  `identity.assigned-once-changed` error; renaming its scope kind or container
156
205
  is not.
206
+
207
+ ## Adopted membership and assignment sources
208
+
209
+ A membership source and an assignment source make the rows of an adopted
210
+ table give rights: the platform reads them in its rights functions, the same
211
+ way as a membership catalog and the system rights catalogs. A role takes its
212
+ key in such a table through `sourceKeys`.
213
+
214
+ - **Member.** The member of a membership source is the table key, so the
215
+ table needs a primary key of one uuid column
216
+ (`rights.source-member-key`). The system rights catalogs of the scope kind
217
+ refer to the member by a composite foreign key within the scope, so the
218
+ table metadata also needs `UNIQUE (<scope column>, <key>)` in `uniques`
219
+ (`rights.source-member-unique`). The `scope`, `user`, `member` and
220
+ `container` columns are uuid, `active` is boolean
221
+ (`rights.source-column-type`). A table with
222
+ `rowLevelSecurity: forced` is refused (`rights.source-forced-rls`).
223
+ - **Without a users catalog.** The user is the `sub` claim of the session read
224
+ as a uuid; the source's user column holds that uuid.
225
+ - **Rows.** `where` is a conjunction of conditions on columns of the table,
226
+ `{ "column": "<column>", "equals": <string | boolean | integer> }` or
227
+ `{ "column": "<column>", "isNull": true | false }`; rows that fail it are
228
+ not part of the source. Use it when one table holds several kinds of rows.
229
+ The plan does not report rows cut off by `where` as unknown role keys.
230
+ - **Scope of an assignment row** (`columns.scope`, scope level only): a column
231
+ whose value must equal the scope of the member; `null` takes the scope from
232
+ the member; omitted means the `scopeColumn` of the table. Omitted on a
233
+ table without `scopeColumn` is `rights.source-scope-column`. The field on
234
+ another level is the same error.
235
+ - **Role through a reference.** `columns.role` is either a column holding the
236
+ role key or `{ "column": "<column>", "references": <CustomTable ref>, "key": "<column>", "where": [<condition>] }`:
237
+ the column is a uuid reference to an adopted role table, `key` is the role
238
+ key there, and the role `where` keeps only the roles of the source (custom
239
+ roles with the same key do not turn a system role on). The role table needs
240
+ a single-column uuid primary key (`rights.source-role-key`, reason
241
+ `primaryKey`). Without a role `where` the `key` column must be unique on
242
+ its own: a non-deferrable unique constraint or a unique index without
243
+ predicate, operator class or collation; otherwise
244
+ `rights.source-role-key`, reason `ambiguous`.
245
+ - **Two sources over one table** (for example global and per-scope
246
+ assignments in one table) need `where` conditions that provably exclude
247
+ each other on one column; add one discriminator column (for example
248
+ `scope_type`) and give each source a different literal, or a literal
249
+ against `isNull`. Otherwise `rights.source-where`, reason `shared`.
250
+ `rights.source-where` also reports a condition on a column that is not
251
+ text, uuid, boolean or integer, and an `equals` literal of the wrong type
252
+ (`isNull` is allowed on any column).
253
+ - **Verbatim set function.** A scope kind with its own `setFunction` (SQL you
254
+ write) must apply the same `where` and `active` conditions of its
255
+ membership source, or the tenant boundary will differ from the platform's
256
+ own "my scopes" function.
257
+ - **Grants.** The application role that applies the metadata owns the rights
258
+ functions (`SECURITY DEFINER`), so it needs `USAGE` on the schema of
259
+ every source and role table, `SELECT` on every such table, and, when a table has row level security enabled,
260
+ must own it or have `BYPASSRLS`; `plan` refuses to write a plan otherwise
261
+ (`plan.rights-source-access`). Rows with a role key no role declares in
262
+ `sourceKeys` grant nothing; `plan` reports them
263
+ (`plan.rights-source-unknown-keys`).
264
+ - **The platform only reads sources.** Whatever writes a source table
265
+ belongs to the application; the rights safeguards (last-holder guards,
266
+ versions, the rights journal) do not apply to those writes, which the
267
+ compiler notes as `rights.adopted-source-writes`.
268
+ - **Commands do not write sources.** The platform puts no actor snapshot
269
+ trigger on an adopted source, its role table or another `CustomTable` that
270
+ the rights facts read. So application SQL must not let the command role
271
+ (`simetra_command`) or `PUBLIC` write such a table, or the compiler fails
272
+ with `rights.snapshot-exempt-writer`. The message names what opens the
273
+ write; fix it as follows:
274
+ - a write grant (also through `ALTER DEFAULT PRIVILEGES`) — remove it;
275
+ - a view with a write grant and without `security_invoker` — remove the
276
+ grant or create the view `WITH (security_invoker = true)`;
277
+ - a `SECURITY DEFINER` function or procedure that writes the table — add
278
+ `REVOKE EXECUTE ON FUNCTION <name>(<argument types>) FROM PUBLIC` after
279
+ it, grant it to no command role, or drop `SECURITY DEFINER`; default
280
+ privileges do not count as that revoke;
281
+ - a `SECURITY DEFINER` trigger function that writes the table — remove its
282
+ trigger or drop `SECURITY DEFINER`.
283
+ - **Deleting a member.** A member of an adopted membership referenced by
284
+ platform role assignments cannot be deleted until those assignments are
285
+ removed (foreign key violation `23503`).
286
+
287
+ ## System rights catalogs
288
+
289
+ The system rights catalogs are Catalogs with `"role": "rightsRoles"` (custom
290
+ roles; standard tabular section `delta`) or `"role": "roleAssignments"`. Their
291
+ level is the catalog's own scope. A custom action on one is
292
+ `rights.system-catalog-custom-action`.
293
+ Run `simetra roles` before the first `plan` on a new database.
294
+
295
+ To administer rights, grant `read` and `administerRights` explicitly on the
296
+ system catalog at extent `any`. Neither `grantAll` nor `withAction` selects
297
+ system catalogs; a role with `fullAccess` includes their administration at
298
+ its own level. A container role cannot administer a scope's rights catalogs.
299
+ A rights administrator role must also grant `read` on the membership catalog
300
+ of its level; `administerRights` gives no implicit read, so without it an
301
+ assignment cannot address a member.
302
+ Use `administerRights` as the command's root action. It has no natural DML
303
+ counterpart, so another command cannot write a rights catalog as a side effect.
304
+
305
+ Rights changes are checked at transaction end against the actor's rights
306
+ captured before its first command change. The effective rights added by a
307
+ custom role or assignment must be covered by that snapshot. This includes
308
+ rights enabled by removing a denial or satisfying an action's requirements.
309
+ Changing an assignment's member or container checks the full new grant.
310
+ A transaction can create a role and then add its restricting denials before
311
+ the final check. A custom role that an assignment still references cannot be
312
+ deleted: marking it for deletion fails with `command.forbidden`, detail
313
+ `role-in-use`, and a physical delete fails with a foreign key violation
314
+ (`23503`). Other failures use `command.forbidden` with detail `escalation`.
315
+
316
+ Removing the last effective direct holder of a `fullAccess` archetype fails
317
+ with detail `last-full-access`. Custom roles based on that archetype do not
318
+ count as holders. Transfer the assignment within one transaction. Deleting
319
+ the scope root in that transaction removes the check when the assignment's
320
+ scope is bound to that root by a foreign key. Concurrent changes may need a
321
+ transaction retry after a serialization failure or deadlock.
322
+
323
+ An occupied membership row cannot be reassigned to another user. Only the
324
+ invited user can fill an empty user reference, and only if that user does not
325
+ already belong to the scope.
326
+ A row cannot be re-inserted under an id that a rights assignment still
327
+ references; for a user, a membership row also blocks it. Other application
328
+ references do not block re-insertion. Commands do not change the users
329
+ catalog's `invalid` or `userKind`; provider provisioning and invalidation
330
+ remain platform operations.
331
+
332
+ Direct writes without command context fail with detail `rights-context`.
333
+ Platform tools use a maintenance mode that bypasses only the actor's grant
334
+ subset check, preserving the in-use-role and last-holder checks. Application
335
+ SQL must not enable it, and neither provider service logins nor the
336
+ platform's runtime login `simetra_runtime` can enable it.
337
+ `TRUNCATE` is refused for guarded rights, membership, user and dimension
338
+ relation tables, including in maintenance mode.
339
+
340
+ For a rights cache key, combine `simetra.rights_projection_version()` with
341
+ `simetra.rights_versions()`: the former identifies the compiled rights rules,
342
+ the latter tracks changes in the caller's scopes. Writes to adopted tables
343
+ do not bump the rights version, because the platform puts no triggers on
344
+ them: this covers adopted membership and assignment sources and a dimension
345
+ relation that is an adopted `CustomTable`. The platform rights journal
346
+ records changes to system rights catalogs in the same transaction, including
347
+ the actor (null for maintenance), reason, and before/after images.
348
+
349
+ When applying metadata changes, inspect the plan's `plan.rights-orphans`
350
+ warnings about orphaned role, assignment and delta references; those rows
351
+ grant no matching rights. Clean them with commands ("Rights administration
352
+ commands" below). The plan refuses a code change that would remove
353
+ the last effective `fullAccess` holder with `plan.rights-full-access-lost`. Restore the holder or transfer administration before
354
+ applying that change.
355
+
356
+ ## Rights administration commands
357
+
358
+ Administer rights with the ordinary operations of the system rights
359
+ catalogs through `runtime.run` (`simetra/server`). Every operation on them
360
+ runs under `administerRights`:
361
+
362
+ - custom role: `create`, `update` with `expectedVersion` (the `delta`
363
+ tabular section is replaced whole), `markDeletion` on the roles catalog;
364
+ - assignment: `create`, and `delete` with `expectedVersion`, on the role
365
+ assignments catalog.
366
+
367
+ The runtime adds no rights checks of its own. The database judges the
368
+ transaction at `COMMIT`, and the runtime maps its refusals:
369
+ `command.forbidden` with `escalation`, `role-in-use` or `last-full-access`,
370
+ and `command.conflict` for a stale version. Assigning a role to yourself
371
+ fails at `COMMIT` with `escalation`.
372
+
373
+ **Rights inside a transaction.** A command uses the rights its actor had
374
+ when its transaction started. Before the first change of a table that feeds
375
+ the actor's rights (role assignments, custom roles, membership, users,
376
+ dimension relations, tables read by verbatim dimension and scope set
377
+ functions), the database takes a snapshot of those rights. The command core,
378
+ row level security and the rights functions then read the snapshot until the
379
+ transaction ends. So a right granted inside a command's transaction takes
380
+ effect from the next transaction: a phase or hook that uses a role the batch
381
+ has just assigned to its own actor fails with `command.forbidden`, reason
382
+ `no-right`, and a read returns no rows. This also holds for host code that
383
+ writes with the runtime login directly. Phases and hooks may run in any
384
+ order: a membership `onWrite` that assigns a role with `ctx.run` does not
385
+ limit the next phases of the batch. A read only transaction does not take a
386
+ snapshot.
387
+
388
+ **Transfer `fullAccess`** in one batch: assign the archetype to the new
389
+ holder and delete your own assignment, in either order. The last-holder check
390
+ runs at `COMMIT`, and the command core reads your rights at the start of the
391
+ transaction.
392
+
393
+ **Clean orphans.** `simetra plan --format json` returns `rightsOrphans` with
394
+ `roles`, `assignments` and `deltas`, also when the status is `unchanged`.
395
+ Each entry has the catalog `object`, the row `id`, the expected `version`
396
+ and, on a scope level, the `scope`. A delta row and an assignment of an
397
+ orphan role also have the `role` id; the version of a delta row is its
398
+ role's version. The list is complete; the warning text shows only the first
399
+ rows. Send one batch in this order:
400
+
401
+ 1. `delete` every entry of `assignments`;
402
+ 2. `markDeletion` every entry of `roles`;
403
+ 3. `update` every role named in `deltas`, with its delta without those rows.
404
+
405
+ `update` replaces the whole delta, so send the rows that stay. The runtime
406
+ does not read tabular sections yet: read them with your own query. A stale
407
+ version fails with `command.conflict`; run `plan` again and rebuild the
408
+ batch.
409
+
410
+ ## Tenant creation
411
+
412
+ A tenant is a row of a scope root: the catalog its scope kind is rooted on.
413
+ The scope kind, not the creator's role, picks the model:
414
+
415
+ - **Creator model**: the scope kind has a scope role with `rootCreator: true`.
416
+ The `create` of the root also inserts the creator's member row and assigns
417
+ the creator that role, in the same transaction. The creator holds the role
418
+ in exactly the new scope already inside this transaction; `COMMIT` judges
419
+ everything else as usual.
420
+ - **Operator model**: no `rootCreator` role. The root is created without a
421
+ member, and the first owner comes from `tenantFullAccess` of the rights
422
+ maintenance tool.
423
+
424
+ **The right to create a root** comes only from an explicit `create` grant on
425
+ the root in a `global` or `everyUser` role. `grantAll`, `withAction` and
426
+ `fullAccess` do not give it, and a scope or container role cannot grant it
427
+ (`rights.grant-level`). Self-service is `everyUser` with `create@own`: the
428
+ root needs `trackAuthor` and `"createdBy"` in its `ownership`
429
+ (`rights.root-create-author`), because the new row has no other owner. An
430
+ operator is a `global` role with `create@any`. Creating a root needs no
431
+ `read` on it. A global `fullAccess` holder can still assign a global role
432
+ that grants it.
433
+
434
+ **A scope kind with `rootCreator`** needs (each is a compiler error):
435
+
436
+ - a membership catalog with `membership`, not an adopted membership source
437
+ (`rights.root-creator-membership`);
438
+ - `setFunction: "membership"` (`rights.root-creator-set-function`);
439
+ - the system role assignments catalog of its level
440
+ (`rights.root-creator-assignments`), and no required attribute without a
441
+ `defaultValue` on that catalog (`rights.root-creator-assignment-required`):
442
+ the creator's assignment is inserted with the key, scope, member and role
443
+ only;
444
+ - `rootCreator` on a scope role (`rights.root-creator-level`), on one role of
445
+ the scope kind (`rights.root-creator-duplicate`);
446
+ - no SQL subscription (`beforeWrite`, `onWrite`) that fires on `INSERT` of
447
+ its membership catalog or role assignments catalog
448
+ (`rights.root-creator-subscription`): the platform writes the creator's
449
+ rows as the table owner. Put that logic in a command hook of the catalog.
450
+
451
+ A `rootCreator` role without `fullAccess` and without `administerRights` on
452
+ the rights catalogs of its level gets the warning
453
+ `rights.root-creator-no-admin`: the creator could not assign roles in the new
454
+ tenant. A `rootCreator` role on a scope kind whose root no command creates
455
+ (the root is `Users`: provisioning creates its rows) gets the warning
456
+ `rights.root-creator-unreachable`: no tenant creation assigns the role, so
457
+ assign the first role with the operator tool.
458
+
459
+ **The operation** goes through `runtime.run` like any other:
460
+
461
+ ```jsonc
462
+ {
463
+ "action": "create",
464
+ "object": { "kind": "Catalog", "name": "<root>" },
465
+ "values": { … },
466
+ "creatorMember": { … }
467
+ }
468
+ ```
469
+
470
+ - Do not pass `id`: the runtime generates the root id (and the member row id)
471
+ once per `runtime.run`, and a retry reuses them. A `ctx.run` from a hook
472
+ generates new ids on each attempt, because the hook runs again. `id` or `tabularSections` on a root
473
+ `create` fails with `command.invalid`; fill tabular sections with a later
474
+ `update`.
475
+ - `creatorMember` holds attributes of the creator's member row that a command
476
+ may insert; the platform sets the key, the scope and the user. It defaults
477
+ to `{}`. It is allowed only on the root of a kind with `rootCreator`. A
478
+ missing required attribute, or a reference to a row that is missing or that
479
+ the creator cannot read, fails with `command.invalid` and the path
480
+ `creatorMember.<attribute>`.
481
+ - Hooks run in this order: `beforeWrite` of the root, `beforeWrite` of the
482
+ membership catalog (draft `{ values: creatorMember }`, `ctx.id` is the
483
+ member row id, `ctx.isNew`), `onWrite` of the root, `onWrite` of the
484
+ membership catalog. A membership `onWrite` may assign more roles with
485
+ `ctx.run`; `COMMIT` judges them as ordinary commands.
486
+ - `rows[0]` of the result is the root row. In the operator model the creator
487
+ cannot read the new root, so the row has only its key and version (`1`).
488
+ The member row goes only to the membership `onWrite`. If the
489
+ `rootCreator` role cannot read the membership catalog, that row also has
490
+ only its key and version (`1`).
491
+
492
+ The rights journal records the creator's assignment with the reason
493
+ `root-creator`. A later change of that assignment is an ordinary command.
494
+
495
+ ## Rights maintenance tool
496
+
497
+ `simetra rights-maintenance '<json>' [dir] --yes` writes one assignment
498
+ outside commands, in the database maintenance mode. It is a command line
499
+ tool only, not an MCP tool, and without `--yes` it does not connect. Connect
500
+ as the migrator that applied the plan (`SIMETRA_DATABASE_URL`): maintenance
501
+ mode works only for the login that owns the rights functions. The input has
502
+ one action:
503
+
504
+ - `{ "action": "firstGlobalAssignment", "user": "<uuid>", "role": "<role>" }`
505
+ — the first global assignment of a new database. Once any global
506
+ assignment exists, it fails with `maintenance.not-first`.
507
+ - `{ "action": "restoreGlobalFullAccess", "user": "<uuid>", "role": "<role>" }`
508
+ — a global `fullAccess` holder when no effective one is left. Omit `role`
509
+ when the project has exactly one assignable global `fullAccess` role.
510
+ - `{ "action": "tenantFullAccess", "scopeKind": "<scope kind>", "scope": "<uuid>", "user": "<uuid>", "role": "<role>", "member": { … } }`
511
+ — the `fullAccess` owner of one scope without an effective holder. Use it
512
+ for the first owner when an operator creates tenants (no `rootCreator`
513
+ role), and for a tenant whose only holder became invalid. If the user is
514
+ already a member, the member gets the role; do not pass `member` then.
515
+ Otherwise the tool inserts a member row with the `member` values,
516
+ attributes of your membership catalog that a command may insert. The tool
517
+ sets the key, the scope and the user itself, and checks the references in
518
+ `member` like a command does: the target must exist in the same scope.
519
+
520
+ The role must be `assignable`. For `tenantFullAccess` it is a `fullAccess`
521
+ role of exactly that scope kind. The tool refuses without writing and exits
522
+ with a non-zero code when:
523
+
524
+ - another `apply` or maintenance run holds the lock (`maintenance.busy`);
525
+ - the applied model is not the compiled one (`maintenance.model-mismatch`):
526
+ run `plan` and `apply` first;
527
+ - the user, the scope or the role is unknown, or the role does not fit the
528
+ action;
529
+ - the level already has an effective holder (`maintenance.holder-exists`);
530
+ - the user would not be an effective holder (`maintenance.not-effective`);
531
+ - a required member attribute has no value (`maintenance.member-required`);
532
+ - a reference in `member` (also an array `Ref` or a polymorphic pair) points
533
+ to a missing row or a row of another scope (`maintenance.invalid-member`,
534
+ with the `path` of the attribute).
535
+
536
+ `maintenance.refused` means that the database refused the write. Most often
537
+ the login is not the migrator. `maintenance.outcome-unknown` means that the
538
+ connection dropped after the write; run the tool again to see the state. The
539
+ rights journal records the write with no actor and the reason
540
+ `maintenance`. The tool is verified against the local Supabase stack,
541
+ directly and through its pooler; the hosted pooler is not verified yet.
542
+
543
+ ## Rights analysis
544
+
545
+ The database answers rights questions with the same rights functions the
546
+ policies use. `simetra.rights_mine()` returns the caller's own rights; any
547
+ user may call it. The other analysis functions disclose someone else's
548
+ rights, so they return rows only when the caller holds `administerRights` on
549
+ the role assignments catalog of the requested level (scope kind and scope,
550
+ or the global level with a zero uuid for both). Without that right they
551
+ return nothing, never an error:
552
+
553
+ - `simetra.rights_member(scope_kind, scope, member)` — the rights of a member
554
+ of that scope, with their origin (`via` is `archetype` or `delta`). The
555
+ member must belong to the scope; the global form takes a user id.
556
+ - `simetra.rights_who(action, object, scope_kind, scope)` — members of that
557
+ scope whose roles grant the action on the object. It answers per scope or
558
+ for the global level; it does not merge tenants.
559
+ - `simetra.rights_explain(object, row, user)` — one row per read policy of
560
+ the object with `has_right` and `carrier_match`. A row is visible to the
561
+ user when every `restrictive` row matches and at least one `permissive` row
562
+ has both flags. It explains only rows the caller can read, and only for a
563
+ user who belongs to the row's scope; an unknown row returns nothing. A
564
+ `publicRead` row is the exception: any user gets its single public-read
565
+ row, matching what row-level security shows.
566
+ `carrier_match` is null when the carrier set comes from a verbatim
567
+ function of yours and the user is not the caller. Register and constant
568
+ rows are not explained.
569
+ - `simetra.rights_journal_of(scope_kind, scope, limit)` — the rights journal
570
+ of that scope, newest first, at most 1000 rows.
571
+
572
+ The service role cannot execute these functions. They read claims, so they
573
+ return nothing in maintenance mode or without a session user. Build a client
574
+ rights snapshot from these functions and cache it by the rights cache key
575
+ above.
576
+
577
+ ## Supported limits
578
+
579
+ Measured on one developer machine (PostgreSQL 17.6, local Supabase stack;
580
+ reads and rights commands 2026-11-05, carrier rows 2026-10-31); treat them as
581
+ orders of magnitude, not guarantees.
582
+
583
+ | Scenario | Volume | Time |
584
+ | --- | --- | --- |
585
+ | Read a scoped table, dimension values per user | 20,000 values, 120,000 rows | list 0.57 s, point 18 ms |
586
+ | Read a scoped table, `fullAccess` holder in N scopes | 100 / 500 / 2,000 scopes, 100,000 rows | list 0.05 / 0.2 / 1.2 s |
587
+ | Owner carrier change, tabular section fan-out | 100,000 / 300,000 section rows | 2.5 / 8.0 s |
588
+ | Carrier backfill plan step | 1,000,000 section rows | 36 s |
589
+ | One rights command (assignment) in its own transaction | 11,000 assignments | 28 ms |
590
+ | Rights commands in one transaction | 1,000 commands | 7.1 s |
591
+ | Create a tenant with its creator, actor in N scopes | 1 / 2,000 scopes | core 29 / 237 ms, `COMMIT` 30 ms / 5.8 s |
592
+
593
+ - A `fullAccess` holder costs about 0.6 ms per scope on a list read: up to
594
+ about 1,500 scopes a read stays under a second. The scope count is data, so
595
+ no compiler warning covers it.
596
+ - Split batch rights changes into transactions: one command takes about
597
+ 30 ms, while a thousand commands in one transaction take about 7 s.
598
+
599
+ ## Who reads what
600
+
601
+ Rows are read through rights, not by default. In a project with at least one
602
+ role and a session user (a users catalog, or a membership source on a provider
603
+ whose user id is the `sub` claim):
604
+
605
+ - A table of a kind is read only through a role's `read` right on its object,
606
+ at the extent of the grant (`any`, `own`, a dimension, in a container); its
607
+ tabular sections and a register's movements and totals follow the object.
608
+ `publicRead` opens every row instead and needs no right. It is an error
609
+ (`rights.public-read-forbidden`) on the users catalog, a membership catalog
610
+ and the rights system catalogs: for public profiles declare a separate
611
+ catalog with the public fields and a reference to the users catalog.
612
+ - A scoped object is read only inside the reader's scopes, and only through a
613
+ role assigned there: membership in a tenant without a role reads nothing.
614
+ - The users catalog shows only yourself and the users of members of your
615
+ scopes (when the project has a membership catalog or a membership source),
616
+ whatever the role grants
617
+ on it: a scope-level `read` on the users catalog reads only those.
618
+
619
+ Without any role (or without a session user: no users catalog and no
620
+ membership source on such a provider) no right opens a row: only
621
+ `publicRead` objects are readable. `rights.scope-kind-unread` (a warning on
622
+ the scope kind in `project.meta.json`) says that no role reads any object of
623
+ that scope kind, so nobody will see their rows: grant `read` to a role or
624
+ declare one with `fullAccess`.
625
+
626
+ ## Who writes what
627
+
628
+ Kind tables are written **only by commands**: the write policies grant the
629
+ command role and nobody else.
630
+
631
+ - The **root row** of a command is checked against the right of the command's
632
+ **own action** — the standard action (`create`, `update`, `delete`, `post`,
633
+ …) or the custom action itself, not the actions it `requires` — and only
634
+ through that action's DML (`create` inserts, `delete` deletes, the rest
635
+ update).
636
+ - **Every other row** is checked against the right of the **natural action of
637
+ its DML**, a fact of the kind (`create` for an insert, `update` for an
638
+ update, `delete` for a delete; `write` on an information register; `update`
639
+ for both on a constant); a tabular-section row is checked against `update`
640
+ of its owner. A DML with no natural action on that
641
+ table is refused.
642
+ - An action without `tabularSections` does not write tabular sections. The
643
+ transition columns (`posted`, the deletion mark) change only through `post`,
644
+ `unpost` and `markDeletion`; `post` only sets `posted` and `unpost` only
645
+ clears it, while `markDeletion` may set or clear the mark. A custom action is
646
+ not a transition action and cannot name a transition column in its header
647
+ effects (`rights.custom-action-effects`, reason `transitionColumn`).
648
+ - The ref key of a catalog, document or tabular-section row never changes —
649
+ not in a command, not in application SQL, not in maintenance mode, and also
650
+ in a project without rights (`42501` `command.forbidden`, detail `effect`,
651
+ the key column). The scope column of a row cannot be changed by any command,
652
+ and a tabular-section row cannot be moved to another owner;
653
+ a custom action that names the key in its header effects is refused
654
+ (`rights.custom-action-effects`, reason `immutableColumn`).
655
+ - A `beforeWrite` handler that, under a transition, changes another column makes
656
+ the command fail. Put a computation done at posting into the command hook or
657
+ into the effects of a custom action.
658
+ - Application SQL never writes a table of a kind and never changes the
659
+ platform's session settings: `SKILL.md`, section "The SQL module: closed
660
+ forms only", names the reference with those rules.
661
+
662
+ ## Generated privileges
663
+
664
+ Privileges on what the platform owns are generated: `EXECUTE` on a declared
665
+ scope set function and on a subscription handler, and every grant on a kind's
666
+ tables. In debt SQL (a `CustomTable` or `PgEnum` module, the shared
667
+ `metadata/sql/`) a verbatim `GRANT`/`REVOKE` on them fails with
668
+ `sql.generated-grant` (a kind's own module rejects any). `simetra fix` removes
669
+ the statement (and its file, if left empty) only when every target belongs to
670
+ the platform; one that also names other objects you split by hand. A grant
671
+ on another overload of the same name is not affected.
672
+
673
+ ## Platform roles
674
+
675
+ `simetra roles` creates the cluster roles the platform needs. It creates
676
+ `simetra_command` (`NOLOGIN NOINHERIT`, on the target and the shadow server)
677
+ and, when a project file is present, the runtime login role `simetra_runtime`
678
+ on the target only (`LOGIN NOINHERIT`, a member of `simetra_command` and, for
679
+ `supabase`, of `authenticated`, each `WITH INHERIT FALSE, SET TRUE`). It reads
680
+ `database.provider` from the project file; without a project file it skips the
681
+ login role, and the output says why. The operator sets the login role's
682
+ password; the platform keeps no secrets.
683
+
684
+ The login role needs `ADMIN OPTION` on each of its parent roles in the role
685
+ that runs `roles`. Without it `roles` still creates the grant roles, but
686
+ refuses the login role with `roles.admin-option-missing` and does not create
687
+ it. An existing role is only checked, never changed.
688
+
689
+ `plan.platform-role-shape` with the violation `request-role:<role>` means an
690
+ API request role (such as `authenticated`) and a platform role are members of
691
+ each other in some form, even `ADMIN` alone; revoke that membership.
692
+
693
+ ## Reading past row-level security
694
+
695
+ `sql.definer-reads-rls` (a warning) marks a unit that reads a table under
696
+ row-level security as its owner, so the caller's policies do not apply, while
697
+ a role of the API (or `PUBLIC`) can run or read it:
698
+
699
+ - a `SECURITY DEFINER` function that someone may `EXECUTE`;
700
+ - a view without `security_invoker` that someone may `SELECT`;
701
+ - a materialized view, which stores the rows its owner read at `REFRESH`.
702
+
703
+ The diagnostic lists the tables it found. The compiler does not see the owner of a function,
704
+ so one whose owner is itself subject to row-level security is flagged too.
705
+ Convert in this order of preference:
706
+
707
+ 1. Read through a right and a policy: let the caller read the kind's table
708
+ itself, so the platform's policies decide, and make the unit
709
+ `SECURITY INVOKER` (a view: `WITH (security_invoker = true)` in
710
+ `CREATE VIEW`; a separate `ALTER VIEW` is not accepted in `.sql` files).
711
+ 2. If the unit must run as its owner, check the caller's membership or rights
712
+ inside it, or filter the rows with the rights functions in a view.
713
+ 3. If no API caller needs it, `REVOKE EXECUTE` (function) or `REVOKE SELECT`
714
+ (view, materialized view) from `PUBLIC` and from each API role. A
715
+ materialized view that serves callers is replaced by a view with
716
+ `security_invoker`.
717
+
718
+ ## Carrier copies
719
+
720
+ A tabular section cannot be read by the carriers of its owner without a
721
+ copy, and neither can an object whose carrier is a path. So the platform
722
+ keeps copies: every tabular section of an object with carriers gets a column
723
+ per carrier (named like the owner's column), and a path carrier gets a column
724
+ in the object row. The copies are written only by the platform:
725
+
726
+ - A value the client writes into a copy is replaced by the value of the
727
+ source row; a request that changes only copies is a resave.
728
+ - When the source changes, the copies of dependent rows (and of their tabular
729
+ sections) follow. This propagation is not a change of the row: it does not
730
+ bump `version` or `updatedAt` and fires no subscription and no platform
731
+ `BEFORE UPDATE` guard.
732
+ - The names are taken: an attribute under a copy's name is
733
+ `rights.carrier-column-taken`; rename the attribute.
734
+ - A `beforeWrite` handler on such a table never assigns a copy or the column
735
+ of the first path step (`subscription.handler-assigns-carrier`: assign them
736
+ in the command hook) and never reads a copy as `NEW.<copy>`
737
+ (`subscription.handler-reads-carrier`: on `INSERT` it may still hold the
738
+ client's value); look the carrier up in the row the first step points to.
739
+ - On a table with copies the row mechanics are two triggers,
740
+ `<table>_simetra_touch` on `INSERT` and `<table>_simetra_touch_update` on an
741
+ `UPDATE` that changes the content, and a write subscription without
742
+ `whenChanged` gets the pair `<physicalName>` (`UPDATE`, content changed)
743
+ and `<physicalName>_insert` (`INSERT`), so propagation triggers neither.
744
+ - A new copy on a table that already has rows is filled by a plan step of
745
+ origin `carriers` (class `dataStep`, no `--allow` needed) that runs right
746
+ after the engine steps, in the same transaction: each row takes the value
747
+ from the row its composite foreign key points to, and a row without a link
748
+ gets `NULL`. The step holds `SHARE ROW EXCLUSIVE` on the table and on its
749
+ source until the apply ends. Triggers stay on, and a copy-only change does
750
+ not bump `version` or fire subscriptions.
751
+ - When a copy takes the place of a column that was not a copy (a removed
752
+ attribute that still holds data, or an adopted column the promotion
753
+ mapping left out), the step overwrites that data: it is class
754
+ `destructive` and needs `--allow <step id>` like any other step that loses
755
+ data. A copy column the plan itself creates is always filled, even if the
756
+ last applied snapshot says the copy already exists (for example, an
757
+ administrator dropped it by hand and the drift was accepted).