shutro 0.3.2 → 0.3.4
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/core/env/env.d.ts +6 -1
- package/dist/core/env/env.js +15 -4
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/recordset/recordset.js +5 -3
- package/dist/core/recordset/security.js +4 -1
- package/dist/core/security/runtime.d.ts +22 -0
- package/dist/core/security/runtime.js +33 -0
- package/package.json +1 -1
package/dist/core/env/env.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import type { ChangeRow } from '../recordset/changes.js';
|
|
|
10
10
|
import { type RecordCache } from '../recordset/recordset.js';
|
|
11
11
|
import type { ListLoaded, ModelName, ModelRef, RecordSet } from '../recordset/types.js';
|
|
12
12
|
import type { Registry } from '../registry/registry.js';
|
|
13
|
-
import { type RuntimeSecurity } from '../security/runtime.js';
|
|
13
|
+
import { type RuntimeSecurity, type RuntimeSecurityDescription } from '../security/runtime.js';
|
|
14
14
|
import { type Identity, type ResolvedIdentity } from './identity.js';
|
|
15
15
|
import type { Inspector } from './inspector.js';
|
|
16
16
|
/** Typed context flags (RFC 0003 §7.4). Module-contributed flags arrive with the registry's second half. */
|
|
@@ -387,6 +387,11 @@ export interface BootstrapOptions {
|
|
|
387
387
|
}
|
|
388
388
|
/** `env.securityAdmin` (RFC 0014 §7.10). */
|
|
389
389
|
export interface SecurityAdmin {
|
|
390
|
+
/**
|
|
391
|
+
* The runtime additions as stored: roles, model access, record rules, permission grants and field groups (code
|
|
392
|
+
* declarations are in the registry). Read through this transaction.
|
|
393
|
+
*/
|
|
394
|
+
describe(): Promise<RuntimeSecurityDescription>;
|
|
390
395
|
/** Creates or updates a runtime role. */
|
|
391
396
|
saveRole(role: {
|
|
392
397
|
readonly code: string;
|
package/dist/core/env/env.js
CHANGED
|
@@ -14,7 +14,7 @@ import { invalidateCached } from '../recordset/compute.js';
|
|
|
14
14
|
import { companyCurrencyId, createRecordCache, currencyById, flushAll, modelRef, } from '../recordset/recordset.js';
|
|
15
15
|
import { assertAccess, assertPermission, assertRecords, canAccess, canField, hasPermission, } from '../recordset/security.js';
|
|
16
16
|
import { mapConstraintError } from '../recordset/sqlErrors.js';
|
|
17
|
-
import { administratorCodes, changeStatement, closureWith, isAdministratorWith, loadRuntimeSecurity, SECURITY_CHANNEL, validateChange, } from '../security/runtime.js';
|
|
17
|
+
import { administratorCodes, changeStatement, closureWith, describeRuntimeSecurity, isAdministratorWith, loadRuntimeSecurity, SECURITY_CHANNEL, validateChange, } from '../security/runtime.js';
|
|
18
18
|
import { assertGrants, assertLang, assertReason, resolveIdentity, SYSTEM_UID, } from './identity.js';
|
|
19
19
|
import { assertTimeZone, calendarDateIn } from './time.js';
|
|
20
20
|
const DEFAULT_FLAGS = Object.freeze({
|
|
@@ -170,15 +170,18 @@ export class Env {
|
|
|
170
170
|
* registry, audited, and reloaded by every process once committed. Needs `createShutro({ runtimeSecurity: true })`.
|
|
171
171
|
*/
|
|
172
172
|
get securityAdmin() {
|
|
173
|
-
const
|
|
174
|
-
const scope = internalsOf(this);
|
|
173
|
+
const allowed = () => {
|
|
175
174
|
if (!this.#runtime.security.enabled)
|
|
176
175
|
throw new InvalidIdentityError('Runtime security is not enabled.', 'Pass runtimeSecurity: true to createShutro.');
|
|
177
176
|
if (!this.su && !this.administrator)
|
|
178
177
|
throw new AccessError('security', 'manage', {
|
|
179
|
-
problem: 'only administrators can change roles, access and rules.',
|
|
178
|
+
problem: 'only administrators can read or change roles, access and rules.',
|
|
180
179
|
hint: 'Use an administrator account (defineGroup({ administrator: true })).',
|
|
181
180
|
});
|
|
181
|
+
};
|
|
182
|
+
const apply = async (change) => {
|
|
183
|
+
const scope = internalsOf(this);
|
|
184
|
+
allowed();
|
|
182
185
|
// Read through this transaction: roles saved earlier in it count.
|
|
183
186
|
const overlay = await loadRuntimeSecurity({
|
|
184
187
|
query: (q) => scope.query(q),
|
|
@@ -192,6 +195,14 @@ export class Env {
|
|
|
192
195
|
});
|
|
193
196
|
};
|
|
194
197
|
return {
|
|
198
|
+
describe: async () => {
|
|
199
|
+
allowed();
|
|
200
|
+
const scope = internalsOf(this);
|
|
201
|
+
// Through this transaction: changes made earlier in it show.
|
|
202
|
+
return describeRuntimeSecurity({
|
|
203
|
+
query: (q) => scope.query(q),
|
|
204
|
+
});
|
|
205
|
+
},
|
|
195
206
|
saveRole: (role) => apply({ kind: 'role', ...role }),
|
|
196
207
|
deleteRole: (code) => apply({ kind: 'deleteRole', code }),
|
|
197
208
|
setAccess: (role, model, perms) => apply({ kind: 'access', role, model, perms }),
|
package/dist/core/index.d.ts
CHANGED
|
@@ -48,4 +48,4 @@ export { columnDefault, createTableStatements, historyStatements, historyTable,
|
|
|
48
48
|
export { type AnyExtension, type Extension, type ExtensionDefinition, extendModel, type FieldOverride, isExtension, type OverrideFn, type Replacement, replace, } from './registry/extension.js';
|
|
49
49
|
export { compareVersions, type DataMigration, defineModule, isModule, type MigrationContext, type Module, type ModuleBase, type ModuleDefinition, } from './registry/module.js';
|
|
50
50
|
export { buildRegistry, type MethodLayer, type Origin, type RegisteredModel, type Registry, type RegistryModelDescription, type RegistryOptions, type SchemaOfModule, type SchemaOfModules, type SecurityOptions, targetOf, } from './registry/registry.js';
|
|
51
|
-
export { loadRuntimeSecurity, type RuntimeRole, type RuntimeRule, type RuntimeSecurity, runtimeSecurityStatements, SECURITY_CHANNEL, } from './security/runtime.js';
|
|
51
|
+
export { describeRuntimeSecurity, loadRuntimeSecurity, type RuntimeRole, type RuntimeRule, type RuntimeSecurity, type RuntimeSecurityDescription, runtimeSecurityStatements, SECURITY_CHANNEL, } from './security/runtime.js';
|
package/dist/core/index.js
CHANGED
|
@@ -39,4 +39,4 @@ export { columnDefault, createTableStatements, historyStatements, historyTable,
|
|
|
39
39
|
export { extendModel, isExtension, replace, } from './registry/extension.js';
|
|
40
40
|
export { compareVersions, defineModule, isModule, } from './registry/module.js';
|
|
41
41
|
export { buildRegistry, targetOf, } from './registry/registry.js';
|
|
42
|
-
export { loadRuntimeSecurity, runtimeSecurityStatements, SECURITY_CHANNEL, } from './security/runtime.js';
|
|
42
|
+
export { describeRuntimeSecurity, loadRuntimeSecurity, runtimeSecurityStatements, SECURITY_CHANNEL, } from './security/runtime.js';
|
|
@@ -26,6 +26,8 @@ import { availableTransitions, runTransition } from './transitions.js';
|
|
|
26
26
|
import { companyValue } from './values.js';
|
|
27
27
|
import { assertExpected, createRecords, flushDirty, unlinkRecords, writeRecords, } from './writes.js';
|
|
28
28
|
const CHUNK = 1000;
|
|
29
|
+
/** The alias of the parent's id in batched x2many loads: no column can have it (a target's own `parent_id` did, issue #33). */
|
|
30
|
+
const PARENT_KEY = '__shutro_parent';
|
|
29
31
|
/** @internal */
|
|
30
32
|
export function createRecordCache() {
|
|
31
33
|
return { records: new Map(), currencies: new Map(), uoms: new Map() };
|
|
@@ -241,15 +243,15 @@ async function fetchX2Many(loader, model, key, parents, sub, user = false) {
|
|
|
241
243
|
let rows;
|
|
242
244
|
if (field.kind === 'one2many') {
|
|
243
245
|
const inverse = sql.id(target.columns[field.inverseField ?? ''] ?? '');
|
|
244
|
-
rows = await loader.internals.query(compile(sql `SELECT ${t}.${inverse} AS ${sql.id(
|
|
246
|
+
rows = await loader.internals.query(compile(sql `SELECT ${t}.${inverse} AS ${sql.id(PARENT_KEY)}, ${columns} FROM ${sourceOf(loader.internals, target)} AS ${t} WHERE ${t}.${inverse} = ANY(${chunk})${scope} ORDER BY ${order}`));
|
|
245
247
|
}
|
|
246
248
|
else {
|
|
247
249
|
const j = junctionTable(model, field, target);
|
|
248
|
-
rows = await loader.internals.query(compile(sql `SELECT ${sql.id('rel', j.sourceColumn)} AS ${sql.id(
|
|
250
|
+
rows = await loader.internals.query(compile(sql `SELECT ${sql.id('rel', j.sourceColumn)} AS ${sql.id(PARENT_KEY)}, ${columns} FROM ${sql.id(j.table)} AS ${sql.id('rel')} INNER JOIN ${sql.id(target.table)} AS ${t} ON ${t}.${sql.id('id')} = ${sql.id('rel', j.targetColumn)} WHERE ${sql.id('rel', j.sourceColumn)} = ANY(${chunk})${scope} ORDER BY ${order}`));
|
|
249
251
|
}
|
|
250
252
|
for (const row of rows) {
|
|
251
253
|
const childId = hydrate(loader, target, row, wanted);
|
|
252
|
-
children.get(Number(row[
|
|
254
|
+
children.get(Number(row[PARENT_KEY]))?.push(childId);
|
|
253
255
|
}
|
|
254
256
|
}
|
|
255
257
|
for (const [parent, ids] of children)
|
|
@@ -107,7 +107,10 @@ export function canField(internals, model, key, kind) {
|
|
|
107
107
|
if (field === undefined ||
|
|
108
108
|
internals.env.su ||
|
|
109
109
|
internals.env.administrator ||
|
|
110
|
-
!internals.registry.security.enforce
|
|
110
|
+
!internals.registry.security.enforce ||
|
|
111
|
+
// A compute is the engine's: it reads what it needs for any user (issue #34). Its result is still hidden from
|
|
112
|
+
// readers by the field's own groups: projections and onchange results filter outside the compute.
|
|
113
|
+
internals.compute.computing.size > 0)
|
|
111
114
|
return true;
|
|
112
115
|
if (fieldAllowed(fieldGroupsOf(internals, model, key), internals.env.groups, kind))
|
|
113
116
|
return true;
|
|
@@ -44,6 +44,28 @@ export declare function runtimeSecurityStatements(): readonly {
|
|
|
44
44
|
}[];
|
|
45
45
|
/** Loads the runtime additions (creating the tables on first use). */
|
|
46
46
|
export declare function loadRuntimeSecurity(driver: Pick<Driver, 'query'>): Promise<RuntimeSecurity>;
|
|
47
|
+
/** The runtime additions as plain lists, for administration screens (RFC 0014 §7.10). */
|
|
48
|
+
export interface RuntimeSecurityDescription {
|
|
49
|
+
readonly roles: readonly RuntimeRole[];
|
|
50
|
+
readonly access: readonly {
|
|
51
|
+
readonly role: string;
|
|
52
|
+
readonly model: string;
|
|
53
|
+
readonly perms: readonly Perm[];
|
|
54
|
+
}[];
|
|
55
|
+
readonly rules: readonly RuntimeRule[];
|
|
56
|
+
readonly permissions: readonly {
|
|
57
|
+
readonly role: string;
|
|
58
|
+
readonly permission: string;
|
|
59
|
+
}[];
|
|
60
|
+
readonly fields: readonly {
|
|
61
|
+
readonly model: string;
|
|
62
|
+
readonly field: string;
|
|
63
|
+
readonly read: readonly string[];
|
|
64
|
+
readonly write: readonly string[];
|
|
65
|
+
}[];
|
|
66
|
+
}
|
|
67
|
+
/** Reads the runtime additions as stored (creating the tables on first use), ordered for display. */
|
|
68
|
+
export declare function describeRuntimeSecurity(driver: Pick<Driver, 'query'>): Promise<RuntimeSecurityDescription>;
|
|
47
69
|
/**
|
|
48
70
|
* The groups of a field with the runtime overlay (RFC 0014 §7.10): the code groups plus the roles given in the
|
|
49
71
|
* overlay. A field without code groups that gets runtime roles becomes restricted to them: the one narrowing the
|
|
@@ -77,6 +77,39 @@ export async function loadRuntimeSecurity(driver) {
|
|
|
77
77
|
fields: fieldMap,
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
|
+
/** Reads the runtime additions as stored (creating the tables on first use), ordered for display. */
|
|
81
|
+
export async function describeRuntimeSecurity(driver) {
|
|
82
|
+
for (const text of DDL)
|
|
83
|
+
await driver.query({ text, params: [] });
|
|
84
|
+
// One after the other: one transaction, one connection.
|
|
85
|
+
const roles = await driver.query({
|
|
86
|
+
text: 'SELECT code, label, implies, administrator FROM shutro_role ORDER BY code',
|
|
87
|
+
params: [],
|
|
88
|
+
});
|
|
89
|
+
const access = await driver.query({
|
|
90
|
+
text: 'SELECT role, model, perms FROM shutro_role_access ORDER BY role, model',
|
|
91
|
+
params: [],
|
|
92
|
+
});
|
|
93
|
+
const rules = await driver.query({
|
|
94
|
+
text: 'SELECT name, role, model, perms, domain FROM shutro_role_rule ORDER BY name',
|
|
95
|
+
params: [],
|
|
96
|
+
});
|
|
97
|
+
const permissions = await driver.query({
|
|
98
|
+
text: 'SELECT role, permission FROM shutro_role_permission ORDER BY role, permission',
|
|
99
|
+
params: [],
|
|
100
|
+
});
|
|
101
|
+
const fields = await driver.query({
|
|
102
|
+
text: 'SELECT model, field, read, write FROM shutro_role_field ORDER BY model, field',
|
|
103
|
+
params: [],
|
|
104
|
+
});
|
|
105
|
+
return Object.freeze({
|
|
106
|
+
roles: roles.map((r) => Object.freeze({ ...r, implies: Object.freeze([...r.implies]) })),
|
|
107
|
+
access: access.map((a) => Object.freeze({ ...a, perms: Object.freeze([...a.perms]) })),
|
|
108
|
+
rules: rules.map((r) => Object.freeze({ ...r, perms: Object.freeze([...r.perms]) })),
|
|
109
|
+
permissions: permissions.map((p) => Object.freeze({ ...p })),
|
|
110
|
+
fields: fields.map((f) => Object.freeze({ ...f, read: Object.freeze([...f.read]), write: Object.freeze([...f.write]) })),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
80
113
|
/**
|
|
81
114
|
* The groups of a field with the runtime overlay (RFC 0014 §7.10): the code groups plus the roles given in the
|
|
82
115
|
* overlay. A field without code groups that gets runtime roles becomes restricted to them: the one narrowing the
|
package/package.json
CHANGED