shutro 0.3.2 → 0.3.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.
@@ -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;
@@ -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 apply = async (change) => {
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 }),
@@ -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';
@@ -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';
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shutro",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "The ORM built for business software: type-safe, secure-by-default, multi-company, audit-ready PostgreSQL ORM for Node.js and TypeScript.",
5
5
  "keywords": [
6
6
  "orm",