@venizia/ignis-connectors 0.4.1 → 0.4.3
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 +2 -2
- package/dist/cjs/common/scope-filter.d.ts +1 -1
- package/dist/cjs/common/scope-filter.d.ts.map +1 -1
- package/dist/cjs/common/scope-filter.js +33 -7
- package/dist/cjs/common/scope-filter.js.map +1 -1
- package/dist/cjs/http/common/constants.d.ts +11 -0
- package/dist/cjs/http/common/constants.d.ts.map +1 -0
- package/dist/cjs/http/common/constants.js +14 -0
- package/dist/cjs/http/common/constants.js.map +1 -0
- package/dist/cjs/http/common/wire.d.ts +2 -1
- package/dist/cjs/http/common/wire.d.ts.map +1 -1
- package/dist/cjs/http/common/wire.js +2 -1
- package/dist/cjs/http/common/wire.js.map +1 -1
- package/dist/cjs/http/datasource.d.ts +2 -1
- package/dist/cjs/http/datasource.d.ts.map +1 -1
- package/dist/cjs/http/datasource.js +2 -1
- package/dist/cjs/http/datasource.js.map +1 -1
- package/dist/cjs/http/index.d.ts +1 -0
- package/dist/cjs/http/index.d.ts.map +1 -1
- package/dist/cjs/http/index.js +1 -0
- package/dist/cjs/http/index.js.map +1 -1
- package/dist/cjs/http/repository.d.ts.map +1 -1
- package/dist/cjs/http/repository.js +5 -4
- package/dist/cjs/http/repository.js.map +1 -1
- package/dist/cjs/relational/core/repositories/common/constants.d.ts +11 -0
- package/dist/cjs/relational/core/repositories/common/constants.d.ts.map +1 -0
- package/dist/cjs/relational/core/repositories/common/constants.js +14 -0
- package/dist/cjs/relational/core/repositories/common/constants.js.map +1 -0
- package/dist/cjs/relational/core/repositories/common/index.d.ts +1 -0
- package/dist/cjs/relational/core/repositories/common/index.d.ts.map +1 -1
- package/dist/cjs/relational/core/repositories/common/index.js +1 -0
- package/dist/cjs/relational/core/repositories/common/index.js.map +1 -1
- package/dist/cjs/relational/core/repositories/common/scope-write-guard.d.ts +71 -0
- package/dist/cjs/relational/core/repositories/common/scope-write-guard.d.ts.map +1 -0
- package/dist/cjs/relational/core/repositories/common/scope-write-guard.js +377 -0
- package/dist/cjs/relational/core/repositories/common/scope-write-guard.js.map +1 -0
- package/dist/cjs/relational/core/repositories/core/base.d.ts +22 -4
- package/dist/cjs/relational/core/repositories/core/base.d.ts.map +1 -1
- package/dist/cjs/relational/core/repositories/core/base.js +49 -10
- package/dist/cjs/relational/core/repositories/core/base.js.map +1 -1
- package/dist/cjs/relational/core/repositories/core/persistable.d.ts.map +1 -1
- package/dist/cjs/relational/core/repositories/core/persistable.js +8 -1
- package/dist/cjs/relational/core/repositories/core/persistable.js.map +1 -1
- package/dist/cjs/relational/core/repositories/dialect/filter.d.ts +2 -2
- package/dist/cjs/relational/core/repositories/dialect/filter.js +3 -3
- package/dist/cjs/relational/core/repositories/dialect/filter.js.map +1 -1
- package/dist/cjs/relational/core/repositories/sqls/recursive-tree.d.ts +9 -0
- package/dist/cjs/relational/core/repositories/sqls/recursive-tree.d.ts.map +1 -1
- package/dist/cjs/relational/core/repositories/sqls/recursive-tree.js +14 -3
- package/dist/cjs/relational/core/repositories/sqls/recursive-tree.js.map +1 -1
- package/dist/cjs/relational/postgres/models/common/constants.d.ts +11 -0
- package/dist/cjs/relational/postgres/models/common/constants.d.ts.map +1 -0
- package/dist/cjs/relational/postgres/models/common/constants.js +14 -0
- package/dist/cjs/relational/postgres/models/common/constants.js.map +1 -0
- package/dist/cjs/relational/postgres/models/common/index.d.ts +1 -0
- package/dist/cjs/relational/postgres/models/common/index.d.ts.map +1 -1
- package/dist/cjs/relational/postgres/models/common/index.js +1 -0
- package/dist/cjs/relational/postgres/models/common/index.js.map +1 -1
- package/dist/cjs/relational/postgres/models/common/types/enrichers.d.ts +5 -3
- package/dist/cjs/relational/postgres/models/common/types/enrichers.d.ts.map +1 -1
- package/dist/cjs/relational/postgres/models/common/types/enrichers.js.map +1 -1
- package/dist/cjs/relational/postgres/models/enrichers/principal.enricher.d.ts +4 -3
- package/dist/cjs/relational/postgres/models/enrichers/principal.enricher.d.ts.map +1 -1
- package/dist/cjs/relational/postgres/models/enrichers/principal.enricher.js.map +1 -1
- package/dist/cjs/relational/sqlite/models/common/types/enrichers.d.ts +3 -2
- package/dist/cjs/relational/sqlite/models/common/types/enrichers.d.ts.map +1 -1
- package/dist/cjs/relational/sqlite/models/common/types/enrichers.js.map +1 -1
- package/dist/cjs/relational/sqlite/models/enrichers/principal.enricher.d.ts +4 -3
- package/dist/cjs/relational/sqlite/models/enrichers/principal.enricher.d.ts.map +1 -1
- package/dist/cjs/relational/sqlite/models/enrichers/principal.enricher.js.map +1 -1
- package/dist/cjs/search/core/controllers/abstract.d.ts +4 -0
- package/dist/cjs/search/core/controllers/abstract.d.ts.map +1 -1
- package/dist/cjs/search/core/controllers/abstract.js +15 -1
- package/dist/cjs/search/core/controllers/abstract.js.map +1 -1
- package/dist/cjs/search/core/controllers/factory.d.ts +15 -1
- package/dist/cjs/search/core/controllers/factory.d.ts.map +1 -1
- package/dist/cjs/search/core/controllers/factory.js +7 -1
- package/dist/cjs/search/core/controllers/factory.js.map +1 -1
- package/dist/cjs/search/core/controllers/index.d.ts +1 -0
- package/dist/cjs/search/core/controllers/index.d.ts.map +1 -1
- package/dist/cjs/search/core/controllers/index.js.map +1 -1
- package/dist/cjs/search/core/controllers/scope.d.ts +28 -0
- package/dist/cjs/search/core/controllers/scope.d.ts.map +1 -0
- package/dist/cjs/search/core/controllers/scope.js +68 -0
- package/dist/cjs/search/core/controllers/scope.js.map +1 -0
- package/dist/esm/common/scope-filter.d.ts +1 -1
- package/dist/esm/common/scope-filter.d.ts.map +1 -1
- package/dist/esm/common/scope-filter.js +34 -8
- package/dist/esm/common/scope-filter.js.map +1 -1
- package/dist/esm/http/common/constants.d.ts +11 -0
- package/dist/esm/http/common/constants.d.ts.map +1 -0
- package/dist/esm/http/common/constants.js +10 -0
- package/dist/esm/http/common/constants.js.map +1 -0
- package/dist/esm/http/common/wire.d.ts +2 -1
- package/dist/esm/http/common/wire.d.ts.map +1 -1
- package/dist/esm/http/common/wire.js +2 -1
- package/dist/esm/http/common/wire.js.map +1 -1
- package/dist/esm/http/datasource.d.ts +2 -1
- package/dist/esm/http/datasource.d.ts.map +1 -1
- package/dist/esm/http/datasource.js +2 -1
- package/dist/esm/http/datasource.js.map +1 -1
- package/dist/esm/http/index.d.ts +1 -0
- package/dist/esm/http/index.d.ts.map +1 -1
- package/dist/esm/http/index.js +1 -0
- package/dist/esm/http/index.js.map +1 -1
- package/dist/esm/http/repository.d.ts.map +1 -1
- package/dist/esm/http/repository.js +5 -4
- package/dist/esm/http/repository.js.map +1 -1
- package/dist/esm/relational/core/repositories/common/constants.d.ts +11 -0
- package/dist/esm/relational/core/repositories/common/constants.d.ts.map +1 -0
- package/dist/esm/relational/core/repositories/common/constants.js +10 -0
- package/dist/esm/relational/core/repositories/common/constants.js.map +1 -0
- package/dist/esm/relational/core/repositories/common/index.d.ts +1 -0
- package/dist/esm/relational/core/repositories/common/index.d.ts.map +1 -1
- package/dist/esm/relational/core/repositories/common/index.js +1 -0
- package/dist/esm/relational/core/repositories/common/index.js.map +1 -1
- package/dist/esm/relational/core/repositories/common/scope-write-guard.d.ts +71 -0
- package/dist/esm/relational/core/repositories/common/scope-write-guard.d.ts.map +1 -0
- package/dist/esm/relational/core/repositories/common/scope-write-guard.js +373 -0
- package/dist/esm/relational/core/repositories/common/scope-write-guard.js.map +1 -0
- package/dist/esm/relational/core/repositories/core/base.d.ts +22 -4
- package/dist/esm/relational/core/repositories/core/base.d.ts.map +1 -1
- package/dist/esm/relational/core/repositories/core/base.js +49 -10
- package/dist/esm/relational/core/repositories/core/base.js.map +1 -1
- package/dist/esm/relational/core/repositories/core/persistable.d.ts.map +1 -1
- package/dist/esm/relational/core/repositories/core/persistable.js +8 -1
- package/dist/esm/relational/core/repositories/core/persistable.js.map +1 -1
- package/dist/esm/relational/core/repositories/dialect/filter.d.ts +2 -2
- package/dist/esm/relational/core/repositories/dialect/filter.js +3 -3
- package/dist/esm/relational/core/repositories/dialect/filter.js.map +1 -1
- package/dist/esm/relational/core/repositories/sqls/recursive-tree.d.ts +9 -0
- package/dist/esm/relational/core/repositories/sqls/recursive-tree.d.ts.map +1 -1
- package/dist/esm/relational/core/repositories/sqls/recursive-tree.js +15 -4
- package/dist/esm/relational/core/repositories/sqls/recursive-tree.js.map +1 -1
- package/dist/esm/relational/postgres/models/common/constants.d.ts +11 -0
- package/dist/esm/relational/postgres/models/common/constants.d.ts.map +1 -0
- package/dist/esm/relational/postgres/models/common/constants.js +10 -0
- package/dist/esm/relational/postgres/models/common/constants.js.map +1 -0
- package/dist/esm/relational/postgres/models/common/index.d.ts +1 -0
- package/dist/esm/relational/postgres/models/common/index.d.ts.map +1 -1
- package/dist/esm/relational/postgres/models/common/index.js +1 -0
- package/dist/esm/relational/postgres/models/common/index.js.map +1 -1
- package/dist/esm/relational/postgres/models/common/types/enrichers.d.ts +5 -3
- package/dist/esm/relational/postgres/models/common/types/enrichers.d.ts.map +1 -1
- package/dist/esm/relational/postgres/models/common/types/enrichers.js.map +1 -1
- package/dist/esm/relational/postgres/models/enrichers/principal.enricher.d.ts +4 -3
- package/dist/esm/relational/postgres/models/enrichers/principal.enricher.d.ts.map +1 -1
- package/dist/esm/relational/postgres/models/enrichers/principal.enricher.js.map +1 -1
- package/dist/esm/relational/sqlite/models/common/types/enrichers.d.ts +3 -2
- package/dist/esm/relational/sqlite/models/common/types/enrichers.d.ts.map +1 -1
- package/dist/esm/relational/sqlite/models/common/types/enrichers.js.map +1 -1
- package/dist/esm/relational/sqlite/models/enrichers/principal.enricher.d.ts +4 -3
- package/dist/esm/relational/sqlite/models/enrichers/principal.enricher.d.ts.map +1 -1
- package/dist/esm/relational/sqlite/models/enrichers/principal.enricher.js.map +1 -1
- package/dist/esm/search/core/controllers/abstract.d.ts +4 -0
- package/dist/esm/search/core/controllers/abstract.d.ts.map +1 -1
- package/dist/esm/search/core/controllers/abstract.js +15 -1
- package/dist/esm/search/core/controllers/abstract.js.map +1 -1
- package/dist/esm/search/core/controllers/factory.d.ts +15 -1
- package/dist/esm/search/core/controllers/factory.d.ts.map +1 -1
- package/dist/esm/search/core/controllers/factory.js +7 -1
- package/dist/esm/search/core/controllers/factory.js.map +1 -1
- package/dist/esm/search/core/controllers/index.d.ts +1 -0
- package/dist/esm/search/core/controllers/index.d.ts.map +1 -1
- package/dist/esm/search/core/controllers/index.js.map +1 -1
- package/dist/esm/search/core/controllers/scope.d.ts +28 -0
- package/dist/esm/search/core/controllers/scope.d.ts.map +1 -0
- package/dist/esm/search/core/controllers/scope.js +64 -0
- package/dist/esm/search/core/controllers/scope.js.map +1 -0
- package/package.json +6 -6
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScopeWriteGuard = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const core_1 = require("@venizia/ignis-helpers/core");
|
|
6
|
+
const ignis_kernel_1 = require("@venizia/ignis-kernel");
|
|
7
|
+
const common_1 = require("@venizia/ignis-helpers/common");
|
|
8
|
+
const scope_filter_1 = require("./scope-filter");
|
|
9
|
+
const WRITE_DENIED_STATUS = common_1.HTTP.ResultCodes.RS_4.Forbidden;
|
|
10
|
+
const isScalar = (value) => {
|
|
11
|
+
return (value === null ||
|
|
12
|
+
value instanceof Date ||
|
|
13
|
+
['string', 'number', 'bigint', 'boolean'].includes(typeof value));
|
|
14
|
+
};
|
|
15
|
+
const isRecord = (value) => {
|
|
16
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
17
|
+
};
|
|
18
|
+
const normalise = (value) => {
|
|
19
|
+
if (value === null) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return value instanceof Date ? value.toISOString() : String(value);
|
|
23
|
+
};
|
|
24
|
+
const intersect = (opts) => {
|
|
25
|
+
return new Set([...opts.left].filter((value) => opts.right.has(value)));
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The write side of `@model` settings.access. The resolved scope is a `where`; the guard reads
|
|
29
|
+
* from it the columns it constrains and the values it allows, and holds every written row to them.
|
|
30
|
+
*
|
|
31
|
+
* Only shapes that can be checked are read: a column equal to a scalar or `null`, `{ eq }`, `{ inq }`
|
|
32
|
+
* (or `in`), an array, `and` of those, and an `or` whose branches all constrain the same column. Anything
|
|
33
|
+
* else - an `or` over several columns, ranges, `like`, a nested path - makes
|
|
34
|
+
* the scope unverifiable, and the write is refused with its own message instead of being skipped.
|
|
35
|
+
*/
|
|
36
|
+
class ScopeWriteGuard {
|
|
37
|
+
static extractConstraints(opts) {
|
|
38
|
+
const constraints = new Map();
|
|
39
|
+
const reason = ScopeWriteGuard.collect({
|
|
40
|
+
where: opts.where,
|
|
41
|
+
constraints,
|
|
42
|
+
});
|
|
43
|
+
return reason === undefined
|
|
44
|
+
? { isVerifiable: true, constraints }
|
|
45
|
+
: { isVerifiable: false, reason };
|
|
46
|
+
}
|
|
47
|
+
/** Adds the where's constraints to `constraints`; returns the reason it cannot be verified, if any. */
|
|
48
|
+
static collect(opts) {
|
|
49
|
+
const { where, constraints } = opts;
|
|
50
|
+
for (const [key, value] of Object.entries(where)) {
|
|
51
|
+
if (key === 'and') {
|
|
52
|
+
if (!Array.isArray(value)) {
|
|
53
|
+
return "'and' is not a list";
|
|
54
|
+
}
|
|
55
|
+
for (const part of value) {
|
|
56
|
+
const reason = ScopeWriteGuard.collect({
|
|
57
|
+
where: part,
|
|
58
|
+
constraints,
|
|
59
|
+
});
|
|
60
|
+
if (reason !== undefined) {
|
|
61
|
+
return reason;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (key === 'or') {
|
|
67
|
+
const union = ScopeWriteGuard.unionOfBranches({ branches: value });
|
|
68
|
+
if (typeof union === 'string') {
|
|
69
|
+
return union;
|
|
70
|
+
}
|
|
71
|
+
const existing = constraints.get(union.column);
|
|
72
|
+
constraints.set(union.column, existing ? intersect({ left: existing, right: union.allowed }) : union.allowed);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (key.includes('.')) {
|
|
76
|
+
return `the nested path '${key}' cannot be checked on a write`;
|
|
77
|
+
}
|
|
78
|
+
const allowed = ScopeWriteGuard.allowedValues({ value });
|
|
79
|
+
if (typeof allowed === 'string') {
|
|
80
|
+
return `column '${key}': ${allowed}`;
|
|
81
|
+
}
|
|
82
|
+
const existing = constraints.get(key);
|
|
83
|
+
constraints.set(key, existing ? intersect({ left: existing, right: allowed }) : allowed);
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* An `or` is checkable only when every branch constrains the same single column with a scalar,
|
|
89
|
+
* `null`, `eq`, `in` or `inq`: the allowed values are then the union of the branches. This is the
|
|
90
|
+
* shared-rows scope, `{ or: [{ merchantId: { in: ids } }, { merchantId: null }] }`. Any other `or`
|
|
91
|
+
* (several columns, ranges, nested `and` or `or`) cannot be checked.
|
|
92
|
+
*/
|
|
93
|
+
static unionOfBranches(opts) {
|
|
94
|
+
const { branches } = opts;
|
|
95
|
+
if (!Array.isArray(branches) || branches.length === 0) {
|
|
96
|
+
return "'or' is not a list";
|
|
97
|
+
}
|
|
98
|
+
let column;
|
|
99
|
+
const allowed = new Set();
|
|
100
|
+
for (const branch of branches) {
|
|
101
|
+
const entries = Object.entries((branch ?? {}));
|
|
102
|
+
if (entries.length !== 1) {
|
|
103
|
+
return "an 'or' branch with several conditions cannot be checked on a write";
|
|
104
|
+
}
|
|
105
|
+
const [key, value] = entries[0];
|
|
106
|
+
if (key === 'and' || key === 'or' || key.includes('.')) {
|
|
107
|
+
return `an 'or' branch over '${key}' cannot be checked on a write`;
|
|
108
|
+
}
|
|
109
|
+
if (column !== undefined && column !== key) {
|
|
110
|
+
return "an 'or' over different columns cannot be checked on a write";
|
|
111
|
+
}
|
|
112
|
+
column = key;
|
|
113
|
+
const values = ScopeWriteGuard.allowedValues({ value });
|
|
114
|
+
if (typeof values === 'string') {
|
|
115
|
+
return `column '${key}': ${values}`;
|
|
116
|
+
}
|
|
117
|
+
values.forEach((entry) => allowed.add(entry));
|
|
118
|
+
}
|
|
119
|
+
return { column: column, allowed };
|
|
120
|
+
}
|
|
121
|
+
/** The values one column's condition allows, or a string naming why it cannot be checked. */
|
|
122
|
+
static allowedValues(opts) {
|
|
123
|
+
const { value } = opts;
|
|
124
|
+
if (isScalar(value)) {
|
|
125
|
+
return new Set([normalise(value)]);
|
|
126
|
+
}
|
|
127
|
+
if (Array.isArray(value)) {
|
|
128
|
+
return value.every(isScalar)
|
|
129
|
+
? new Set(value.map(normalise))
|
|
130
|
+
: 'a list with a non-scalar entry';
|
|
131
|
+
}
|
|
132
|
+
if (typeof value !== 'object' || value === undefined) {
|
|
133
|
+
return 'an unsupported condition';
|
|
134
|
+
}
|
|
135
|
+
const operators = Object.keys(value);
|
|
136
|
+
if (operators.length !== 1) {
|
|
137
|
+
return `a condition with ${operators.length} operators cannot be checked on a write`;
|
|
138
|
+
}
|
|
139
|
+
const [operator] = operators;
|
|
140
|
+
const operand = value[operator];
|
|
141
|
+
if (operator === ignis_kernel_1.QueryOperators.EQ && isScalar(operand)) {
|
|
142
|
+
return new Set([normalise(operand)]);
|
|
143
|
+
}
|
|
144
|
+
if ((operator === ignis_kernel_1.QueryOperators.INQ || operator === ignis_kernel_1.QueryOperators.IN) &&
|
|
145
|
+
Array.isArray(operand)) {
|
|
146
|
+
return operand.every(isScalar)
|
|
147
|
+
? new Set(operand.map(normalise))
|
|
148
|
+
: 'a list with a non-scalar entry';
|
|
149
|
+
}
|
|
150
|
+
return `the operator '${operator}' cannot be checked on a write`;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The scope as a MUTATION `where` (update, delete, soft delete, restore). A scope with a `null`
|
|
154
|
+
* branch - `{ or: [{ merchantId: { in: ids } }, { merchantId: null }] }` - lets the caller READ the
|
|
155
|
+
* shared rows. Changing or deleting one is the same permission as creating one, `write.sharedRows`,
|
|
156
|
+
* so without it the `null` branch is removed and only the caller's own rows match. With
|
|
157
|
+
* `sharedRows` the scope is returned as it is.
|
|
158
|
+
*
|
|
159
|
+
* Only the shapes the write guard can read are narrowed. Any other shape cannot be shown to exclude
|
|
160
|
+
* the shared rows, so the mutation is refused, as an update with a payload already is.
|
|
161
|
+
*/
|
|
162
|
+
static mutationWhere(opts) {
|
|
163
|
+
const { settings, model, operation, scopeWhere } = opts;
|
|
164
|
+
if (settings.write?.sharedRows === true) {
|
|
165
|
+
return scopeWhere;
|
|
166
|
+
}
|
|
167
|
+
const narrowed = ScopeWriteGuard.withoutShared({ where: scopeWhere });
|
|
168
|
+
if (narrowed.kind === 'unverifiable') {
|
|
169
|
+
throw ScopeWriteGuard.denied({
|
|
170
|
+
model,
|
|
171
|
+
operation,
|
|
172
|
+
detail: `the resolved scope cannot be verified on a write | ${narrowed.reason}`,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return narrowed.kind === 'none' ? scope_filter_1.ScopeFilterDenial.where() : narrowed.where;
|
|
176
|
+
}
|
|
177
|
+
/** `where` with every `null` the scope allows removed; `none` when nothing but `null` was allowed. */
|
|
178
|
+
static withoutShared(opts) {
|
|
179
|
+
const result = {};
|
|
180
|
+
for (const [key, value] of Object.entries(opts.where)) {
|
|
181
|
+
if (key === 'and' || key === 'or') {
|
|
182
|
+
if (!Array.isArray(value) || (key === 'or' && value.length === 0)) {
|
|
183
|
+
return { kind: 'unverifiable', reason: `'${key}' is not a list` };
|
|
184
|
+
}
|
|
185
|
+
// An 'or' is read only when every branch constrains the same column, as the guard reads it: a branch over another column
|
|
186
|
+
// could match a shared row without a null in sight.
|
|
187
|
+
if (key === 'or') {
|
|
188
|
+
const union = ScopeWriteGuard.unionOfBranches({ branches: value });
|
|
189
|
+
if (typeof union === 'string') {
|
|
190
|
+
return { kind: 'unverifiable', reason: union };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const kept = [];
|
|
194
|
+
for (const part of value) {
|
|
195
|
+
const narrowed = ScopeWriteGuard.withoutShared({ where: isRecord(part) ? part : {} });
|
|
196
|
+
if (narrowed.kind === 'unverifiable') {
|
|
197
|
+
return narrowed;
|
|
198
|
+
}
|
|
199
|
+
if (narrowed.kind === 'none') {
|
|
200
|
+
// One impossible 'and' part makes the whole 'and' impossible; an impossible 'or' branch just drops out.
|
|
201
|
+
if (key === 'and') {
|
|
202
|
+
return narrowed;
|
|
203
|
+
}
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
kept.push(narrowed.where);
|
|
207
|
+
}
|
|
208
|
+
if (key === 'or' && kept.length === 0) {
|
|
209
|
+
return { kind: 'none' };
|
|
210
|
+
}
|
|
211
|
+
result[key] = kept;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (key.includes('.')) {
|
|
215
|
+
return {
|
|
216
|
+
kind: 'unverifiable',
|
|
217
|
+
reason: `the nested path '${key}' cannot be checked on a write`,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const narrowed = ScopeWriteGuard.conditionWithoutNull({ value });
|
|
221
|
+
if (narrowed.kind !== 'condition') {
|
|
222
|
+
return narrowed.kind === 'none'
|
|
223
|
+
? { kind: 'none' }
|
|
224
|
+
: { kind: 'unverifiable', reason: `column '${key}': ${narrowed.reason}` };
|
|
225
|
+
}
|
|
226
|
+
result[key] = narrowed.value;
|
|
227
|
+
}
|
|
228
|
+
return { kind: 'where', where: result };
|
|
229
|
+
}
|
|
230
|
+
/** One column's condition without its `null` values, reading the same shapes `allowedValues` reads. */
|
|
231
|
+
static conditionWithoutNull(opts) {
|
|
232
|
+
const { value } = opts;
|
|
233
|
+
const list = (entries) => {
|
|
234
|
+
if (!entries.every(isScalar)) {
|
|
235
|
+
return { kind: 'unverifiable', reason: 'a list with a non-scalar entry' };
|
|
236
|
+
}
|
|
237
|
+
const kept = entries.filter((entry) => entry !== null);
|
|
238
|
+
return kept.length === 0
|
|
239
|
+
? { kind: 'none' }
|
|
240
|
+
: { kind: 'condition', value: kept };
|
|
241
|
+
};
|
|
242
|
+
if (isScalar(value)) {
|
|
243
|
+
return value === null ? { kind: 'none' } : { kind: 'condition', value };
|
|
244
|
+
}
|
|
245
|
+
if (Array.isArray(value)) {
|
|
246
|
+
return list(value);
|
|
247
|
+
}
|
|
248
|
+
if (!isRecord(value)) {
|
|
249
|
+
return { kind: 'unverifiable', reason: 'an unsupported condition' };
|
|
250
|
+
}
|
|
251
|
+
const operators = Object.keys(value);
|
|
252
|
+
if (operators.length !== 1) {
|
|
253
|
+
return {
|
|
254
|
+
kind: 'unverifiable',
|
|
255
|
+
reason: `a condition with ${operators.length} operators cannot be checked on a write`,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
const [operator] = operators;
|
|
259
|
+
const operand = value[operator];
|
|
260
|
+
if (operator === ignis_kernel_1.QueryOperators.EQ && isScalar(operand)) {
|
|
261
|
+
return operand === null ? { kind: 'none' } : { kind: 'condition', value };
|
|
262
|
+
}
|
|
263
|
+
if ((operator === ignis_kernel_1.QueryOperators.INQ || operator === ignis_kernel_1.QueryOperators.IN) &&
|
|
264
|
+
Array.isArray(operand)) {
|
|
265
|
+
const narrowed = list(operand);
|
|
266
|
+
return narrowed.kind === 'condition'
|
|
267
|
+
? { kind: 'condition', value: { [operator]: narrowed.value } }
|
|
268
|
+
: narrowed;
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
kind: 'unverifiable',
|
|
272
|
+
reason: `the operator '${operator}' cannot be checked on a write`,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Holds `rows` to the scope `settings.resolve()` returns now. Returns the rows to write: the same
|
|
277
|
+
* objects when nothing changes, copies with the scope columns filled in for `assign`. Throws, and
|
|
278
|
+
* so writes nothing, when any row is outside the scope.
|
|
279
|
+
*/
|
|
280
|
+
static apply(opts) {
|
|
281
|
+
const { settings, model, rows, operation } = opts;
|
|
282
|
+
const scope = settings.resolve();
|
|
283
|
+
if (scope === ignis_kernel_1.ScopeFilters.UNRESTRICTED) {
|
|
284
|
+
return rows;
|
|
285
|
+
}
|
|
286
|
+
if (scope === null || scope === undefined) {
|
|
287
|
+
// An update is already confined by the scoped `where`, which under `deny` matches no row; the
|
|
288
|
+
// guard adds nothing there. A create has no such `where`, so an unresolved scope refuses it.
|
|
289
|
+
if (settings.onMissing === ignis_kernel_1.ScopeFilterMissingBehaviors.ALLOW ||
|
|
290
|
+
operation === constants_1.ScopeWriteOperations.UPDATE) {
|
|
291
|
+
return rows;
|
|
292
|
+
}
|
|
293
|
+
throw ScopeWriteGuard.denied({
|
|
294
|
+
model,
|
|
295
|
+
operation,
|
|
296
|
+
detail: 'no scope could be resolved for this caller',
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
const extraction = ScopeWriteGuard.extractConstraints({ where: scope });
|
|
300
|
+
if (!extraction.isVerifiable) {
|
|
301
|
+
throw ScopeWriteGuard.denied({
|
|
302
|
+
model,
|
|
303
|
+
operation,
|
|
304
|
+
detail: `the resolved scope cannot be verified on a write | ${extraction.reason}`,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
const { constraints } = extraction;
|
|
308
|
+
const declared = settings.columns;
|
|
309
|
+
if (declared) {
|
|
310
|
+
const undeclared = [...constraints.keys()].find((column) => !declared.includes(column));
|
|
311
|
+
if (undeclared !== undefined) {
|
|
312
|
+
throw ScopeWriteGuard.denied({
|
|
313
|
+
model,
|
|
314
|
+
operation,
|
|
315
|
+
detail: `the resolved scope constrains column '${undeclared}', which access.columns does not declare`,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const isAssign = settings.write?.mode === ignis_kernel_1.ScopeWriteGuards.ASSIGN && operation === 'create';
|
|
320
|
+
const sharedRows = settings.write?.sharedRows === true;
|
|
321
|
+
return rows.map((row) => {
|
|
322
|
+
let result = row;
|
|
323
|
+
for (const [column, allowed] of constraints) {
|
|
324
|
+
const value = row[column];
|
|
325
|
+
if (value === undefined) {
|
|
326
|
+
if (operation === 'update') {
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
// `assign` first: stamping gives the row an owner, which is safer than leaving it shared.
|
|
330
|
+
const only = allowed.size === 1 ? [...allowed][0] : undefined;
|
|
331
|
+
if (isAssign && only !== undefined && only !== null) {
|
|
332
|
+
result = { ...result, [column]: only };
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
// Absent means the database default, usually `null`, so it is held to the `null` rule.
|
|
336
|
+
if (sharedRows) {
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
throw ScopeWriteGuard.denied({
|
|
340
|
+
model,
|
|
341
|
+
operation,
|
|
342
|
+
column,
|
|
343
|
+
detail: 'the scope column is missing',
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
const normalised = isScalar(value) ? normalise(value) : undefined;
|
|
347
|
+
// A `null` branch in the scope lets this caller READ shared rows. Writing one (or moving a
|
|
348
|
+
// row into the shared set) is a separate permission: only `sharedRows` grants it.
|
|
349
|
+
const isOutside = normalised === null ? !sharedRows : !allowed.has(normalised);
|
|
350
|
+
if (normalised === undefined || isOutside) {
|
|
351
|
+
throw ScopeWriteGuard.denied({
|
|
352
|
+
model,
|
|
353
|
+
operation,
|
|
354
|
+
column,
|
|
355
|
+
detail: 'the value is outside the resolved scope',
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/** Names the model and the column, never a value: the caller may be a request. */
|
|
363
|
+
static denied(opts) {
|
|
364
|
+
const { model, operation, column, detail } = opts;
|
|
365
|
+
return (0, core_1.getError)({
|
|
366
|
+
statusCode: WRITE_DENIED_STATUS,
|
|
367
|
+
messageCode: ignis_kernel_1.RepositoryErrorCodes.SCOPE_WRITE_DENIED,
|
|
368
|
+
message: [
|
|
369
|
+
`[${operation}] write refused by the access scope | entity: ${model}`,
|
|
370
|
+
...(column ? [`column: ${column}`] : []),
|
|
371
|
+
detail,
|
|
372
|
+
].join(' | '),
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
exports.ScopeWriteGuard = ScopeWriteGuard;
|
|
377
|
+
//# sourceMappingURL=scope-write-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope-write-guard.js","sourceRoot":"","sources":["../../../../../../src/relational/core/repositories/common/scope-write-guard.ts"],"names":[],"mappings":";;;AAAA,2CAA8E;AAC9E,sDAAuD;AACvD,wDAM+B;AAE/B,0DAAqD;AACrD,iDAAmD;AAiBnD,MAAM,mBAAmB,GAAG,aAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC;AAE5D,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoB,EAAE;IACpD,OAAO,CACL,KAAK,KAAK,IAAI;QACd,KAAK,YAAY,IAAI;QACrB,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,KAAK,CAAC,CACjE,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE;IACpE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,CAAC,KAAc,EAAiB,EAAE;IAClD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACrE,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,CA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AU,EAAE,mBAAmB;YAC/B,WAAW,EAAE,mCAAoB,CAAC,kBAAkB;YACpD,OAAO,EAAE;gBACP,IAAI,SAAS,iDAAiD,KAAK,EAAE;gBACrE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACxC,MAAM;aACP,CAAC,IAAI,CAAC,KAAK,CAAC;SACd,CAAC,CAAC;IACL,CAAC;CACF;AAlcD,0CAkcC","sourcesContent":["import { ScopeWriteOperations, type TScopeWriteOperation } from './constants';\nimport { getError } from '@venizia/ignis-helpers/core';\nimport {\n QueryOperators,\n RepositoryErrorCodes,\n ScopeFilterMissingBehaviors,\n ScopeFilters,\n ScopeWriteGuards,\n} from '@venizia/ignis-kernel';\nimport type { IModelAccessSettings, TWhere } from '@venizia/ignis-kernel';\nimport { HTTP } from '@venizia/ignis-helpers/common';\nimport { ScopeFilterDenial } from './scope-filter';\n\ntype TScalar = string | number | bigint | boolean | Date | null;\n\n/** The allowed values per scope column, each normalised so `1` and `'1'` and equal dates compare equal. */\ntype TConstraints = Map<string, Set<string | null>>;\n\ntype TExtraction =\n | { isVerifiable: true; constraints: TConstraints }\n | { isVerifiable: false; reason: string };\n\n/** What the scope becomes for a write: a `where`, no row at all, or a shape that cannot be narrowed. */\ntype TNarrowed =\n | { kind: 'where'; where: TWhere }\n | { kind: 'none' }\n | { kind: 'unverifiable'; reason: string };\n\nconst WRITE_DENIED_STATUS = HTTP.ResultCodes.RS_4.Forbidden;\n\nconst isScalar = (value: unknown): value is TScalar => {\n return (\n value === null ||\n value instanceof Date ||\n ['string', 'number', 'bigint', 'boolean'].includes(typeof value)\n );\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> => {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n};\n\nconst normalise = (value: TScalar): string | null => {\n if (value === null) {\n return null;\n }\n\n return value instanceof Date ? value.toISOString() : String(value);\n};\n\nconst intersect = (opts: { left: Set<string | null>; right: Set<string | null> }) => {\n return new Set([...opts.left].filter((value) => opts.right.has(value)));\n};\n\n/**\n * The write side of `@model` settings.access. The resolved scope is a `where`; the guard reads\n * from it the columns it constrains and the values it allows, and holds every written row to them.\n *\n * Only shapes that can be checked are read: a column equal to a scalar or `null`, `{ eq }`, `{ inq }`\n * (or `in`), an array, `and` of those, and an `or` whose branches all constrain the same column. Anything\n * else - an `or` over several columns, ranges, `like`, a nested path - makes\n * the scope unverifiable, and the write is refused with its own message instead of being skipped.\n */\nexport class ScopeWriteGuard {\n static extractConstraints(opts: { where: TWhere }): TExtraction {\n const constraints: TConstraints = new Map();\n const reason = ScopeWriteGuard.collect({\n where: opts.where as Record<string, unknown>,\n constraints,\n });\n\n return reason === undefined\n ? { isVerifiable: true, constraints }\n : { isVerifiable: false, reason };\n }\n\n /** Adds the where's constraints to `constraints`; returns the reason it cannot be verified, if any. */\n private static collect(opts: {\n where: Record<string, unknown>;\n constraints: TConstraints;\n }): string | undefined {\n const { where, constraints } = opts;\n\n for (const [key, value] of Object.entries(where)) {\n if (key === 'and') {\n if (!Array.isArray(value)) {\n return \"'and' is not a list\";\n }\n\n for (const part of value) {\n const reason = ScopeWriteGuard.collect({\n where: part as Record<string, unknown>,\n constraints,\n });\n if (reason !== undefined) {\n return reason;\n }\n }\n\n continue;\n }\n\n if (key === 'or') {\n const union = ScopeWriteGuard.unionOfBranches({ branches: value });\n if (typeof union === 'string') {\n return union;\n }\n\n const existing = constraints.get(union.column);\n constraints.set(\n union.column,\n existing ? intersect({ left: existing, right: union.allowed }) : union.allowed,\n );\n continue;\n }\n\n if (key.includes('.')) {\n return `the nested path '${key}' cannot be checked on a write`;\n }\n\n const allowed = ScopeWriteGuard.allowedValues({ value });\n if (typeof allowed === 'string') {\n return `column '${key}': ${allowed}`;\n }\n\n const existing = constraints.get(key);\n constraints.set(key, existing ? intersect({ left: existing, right: allowed }) : allowed);\n }\n\n return undefined;\n }\n\n /**\n * An `or` is checkable only when every branch constrains the same single column with a scalar,\n * `null`, `eq`, `in` or `inq`: the allowed values are then the union of the branches. This is the\n * shared-rows scope, `{ or: [{ merchantId: { in: ids } }, { merchantId: null }] }`. Any other `or`\n * (several columns, ranges, nested `and` or `or`) cannot be checked.\n */\n private static unionOfBranches(opts: {\n branches: unknown;\n }): { column: string; allowed: Set<string | null> } | string {\n const { branches } = opts;\n\n if (!Array.isArray(branches) || branches.length === 0) {\n return \"'or' is not a list\";\n }\n\n let column: string | undefined;\n const allowed = new Set<string | null>();\n\n for (const branch of branches) {\n const entries = Object.entries((branch ?? {}) as Record<string, unknown>);\n if (entries.length !== 1) {\n return \"an 'or' branch with several conditions cannot be checked on a write\";\n }\n\n const [key, value] = entries[0];\n if (key === 'and' || key === 'or' || key.includes('.')) {\n return `an 'or' branch over '${key}' cannot be checked on a write`;\n }\n\n if (column !== undefined && column !== key) {\n return \"an 'or' over different columns cannot be checked on a write\";\n }\n\n column = key;\n const values = ScopeWriteGuard.allowedValues({ value });\n if (typeof values === 'string') {\n return `column '${key}': ${values}`;\n }\n\n values.forEach((entry) => allowed.add(entry));\n }\n\n return { column: column as string, allowed };\n }\n\n /** The values one column's condition allows, or a string naming why it cannot be checked. */\n private static allowedValues(opts: { value: unknown }): Set<string | null> | string {\n const { value } = opts;\n\n if (isScalar(value)) {\n return new Set([normalise(value)]);\n }\n\n if (Array.isArray(value)) {\n return value.every(isScalar)\n ? new Set(value.map(normalise))\n : 'a list with a non-scalar entry';\n }\n\n if (typeof value !== 'object' || value === undefined) {\n return 'an unsupported condition';\n }\n\n const operators = Object.keys(value as object);\n if (operators.length !== 1) {\n return `a condition with ${operators.length} operators cannot be checked on a write`;\n }\n\n const [operator] = operators;\n const operand = (value as Record<string, unknown>)[operator];\n\n if (operator === QueryOperators.EQ && isScalar(operand)) {\n return new Set([normalise(operand)]);\n }\n\n if (\n (operator === QueryOperators.INQ || operator === QueryOperators.IN) &&\n Array.isArray(operand)\n ) {\n return operand.every(isScalar)\n ? new Set(operand.map(normalise))\n : 'a list with a non-scalar entry';\n }\n\n return `the operator '${operator}' cannot be checked on a write`;\n }\n\n /**\n * The scope as a MUTATION `where` (update, delete, soft delete, restore). A scope with a `null`\n * branch - `{ or: [{ merchantId: { in: ids } }, { merchantId: null }] }` - lets the caller READ the\n * shared rows. Changing or deleting one is the same permission as creating one, `write.sharedRows`,\n * so without it the `null` branch is removed and only the caller's own rows match. With\n * `sharedRows` the scope is returned as it is.\n *\n * Only the shapes the write guard can read are narrowed. Any other shape cannot be shown to exclude\n * the shared rows, so the mutation is refused, as an update with a payload already is.\n */\n static mutationWhere(opts: {\n settings: IModelAccessSettings;\n model: string;\n operation: string;\n scopeWhere: TWhere;\n }): TWhere {\n const { settings, model, operation, scopeWhere } = opts;\n\n if (settings.write?.sharedRows === true) {\n return scopeWhere;\n }\n\n const narrowed = ScopeWriteGuard.withoutShared({ where: scopeWhere });\n\n if (narrowed.kind === 'unverifiable') {\n throw ScopeWriteGuard.denied({\n model,\n operation,\n detail: `the resolved scope cannot be verified on a write | ${narrowed.reason}`,\n });\n }\n\n return narrowed.kind === 'none' ? ScopeFilterDenial.where() : narrowed.where;\n }\n\n /** `where` with every `null` the scope allows removed; `none` when nothing but `null` was allowed. */\n private static withoutShared(opts: { where: TWhere }): TNarrowed {\n const result: TWhere = {};\n\n for (const [key, value] of Object.entries(opts.where)) {\n if (key === 'and' || key === 'or') {\n if (!Array.isArray(value) || (key === 'or' && value.length === 0)) {\n return { kind: 'unverifiable', reason: `'${key}' is not a list` };\n }\n\n // An 'or' is read only when every branch constrains the same column, as the guard reads it: a branch over another column\n // could match a shared row without a null in sight.\n if (key === 'or') {\n const union = ScopeWriteGuard.unionOfBranches({ branches: value });\n if (typeof union === 'string') {\n return { kind: 'unverifiable', reason: union };\n }\n }\n\n const kept: TWhere[] = [];\n for (const part of value) {\n const narrowed = ScopeWriteGuard.withoutShared({ where: isRecord(part) ? part : {} });\n\n if (narrowed.kind === 'unverifiable') {\n return narrowed;\n }\n\n if (narrowed.kind === 'none') {\n // One impossible 'and' part makes the whole 'and' impossible; an impossible 'or' branch just drops out.\n if (key === 'and') {\n return narrowed;\n }\n continue;\n }\n\n kept.push(narrowed.where);\n }\n\n if (key === 'or' && kept.length === 0) {\n return { kind: 'none' };\n }\n\n result[key] = kept;\n continue;\n }\n\n if (key.includes('.')) {\n return {\n kind: 'unverifiable',\n reason: `the nested path '${key}' cannot be checked on a write`,\n };\n }\n\n const narrowed = ScopeWriteGuard.conditionWithoutNull({ value });\n if (narrowed.kind !== 'condition') {\n return narrowed.kind === 'none'\n ? { kind: 'none' }\n : { kind: 'unverifiable', reason: `column '${key}': ${narrowed.reason}` };\n }\n\n result[key] = narrowed.value;\n }\n\n return { kind: 'where', where: result };\n }\n\n /** One column's condition without its `null` values, reading the same shapes `allowedValues` reads. */\n private static conditionWithoutNull(opts: {\n value: unknown;\n }):\n | { kind: 'condition'; value: unknown }\n | { kind: 'none' }\n | { kind: 'unverifiable'; reason: string } {\n const { value } = opts;\n\n const list = (entries: unknown[]) => {\n if (!entries.every(isScalar)) {\n return { kind: 'unverifiable' as const, reason: 'a list with a non-scalar entry' };\n }\n\n const kept = entries.filter((entry) => entry !== null);\n return kept.length === 0\n ? { kind: 'none' as const }\n : { kind: 'condition' as const, value: kept };\n };\n\n if (isScalar(value)) {\n return value === null ? { kind: 'none' } : { kind: 'condition', value };\n }\n\n if (Array.isArray(value)) {\n return list(value);\n }\n\n if (!isRecord(value)) {\n return { kind: 'unverifiable', reason: 'an unsupported condition' };\n }\n\n const operators = Object.keys(value);\n if (operators.length !== 1) {\n return {\n kind: 'unverifiable',\n reason: `a condition with ${operators.length} operators cannot be checked on a write`,\n };\n }\n\n const [operator] = operators;\n const operand = value[operator];\n\n if (operator === QueryOperators.EQ && isScalar(operand)) {\n return operand === null ? { kind: 'none' } : { kind: 'condition', value };\n }\n\n if (\n (operator === QueryOperators.INQ || operator === QueryOperators.IN) &&\n Array.isArray(operand)\n ) {\n const narrowed = list(operand);\n return narrowed.kind === 'condition'\n ? { kind: 'condition', value: { [operator]: narrowed.value } }\n : narrowed;\n }\n\n return {\n kind: 'unverifiable',\n reason: `the operator '${operator}' cannot be checked on a write`,\n };\n }\n\n /**\n * Holds `rows` to the scope `settings.resolve()` returns now. Returns the rows to write: the same\n * objects when nothing changes, copies with the scope columns filled in for `assign`. Throws, and\n * so writes nothing, when any row is outside the scope.\n */\n static apply<Row extends Record<string, unknown>>(opts: {\n settings: IModelAccessSettings;\n model: string;\n rows: Row[];\n operation: TScopeWriteOperation;\n }): Row[] {\n const { settings, model, rows, operation } = opts;\n const scope = settings.resolve();\n\n if (scope === ScopeFilters.UNRESTRICTED) {\n return rows;\n }\n\n if (scope === null || scope === undefined) {\n // An update is already confined by the scoped `where`, which under `deny` matches no row; the\n // guard adds nothing there. A create has no such `where`, so an unresolved scope refuses it.\n if (\n settings.onMissing === ScopeFilterMissingBehaviors.ALLOW ||\n operation === ScopeWriteOperations.UPDATE\n ) {\n return rows;\n }\n\n throw ScopeWriteGuard.denied({\n model,\n operation,\n detail: 'no scope could be resolved for this caller',\n });\n }\n\n const extraction = ScopeWriteGuard.extractConstraints({ where: scope });\n if (!extraction.isVerifiable) {\n throw ScopeWriteGuard.denied({\n model,\n operation,\n detail: `the resolved scope cannot be verified on a write | ${extraction.reason}`,\n });\n }\n\n const { constraints } = extraction;\n const declared = settings.columns;\n\n if (declared) {\n const undeclared = [...constraints.keys()].find((column) => !declared.includes(column));\n if (undeclared !== undefined) {\n throw ScopeWriteGuard.denied({\n model,\n operation,\n detail: `the resolved scope constrains column '${undeclared}', which access.columns does not declare`,\n });\n }\n }\n\n const isAssign = settings.write?.mode === ScopeWriteGuards.ASSIGN && operation === 'create';\n const sharedRows = settings.write?.sharedRows === true;\n\n return rows.map((row) => {\n let result = row;\n\n for (const [column, allowed] of constraints) {\n const value = (row as Record<string, unknown>)[column];\n\n if (value === undefined) {\n if (operation === 'update') {\n continue;\n }\n\n // `assign` first: stamping gives the row an owner, which is safer than leaving it shared.\n const only = allowed.size === 1 ? [...allowed][0] : undefined;\n if (isAssign && only !== undefined && only !== null) {\n result = { ...result, [column]: only };\n continue;\n }\n\n // Absent means the database default, usually `null`, so it is held to the `null` rule.\n if (sharedRows) {\n continue;\n }\n\n throw ScopeWriteGuard.denied({\n model,\n operation,\n column,\n detail: 'the scope column is missing',\n });\n }\n\n const normalised = isScalar(value) ? normalise(value) : undefined;\n // A `null` branch in the scope lets this caller READ shared rows. Writing one (or moving a\n // row into the shared set) is a separate permission: only `sharedRows` grants it.\n const isOutside = normalised === null ? !sharedRows : !allowed.has(normalised as string);\n\n if (normalised === undefined || isOutside) {\n throw ScopeWriteGuard.denied({\n model,\n operation,\n column,\n detail: 'the value is outside the resolved scope',\n });\n }\n }\n\n return result;\n });\n }\n\n /** Names the model and the column, never a value: the caller may be a request. */\n private static denied(opts: {\n model: string;\n operation: string;\n column?: string;\n detail: string;\n }) {\n const { model, operation, column, detail } = opts;\n\n return getError({\n statusCode: WRITE_DENIED_STATUS,\n messageCode: RepositoryErrorCodes.SCOPE_WRITE_DENIED,\n message: [\n `[${operation}] write refused by the access scope | entity: ${model}`,\n ...(column ? [`column: ${column}`] : []),\n detail,\n ].join(' | '),\n });\n }\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TScopeWriteOperation } from '../common/constants';
|
|
2
|
+
import type { IdType, IExtraOptions, IPersistableRepository, IModelAccessSettings, ITransaction, TCount, TByIdFilter, TDataRange, TDrizzleQueryOptions, TFields, TFilter, TFindOneOptions, TFindOptions, TFindRangeOptions, TLockOptions, TRepositoryOperationScope, TRow, TWhere } from '@venizia/ignis-kernel';
|
|
2
3
|
import { AbstractRepository } from '@venizia/ignis-kernel';
|
|
3
4
|
import type { IRelationalDataSource } from '../../datasources/common';
|
|
4
5
|
import type { BaseRelationalEntity, TTableInsert, TTableObject, TTableSchemaWithId } from '../../models';
|
|
@@ -45,10 +46,24 @@ export declare abstract class RelationalBaseRepository<EntitySchema extends TTab
|
|
|
45
46
|
getDefaultFilter(): TFilter | undefined;
|
|
46
47
|
getDefaultLimit(): number | undefined;
|
|
47
48
|
hasDefaultFilter(): boolean;
|
|
48
|
-
/** Same override seam as `getDefaultFilter()` - a repository with no resolvable `@model` entity (a synthetic test double, a hand-built stub) overrides this to avoid touching `modelSettings`/`entity` at all. */
|
|
49
|
-
getScopeFilterSettings(): IScopeFilterSettings | undefined;
|
|
50
49
|
/**
|
|
51
|
-
*
|
|
50
|
+
* The model's `access` settings. Same override seam as `getDefaultFilter()` - a repository with no
|
|
51
|
+
* resolvable `@model` entity (a synthetic test double, a hand-built stub) overrides this to avoid
|
|
52
|
+
* touching `modelSettings`/`entity` at all.
|
|
53
|
+
*/
|
|
54
|
+
getAccessSettings(): IModelAccessSettings | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Holds written rows to the resolved row scope (`access.write`): refuses a row outside
|
|
57
|
+
* it, and for `assign` returns copies with the scope columns filled in. Nothing is written when it
|
|
58
|
+
* throws. Like the read scope, it is not skippable by `shouldSkipDefaultFilter`; a caller that is
|
|
59
|
+
* trusted to write across scopes resolves to `ScopeFilters.UNRESTRICTED`.
|
|
60
|
+
*/
|
|
61
|
+
protected guardScopedWrite<Row extends Record<string, unknown>>(opts: {
|
|
62
|
+
rows: Row[];
|
|
63
|
+
operation: TScopeWriteOperation;
|
|
64
|
+
}): Row[];
|
|
65
|
+
/**
|
|
66
|
+
* Row scope from `@model` settings.access - AND-composed like a default filter, but never
|
|
52
67
|
* skippable via `shouldSkipDefaultFilter`: that flag serves `restore()`'s deliberate reach past
|
|
53
68
|
* soft-delete, which must not also reach past tenant/ownership scoping.
|
|
54
69
|
*
|
|
@@ -60,12 +75,15 @@ export declare abstract class RelationalBaseRepository<EntitySchema extends TTab
|
|
|
60
75
|
protected applyScopeFilter<DO = any>(opts: {
|
|
61
76
|
userFilter?: TFilter<DO>;
|
|
62
77
|
dangerouslySkipScopeFilter?: boolean;
|
|
78
|
+
mutation?: string;
|
|
63
79
|
}): TFilter<DO>;
|
|
64
80
|
/** Merges default filter with user filter. Skippable via shouldSkipDefaultFilter - the row scope from applyScopeFilter is not. */
|
|
65
81
|
applyDefaultFilter<DO = any>(opts: {
|
|
66
82
|
userFilter?: TFilter<DO>;
|
|
67
83
|
shouldSkipDefaultFilter?: boolean;
|
|
68
84
|
dangerouslySkipScopeFilter?: boolean;
|
|
85
|
+
/** The name of the write (`update`, `delete`) when the filter selects rows to change: the row scope then leaves out the shared rows unless `write.sharedRows`. */
|
|
86
|
+
mutation?: string;
|
|
69
87
|
}): TFilter<DO>;
|
|
70
88
|
beginTransaction(opts?: TRelationalTransactionOptionsOf<TDataSource>): Promise<TRelationalTransactionOf<TDataSource>>;
|
|
71
89
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../../src/relational/core/repositories/core/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,aAAa,EACb,sBAAsB,EACtB,oBAAoB,EACpB,YAAY,EACZ,MAAM,EACN,WAAW,EACX,UAAU,EACV,oBAAoB,EACpB,OAAO,EACP,OAAO,EACP,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,YAAY,EACZ,yBAAyB,EACzB,IAAI,EACJ,MAAM,EACP,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,kBAAkB,EAGnB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAGlF,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAGvE,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACvB,wBAAwB,EACxB,sBAAsB,EACtB,wBAAwB,EACxB,+BAA+B,EAChC,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../../src/relational/core/repositories/core/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,EACV,MAAM,EACN,aAAa,EACb,sBAAsB,EACtB,oBAAoB,EACpB,YAAY,EACZ,MAAM,EACN,WAAW,EACX,UAAU,EACV,oBAAoB,EACpB,OAAO,EACP,OAAO,EACP,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,YAAY,EACZ,yBAAyB,EACzB,IAAI,EACJ,MAAM,EACP,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,kBAAkB,EAGnB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAGlF,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,YAAY,EACZ,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAGvE,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACvB,wBAAwB,EACxB,sBAAsB,EACtB,wBAAwB,EACxB,+BAA+B,EAChC,MAAM,WAAW,CAAC;AAInB,iTAAiT;AACjT,8BAAsB,wBAAwB,CAC5C,YAAY,SAAS,kBAAkB,GAAG,kBAAkB,EAC5D,UAAU,SAAS,YAAY,CAAC,YAAY,CAAC,GAAG,YAAY,CAAC,YAAY,CAAC,EAC1E,aAAa,SAAS,YAAY,CAAC,YAAY,CAAC,GAAG,YAAY,CAAC,YAAY,CAAC,EAC7E,YAAY,SAAS,aAAa,GAAG,uBAAuB,EAC5D,WAAW,SAAS,qBAAqB,GAAG,qBAAqB,CAEjE,SAAQ,kBAAkB,CAAC,UAAU,EAAE,aAAa,EAAE,YAAY,CAClE,YA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ction_state_1 = require("../../datasources/transaction-state");
|
|
|
7
7
|
const core_1 = require("@venizia/ignis-helpers/core");
|
|
8
8
|
const drizzle_orm_1 = require("drizzle-orm");
|
|
9
9
|
const common_2 = require("../common");
|
|
10
|
+
const scope_write_guard_1 = require("../common/scope-write-guard");
|
|
10
11
|
/** Relational implementation of `AbstractRepository`: adds the query dialect + hidden-column exclusion on top of the neutral base, and reaches the database only through `IRelationalQueryExecutor`. Both engine-facing parameters default to the neutral SQL contracts; an engine binds them by subclassing. */
|
|
11
12
|
class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
12
13
|
/**
|
|
@@ -83,7 +84,7 @@ class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
|
83
84
|
return this._visibleColumns;
|
|
84
85
|
}
|
|
85
86
|
getDefaultFilter() {
|
|
86
|
-
return this.modelSettings?.
|
|
87
|
+
return this.modelSettings?.query?.filter;
|
|
87
88
|
}
|
|
88
89
|
getDefaultLimit() {
|
|
89
90
|
return this.defaultLimit;
|
|
@@ -92,12 +93,34 @@ class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
|
92
93
|
const defaultFilter = this.getDefaultFilter();
|
|
93
94
|
return defaultFilter !== undefined && Object.keys(defaultFilter).length > 0;
|
|
94
95
|
}
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
/**
|
|
97
|
+
* The model's `access` settings. Same override seam as `getDefaultFilter()` - a repository with no
|
|
98
|
+
* resolvable `@model` entity (a synthetic test double, a hand-built stub) overrides this to avoid
|
|
99
|
+
* touching `modelSettings`/`entity` at all.
|
|
100
|
+
*/
|
|
101
|
+
getAccessSettings() {
|
|
102
|
+
return this.modelSettings?.access;
|
|
98
103
|
}
|
|
99
104
|
/**
|
|
100
|
-
*
|
|
105
|
+
* Holds written rows to the resolved row scope (`access.write`): refuses a row outside
|
|
106
|
+
* it, and for `assign` returns copies with the scope columns filled in. Nothing is written when it
|
|
107
|
+
* throws. Like the read scope, it is not skippable by `shouldSkipDefaultFilter`; a caller that is
|
|
108
|
+
* trusted to write across scopes resolves to `ScopeFilters.UNRESTRICTED`.
|
|
109
|
+
*/
|
|
110
|
+
guardScopedWrite(opts) {
|
|
111
|
+
const settings = this.getAccessSettings();
|
|
112
|
+
if (!settings) {
|
|
113
|
+
return opts.rows;
|
|
114
|
+
}
|
|
115
|
+
return scope_write_guard_1.ScopeWriteGuard.apply({
|
|
116
|
+
settings,
|
|
117
|
+
model: this.entity.name,
|
|
118
|
+
rows: opts.rows,
|
|
119
|
+
operation: opts.operation,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Row scope from `@model` settings.access - AND-composed like a default filter, but never
|
|
101
124
|
* skippable via `shouldSkipDefaultFilter`: that flag serves `restore()`'s deliberate reach past
|
|
102
125
|
* soft-delete, which must not also reach past tenant/ownership scoping.
|
|
103
126
|
*
|
|
@@ -107,11 +130,11 @@ class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
|
107
130
|
* reviewed at the repository - never inferable from a request.
|
|
108
131
|
*/
|
|
109
132
|
applyScopeFilter(opts) {
|
|
110
|
-
const { userFilter, dangerouslySkipScopeFilter } = opts;
|
|
133
|
+
const { userFilter, dangerouslySkipScopeFilter, mutation } = opts;
|
|
111
134
|
if (dangerouslySkipScopeFilter) {
|
|
112
135
|
return userFilter ?? {};
|
|
113
136
|
}
|
|
114
|
-
const scopeFilterSettings = this.
|
|
137
|
+
const scopeFilterSettings = this.getAccessSettings();
|
|
115
138
|
if (!scopeFilterSettings) {
|
|
116
139
|
return userFilter ?? {};
|
|
117
140
|
}
|
|
@@ -123,7 +146,19 @@ class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
|
123
146
|
return userFilter ?? {};
|
|
124
147
|
}
|
|
125
148
|
if (scopeWhere !== null && scopeWhere !== undefined) {
|
|
126
|
-
|
|
149
|
+
// A write reaches only the rows it may write: the shared (null) rows a read scope allows need `write.sharedRows`.
|
|
150
|
+
const effectiveWhere = mutation === undefined
|
|
151
|
+
? scopeWhere
|
|
152
|
+
: scope_write_guard_1.ScopeWriteGuard.mutationWhere({
|
|
153
|
+
settings: scopeFilterSettings,
|
|
154
|
+
model: this.entity.name,
|
|
155
|
+
operation: mutation,
|
|
156
|
+
scopeWhere,
|
|
157
|
+
});
|
|
158
|
+
return this.queryDialect.mergeFilter({
|
|
159
|
+
defaultFilter: { where: effectiveWhere },
|
|
160
|
+
userFilter,
|
|
161
|
+
});
|
|
127
162
|
}
|
|
128
163
|
if (scopeFilterSettings.onMissing === ignis_kernel_1.ScopeFilterMissingBehaviors.ALLOW) {
|
|
129
164
|
return userFilter ?? {};
|
|
@@ -137,8 +172,12 @@ class RelationalBaseRepository extends ignis_kernel_1.AbstractRepository {
|
|
|
137
172
|
}
|
|
138
173
|
/** Merges default filter with user filter. Skippable via shouldSkipDefaultFilter - the row scope from applyScopeFilter is not. */
|
|
139
174
|
applyDefaultFilter(opts) {
|
|
140
|
-
const { userFilter, shouldSkipDefaultFilter, dangerouslySkipScopeFilter } = opts;
|
|
141
|
-
const scopedFilter = this.applyScopeFilter({
|
|
175
|
+
const { userFilter, shouldSkipDefaultFilter, dangerouslySkipScopeFilter, mutation } = opts;
|
|
176
|
+
const scopedFilter = this.applyScopeFilter({
|
|
177
|
+
userFilter,
|
|
178
|
+
dangerouslySkipScopeFilter,
|
|
179
|
+
mutation,
|
|
180
|
+
});
|
|
142
181
|
if (shouldSkipDefaultFilter) {
|
|
143
182
|
return scopedFilter;
|
|
144
183
|
}
|