@stonyx/orm 0.3.2-alpha.58 → 0.3.2-alpha.59

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.
package/README.md CHANGED
@@ -397,7 +397,7 @@ parsing anything.
397
397
  | Key | Value |
398
398
  |---|---|
399
399
  | `model` | The model this route was mounted for, as a **model name**: kebab-case, exactly as declared under `config.orm.paths.model` and keyed in the store — `'owner'`, `'animal'`, `'phone-number'`. **Not** the pluralized, dasherized, mount-prefixed *route* name. |
400
- | `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary. Never an HTTP method name like `'GET'`. `undefined` when the dispatched method has no entry in the framework's method map. |
400
+ | `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary *on this path*. Never an HTTP method name like `'GET'`, and **not** the hook vocabulary either (see [below](#operation-is-not-the-hook-operation)). `undefined` when the dispatched method has no entry in the framework's method map. |
401
401
 
402
402
  So a predicate can be written without reference to any URL:
403
403
 
@@ -420,6 +420,49 @@ a URL shape nobody anticipated. `model` is fixed at mount time and no request
420
420
  can influence it — not a mount prefix, not a query string, not a case-varied
421
421
  path, not an absolute-form request target.
422
422
 
423
+ #### What the context does not tell you: which surface
424
+
425
+ It names **which model and which verb**, not **which route**. Measured over the
426
+ live router, six surfaces produce one identical context:
427
+
428
+ ```
429
+ GET /owners { model: 'owner', operation: 'read' }
430
+ GET /owners/gina { model: 'owner', operation: 'read' }
431
+ GET /owners/gina/pets { model: 'owner', operation: 'read' }
432
+ GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
433
+ GET /owners/archived { model: 'owner', operation: 'read' }
434
+ GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
435
+ ```
436
+
437
+ So a rule that depends on the **sub-path** still needs `request.path` —
438
+ mount-relative and query-free, and the one read of argument one that
439
+ [Identifying the collection](#identifying-the-collection) sanctions. The sample
440
+ access class shipped with this repo has such a rule: its `/archived` deny
441
+ **cannot be expressed from the context alone**, and a predicate migrated to
442
+ context-only would silently drop it — a deny becoming an allow.
443
+
444
+ Note also that the related-resource and `?include=` surfaces serve *another
445
+ model's* records under `model: 'owner'`, and the context gives a predicate no
446
+ signal that it is authorizing a related-resource route. That is
447
+ [#196](https://github.com/abofs/stonyx-orm/issues/196).
448
+
449
+ #### `operation` is not the hook `operation`
450
+
451
+ This module exposes a **second** `operation` vocabulary, on an identically-named
452
+ key of an identically-shaped context object:
453
+ [hook contexts](#hook-context-object) carry `list` / `get` / `create` /
454
+ `update` / `delete`. The access vocabulary collapses `list` and `get` into
455
+ `'read'`, so for one `GET /animals/1` a hook sees `'get'` while `access()` sees
456
+ `'read'` — and a predicate cannot distinguish a collection read from a
457
+ record read.
458
+
459
+ "No second vocabulary" above is a statement about the **access path**, where
460
+ both the context and the permission array come from one method map. It is not a
461
+ statement about the module. Writing `operation === 'get'` in a predicate never
462
+ matches, and a predicate that stops matching falls through to the permission
463
+ array — so the misreading is fail-open shaped. In TypeScript the exported
464
+ `AccessOperation` union makes it a compile error.
465
+
423
466
  The four `operation` values are the same four strings the permission-array
424
467
  return shape is written in (`['read', 'create', 'update', 'delete']`), because
425
468
  both come from one method map inside the framework. The two forms cannot
@@ -459,18 +502,52 @@ request routed to a *different* model:
459
502
  import Orm from '@stonyx/orm';
460
503
 
461
504
  const predicate = Orm.instance.getAccess('animal');
462
- const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
505
+ if (!predicate) return deny;
506
+
507
+ const verdict = predicate(request, { model: 'animal', operation: 'read' });
463
508
  ```
464
509
 
465
- `Orm.instance.getAccess(modelName)` returns the predicate, or `undefined` when
466
- that model has no access class. The raw map is `Orm.instance.accessFunctions`, keyed
467
- by model name; prefer `getAccess()`.
510
+ **`undefined` means no predicate could be resolved — not that the model is
511
+ unrestricted. Treat it as deny.** It covers a model with no access class *and* a
512
+ model whose access class failed to **load**: a load failure is caught and warned
513
+ about, and the partial map is published anyway, so a missing key is not evidence
514
+ of an unrestricted model. This is the same rule as `operation === undefined`
515
+ above, and for the same reason.
516
+
517
+ The raw map is `Orm.instance.accessFunctions`, keyed by model name; prefer
518
+ `getAccess()` — it is guarded against inherited `Object.prototype` members and a
519
+ direct index is not. Note that it maps a model name to the predicate of the
520
+ access *class* that claims it, which may claim many models: against this repo's
521
+ sample, `getAccess('owner') === getAccess('animal')`.
522
+
523
+ #### Passing the context makes a model-correct answer *possible*
524
+
525
+ It does not make the answer model-correct on its own. **The resolved predicate
526
+ has to read the context.** Measured against the access class shipped with this
527
+ repo, on a request Express dispatched to `GET /owners/angela`, asked about
528
+ **animals**:
529
+
530
+ ```
531
+ getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
532
+ -> record => record.id !== 'angela' && record.id !== 'restricted'
533
+ ```
534
+
535
+ That is the **owners** filter, and it returns `true` for animal 21 — the record
536
+ hidden on every animal surface. Under a mount that predicate recognizes neither
537
+ way it is worse still: it falls through to
538
+ `['read', 'create', 'update', 'delete']`, a full CRUD grant.
539
+
540
+ Either way the context was supplied and the answer is not the animal answer, and
541
+ it is wrong in the direction that **grants**. That predicate is single-argument
542
+ and identifies its collection from the request, so it answered about the
543
+ collection the request is *addressed to* while being asked about another one.
544
+ Every predicate in this repo, and in every consumer tree, is single-argument on
545
+ the day this ships, and a caller has no supported way to tell which kind it
546
+ resolved. The boot-time arity warning that would surface it is
547
+ [#213](https://github.com/abofs/stonyx-orm/issues/213).
468
548
 
469
- Passing the context explicitly is what makes the answer **model-correct**. A
470
- predicate that identifies its collection from the request would otherwise answer
471
- about the collection the request is *addressed to* while being asked about
472
- another one — and per the five variants below, it answers wrong in the direction
473
- that grants access.
549
+ So: pass the context, and do not treat a resolved predicate's answer as
550
+ model-specific until that predicate has been migrated to read it.
474
551
 
475
552
  ### Return values
476
553
 
@@ -607,15 +684,19 @@ sub-paths beneath the mount, as the `/archived` deny above does.
607
684
  [#202](https://github.com/abofs/stonyx-orm/issues/202): `access()` never
608
685
  received the model structurally and `setup-rest-server.ts` discarded the
609
686
  model→predicate map at boot. **#202 has landed and both halves now exist** —
610
- see [The access context](#the-access-context-second-argument): `context.model`
611
- makes the answer model-correct and `Orm.instance.getAccess(modelName)` makes
612
- another model's predicate reachable. **The mechanism exists; the ORM does not
613
- yet use it on this path.** The re-parenting write above is still unblocked —
614
- that enforcement is
687
+ see [The access context](#the-access-context-second-argument):
688
+ `Orm.instance.getAccess(modelName)` makes another model's predicate
689
+ **reachable**, and `context.model` makes a **model-correct answer possible**
690
+ possible, not guaranteed: the resolved predicate has to read the context, and
691
+ every predicate in tree is still single-argument
692
+ ([#213](https://github.com/abofs/stonyx-orm/issues/213)), so today it answers
693
+ about the collection the request is addressed to. **The mechanism exists; the
694
+ ORM does not yet use it on this path.** The re-parenting write above is still
695
+ **not refused** — that enforcement is
615
696
  [#196](https://github.com/abofs/stonyx-orm/issues/196) and
616
697
  [#207](https://github.com/abofs/stonyx-orm/issues/207), which were blocked on
617
- #202 and are now unblocked. Until they land, do not rely on a filter to keep a
618
- record unmodifiable; keep the *writable* collections' predicates as tight as
698
+ #202 and are now free to proceed. Until they land, do not rely on a filter to
699
+ keep a record unmodifiable; keep the *writable* collections' predicates as tight as
619
700
  the hidden ones.
620
701
  - **Authorization by identifying the collection is a consumer-side
621
702
  reconstruction of information the framework already holds.** `access()`
package/dist/main.d.ts CHANGED
@@ -85,7 +85,8 @@ export default class Orm {
85
85
  constructor(options?: OrmOptions);
86
86
  init(): Promise<void>;
87
87
  /**
88
- * Resolve a model's `access` predicate by model name (abofs/stonyx-orm#202).
88
+ * Resolve the `access` predicate registered for a model name
89
+ * (abofs/stonyx-orm#202).
89
90
  *
90
91
  * This is the supported way to reach another model's predicate while
91
92
  * servicing a request routed to a different model. Call it with the model
@@ -94,13 +95,45 @@ export default class Orm {
94
95
  *
95
96
  * ```js
96
97
  * const predicate = Orm.instance.getAccess('animal');
97
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
98
+ * if (!predicate) return deny;
99
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
98
100
  * ```
99
101
  *
100
- * Passing the context is not optional in practice. A predicate that
101
- * identifies its collection from the request would otherwise answer about the
102
- * collection the request is ADDRESSED TO -- owners -- while being asked about
103
- * animals, and per #202's thesis it answers wrong in the granting direction.
102
+ * WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
103
+ * which is not necessarily specific to it: one class may claim many models
104
+ * and declares one `access` method, so
105
+ * `getAccess('owner') === getAccess('animal')` is `true` against this repo's
106
+ * fixture. See {@link Orm#accessFunctions}.
107
+ *
108
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
109
+ * includes a model whose access class failed to LOAD -- `setup-rest-server`
110
+ * catches, warns and publishes the partial map -- so it is not the same claim
111
+ * as "this model is unrestricted". Treat it as DENY, the same way
112
+ * `AccessContext.operation === undefined` is treated.
113
+ *
114
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
115
+ * its own, make the answer model-correct: the resolved predicate has to READ
116
+ * the context. Measured against this repo's shipped access class on a request
117
+ * express dispatched to `GET /owners/angela`, asked about ANIMALS:
118
+ *
119
+ * ```
120
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
121
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
122
+ * ```
123
+ *
124
+ * The OWNERS filter, which returns `true` for animal 21 -- the record hidden
125
+ * on every animal surface. Under a mount that predicate recognises neither
126
+ * way it falls through to `['read', 'create', 'update', 'delete']`, a full
127
+ * CRUD grant. Either way: context supplied, answer not the animal answer,
128
+ * wrong in the GRANTING direction, because that predicate is arity-1 and
129
+ * identifies its collection from the request. AC9 asserts the first case on a
130
+ * live dispatch.
131
+ *
132
+ * Every predicate in this repo and in every consumer tree is arity-1 today,
133
+ * and there is no supported way for the caller to tell which kind it got; the
134
+ * boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
135
+ * the context, and do not treat a resolved predicate's answer as
136
+ * model-specific until that predicate reads it.
104
137
  *
105
138
  * OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
106
139
  * prototype chain, so `getAccess('constructor')` resolved `Object` and
package/dist/main.js CHANGED
@@ -191,7 +191,8 @@ export default class Orm {
191
191
  Orm.initialized = true;
192
192
  }
193
193
  /**
194
- * Resolve a model's `access` predicate by model name (abofs/stonyx-orm#202).
194
+ * Resolve the `access` predicate registered for a model name
195
+ * (abofs/stonyx-orm#202).
195
196
  *
196
197
  * This is the supported way to reach another model's predicate while
197
198
  * servicing a request routed to a different model. Call it with the model
@@ -200,13 +201,45 @@ export default class Orm {
200
201
  *
201
202
  * ```js
202
203
  * const predicate = Orm.instance.getAccess('animal');
203
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
204
+ * if (!predicate) return deny;
205
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
204
206
  * ```
205
207
  *
206
- * Passing the context is not optional in practice. A predicate that
207
- * identifies its collection from the request would otherwise answer about the
208
- * collection the request is ADDRESSED TO -- owners -- while being asked about
209
- * animals, and per #202's thesis it answers wrong in the granting direction.
208
+ * WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
209
+ * which is not necessarily specific to it: one class may claim many models
210
+ * and declares one `access` method, so
211
+ * `getAccess('owner') === getAccess('animal')` is `true` against this repo's
212
+ * fixture. See {@link Orm#accessFunctions}.
213
+ *
214
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
215
+ * includes a model whose access class failed to LOAD -- `setup-rest-server`
216
+ * catches, warns and publishes the partial map -- so it is not the same claim
217
+ * as "this model is unrestricted". Treat it as DENY, the same way
218
+ * `AccessContext.operation === undefined` is treated.
219
+ *
220
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
221
+ * its own, make the answer model-correct: the resolved predicate has to READ
222
+ * the context. Measured against this repo's shipped access class on a request
223
+ * express dispatched to `GET /owners/angela`, asked about ANIMALS:
224
+ *
225
+ * ```
226
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
227
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
228
+ * ```
229
+ *
230
+ * The OWNERS filter, which returns `true` for animal 21 -- the record hidden
231
+ * on every animal surface. Under a mount that predicate recognises neither
232
+ * way it falls through to `['read', 'create', 'update', 'delete']`, a full
233
+ * CRUD grant. Either way: context supplied, answer not the animal answer,
234
+ * wrong in the GRANTING direction, because that predicate is arity-1 and
235
+ * identifies its collection from the request. AC9 asserts the first case on a
236
+ * live dispatch.
237
+ *
238
+ * Every predicate in this repo and in every consumer tree is arity-1 today,
239
+ * and there is no supported way for the caller to tell which kind it got; the
240
+ * boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
241
+ * the context, and do not treat a resolved predicate's answer as
242
+ * model-specific until that predicate reads it.
210
243
  *
211
244
  * OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
212
245
  * prototype chain, so `getAccess('constructor')` resolved `Object` and
@@ -18,12 +18,26 @@
18
18
  *
19
19
  * context.operation The operation being authorised. Exactly one of the four
20
20
  * verbs `'read'`, `'create'`, `'update'`, `'delete'` --
21
- * there is no second vocabulary, and it is never an HTTP
21
+ * no second vocabulary ON THIS PATH, and never an HTTP
22
22
  * method name like `'GET'`. These are the same four
23
23
  * strings the permission-array return shape is written in
24
24
  * (`['read', 'create']`), because both come from the one
25
25
  * `methodAccessMap` below.
26
26
  *
27
+ * NOT the hook vocabulary. `HookContext.operation`
28
+ * (`src/hooks.ts`, documented under "Hook Context Object"
29
+ * in the README) carries `'list' | 'get' | 'create' |
30
+ * 'update' | 'delete'` on an identically-named key of an
31
+ * identically-shaped context object, and the access
32
+ * vocabulary collapses `list` and `get` into `'read'`. For
33
+ * one `GET /animals/1` a hook sees `'get'` and `access()`
34
+ * sees `'read'`, so a predicate cannot tell a collection
35
+ * read from a record read. `AccessOperation` makes
36
+ * `operation === 'get'` a compile error for a TypeScript
37
+ * consumer, because a predicate that stops matching falls
38
+ * through to the permission array -- the misreading is
39
+ * fail-open shaped.
40
+ *
27
41
  * `undefined` when the dispatched method has no entry in
28
42
  * that map. Express delivers `HEAD` to the `GET` handler,
29
43
  * so this is reachable. It is left undefined rather than
@@ -35,6 +49,26 @@
35
49
  * is no string to parse, no variant to miss, and no way to fail open through a
36
50
  * URL shape nobody anticipated.
37
51
  *
52
+ * WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
53
+ * the verb, not the route. Measured over the live router, six surfaces produce
54
+ * one identical context:
55
+ *
56
+ * GET /owners { model: 'owner', operation: 'read' }
57
+ * GET /owners/gina { model: 'owner', operation: 'read' }
58
+ * GET /owners/gina/pets { model: 'owner', operation: 'read' }
59
+ * GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
60
+ * GET /owners/archived { model: 'owner', operation: 'read' }
61
+ * GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
62
+ *
63
+ * So a rule that depends on the SUB-PATH still needs `request.path` -- which is
64
+ * mount-relative and query-free, and is the one read of argument one the
65
+ * warning below sanctions. This repo's own fixture has such a rule: its
66
+ * `/archived` deny cannot be expressed from the context alone, and a predicate
67
+ * migrated to context-only would silently drop it, turning a deny into an
68
+ * allow. The related-resource and `?include=` surfaces serve ANOTHER model's
69
+ * records under `model: 'owner'`, and the context gives no signal of that
70
+ * (abofs/stonyx-orm#196).
71
+ *
38
72
  * `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
39
73
  * matching but BEFORE any handler executes (`@stonyx/rest-server`
40
74
  * `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
@@ -53,9 +87,37 @@
53
87
  * an owners route -- use the boot-time registry:
54
88
  *
55
89
  * const predicate = Orm.instance.getAccess('animal');
56
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
57
- *
58
- * Passing the context explicitly is what makes that answer model-CORRECT.
90
+ * if (!predicate) return deny;
91
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
92
+ *
93
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
94
+ * includes the case where the model has an access class that failed to load,
95
+ * because `setup-rest-server.ts` catches a load failure, warns, and publishes
96
+ * whatever partial map it had. It does NOT mean the model is unrestricted.
97
+ * Treat it as DENY, the same way `operation === undefined` is treated above.
98
+ *
99
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
100
+ * the answer model-correct on its own -- the resolved predicate has to READ it.
101
+ * Measured against this repo's own shipped access class, on a request express
102
+ * dispatched to `GET /owners/angela`, asked about ANIMALS:
103
+ *
104
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
105
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
106
+ *
107
+ * That is the OWNERS filter, and it returns `true` for animal 21 -- the record
108
+ * hidden on every animal surface. Under a mount that predicate recognises
109
+ * neither way it is worse: it falls through to
110
+ * `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
111
+ * context was supplied and the answer is not the animal answer, and it is wrong
112
+ * in the GRANTING direction, because that predicate is arity-1 and identifies
113
+ * its collection from the request. (The first of these is asserted on a live
114
+ * dispatch by AC9 in test/integration/orm-test.ts.)
115
+ *
116
+ * Every predicate in this repo and in every consumer tree is arity-1 on the day
117
+ * this ships, and the caller has no supported way to tell which kind it got --
118
+ * the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
119
+ * So: pass the context, and do not treat a resolved predicate's answer as
120
+ * model-specific until that predicate has been migrated to read the context.
59
121
  *
60
122
  * ---------------------------------------------------------------------------
61
123
  * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
@@ -18,12 +18,26 @@
18
18
  *
19
19
  * context.operation The operation being authorised. Exactly one of the four
20
20
  * verbs `'read'`, `'create'`, `'update'`, `'delete'` --
21
- * there is no second vocabulary, and it is never an HTTP
21
+ * no second vocabulary ON THIS PATH, and never an HTTP
22
22
  * method name like `'GET'`. These are the same four
23
23
  * strings the permission-array return shape is written in
24
24
  * (`['read', 'create']`), because both come from the one
25
25
  * `methodAccessMap` below.
26
26
  *
27
+ * NOT the hook vocabulary. `HookContext.operation`
28
+ * (`src/hooks.ts`, documented under "Hook Context Object"
29
+ * in the README) carries `'list' | 'get' | 'create' |
30
+ * 'update' | 'delete'` on an identically-named key of an
31
+ * identically-shaped context object, and the access
32
+ * vocabulary collapses `list` and `get` into `'read'`. For
33
+ * one `GET /animals/1` a hook sees `'get'` and `access()`
34
+ * sees `'read'`, so a predicate cannot tell a collection
35
+ * read from a record read. `AccessOperation` makes
36
+ * `operation === 'get'` a compile error for a TypeScript
37
+ * consumer, because a predicate that stops matching falls
38
+ * through to the permission array -- the misreading is
39
+ * fail-open shaped.
40
+ *
27
41
  * `undefined` when the dispatched method has no entry in
28
42
  * that map. Express delivers `HEAD` to the `GET` handler,
29
43
  * so this is reachable. It is left undefined rather than
@@ -35,6 +49,26 @@
35
49
  * is no string to parse, no variant to miss, and no way to fail open through a
36
50
  * URL shape nobody anticipated.
37
51
  *
52
+ * WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
53
+ * the verb, not the route. Measured over the live router, six surfaces produce
54
+ * one identical context:
55
+ *
56
+ * GET /owners { model: 'owner', operation: 'read' }
57
+ * GET /owners/gina { model: 'owner', operation: 'read' }
58
+ * GET /owners/gina/pets { model: 'owner', operation: 'read' }
59
+ * GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
60
+ * GET /owners/archived { model: 'owner', operation: 'read' }
61
+ * GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
62
+ *
63
+ * So a rule that depends on the SUB-PATH still needs `request.path` -- which is
64
+ * mount-relative and query-free, and is the one read of argument one the
65
+ * warning below sanctions. This repo's own fixture has such a rule: its
66
+ * `/archived` deny cannot be expressed from the context alone, and a predicate
67
+ * migrated to context-only would silently drop it, turning a deny into an
68
+ * allow. The related-resource and `?include=` surfaces serve ANOTHER model's
69
+ * records under `model: 'owner'`, and the context gives no signal of that
70
+ * (abofs/stonyx-orm#196).
71
+ *
38
72
  * `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
39
73
  * matching but BEFORE any handler executes (`@stonyx/rest-server`
40
74
  * `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
@@ -53,9 +87,37 @@
53
87
  * an owners route -- use the boot-time registry:
54
88
  *
55
89
  * const predicate = Orm.instance.getAccess('animal');
56
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
90
+ * if (!predicate) return deny;
91
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
92
+ *
93
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
94
+ * includes the case where the model has an access class that failed to load,
95
+ * because `setup-rest-server.ts` catches a load failure, warns, and publishes
96
+ * whatever partial map it had. It does NOT mean the model is unrestricted.
97
+ * Treat it as DENY, the same way `operation === undefined` is treated above.
98
+ *
99
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
100
+ * the answer model-correct on its own -- the resolved predicate has to READ it.
101
+ * Measured against this repo's own shipped access class, on a request express
102
+ * dispatched to `GET /owners/angela`, asked about ANIMALS:
103
+ *
104
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
105
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
106
+ *
107
+ * That is the OWNERS filter, and it returns `true` for animal 21 -- the record
108
+ * hidden on every animal surface. Under a mount that predicate recognises
109
+ * neither way it is worse: it falls through to
110
+ * `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
111
+ * context was supplied and the answer is not the animal answer, and it is wrong
112
+ * in the GRANTING direction, because that predicate is arity-1 and identifies
113
+ * its collection from the request. (The first of these is asserted on a live
114
+ * dispatch by AC9 in test/integration/orm-test.ts.)
57
115
  *
58
- * Passing the context explicitly is what makes that answer model-CORRECT.
116
+ * Every predicate in this repo and in every consumer tree is arity-1 on the day
117
+ * this ships, and the caller has no supported way to tell which kind it got --
118
+ * the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
119
+ * So: pass the context, and do not treat a resolved predicate's answer as
120
+ * model-specific until that predicate has been migrated to read the context.
59
121
  *
60
122
  * ---------------------------------------------------------------------------
61
123
  * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-alpha.58",
7
+ "version": "0.3.2-alpha.59",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
package/src/main.ts CHANGED
@@ -244,7 +244,8 @@ export default class Orm {
244
244
  }
245
245
 
246
246
  /**
247
- * Resolve a model's `access` predicate by model name (abofs/stonyx-orm#202).
247
+ * Resolve the `access` predicate registered for a model name
248
+ * (abofs/stonyx-orm#202).
248
249
  *
249
250
  * This is the supported way to reach another model's predicate while
250
251
  * servicing a request routed to a different model. Call it with the model
@@ -253,13 +254,45 @@ export default class Orm {
253
254
  *
254
255
  * ```js
255
256
  * const predicate = Orm.instance.getAccess('animal');
256
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
257
+ * if (!predicate) return deny;
258
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
257
259
  * ```
258
260
  *
259
- * Passing the context is not optional in practice. A predicate that
260
- * identifies its collection from the request would otherwise answer about the
261
- * collection the request is ADDRESSED TO -- owners -- while being asked about
262
- * animals, and per #202's thesis it answers wrong in the granting direction.
261
+ * WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
262
+ * which is not necessarily specific to it: one class may claim many models
263
+ * and declares one `access` method, so
264
+ * `getAccess('owner') === getAccess('animal')` is `true` against this repo's
265
+ * fixture. See {@link Orm#accessFunctions}.
266
+ *
267
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
268
+ * includes a model whose access class failed to LOAD -- `setup-rest-server`
269
+ * catches, warns and publishes the partial map -- so it is not the same claim
270
+ * as "this model is unrestricted". Treat it as DENY, the same way
271
+ * `AccessContext.operation === undefined` is treated.
272
+ *
273
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
274
+ * its own, make the answer model-correct: the resolved predicate has to READ
275
+ * the context. Measured against this repo's shipped access class on a request
276
+ * express dispatched to `GET /owners/angela`, asked about ANIMALS:
277
+ *
278
+ * ```
279
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
280
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
281
+ * ```
282
+ *
283
+ * The OWNERS filter, which returns `true` for animal 21 -- the record hidden
284
+ * on every animal surface. Under a mount that predicate recognises neither
285
+ * way it falls through to `['read', 'create', 'update', 'delete']`, a full
286
+ * CRUD grant. Either way: context supplied, answer not the animal answer,
287
+ * wrong in the GRANTING direction, because that predicate is arity-1 and
288
+ * identifies its collection from the request. AC9 asserts the first case on a
289
+ * live dispatch.
290
+ *
291
+ * Every predicate in this repo and in every consumer tree is arity-1 today,
292
+ * and there is no supported way for the caller to tell which kind it got; the
293
+ * boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
294
+ * the context, and do not treat a resolved predicate's answer as
295
+ * model-specific until that predicate reads it.
263
296
  *
264
297
  * OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
265
298
  * prototype chain, so `getAccess('constructor')` resolved `Object` and
@@ -18,12 +18,26 @@
18
18
  *
19
19
  * context.operation The operation being authorised. Exactly one of the four
20
20
  * verbs `'read'`, `'create'`, `'update'`, `'delete'` --
21
- * there is no second vocabulary, and it is never an HTTP
21
+ * no second vocabulary ON THIS PATH, and never an HTTP
22
22
  * method name like `'GET'`. These are the same four
23
23
  * strings the permission-array return shape is written in
24
24
  * (`['read', 'create']`), because both come from the one
25
25
  * `methodAccessMap` below.
26
26
  *
27
+ * NOT the hook vocabulary. `HookContext.operation`
28
+ * (`src/hooks.ts`, documented under "Hook Context Object"
29
+ * in the README) carries `'list' | 'get' | 'create' |
30
+ * 'update' | 'delete'` on an identically-named key of an
31
+ * identically-shaped context object, and the access
32
+ * vocabulary collapses `list` and `get` into `'read'`. For
33
+ * one `GET /animals/1` a hook sees `'get'` and `access()`
34
+ * sees `'read'`, so a predicate cannot tell a collection
35
+ * read from a record read. `AccessOperation` makes
36
+ * `operation === 'get'` a compile error for a TypeScript
37
+ * consumer, because a predicate that stops matching falls
38
+ * through to the permission array -- the misreading is
39
+ * fail-open shaped.
40
+ *
27
41
  * `undefined` when the dispatched method has no entry in
28
42
  * that map. Express delivers `HEAD` to the `GET` handler,
29
43
  * so this is reachable. It is left undefined rather than
@@ -35,6 +49,26 @@
35
49
  * is no string to parse, no variant to miss, and no way to fail open through a
36
50
  * URL shape nobody anticipated.
37
51
  *
52
+ * WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
53
+ * the verb, not the route. Measured over the live router, six surfaces produce
54
+ * one identical context:
55
+ *
56
+ * GET /owners { model: 'owner', operation: 'read' }
57
+ * GET /owners/gina { model: 'owner', operation: 'read' }
58
+ * GET /owners/gina/pets { model: 'owner', operation: 'read' }
59
+ * GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
60
+ * GET /owners/archived { model: 'owner', operation: 'read' }
61
+ * GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
62
+ *
63
+ * So a rule that depends on the SUB-PATH still needs `request.path` -- which is
64
+ * mount-relative and query-free, and is the one read of argument one the
65
+ * warning below sanctions. This repo's own fixture has such a rule: its
66
+ * `/archived` deny cannot be expressed from the context alone, and a predicate
67
+ * migrated to context-only would silently drop it, turning a deny into an
68
+ * allow. The related-resource and `?include=` surfaces serve ANOTHER model's
69
+ * records under `model: 'owner'`, and the context gives no signal of that
70
+ * (abofs/stonyx-orm#196).
71
+ *
38
72
  * `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
39
73
  * matching but BEFORE any handler executes (`@stonyx/rest-server`
40
74
  * `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
@@ -53,9 +87,37 @@
53
87
  * an owners route -- use the boot-time registry:
54
88
  *
55
89
  * const predicate = Orm.instance.getAccess('animal');
56
- * const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
90
+ * if (!predicate) return deny;
91
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
92
+ *
93
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
94
+ * includes the case where the model has an access class that failed to load,
95
+ * because `setup-rest-server.ts` catches a load failure, warns, and publishes
96
+ * whatever partial map it had. It does NOT mean the model is unrestricted.
97
+ * Treat it as DENY, the same way `operation === undefined` is treated above.
98
+ *
99
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
100
+ * the answer model-correct on its own -- the resolved predicate has to READ it.
101
+ * Measured against this repo's own shipped access class, on a request express
102
+ * dispatched to `GET /owners/angela`, asked about ANIMALS:
103
+ *
104
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
105
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
106
+ *
107
+ * That is the OWNERS filter, and it returns `true` for animal 21 -- the record
108
+ * hidden on every animal surface. Under a mount that predicate recognises
109
+ * neither way it is worse: it falls through to
110
+ * `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
111
+ * context was supplied and the answer is not the animal answer, and it is wrong
112
+ * in the GRANTING direction, because that predicate is arity-1 and identifies
113
+ * its collection from the request. (The first of these is asserted on a live
114
+ * dispatch by AC9 in test/integration/orm-test.ts.)
57
115
  *
58
- * Passing the context explicitly is what makes that answer model-CORRECT.
116
+ * Every predicate in this repo and in every consumer tree is arity-1 on the day
117
+ * this ships, and the caller has no supported way to tell which kind it got --
118
+ * the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
119
+ * So: pass the context, and do not treat a resolved predicate's answer as
120
+ * model-specific until that predicate has been migrated to read the context.
59
121
  *
60
122
  * ---------------------------------------------------------------------------
61
123
  * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.