@spotto/semantic-query 1.0.70-alpha.38 → 1.0.70-alpha.40

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/dist/rules.js CHANGED
@@ -1,322 +1,322 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.usableActionIds = exports.canUseAction = exports.grantsEveryAction = exports.groupGrantsAction = exports.accessFor = exports.prepareAccess = exports.canViewFieldAnywhere = exports.fieldLevelFor = exports.fieldGrantLevel = exports.assetLevelFor = exports.isUnconfined = exports.hasLocationLimit = exports.locationUnionFor = exports.scopeQueryFor = exports.ruleToQuery = exports.canonicalAccessPath = exports.ruleIsUnlimited = exports.ruleLevel = exports.ruleHasConditions = exports.ruleOf = exports.higherLevel = exports.lowerLevel = void 0;
4
- // Project Dependencies
5
- const evaluate_1 = require("./evaluate");
6
- const validate_1 = require("./validate");
7
- // ---------------------------------------------------------------------------
8
- // Levels
9
- // ---------------------------------------------------------------------------
10
- const RANK = { NONE: 0, VIEW: 1, EDIT: 2 };
11
- /** The less permissive of two levels. */
12
- function lowerLevel(a, b) {
13
- return RANK[a] <= RANK[b] ? a : b;
14
- }
15
- exports.lowerLevel = lowerLevel;
16
- /** The more permissive of two levels. */
17
- function higherLevel(a, b) {
18
- return RANK[a] >= RANK[b] ? a : b;
19
- }
20
- exports.higherLevel = higherLevel;
21
- // ---------------------------------------------------------------------------
22
- // Rules
23
- // ---------------------------------------------------------------------------
24
- /** The group's rule: one per group in this release. */
25
- function ruleOf(group) {
26
- var _a;
27
- return (_a = group.assetAccessRules) === null || _a === void 0 ? void 0 : _a[0];
28
- }
29
- exports.ruleOf = ruleOf;
30
- function ruleHasConditions(rule) {
31
- return rule.conditions !== undefined && rule.conditions !== null;
32
- }
33
- exports.ruleHasConditions = ruleHasConditions;
34
- /** The group's rule when it carries conditions, which is the only kind that grants assets. */
35
- function conditionedRuleOf(group) {
36
- const rule = ruleOf(group);
37
- return rule !== undefined && ruleHasConditions(rule) ? rule : undefined;
38
- }
39
- function isDefined(value) {
40
- return value !== undefined;
41
- }
42
- /** A conditions rule's level. The validator requires it; a stored rule without one reads as VIEW, the narrower answer. */
43
- function ruleLevel(rule) {
44
- return rule.level === 'EDIT' ? 'EDIT' : 'VIEW';
45
- }
46
- exports.ruleLevel = ruleLevel;
47
- /**
48
- * Unlimited: conditions present, locations and asset types both at ALL and no
49
- * custom-field filter. Such a rule grants every asset. A rule-less group is
50
- * not unlimited, because it grants no assets at all. Section 2.
51
- */
52
- function ruleIsUnlimited(rule) {
53
- if (!ruleHasConditions(rule))
54
- return false;
55
- const { locations, assetTypes, customFields } = rule.conditions;
56
- return (locations.mode === 'ALL' && assetTypes.mode === 'ALL' && !(customFields === null || customFields === void 0 ? void 0 : customFields.length));
57
- }
58
- exports.ruleIsUnlimited = ruleIsUnlimited;
59
- /**
60
- * A path with its trailing pipe. The validators store the canonical form; this
61
- * guards a document written before them, exactly as the old scope resolver
62
- * did at match time.
63
- */
64
- function canonicalAccessPath(path) {
65
- return path.endsWith('|') ? path : `${path}|`;
66
- }
67
- exports.canonicalAccessPath = canonicalAccessPath;
68
- function selectedPaths(selection) {
69
- var _a;
70
- if (selection.mode === 'ALL')
71
- return undefined;
72
- return ((_a = selection.paths) !== null && _a !== void 0 ? _a : []).map(canonicalAccessPath);
73
- }
74
- /**
75
- * Section 4.1: a rule's conditions as a semantic query.
76
- *
77
- * locations SELECTED -> or[ per path: homeLocation subtree, location subtree ]
78
- * assetTypes SELECTED -> or[ per path: assetType subtree ]
79
- * each filter -> or[ per value: customField eq value ]
80
- * rule -> and of every present dimension (the query's top level)
81
- *
82
- * Every condition kind here already exists in the evaluator and in both
83
- * compilers, so the translation adds no form to any of them. The language
84
- * keys a custom field by name, so the field id is resolved through the
85
- * catalogue the caller already holds.
86
- *
87
- * Returns undefined when the rule contributes nothing: it has no conditions
88
- * (a rule-less group), a SELECTED dimension names no path, or a filter names
89
- * a field the catalogue lacks or no value. The propagation rules keep the
90
- * last two from being stored; if one is met anyway the rule grants nothing
91
- * rather than everything.
92
- */
93
- function ruleToQuery(rule, fields) {
94
- if (!ruleHasConditions(rule))
95
- return undefined;
96
- const { locations, assetTypes, customFields } = rule.conditions;
97
- const locationPaths = selectedPaths(locations);
98
- const typePaths = selectedPaths(assetTypes);
99
- if ((locationPaths === null || locationPaths === void 0 ? void 0 : locationPaths.length) === 0 || (typePaths === null || typePaths === void 0 ? void 0 : typePaths.length) === 0)
100
- return undefined;
101
- const fieldById = new Map(fields.map((field) => [field.id, field]));
102
- const filterGroups = (customFields !== null && customFields !== void 0 ? customFields : []).map((filter) => {
103
- var _a;
104
- const field = fieldById.get(filter.fieldId);
105
- if (!field || !((_a = filter.values) === null || _a === void 0 ? void 0 : _a.length))
106
- return undefined;
107
- return {
108
- type: 'or',
109
- conditions: filter.values.map((value) => ({
110
- type: 'customField',
111
- fieldName: field.name,
112
- operator: 'eq',
113
- value,
114
- })),
115
- };
116
- });
117
- if (!filterGroups.every(isDefined))
118
- return undefined;
119
- const locationGroup = locationPaths
120
- ? [
121
- {
122
- type: 'or',
123
- conditions: locationPaths.flatMap((path) => [
124
- { type: 'homeLocation', path, includeSublocations: true },
125
- { type: 'location', path, includeSublocations: true },
126
- ]),
127
- },
128
- ]
129
- : [];
130
- const typeGroup = typePaths
131
- ? [
132
- {
133
- type: 'or',
134
- conditions: typePaths.map((path) => ({ type: 'assetType', path, includeSubtypes: true })),
135
- },
136
- ]
137
- : [];
138
- return { conditions: [...locationGroup, ...typeGroup, ...filterGroups] };
139
- }
140
- exports.ruleToQuery = ruleToQuery;
141
- function scopeQueryFor(caller, fields) {
142
- if (caller.open || caller.exempt)
143
- return { kind: 'unfiltered' };
144
- const rules = caller.groups.map(conditionedRuleOf).filter(isDefined);
145
- if (rules.some(ruleIsUnlimited))
146
- return { kind: 'unfiltered' };
147
- const arms = rules.map((rule) => ruleToQuery(rule, fields)).filter(isDefined);
148
- if (!arms.length)
149
- return { kind: 'none' };
150
- const query = arms.length === 1
151
- ? arms[0]
152
- : {
153
- conditions: [
154
- {
155
- type: 'or',
156
- conditions: arms.map((arm) => ({ type: 'and', conditions: arm.conditions })),
157
- },
158
- ],
159
- };
160
- return { kind: 'query', query, usesCustomFields: (0, validate_1.queryUsesCustomFields)(query) };
161
- }
162
- exports.scopeQueryFor = scopeQueryFor;
163
- function locationUnionFor(caller) {
164
- if (caller.open || caller.exempt)
165
- return { all: true };
166
- const dimensions = caller.groups
167
- .map(conditionedRuleOf)
168
- .filter(isDefined)
169
- .map((rule) => rule.conditions.locations);
170
- if (dimensions.some((locations) => locations.mode === 'ALL'))
171
- return { all: true };
172
- const paths = new Set(dimensions.flatMap((locations) => { var _a; return ((_a = locations.paths) !== null && _a !== void 0 ? _a : []).map(canonicalAccessPath); }));
173
- return { all: false, paths: [...paths].sort() };
174
- }
175
- exports.locationUnionFor = locationUnionFor;
176
- /**
177
- * Section 2, location limit: not open or exempt, and no conditions group with
178
- * locations at ALL. A caller in no group has one, since their union is empty.
179
- */
180
- function hasLocationLimit(caller) {
181
- return !locationUnionFor(caller).all;
182
- }
183
- exports.hasLocationLimit = hasLocationLimit;
184
- /**
185
- * Section 2, unconfined: open or exempt, or a member of a group whose rule is
186
- * unlimited. Only unconfined callers administer groups and memberships.
187
- */
188
- function isUnconfined(caller) {
189
- if (caller.open || caller.exempt)
190
- return true;
191
- return caller.groups.map(conditionedRuleOf).filter(isDefined).some(ruleIsUnlimited);
192
- }
193
- exports.isUnconfined = isUnconfined;
194
- /** The asset level over the groups that match it. */
195
- function assetLevelFor(caller, matching) {
196
- if (caller.open || caller.exempt)
197
- return 'EDIT';
198
- if (!matching.length)
199
- return 'NONE';
200
- const edits = matching.some((group) => {
201
- const rule = ruleOf(group);
202
- return rule !== undefined && ruleLevel(rule) === 'EDIT';
203
- });
204
- return edits ? 'EDIT' : 'VIEW';
205
- }
206
- exports.assetLevelFor = assetLevelFor;
207
- /** What a field access grants on one field: `ALL` its level, `SELECTED` the entry's level or NONE. */
208
- function fieldGrantLevel(fieldAccess, fieldId) {
209
- var _a, _b;
210
- if (fieldAccess.mode === 'ALL')
211
- return fieldAccess.level;
212
- return (_b = (_a = fieldAccess.fields.find((grant) => grant.fieldId === fieldId)) === null || _a === void 0 ? void 0 : _a.level) !== null && _b !== void 0 ? _b : 'NONE';
213
- }
214
- exports.fieldGrantLevel = fieldGrantLevel;
215
- /**
216
- * Section 4.4. The cap is the asset level, or VIEW when that is NONE, since
217
- * any asset can be opened. Open callers and the field-admin bypass get the
218
- * cap. Otherwise each group contributes: a matching conditions group its
219
- * grant capped at its rule's level, a rule-less group its grant capped at the
220
- * cap, and the most permissive contribution wins.
221
- */
222
- function fieldLevelFor(caller, matching, assetLevel, fieldId) {
223
- const cap = assetLevel === 'NONE' ? 'VIEW' : assetLevel;
224
- if (caller.open || caller.fieldsAdmin)
225
- return cap;
226
- const matchingIds = new Set(matching.map((group) => group.id));
227
- const contribution = (group) => {
228
- const rule = ruleOf(group);
229
- if (!rule)
230
- return 'NONE';
231
- const grant = fieldGrantLevel(rule.fieldAccess, fieldId);
232
- if (grant === 'NONE')
233
- return 'NONE';
234
- if (!ruleHasConditions(rule))
235
- return lowerLevel(grant, cap);
236
- return matchingIds.has(group.id) ? lowerLevel(grant, ruleLevel(rule)) : 'NONE';
237
- };
238
- return caller.groups.reduce((best, group) => higherLevel(best, contribution(group)), 'NONE');
239
- }
240
- exports.fieldLevelFor = fieldLevelFor;
241
- /**
242
- * Whether the caller can see the field on SOME asset: the test for a column
243
- * or a picker entry (section 6.1 `canViewFieldAnywhere`, section 5.4 for the
244
- * CSV columns). Open callers and the field-admin bypass see every column.
245
- */
246
- function canViewFieldAnywhere(caller, fieldId) {
247
- if (caller.open || caller.fieldsAdmin)
248
- return true;
249
- return caller.groups.some((group) => {
250
- const rule = ruleOf(group);
251
- return rule !== undefined && fieldGrantLevel(rule.fieldAccess, fieldId) !== 'NONE';
252
- });
253
- }
254
- exports.canViewFieldAnywhere = canViewFieldAnywhere;
255
- function prepareAccess(caller, schema, opts = {}) {
256
- // An open caller never needs the groups evaluated: every answer is the cap.
257
- const tests = caller.open
258
- ? []
259
- : caller.groups.flatMap((group) => {
260
- const rule = conditionedRuleOf(group);
261
- const query = rule && ruleToQuery(rule, schema.fields);
262
- return query ? [{ group, test: (0, evaluate_1.semanticPredicate)(query, schema, opts) }] : [];
263
- });
264
- return {
265
- scope: scopeQueryFor(caller, schema.fields),
266
- locationUnion: locationUnionFor(caller),
267
- unconfined: isUnconfined(caller),
268
- accessFor(asset) {
269
- const matching = tests.filter(({ test }) => test(asset)).map(({ group }) => group);
270
- const level = assetLevelFor(caller, matching);
271
- return {
272
- visible: level !== 'NONE',
273
- editable: level === 'EDIT',
274
- level,
275
- matching,
276
- fieldLevel: (fieldId) => fieldLevelFor(caller, matching, level, fieldId),
277
- };
278
- },
279
- };
280
- }
281
- exports.prepareAccess = prepareAccess;
282
- /** One asset, one answer. Prefer `prepareAccess` when evaluating many. */
283
- function accessFor(caller, asset, schema, opts = {}) {
284
- return prepareAccess(caller, schema, opts).accessFor(asset);
285
- }
286
- exports.accessFor = accessFor;
287
- // ---------------------------------------------------------------------------
288
- // Actions: section 4.8
289
- // ---------------------------------------------------------------------------
290
- function groupGrantsAction(group, actionId) {
291
- const access = group.actionAccess;
292
- if (!access)
293
- return false;
294
- return access.mode === 'ALL' || access.actions.includes(actionId);
295
- }
296
- exports.groupGrantsAction = groupGrantsAction;
297
- /** Whether every action is usable: the open callers, the action-admin bypass, or an All actions group. */
298
- function grantsEveryAction(caller) {
299
- if (caller.open || caller.actionsAdmin)
300
- return true;
301
- return caller.groups.some((group) => { var _a; return ((_a = group.actionAccess) === null || _a === void 0 ? void 0 : _a.mode) === 'ALL'; });
302
- }
303
- exports.grantsEveryAction = grantsEveryAction;
304
- function canUseAction(caller, actionId) {
305
- if (grantsEveryAction(caller))
306
- return true;
307
- return caller.groups.some((group) => groupGrantsAction(group, actionId));
308
- }
309
- exports.canUseAction = canUseAction;
310
- /**
311
- * The complete list the current user record carries, in the order of the
312
- * organisation's actions, so a Selected id that no longer exists never
313
- * appears.
314
- */
315
- function usableActionIds(caller, organisationActionIds) {
316
- if (grantsEveryAction(caller))
317
- return [...organisationActionIds];
318
- const granted = new Set(caller.groups.flatMap((group) => { var _a; return ((_a = group.actionAccess) === null || _a === void 0 ? void 0 : _a.mode) === 'SELECTED' ? group.actionAccess.actions : []; }));
319
- return organisationActionIds.filter((id) => granted.has(id));
320
- }
321
- exports.usableActionIds = usableActionIds;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usableActionIds = exports.canUseAction = exports.grantsEveryAction = exports.groupGrantsAction = exports.accessFor = exports.prepareAccess = exports.canViewFieldAnywhere = exports.fieldLevelFor = exports.fieldGrantLevel = exports.assetLevelFor = exports.isUnconfined = exports.hasLocationLimit = exports.locationUnionFor = exports.scopeQueryFor = exports.ruleToQuery = exports.canonicalAccessPath = exports.ruleIsUnlimited = exports.ruleLevel = exports.ruleHasConditions = exports.ruleOf = exports.higherLevel = exports.lowerLevel = void 0;
4
+ // Project Dependencies
5
+ const evaluate_1 = require("./evaluate");
6
+ const validate_1 = require("./validate");
7
+ // ---------------------------------------------------------------------------
8
+ // Levels
9
+ // ---------------------------------------------------------------------------
10
+ const RANK = { NONE: 0, VIEW: 1, EDIT: 2 };
11
+ /** The less permissive of two levels. */
12
+ function lowerLevel(a, b) {
13
+ return RANK[a] <= RANK[b] ? a : b;
14
+ }
15
+ exports.lowerLevel = lowerLevel;
16
+ /** The more permissive of two levels. */
17
+ function higherLevel(a, b) {
18
+ return RANK[a] >= RANK[b] ? a : b;
19
+ }
20
+ exports.higherLevel = higherLevel;
21
+ // ---------------------------------------------------------------------------
22
+ // Rules
23
+ // ---------------------------------------------------------------------------
24
+ /** The group's rule: one per group in this release. */
25
+ function ruleOf(group) {
26
+ var _a;
27
+ return (_a = group.assetAccessRules) === null || _a === void 0 ? void 0 : _a[0];
28
+ }
29
+ exports.ruleOf = ruleOf;
30
+ function ruleHasConditions(rule) {
31
+ return rule.conditions !== undefined && rule.conditions !== null;
32
+ }
33
+ exports.ruleHasConditions = ruleHasConditions;
34
+ /** The group's rule when it carries conditions, which is the only kind that grants assets. */
35
+ function conditionedRuleOf(group) {
36
+ const rule = ruleOf(group);
37
+ return rule !== undefined && ruleHasConditions(rule) ? rule : undefined;
38
+ }
39
+ function isDefined(value) {
40
+ return value !== undefined;
41
+ }
42
+ /** A conditions rule's level. The validator requires it; a stored rule without one reads as VIEW, the narrower answer. */
43
+ function ruleLevel(rule) {
44
+ return rule.level === 'EDIT' ? 'EDIT' : 'VIEW';
45
+ }
46
+ exports.ruleLevel = ruleLevel;
47
+ /**
48
+ * Unlimited: conditions present, locations and asset types both at ALL and no
49
+ * custom-field filter. Such a rule grants every asset. A rule-less group is
50
+ * not unlimited, because it grants no assets at all. Section 2.
51
+ */
52
+ function ruleIsUnlimited(rule) {
53
+ if (!ruleHasConditions(rule))
54
+ return false;
55
+ const { locations, assetTypes, customFields } = rule.conditions;
56
+ return (locations.mode === 'ALL' && assetTypes.mode === 'ALL' && !(customFields === null || customFields === void 0 ? void 0 : customFields.length));
57
+ }
58
+ exports.ruleIsUnlimited = ruleIsUnlimited;
59
+ /**
60
+ * A path with its trailing pipe. The validators store the canonical form; this
61
+ * guards a document written before them, exactly as the old scope resolver
62
+ * did at match time.
63
+ */
64
+ function canonicalAccessPath(path) {
65
+ return path.endsWith('|') ? path : `${path}|`;
66
+ }
67
+ exports.canonicalAccessPath = canonicalAccessPath;
68
+ function selectedPaths(selection) {
69
+ var _a;
70
+ if (selection.mode === 'ALL')
71
+ return undefined;
72
+ return ((_a = selection.paths) !== null && _a !== void 0 ? _a : []).map(canonicalAccessPath);
73
+ }
74
+ /**
75
+ * Section 4.1: a rule's conditions as a semantic query.
76
+ *
77
+ * locations SELECTED -> or[ per path: homeLocation subtree, location subtree ]
78
+ * assetTypes SELECTED -> or[ per path: assetType subtree ]
79
+ * each filter -> or[ per value: customField eq value ]
80
+ * rule -> and of every present dimension (the query's top level)
81
+ *
82
+ * Every condition kind here already exists in the evaluator and in both
83
+ * compilers, so the translation adds no form to any of them. The language
84
+ * keys a custom field by name, so the field id is resolved through the
85
+ * catalogue the caller already holds.
86
+ *
87
+ * Returns undefined when the rule contributes nothing: it has no conditions
88
+ * (a rule-less group), a SELECTED dimension names no path, or a filter names
89
+ * a field the catalogue lacks or no value. The propagation rules keep the
90
+ * last two from being stored; if one is met anyway the rule grants nothing
91
+ * rather than everything.
92
+ */
93
+ function ruleToQuery(rule, fields) {
94
+ if (!ruleHasConditions(rule))
95
+ return undefined;
96
+ const { locations, assetTypes, customFields } = rule.conditions;
97
+ const locationPaths = selectedPaths(locations);
98
+ const typePaths = selectedPaths(assetTypes);
99
+ if ((locationPaths === null || locationPaths === void 0 ? void 0 : locationPaths.length) === 0 || (typePaths === null || typePaths === void 0 ? void 0 : typePaths.length) === 0)
100
+ return undefined;
101
+ const fieldById = new Map(fields.map((field) => [field.id, field]));
102
+ const filterGroups = (customFields !== null && customFields !== void 0 ? customFields : []).map((filter) => {
103
+ var _a;
104
+ const field = fieldById.get(filter.fieldId);
105
+ if (!field || !((_a = filter.values) === null || _a === void 0 ? void 0 : _a.length))
106
+ return undefined;
107
+ return {
108
+ type: 'or',
109
+ conditions: filter.values.map((value) => ({
110
+ type: 'customField',
111
+ fieldName: field.name,
112
+ operator: 'eq',
113
+ value,
114
+ })),
115
+ };
116
+ });
117
+ if (!filterGroups.every(isDefined))
118
+ return undefined;
119
+ const locationGroup = locationPaths
120
+ ? [
121
+ {
122
+ type: 'or',
123
+ conditions: locationPaths.flatMap((path) => [
124
+ { type: 'homeLocation', path, includeSublocations: true },
125
+ { type: 'location', path, includeSublocations: true },
126
+ ]),
127
+ },
128
+ ]
129
+ : [];
130
+ const typeGroup = typePaths
131
+ ? [
132
+ {
133
+ type: 'or',
134
+ conditions: typePaths.map((path) => ({ type: 'assetType', path, includeSubtypes: true })),
135
+ },
136
+ ]
137
+ : [];
138
+ return { conditions: [...locationGroup, ...typeGroup, ...filterGroups] };
139
+ }
140
+ exports.ruleToQuery = ruleToQuery;
141
+ function scopeQueryFor(caller, fields) {
142
+ if (caller.open || caller.exempt)
143
+ return { kind: 'unfiltered' };
144
+ const rules = caller.groups.map(conditionedRuleOf).filter(isDefined);
145
+ if (rules.some(ruleIsUnlimited))
146
+ return { kind: 'unfiltered' };
147
+ const arms = rules.map((rule) => ruleToQuery(rule, fields)).filter(isDefined);
148
+ if (!arms.length)
149
+ return { kind: 'none' };
150
+ const query = arms.length === 1
151
+ ? arms[0]
152
+ : {
153
+ conditions: [
154
+ {
155
+ type: 'or',
156
+ conditions: arms.map((arm) => ({ type: 'and', conditions: arm.conditions })),
157
+ },
158
+ ],
159
+ };
160
+ return { kind: 'query', query, usesCustomFields: (0, validate_1.queryUsesCustomFields)(query) };
161
+ }
162
+ exports.scopeQueryFor = scopeQueryFor;
163
+ function locationUnionFor(caller) {
164
+ if (caller.open || caller.exempt)
165
+ return { all: true };
166
+ const dimensions = caller.groups
167
+ .map(conditionedRuleOf)
168
+ .filter(isDefined)
169
+ .map((rule) => rule.conditions.locations);
170
+ if (dimensions.some((locations) => locations.mode === 'ALL'))
171
+ return { all: true };
172
+ const paths = new Set(dimensions.flatMap((locations) => { var _a; return ((_a = locations.paths) !== null && _a !== void 0 ? _a : []).map(canonicalAccessPath); }));
173
+ return { all: false, paths: [...paths].sort() };
174
+ }
175
+ exports.locationUnionFor = locationUnionFor;
176
+ /**
177
+ * Section 2, location limit: not open or exempt, and no conditions group with
178
+ * locations at ALL. A caller in no group has one, since their union is empty.
179
+ */
180
+ function hasLocationLimit(caller) {
181
+ return !locationUnionFor(caller).all;
182
+ }
183
+ exports.hasLocationLimit = hasLocationLimit;
184
+ /**
185
+ * Section 2, unconfined: open or exempt, or a member of a group whose rule is
186
+ * unlimited. Only unconfined callers administer groups and memberships.
187
+ */
188
+ function isUnconfined(caller) {
189
+ if (caller.open || caller.exempt)
190
+ return true;
191
+ return caller.groups.map(conditionedRuleOf).filter(isDefined).some(ruleIsUnlimited);
192
+ }
193
+ exports.isUnconfined = isUnconfined;
194
+ /** The asset level over the groups that match it. */
195
+ function assetLevelFor(caller, matching) {
196
+ if (caller.open || caller.exempt)
197
+ return 'EDIT';
198
+ if (!matching.length)
199
+ return 'NONE';
200
+ const edits = matching.some((group) => {
201
+ const rule = ruleOf(group);
202
+ return rule !== undefined && ruleLevel(rule) === 'EDIT';
203
+ });
204
+ return edits ? 'EDIT' : 'VIEW';
205
+ }
206
+ exports.assetLevelFor = assetLevelFor;
207
+ /** What a field access grants on one field: `ALL` its level, `SELECTED` the entry's level or NONE. */
208
+ function fieldGrantLevel(fieldAccess, fieldId) {
209
+ var _a, _b;
210
+ if (fieldAccess.mode === 'ALL')
211
+ return fieldAccess.level;
212
+ return (_b = (_a = fieldAccess.fields.find((grant) => grant.fieldId === fieldId)) === null || _a === void 0 ? void 0 : _a.level) !== null && _b !== void 0 ? _b : 'NONE';
213
+ }
214
+ exports.fieldGrantLevel = fieldGrantLevel;
215
+ /**
216
+ * Section 4.4. The cap is the asset level, or VIEW when that is NONE, since
217
+ * any asset can be opened. Open callers and the field-admin bypass get the
218
+ * cap. Otherwise each group contributes: a matching conditions group its
219
+ * grant capped at its rule's level, a rule-less group its grant capped at the
220
+ * cap, and the most permissive contribution wins.
221
+ */
222
+ function fieldLevelFor(caller, matching, assetLevel, fieldId) {
223
+ const cap = assetLevel === 'NONE' ? 'VIEW' : assetLevel;
224
+ if (caller.open || caller.fieldsAdmin)
225
+ return cap;
226
+ const matchingIds = new Set(matching.map((group) => group.id));
227
+ const contribution = (group) => {
228
+ const rule = ruleOf(group);
229
+ if (!rule)
230
+ return 'NONE';
231
+ const grant = fieldGrantLevel(rule.fieldAccess, fieldId);
232
+ if (grant === 'NONE')
233
+ return 'NONE';
234
+ if (!ruleHasConditions(rule))
235
+ return lowerLevel(grant, cap);
236
+ return matchingIds.has(group.id) ? lowerLevel(grant, ruleLevel(rule)) : 'NONE';
237
+ };
238
+ return caller.groups.reduce((best, group) => higherLevel(best, contribution(group)), 'NONE');
239
+ }
240
+ exports.fieldLevelFor = fieldLevelFor;
241
+ /**
242
+ * Whether the caller can see the field on SOME asset: the test for a column
243
+ * or a picker entry (section 6.1 `canViewFieldAnywhere`, section 5.4 for the
244
+ * CSV columns). Open callers and the field-admin bypass see every column.
245
+ */
246
+ function canViewFieldAnywhere(caller, fieldId) {
247
+ if (caller.open || caller.fieldsAdmin)
248
+ return true;
249
+ return caller.groups.some((group) => {
250
+ const rule = ruleOf(group);
251
+ return rule !== undefined && fieldGrantLevel(rule.fieldAccess, fieldId) !== 'NONE';
252
+ });
253
+ }
254
+ exports.canViewFieldAnywhere = canViewFieldAnywhere;
255
+ function prepareAccess(caller, schema, opts = {}) {
256
+ // An open caller never needs the groups evaluated: every answer is the cap.
257
+ const tests = caller.open
258
+ ? []
259
+ : caller.groups.flatMap((group) => {
260
+ const rule = conditionedRuleOf(group);
261
+ const query = rule && ruleToQuery(rule, schema.fields);
262
+ return query ? [{ group, test: (0, evaluate_1.semanticPredicate)(query, schema, opts) }] : [];
263
+ });
264
+ return {
265
+ scope: scopeQueryFor(caller, schema.fields),
266
+ locationUnion: locationUnionFor(caller),
267
+ unconfined: isUnconfined(caller),
268
+ accessFor(asset) {
269
+ const matching = tests.filter(({ test }) => test(asset)).map(({ group }) => group);
270
+ const level = assetLevelFor(caller, matching);
271
+ return {
272
+ visible: level !== 'NONE',
273
+ editable: level === 'EDIT',
274
+ level,
275
+ matching,
276
+ fieldLevel: (fieldId) => fieldLevelFor(caller, matching, level, fieldId),
277
+ };
278
+ },
279
+ };
280
+ }
281
+ exports.prepareAccess = prepareAccess;
282
+ /** One asset, one answer. Prefer `prepareAccess` when evaluating many. */
283
+ function accessFor(caller, asset, schema, opts = {}) {
284
+ return prepareAccess(caller, schema, opts).accessFor(asset);
285
+ }
286
+ exports.accessFor = accessFor;
287
+ // ---------------------------------------------------------------------------
288
+ // Actions: section 4.8
289
+ // ---------------------------------------------------------------------------
290
+ function groupGrantsAction(group, actionId) {
291
+ const access = group.actionAccess;
292
+ if (!access)
293
+ return false;
294
+ return access.mode === 'ALL' || access.actions.includes(actionId);
295
+ }
296
+ exports.groupGrantsAction = groupGrantsAction;
297
+ /** Whether every action is usable: the open callers, the action-admin bypass, or an All actions group. */
298
+ function grantsEveryAction(caller) {
299
+ if (caller.open || caller.actionsAdmin)
300
+ return true;
301
+ return caller.groups.some((group) => { var _a; return ((_a = group.actionAccess) === null || _a === void 0 ? void 0 : _a.mode) === 'ALL'; });
302
+ }
303
+ exports.grantsEveryAction = grantsEveryAction;
304
+ function canUseAction(caller, actionId) {
305
+ if (grantsEveryAction(caller))
306
+ return true;
307
+ return caller.groups.some((group) => groupGrantsAction(group, actionId));
308
+ }
309
+ exports.canUseAction = canUseAction;
310
+ /**
311
+ * The complete list the current user record carries, in the order of the
312
+ * organisation's actions, so a Selected id that no longer exists never
313
+ * appears.
314
+ */
315
+ function usableActionIds(caller, organisationActionIds) {
316
+ if (grantsEveryAction(caller))
317
+ return [...organisationActionIds];
318
+ const granted = new Set(caller.groups.flatMap((group) => { var _a; return ((_a = group.actionAccess) === null || _a === void 0 ? void 0 : _a.mode) === 'SELECTED' ? group.actionAccess.actions : []; }));
319
+ return organisationActionIds.filter((id) => granted.has(id));
320
+ }
321
+ exports.usableActionIds = usableActionIds;
322
322
  //# sourceMappingURL=rules.js.map