Repository navigation
Expand file tree
/
Copy pathrule-hooks.ts
More file actions
502 lines (475 loc) · 25.3 KB
/
Copy pathrule-hooks.ts
File metadata and controls
502 lines (475 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type { SharingRuleService } from './sharing-rule-service.js';
import type { SharingRuleRow } from '@objectstack/spec/contracts';
import { isMatchAllCriteria, SharingCriteriaValidationError, SharingRuleObjectNotEnforcedError } from './rule-criteria.js';
import type { SharingService } from './sharing-service.js';
import { validationFailure } from '@objectstack/types';
import {
RULE_RECOMPUTE_ROW_CAP,
RuleRegrantQueue,
stashAffectedRows as stashAffectedRowsOnCtx,
readAffectedRows,
type AffectedRows,
} from './bulk-recompute.js';
const SYSTEM_CTX = { isSystem: true, positions: [], permissions: [] } as const;
export const SHARING_RULE_HOOK_PACKAGE = 'plugin-sharing:rules';
/**
* Package id for the `sys_sharing_rule` DATA-change triggers that re-run the
* bind (#2592). Deliberately distinct from {@link SHARING_RULE_HOOK_PACKAGE}
* so {@link unbindAllRuleHooks} — which the rebind itself calls — can never
* tear down the triggers that drive it.
*/
export const RULE_REBIND_TRIGGER_PACKAGE = 'plugin-sharing:rule-rebind';
/**
* Package id for the `sys_sharing_rule` criteria guard (#3896). Separate from
* both packages above so neither rebind can unregister it.
*/
export const RULE_CRITERIA_GUARD_PACKAGE = 'plugin-sharing:rule-criteria-guard';
/** Package id of {@link bindRuleObjectEnforcementGuard}'s hooks. */
export const RULE_OBJECT_ENFORCEMENT_GUARD_PACKAGE = 'plugin-sharing:rule-object-enforcement-guard';
/** Package id of {@link bindRuleRecipientGuard}'s hooks. */
export const RULE_RECIPIENT_GUARD_PACKAGE = 'plugin-sharing:rule-recipient-guard';
/*
* RETIRED — the `isSystem` materialisation skip, and with it the INFO line that
* announced it (`SYSTEM_WRITE_SKIP_NOTICE`, #6783 / #4707 demand 3).
*
* The notice existed because the skip was silent: a fresh install could carry
* active rules, matching records and an empty `sys_record_share`, and nothing
* said so. Its wording named the two remedies — re-evaluate the rules, or
* restart to backfill — and both were real. What made it obsolete is that the
* skip it described is gone, so there is no longer a silent window to announce.
*
* Maintainer ruling 2026-08-31 (verbatim, untranslated — an accepted decision
* is reversed only by a later one, AGENTS.md Prime Directive #13):
*
* 裁定:系统写参与逐记录共享物化 —— 删除 `plugin-sharing` 两个钩子里的
* `isSystem` 跳过,⛔ 不加声明式开关、不以文档代修。
*
* The reasoning that binds here: a sharing rule's declared semantics
* (`status == "approved"` implies the team can see it) is a published promise,
* and a hook that skipped the write which satisfies it made declared different
* from enforced. `isSystem` says the platform is the OPERATOR; it never says
* the consequence need not happen. The specimen was an approval write-back —
* a `lockRecord: true` node mirrors the decision onto the record under a system
* context (`plugin-approvals` `approval-service.ts` `mirrorStatusField`), which
* is the ONLY write that can land while the record is locked — after which a
* teammate relying on the rule could not see the record until somebody ran
* `POST /api/v1/sharing/rules/:id/evaluate` or restarted the server.
*
* The boot backfill in `sharing-plugin.ts` is unaffected and still needed: it
* covers rows written while a rule was inactive or before its hooks were bound,
* and it is the only pass that purges a deactivated rule's grants.
*/
interface MinimalEngine {
registerHook(event: string, handler: (ctx: any) => any | Promise<any>, options?: {
object?: string | string[];
priority?: number;
packageId?: string;
}): void;
unregisterHooksByPackage(packageId: string): number;
/**
* [#4779] Needed to resolve a predicate write's row set in the `before`
* hook. Optional so a caller holding only the hook-registry surface still
* type-checks; absent, every predicate write is treated as unbounded — the
* safe direction (revoke everything, re-grant asynchronously), never the
* silent no-op this issue was filed for.
*/
find?(object: string, options?: any): Promise<any[]>;
}
interface MinimalLogger {
info?: (msg: any, ...rest: any[]) => void;
/**
* Non-optional because this logger is FORWARDED into `stashAffectedRowsOnCtx`
* (bulk-recompute.ts), whose sink guarantees a `warn` channel under #9754. A
* `warn?` here would re-open the silence one module downstream of the place it
* was closed — the forwarding seam is exactly where a guarantee gets lost, and
* `tsc` reported it the moment the callee's contract tightened (#10556).
*
* This shape declares no `error` at all, so it is not itself in the
* optional-error-sink population; what it must not do is hand a
* silence-permitting value to something that promises otherwise.
*/
warn: (msg: any, ...rest: any[]) => void;
}
/**
* The in-process executor for the asynchronous re-grant half of #4779.
*
* Module-scoped on purpose: `bindRuleHooks` is called again on every rule
* rebind (`bindRuleRebindTriggers` unbinds and re-binds the whole package on
* each `sys_sharing_rule` write), and a per-call queue would let a rebind
* orphan re-grants that were still in flight. One queue per process keeps the
* chain — and therefore the serialization guarantee — continuous across
* rebinds. Exported for tests to await; production code never does.
*/
export const ruleRegrantQueue = new RuleRegrantQueue();
/**
* Bind the sharing-rule recompute hooks for every distinct object_name in
* `rules`. Everything runs with SYSTEM_CTX so the evaluator can write
* `sys_record_share` without being blocked by its own enforcement.
*
* Five hooks per object:
*
* - `afterInsert` — recompute the inserted row (unchanged behaviour).
* - `beforeUpdate` / `beforeDelete` — resolve the affected row set and stash
* it for the `after` half (`AFFECTED_ROWS_STASH_KEY`). Must be
* `before`: the write is what makes those rows unfindable. Runs for system
* writes too, and has to: `afterUpdate` now materialises for them, and with
* no stash `readAffectedRows` answers `unbounded` — which would send every
* single-row system update down the object-wide revoke branch below.
* [#6966] The stash rides `HookContext.dispatch.scope`, the engine's
* per-write scratch — NOT the context object. A predicate write dispatches
* `before*` per row (#5574) and builds a fresh context for each, so the
* older "the engine reuses one HookContext across the pair" assumption held
* only for single-id writes and silently dropped every bulk write's stash.
* - `afterUpdate` — recompute per row when the set is bounded (which grants
* AND revokes, so a bulk update INTO a rule's criteria is covered as well
* as one out of it); otherwise revoke the object's rule grants set-based
* and queue the re-grant.
* - `afterDelete` — revoke the deleted rows' rule grants. Nothing can
* re-grant them: `evaluateRule` iterates records that still exist, so a
* grant whose record is gone is unreachable by every reconcile path and
* outlives restarts (the orphan noted at the tail of #4779).
*
* [#5103] This package covers `source: 'rule'` rows only, and only on objects
* that HAVE a rule — which left a manual share on a deleted record orphaned
* forever. The general invariant ("the record is gone, so no share on it can
* be valid") is not the rule subsystem's to enforce and now lives in
* `record-share-cascade.ts`, bound on every sharing-capable object regardless
* of rules. The two are deliberately independent: this one keeps working if
* the cascade is unbound, and its unbounded-delete branch (revoke the object's
* rule grants, re-grant asynchronously) is a rule-only trade the cascade must
* never make on manual rows.
*
* [#4779] `if (!id) return` — the line these hooks used to open with — is
* gone. It read as a cheap guard and was in fact the whole defect: predicate
* (`multi: true`) writes never populate `input.id`, so every bulk write
* skipped recompute entirely and left stale `sys_record_share` rows granting
* access the rules no longer imply.
*
* [#13533] GRANT MATERIALISATION no longer asks whether the writer is the
* platform. The `afterInsert` / `afterUpdate` skips on `session.isSystem` — and
* the `before*` stash skip they depended on — are gone, so a system write
* materialises exactly as a user write does. See the retirement note at the top
* of this file for the ruling and the specimen. `afterDelete` still skips, on
* its own separate grounds, spelled out at that hook.
*
* Caller is responsible for invoking {@link unbindAllRuleHooks} before
* re-binding when the rule set changes.
*/
export function bindRuleHooks(
engine: MinimalEngine,
service: SharingRuleService,
rules: ReadonlyArray<Pick<SharingRuleRow, 'object_name' | 'active'>>,
logger?: MinimalLogger,
): void {
const objects = new Set<string>();
for (const r of rules) {
if (r.active === false) continue;
if (!r.object_name) continue;
objects.add(r.object_name);
}
for (const objectName of objects) {
const opts = { object: objectName, packageId: SHARING_RULE_HOOK_PACKAGE, priority: 180 };
/** Recompute one record; never throws (a hook must not fail the write). */
const recomputeRow = async (id: string): Promise<void> => {
await service.evaluateAllForRecord(objectName, id, SYSTEM_CTX as any);
};
/**
* The unbounded branch: revoke now, re-grant later. Ordered so that the
* process can die between the two and still be safe — the grants are
* already gone, and the `kernel:bootstrapped` backfill re-grants on the
* next start.
*/
const revokeThenQueueRegrant = async (reason: string): Promise<void> => {
await service.revokeRuleGrantsForObject(objectName);
logger?.warn?.(
'[sharing-rule] a bulk write touched more rows than can be recomputed inline — every rule grant on ' +
'this object was revoked and is being re-granted in the background; recipients may briefly lose ' +
'access to records they still qualify for (a restart re-runs the same reconcile)',
{ object: objectName, reason, cap: RULE_RECOMPUTE_ROW_CAP },
);
ruleRegrantQueue.enqueue(
() => service.evaluateAllRulesForObject(objectName).then(() => undefined),
(err: any) => logger?.warn?.(
'[sharing-rule] background re-grant failed — grants stay revoked until the next reconcile ' +
'(any sharing-rule write, or a restart)',
{ object: objectName, error: err?.message },
),
);
};
/**
* [#5103] Delegates to the shared stash: the record-delete cascade binds
* its own `beforeDelete` on the same objects, and whichever of the two runs
* first resolves the row set for both.
*
* [#13533] Unconditional. It used to skip system writes, on the reasoning
* that the recompute half left seeds to the boot backfill — which was true
* only while `afterUpdate` skipped them too. It no longer does, and an
* `after` hook with no stash does not read as "nothing changed": it reads
* as `unbounded` (`readAffectedRows`, deliberately), so the skip would have
* turned every single-row system update — the approval write-back the
* ruling is about — into an object-wide revoke plus an asynchronous
* re-grant. Resolving is also nearly free on that shape: a write that names
* its row short-circuits in `resolveAffectedRows` step 1 without querying.
*/
const stashAffectedRows = async (ctx: any) => {
await stashAffectedRowsOnCtx(engine, objectName, ctx, logger);
};
/** What the `after` hook should act on when no `before` hook ran. */
const affectedFrom = (ctx: any): AffectedRows => readAffectedRows(ctx);
/**
* [#6966] Has this write's `after` work already been done by an earlier row
* of the same fan-out?
*
* What the `after` hooks below act on is the WRITE's row set, not the row
* they happen to be dispatched for — `affectedFrom` returns the whole
* union, and both branches (per-row recompute, object-wide revoke) are
* batch-scoped. A predicate write dispatches them once per matched row, so
* running them unguarded does the batch's work N times: N identical
* object-wide revokes on the unbounded branch, and N×N `recomputeRow` calls
* on the bounded one — quadratic in the batch size, which for a write at
* the cap is a million recomputes for a thousand rows.
*/
const alreadyHandledThisWrite = (ctx: any): boolean =>
ctx?.dispatch?.mode === 'per-row' && ctx.dispatch.index !== 0;
engine.registerHook('afterInsert', async (ctx: any) => {
try {
const data = ctx?.result ?? ctx?.input?.data ?? {};
const id = String((data as any)?.id ?? ctx?.input?.id ?? '');
if (!id) return;
await recomputeRow(id);
} catch (err: any) {
logger?.warn?.('[sharing-rule] hook evaluation failed', { object: objectName, error: err?.message });
}
}, opts);
engine.registerHook('beforeUpdate', stashAffectedRows, opts);
engine.registerHook('beforeDelete', stashAffectedRows, opts);
engine.registerHook('afterUpdate', async (ctx: any) => {
// [#13533] An `isSystem` update INTO a rule's criteria owes grants the
// same way an insert does — that update IS the approval write-back — and
// `evaluateRule` is diff-based, so one out of the criteria revokes just
// as symmetrically. Neither direction asks who wrote the row any more.
if (alreadyHandledThisWrite(ctx)) return;
try {
const affected = affectedFrom(ctx);
if (affected.kind === 'rows') {
for (const id of affected.ids) await recomputeRow(id);
return;
}
await revokeThenQueueRegrant(affected.reason);
} catch (err: any) {
logger?.warn?.('[sharing-rule] hook evaluation failed', { object: objectName, error: err?.message });
}
}, opts);
engine.registerHook('afterDelete', async (ctx: any) => {
// [#13533] The one system-write skip that SURVIVED the ruling, because
// what it skips is a different payload. The insert/update skips dropped
// MATERIALISATION — a grant the rule's own declared semantics promised,
// with nothing else on the write path to deliver it. A delete skips
// REVOCATION, and that consequence is delivered anyway: the general
// invariant "the record is gone, so no share on it can be valid" belongs
// to `record-share-cascade.ts`, which binds on every sharing-capable
// object and stashes for system writes on its own account (#5103), with
// the boot orphan sweep behind it. So this is not `isSystem` used as a
// blanket silencer; it is one subscriber declining work another
// subscriber owns. Removing it would double-revoke, and the rule-only
// trade below (revoke the object's grants, re-grant asynchronously) is
// one the cascade must never make on manual rows.
if ((ctx?.session as any)?.isSystem) return;
if (alreadyHandledThisWrite(ctx)) return;
try {
const affected = affectedFrom(ctx);
if (affected.kind === 'rows') {
await service.revokeRuleGrantsForRecords(objectName, affected.ids);
return;
}
// The deleted set is unknown, so which grants are orphaned is unknown
// too. Revoke them all and let the re-grant restore those whose record
// survived — the same trade the update path makes.
await revokeThenQueueRegrant(affected.reason);
} catch (err: any) {
logger?.warn?.('[sharing-rule] hook evaluation failed', { object: objectName, error: err?.message });
}
}, opts);
}
logger?.info?.('[sharing-rule] hooks bound', { objects: Array.from(objects) });
}
/**
* Unbind the rule-hook package and bind it again on
* {@link SharingRuleService.listRuleHookObjects} — how the plugin binds the
* per-record rule hooks, at boot and on every `sys_sharing_rule` rebind
* (`SharingServicePlugin`'s private `bindRuleHooks`), so the two can never
* derive different object sets.
*
* The object set is read from the rules' DECLARATIONS and rows, never from the
* organizations a declared rule is served in right now: the binding is per
* object and organization-agnostic, and the organization is resolved when a
* record is evaluated. An organization created after the bind is therefore
* served by the hooks already bound, with no rebind and no restart.
*
* The read runs before the unbind, so a failed read leaves the previous
* bindings in place.
*/
export async function rebindRuleHooks(
engine: MinimalEngine,
service: SharingRuleService,
logger?: MinimalLogger,
): Promise<void> {
const objects = await service.listRuleHookObjects();
unbindAllRuleHooks(engine);
bindRuleHooks(engine, service, objects.map((object_name) => ({ object_name, active: true })), logger);
}
export function unbindAllRuleHooks(engine: MinimalEngine): number {
return engine.unregisterHooksByPackage(SHARING_RULE_HOOK_PACKAGE);
}
/**
* [#3896] Reject `sys_sharing_rule` writes whose criteria would share every
* record of the target object.
*
* `SharingRuleService.defineRule` gates the programmatic + REST
* (`POST {basePath}/sharing/rules`) entries, but authoring a rule in Setup is
* a plain data INSERT on this table — it never reaches that method. Without
* this hook the UI path keeps producing rules the evaluator now refuses to
* act on: safe, but silently inert, which is its own authoring trap
* (ADR-0078). Failing the write instead tells the admin the criteria is
* missing while they are still looking at the form.
*
* LAYERING since ADR-0113: the field now declares `required: true` (write
* contract), so the record validator already rejects a missing/null criteria
* on insert and an explicit null-out on update — with the same non-regression
* update semantics this hook pioneered (only a patch that SUPPLIES
* `criteria_json` is checked; a legacy null row can still be `active: false`d).
* This hook remains for what `required` cannot express: the NON-null
* match-all shapes — `'{}'`, an all-vacuous `$and`/`$or`, unparsable JSON, a
* bare scalar — where `isMatchAllCriteria` is the judge and the rejection
* names the offending shape instead of saying "required". Defense in depth on
* the null cases costs one property check and keeps this guard's better
* message on paths where hooks fire before validation.
*/
export function bindRuleCriteriaGuard(engine: MinimalEngine, logger?: MinimalLogger): void {
if (typeof engine.registerHook !== 'function') return;
if (typeof engine.unregisterHooksByPackage === 'function') {
engine.unregisterHooksByPackage(RULE_CRITERIA_GUARD_PACKAGE);
}
const guard = (insert: boolean) => (ctx: any) => {
const data = ctx?.input?.data;
if (!data || typeof data !== 'object' || Array.isArray(data)) return;
const supplied = Object.prototype.hasOwnProperty.call(data, 'criteria_json');
if (!insert && !supplied) return;
if (!isMatchAllCriteria(data.criteria_json)) return;
throw new SharingCriteriaValidationError();
};
const opts = { object: 'sys_sharing_rule', packageId: RULE_CRITERIA_GUARD_PACKAGE, priority: 100 };
engine.registerHook('beforeInsert', guard(true), opts);
engine.registerHook('beforeUpdate', guard(false), opts);
logger?.info?.('[sharing-rule] criteria guard bound on sys_sharing_rule (ADR-0049)');
}
/**
* [#30621] Refuse a `sys_sharing_rule` write whose object is not under
* record-sharing enforcement.
*
* Before, such a rule saved (`201`) and its reconcile then met the ADR-0111 D7
* inert-grant guard and wrote no `sys_record_share`: the rule sat in Setup
* looking active and never applied. The verdict here is the reconcile's own
* ({@link SharingService.sharingRuleObjectRefusal}), so the two cannot disagree.
*
* An insert is checked. An update is checked only when it MOVES the rule onto
* another object (`object_name` supplied and different from the pre-image),
* so an administrator can still switch off a rule saved before this guard.
* System writes pass, as with the declared-name guard: `defineRule` makes the
* same check itself for its non-system callers, and the remaining system
* writers are the platform's own machinery.
*/
export function bindRuleObjectEnforcementGuard(
engine: MinimalEngine,
sharing: Pick<SharingService, 'sharingRuleObjectRefusal'>,
): void {
if (typeof engine.registerHook !== 'function') return;
if (typeof engine.unregisterHooksByPackage === 'function') {
engine.unregisterHooksByPackage(RULE_OBJECT_ENFORCEMENT_GUARD_PACKAGE);
}
const guard = (insert: boolean) => (ctx: any) => {
if ((ctx?.session as any)?.isSystem) return;
const data = ctx?.input?.data;
if (!data || typeof data !== 'object' || Array.isArray(data)) return;
const object = data.object_name;
if (typeof object !== 'string' || object.length === 0) return;
if (!insert) {
const previous = ctx?.previous as Record<string, unknown> | undefined;
if (previous && typeof previous === 'object' && previous.object_name === object) return;
}
const refusal = sharing.sharingRuleObjectRefusal(object);
if (refusal) throw new SharingRuleObjectNotEnforcedError(object, refusal);
};
const opts = { object: 'sys_sharing_rule', packageId: RULE_OBJECT_ENFORCEMENT_GUARD_PACKAGE, priority: 100 };
engine.registerHook('beforeInsert', guard(true), opts);
engine.registerHook('beforeUpdate', guard(false), opts);
}
/**
* [#38254] Refuse a `sys_sharing_rule` write whose `user` recipient names no
* user the rule could share with.
*
* `SharingRuleService.defineRule` refuses such a recipient (#33912), but the
* Setup sharing-rule form writes through the `/data` door, a plain write on
* this table that never reaches that method. Before this guard that door
* stored the rule: it granted no one access, and the author was told it
* saved. The verdict and the sentence are `defineRule`'s own
* ({@link SharingService.userRecipientRefusal}, subject `rule`), so the two
* doors cannot disagree, and the refusal is `VALIDATION_FAILED` (400) under
* `recipient_id`.
*
* Judged in the organization the write leaves the rule in: the
* `organization_id` the write supplies (on an update, a re-point the tenant
* wall admitted), else the stored row's, else the caller's active
* organization (the one the engine stamps on a new row; the Setup form
* posts none). An id that names no
* user and a user of another organization are answered alike, so the door
* is no oracle for who exists in another tenant. With no organization under
* a wall the verdict stands aside, as it does for `defineRule`: the engine
* refuses that insert for every recipient alike.
*
* An insert with a `user` recipient is judged. An update is judged only when
* it leaves a `user` recipient that differs from the pre-image (type or id),
* so a rule whose user has since left can still be switched off or edited
* otherwise. System writes pass, as with the object guard: `defineRule`
* judges its own non-system callers, and the remaining system writers are
* the platform's own machinery.
*/
export function bindRuleRecipientGuard(
engine: MinimalEngine,
sharing: Pick<SharingService, 'userRecipientRefusal'>,
): void {
if (typeof engine.registerHook !== 'function') return;
if (typeof engine.unregisterHooksByPackage === 'function') {
engine.unregisterHooksByPackage(RULE_RECIPIENT_GUARD_PACKAGE);
}
const present = (v: unknown): string | null => (typeof v === 'string' && v.length > 0 ? v : null);
const guard = (insert: boolean) => async (ctx: any) => {
if ((ctx?.session as any)?.isSystem) return;
const data = ctx?.input?.data;
if (!data || typeof data !== 'object' || Array.isArray(data)) return;
const previous = !insert && ctx?.previous && typeof ctx.previous === 'object'
? ctx.previous as Record<string, unknown>
: undefined;
// A key present with the value `undefined` leaves the column alone (the tenant wall reads it the same way), so
// it is not a supplied value: the stored one stands and is the one judged.
const has = (key: string) => Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined;
if (!insert && !has('recipient_type') && !has('recipient_id')) return;
const recipientType = has('recipient_type') ? data.recipient_type : previous?.recipient_type;
if (recipientType !== 'user') return;
const recipientId = present(has('recipient_id') ? data.recipient_id : previous?.recipient_id);
// A missing id is the record validator's to answer (`required`), not this guard's.
if (!recipientId) return;
if (previous && previous.recipient_type === 'user' && previous.recipient_id === recipientId) return;
const organizationId = present(data.organization_id)
?? present(previous?.organization_id)
?? present(ctx?.session?.organizationId);
const refusal = await sharing.userRecipientRefusal(recipientId, organizationId, 'rule');
if (refusal) {
throw validationFailure(refusal, [{ field: 'recipient_id', code: 'invalid_value', message: refusal }]);
}
};
const opts = { object: 'sys_sharing_rule', packageId: RULE_RECIPIENT_GUARD_PACKAGE, priority: 100 };
engine.registerHook('beforeInsert', guard(true), opts);
engine.registerHook('beforeUpdate', guard(false), opts);
}