@things-factory/auth-base 10.1.188 → 10.1.198
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-client/tsconfig.tsbuildinfo +1 -1
- package/dist-server/controllers/signin.d.ts +5 -0
- package/dist-server/controllers/signin.js +25 -0
- package/dist-server/controllers/signin.js.map +1 -1
- package/dist-server/controllers/verification.d.ts +5 -1
- package/dist-server/controllers/verification.js +11 -6
- package/dist-server/controllers/verification.js.map +1 -1
- package/dist-server/index.js +7 -0
- package/dist-server/index.js.map +1 -1
- package/dist-server/middlewares/audit-scope-middleware.d.ts +7 -0
- package/dist-server/middlewares/audit-scope-middleware.js +15 -0
- package/dist-server/middlewares/audit-scope-middleware.js.map +1 -0
- package/dist-server/middlewares/authenticate-401-middleware.js +4 -0
- package/dist-server/middlewares/authenticate-401-middleware.js.map +1 -1
- package/dist-server/middlewares/index.js +3 -0
- package/dist-server/middlewares/index.js.map +1 -1
- package/dist-server/role-tasks.d.ts +2 -0
- package/dist-server/role-tasks.js +20 -0
- package/dist-server/role-tasks.js.map +1 -0
- package/dist-server/router/auth-checkin-router.js +3 -5
- package/dist-server/router/auth-checkin-router.js.map +1 -1
- package/dist-server/router/auth-public-process-router.js +26 -14
- package/dist-server/router/auth-public-process-router.js.map +1 -1
- package/dist-server/router/site-root-router.d.ts +2 -0
- package/dist-server/router/site-root-router.js +2 -0
- package/dist-server/router/site-root-router.js.map +1 -1
- package/dist-server/service/access-request/index.d.ts +1 -1
- package/dist-server/service/audit/audit-action.d.ts +36 -0
- package/dist-server/service/audit/audit-action.js +137 -0
- package/dist-server/service/audit/audit-action.js.map +1 -0
- package/dist-server/service/audit/audit-kinds.d.ts +19 -0
- package/dist-server/service/audit/audit-kinds.js +70 -0
- package/dist-server/service/audit/audit-kinds.js.map +1 -0
- package/dist-server/service/audit/audit-query.d.ts +11 -0
- package/dist-server/service/audit/audit-query.js +295 -0
- package/dist-server/service/audit/audit-query.js.map +1 -0
- package/dist-server/service/audit/audit-recorder.d.ts +77 -0
- package/dist-server/service/audit/audit-recorder.js +247 -0
- package/dist-server/service/audit/audit-recorder.js.map +1 -0
- package/dist-server/service/audit/audit-request.d.ts +28 -0
- package/dist-server/service/audit/audit-request.js +96 -0
- package/dist-server/service/audit/audit-request.js.map +1 -0
- package/dist-server/service/audit/audit-retention.d.ts +12 -0
- package/dist-server/service/audit/audit-retention.js +42 -0
- package/dist-server/service/audit/audit-retention.js.map +1 -0
- package/dist-server/service/audit/audit-subscriber.d.ts +39 -0
- package/dist-server/service/audit/audit-subscriber.js +191 -0
- package/dist-server/service/audit/audit-subscriber.js.map +1 -0
- package/dist-server/service/audit/audit-target.d.ts +19 -0
- package/dist-server/service/audit/audit-target.js +46 -0
- package/dist-server/service/audit/audit-target.js.map +1 -0
- package/dist-server/service/audit/audit-types.d.ts +44 -0
- package/dist-server/service/audit/audit-types.js +161 -0
- package/dist-server/service/audit/audit-types.js.map +1 -0
- package/dist-server/service/audit/index.d.ts +6 -0
- package/dist-server/service/audit/index.js +11 -0
- package/dist-server/service/audit/index.js.map +1 -0
- package/dist-server/service/domain-owner/domain-owner-mutation.js +3 -0
- package/dist-server/service/domain-owner/domain-owner-mutation.js.map +1 -1
- package/dist-server/service/index.d.ts +9 -2
- package/dist-server/service/index.js +19 -4
- package/dist-server/service/index.js.map +1 -1
- package/dist-server/service/open-fields.js +1 -1
- package/dist-server/service/open-fields.js.map +1 -1
- package/dist-server/service/privilege/privilege-directive.js +25 -1
- package/dist-server/service/privilege/privilege-directive.js.map +1 -1
- package/dist-server/service/role/role-mutation.js +8 -1
- package/dist-server/service/role/role-mutation.js.map +1 -1
- package/dist-server/service/role-template/index.d.ts +1 -1
- package/dist-server/service/role-template/role-task.d.ts +74 -0
- package/dist-server/service/role-template/role-task.js +113 -0
- package/dist-server/service/role-template/role-task.js.map +1 -0
- package/dist-server/service/role-template/role-template-gap-resolver.d.ts +9 -5
- package/dist-server/service/role-template/role-template-gap-resolver.js +41 -13
- package/dist-server/service/role-template/role-template-gap-resolver.js.map +1 -1
- package/dist-server/service/role-template/role-template-mutation.js +10 -1
- package/dist-server/service/role-template/role-template-mutation.js.map +1 -1
- package/dist-server/service/role-template/role-template-query.js +4 -2
- package/dist-server/service/role-template/role-template-query.js.map +1 -1
- package/dist-server/service/role-template/role-template-types.d.ts +24 -0
- package/dist-server/service/role-template/role-template-types.js +63 -1
- package/dist-server/service/role-template/role-template-types.js.map +1 -1
- package/dist-server/service/role-template/role-template.d.ts +63 -10
- package/dist-server/service/role-template/role-template.js +92 -18
- package/dist-server/service/role-template/role-template.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/dist-server/utils/check-permission.d.ts +6 -0
- package/dist-server/utils/check-permission.js +23 -8
- package/dist-server/utils/check-permission.js.map +1 -1
- package/package.json +3 -2
- package/tests/activate-page-case.test.ts +53 -0
- package/tests/audit-db.test.ts +394 -0
- package/tests/auth-base-role-tasks.test.ts +58 -0
- package/tests/list-inheritance-db.test.ts +0 -7
- package/tests/mail-request-answers-db.test.ts +139 -0
- package/tests/open-doors.test.ts +2 -2
- package/tests/refusal-cause.test.ts +1 -1
- package/tests/role-task.test.ts +168 -0
- package/tests/role-template-gap-db.test.ts +77 -2
- package/translations/en.json +16 -3
- package/translations/es.json +16 -3
- package/translations/ja.json +16 -3
- package/translations/ko.json +16 -3
- package/translations/ms.json +16 -3
- package/translations/zh.json +16 -3
|
@@ -60,13 +60,24 @@ export type RoleTemplate = {
|
|
|
60
60
|
*/
|
|
61
61
|
description?: string;
|
|
62
62
|
/**
|
|
63
|
-
* Required, and may be empty.
|
|
63
|
+
* Required unless `tasks` is written, and may be empty.
|
|
64
64
|
*
|
|
65
65
|
* `[]` is a statement: this seat opens nothing, it exists for approval routing. A
|
|
66
66
|
* missing `grants` is a different thing — someone forgot — and is rejected here, so the
|
|
67
67
|
* screen can tell "opens nothing on purpose" from "nothing was written down".
|
|
68
|
+
*
|
|
69
|
+
* Read a template's privileges through `templateGrantsOf`, never from here: with `tasks`
|
|
70
|
+
* this is only the part written directly.
|
|
68
71
|
*/
|
|
69
|
-
grants
|
|
72
|
+
grants?: RoleTemplateGrant[];
|
|
73
|
+
/**
|
|
74
|
+
* The work this seat does, by task id (`registerRoleTask`, ADR-0139 decision 11).
|
|
75
|
+
*
|
|
76
|
+
* The seat holds every privilege of every task here, plus `grants`, each privilege once.
|
|
77
|
+
* A task id nobody registered is a definition error: boot reports it and the template is
|
|
78
|
+
* not applied at all, rather than applied without that work.
|
|
79
|
+
*/
|
|
80
|
+
tasks?: string[];
|
|
70
81
|
};
|
|
71
82
|
/**
|
|
72
83
|
* Registered form of a template.
|
|
@@ -97,7 +108,27 @@ export declare function roleTemplateById(id: string): RegisteredRoleTemplate | u
|
|
|
97
108
|
export declare function computedRoleTemplates(): RegisteredRoleTemplate[];
|
|
98
109
|
/** Every template this installation offers — declared by apps, plus the computed two. */
|
|
99
110
|
export declare function allRoleTemplates(): RegisteredRoleTemplate[];
|
|
100
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* **A template's privileges** — its tasks' privileges and its own grants, each privilege once.
|
|
113
|
+
*
|
|
114
|
+
* Every reader of a template's privileges goes through this: the role screen's gap, applying it, the view the
|
|
115
|
+
* template picker draws. A task id nobody registered adds nothing here; `templateDefinitionErrors` says so, and
|
|
116
|
+
* applying the template refuses.
|
|
117
|
+
*/
|
|
118
|
+
export declare function templateGrantsOf(template: RoleTemplate): RoleTemplateGrant[];
|
|
119
|
+
/**
|
|
120
|
+
* What is wrong with a template as declared, before anything is applied (ADR-0139 decision 11).
|
|
121
|
+
*
|
|
122
|
+
* A task id nobody registered, and a privilege a task names that this installation does not declare. A task is
|
|
123
|
+
* registered only by a module that is installed, so either one is a slip in code — a task id mistyped, a module the
|
|
124
|
+
* application names but does not install, a category renamed under a task — and applying the template anyway would
|
|
125
|
+
* give a seat that is short of work it says it does. So these refuse the template.
|
|
126
|
+
*
|
|
127
|
+
* A template's own `grants` naming an undeclared privilege is not here: an application may write grants for modules
|
|
128
|
+
* one installation has and another does not, and those are skipped as before (`unknownTemplateGrants`).
|
|
129
|
+
*/
|
|
130
|
+
export declare function templateDefinitionErrors(template: RoleTemplate, declared?: Record<string, unknown>): string[];
|
|
131
|
+
/** What a role lacks against a template, and what it holds beyond it - see `templateGapAgainst`. */
|
|
101
132
|
export type RoleTemplateGap = {
|
|
102
133
|
templateId: string;
|
|
103
134
|
templateName: string;
|
|
@@ -106,9 +137,15 @@ export type RoleTemplateGap = {
|
|
|
106
137
|
axes: string[];
|
|
107
138
|
}[];
|
|
108
139
|
missingCount: number;
|
|
140
|
+
/** Declared privileges the role holds that the template does not grant — wider than the template, not wrong. */
|
|
141
|
+
extra: {
|
|
142
|
+
category: string;
|
|
143
|
+
axes: string[];
|
|
144
|
+
}[];
|
|
145
|
+
extraCount: number;
|
|
109
146
|
};
|
|
110
147
|
/**
|
|
111
|
-
* **
|
|
148
|
+
* **A role laid against a template** (ADR-0075 decision 1, ADR-0139 decision 11).
|
|
112
149
|
*
|
|
113
150
|
* A template is a seed: the role it made belongs to the administrator afterwards, and nothing
|
|
114
151
|
* pushes a template's later grants into it. When a template gains a door - operato-plant's nine
|
|
@@ -116,20 +153,36 @@ export type RoleTemplateGap = {
|
|
|
116
153
|
* file's header gives: the template's current grants against the role's current privileges,
|
|
117
154
|
* computed, never stored. The screen shows it and the administrator adds it; nothing adds it for them.
|
|
118
155
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* `
|
|
156
|
+
* `extra` is the other side: what the role holds that the template does not grant. It is not a fault —
|
|
157
|
+
* a site may have widened the role on purpose — and nothing takes it away; the screen says "wider than
|
|
158
|
+
* the template". Only privileges this installation declares count on either side: a grant it does not
|
|
159
|
+
* declare is `missingCount` on the template, and a held privilege nothing declares is struck through
|
|
160
|
+
* on the role screen already.
|
|
161
|
+
*/
|
|
162
|
+
export declare function templateGapAgainst(template: RoleTemplate, held: ReadonlySet<string>, declared?: Record<string, unknown>): RoleTemplateGap;
|
|
163
|
+
/**
|
|
164
|
+
* The gap against the template of this role's name — the pair the role list shows unasked.
|
|
165
|
+
*
|
|
166
|
+
* Paired by name, because provenance is not stored. A role whose name no template carries — renamed by its
|
|
167
|
+
* administrator, or named in the site's own words as operato-office's roles are — has no pair here; the role
|
|
168
|
+
* screen lets the administrator pick the template to lay it against (`templateGapAgainst`). The role is never
|
|
169
|
+
* renamed to make a pair: approval lines hold the role, and the name is the site's.
|
|
170
|
+
* `null` when no template carries this name; a gap of 0 missing is a role that is up to date.
|
|
123
171
|
*/
|
|
124
172
|
export declare function templateGapOf(roleName: string, held: ReadonlySet<string>, templates?: RegisteredRoleTemplate[], declared?: Record<string, unknown>): RoleTemplateGap | null;
|
|
125
173
|
/**
|
|
126
|
-
* Names a template
|
|
174
|
+
* Names a template's own grants mention that this installation does not declare.
|
|
127
175
|
*
|
|
128
176
|
* A template that survives a category rename becomes a role that grants nothing, and that
|
|
129
177
|
* is the quietest way this design can fail — the administrator picks "quality inspector",
|
|
130
178
|
* gets a role, and it opens no doors. operato-plant's seed already does this for its own
|
|
131
179
|
* table (`seed-demo-org.ts` warns about grants it could not attach); this is the same
|
|
132
|
-
* check moved to where templates are declared.
|
|
180
|
+
* check moved to where templates are declared. A task's privileges are checked as
|
|
181
|
+
* definition errors instead (`templateDefinitionErrors`).
|
|
133
182
|
*/
|
|
134
183
|
export declare function unknownTemplateGrants(template: RoleTemplate): string[];
|
|
184
|
+
/**
|
|
185
|
+
* Boot's one look at what templates and tasks name — after every module has loaded and `@privilege` has filled
|
|
186
|
+
* `process['PRIVILEGES']`, the first moment both registries and the declarations are complete.
|
|
187
|
+
*/
|
|
135
188
|
export declare function reportUnknownTemplateGrants(): void;
|
|
@@ -5,11 +5,15 @@ exports.roleTemplates = roleTemplates;
|
|
|
5
5
|
exports.roleTemplateById = roleTemplateById;
|
|
6
6
|
exports.computedRoleTemplates = computedRoleTemplates;
|
|
7
7
|
exports.allRoleTemplates = allRoleTemplates;
|
|
8
|
+
exports.templateGrantsOf = templateGrantsOf;
|
|
9
|
+
exports.templateDefinitionErrors = templateDefinitionErrors;
|
|
10
|
+
exports.templateGapAgainst = templateGapAgainst;
|
|
8
11
|
exports.templateGapOf = templateGapOf;
|
|
9
12
|
exports.unknownTemplateGrants = unknownTemplateGrants;
|
|
10
13
|
exports.reportUnknownTemplateGrants = reportUnknownTemplateGrants;
|
|
11
14
|
const env_1 = require("@things-factory/env");
|
|
12
15
|
const privilege_axis_js_1 = require("../privilege/privilege-axis.js");
|
|
16
|
+
const role_task_js_1 = require("./role-task.js");
|
|
13
17
|
const REGISTRY = 'ROLE_TEMPLATES';
|
|
14
18
|
process[REGISTRY] = {};
|
|
15
19
|
function registry() {
|
|
@@ -26,12 +30,15 @@ function registerRoleTemplate(template) {
|
|
|
26
30
|
if (!template?.id || !template?.name) {
|
|
27
31
|
throw new Error('A role template needs both an id and a name.');
|
|
28
32
|
}
|
|
29
|
-
if (!Array.isArray(template.
|
|
33
|
+
if (template.tasks !== undefined && (!Array.isArray(template.tasks) || template.tasks.some(task => !task || typeof task !== 'string'))) {
|
|
34
|
+
throw new Error(`Role template "${template.id}" has tasks that are not a list of task ids.`);
|
|
35
|
+
}
|
|
36
|
+
if (!Array.isArray(template.grants) && !template.tasks?.length) {
|
|
30
37
|
throw new Error(`Role template "${template.id}" has no grants. Write \`grants: []\` if this seat is meant to ` +
|
|
31
38
|
'open nothing — leaving it out is indistinguishable from forgetting, and the screen cannot ' +
|
|
32
39
|
'tell an administrator which one it is.');
|
|
33
40
|
}
|
|
34
|
-
for (const grant of template.grants) {
|
|
41
|
+
for (const grant of template.grants || []) {
|
|
35
42
|
if (!grant?.category || !Array.isArray(grant.axes) || grant.axes.length === 0) {
|
|
36
43
|
throw new Error(`Role template "${template.id}" has a grant without a category or with no axes. ` +
|
|
37
44
|
'Axes are always listed one by one.');
|
|
@@ -50,7 +57,7 @@ function registerRoleTemplate(template) {
|
|
|
50
57
|
if (registry()[template.id]) {
|
|
51
58
|
throw new Error(`Role template "${template.id}" is declared twice.`);
|
|
52
59
|
}
|
|
53
|
-
registry()[template.id] = { ...template };
|
|
60
|
+
registry()[template.id] = { ...template, grants: template.grants || [] };
|
|
54
61
|
}
|
|
55
62
|
function roleTemplates() {
|
|
56
63
|
return Object.values(registry());
|
|
@@ -125,7 +132,37 @@ function allRoleTemplates() {
|
|
|
125
132
|
return [...computedRoleTemplates(), ...roleTemplates()];
|
|
126
133
|
}
|
|
127
134
|
/**
|
|
128
|
-
* **
|
|
135
|
+
* **A template's privileges** — its tasks' privileges and its own grants, each privilege once.
|
|
136
|
+
*
|
|
137
|
+
* Every reader of a template's privileges goes through this: the role screen's gap, applying it, the view the
|
|
138
|
+
* template picker draws. A task id nobody registered adds nothing here; `templateDefinitionErrors` says so, and
|
|
139
|
+
* applying the template refuses.
|
|
140
|
+
*/
|
|
141
|
+
function templateGrantsOf(template) {
|
|
142
|
+
const tasks = (template.tasks || []).map(id => (0, role_task_js_1.roleTaskById)(id)?.grants || []);
|
|
143
|
+
return (0, role_task_js_1.mergeGrants)([...tasks, template.grants || []]);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* What is wrong with a template as declared, before anything is applied (ADR-0139 decision 11).
|
|
147
|
+
*
|
|
148
|
+
* A task id nobody registered, and a privilege a task names that this installation does not declare. A task is
|
|
149
|
+
* registered only by a module that is installed, so either one is a slip in code — a task id mistyped, a module the
|
|
150
|
+
* application names but does not install, a category renamed under a task — and applying the template anyway would
|
|
151
|
+
* give a seat that is short of work it says it does. So these refuse the template.
|
|
152
|
+
*
|
|
153
|
+
* A template's own `grants` naming an undeclared privilege is not here: an application may write grants for modules
|
|
154
|
+
* one installation has and another does not, and those are skipped as before (`unknownTemplateGrants`).
|
|
155
|
+
*/
|
|
156
|
+
function templateDefinitionErrors(template, declared = process['PRIVILEGES'] || {}) {
|
|
157
|
+
return (template.tasks || []).flatMap(id => {
|
|
158
|
+
const task = (0, role_task_js_1.roleTaskById)(id);
|
|
159
|
+
if (!task)
|
|
160
|
+
return [`task "${id}" is not registered`];
|
|
161
|
+
return (0, role_task_js_1.undeclaredTaskGrants)(task, declared).map(grant => `task "${id}" names ${grant}, which this installation does not declare`);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* **A role laid against a template** (ADR-0075 decision 1, ADR-0139 decision 11).
|
|
129
166
|
*
|
|
130
167
|
* A template is a seed: the role it made belongs to the administrator afterwards, and nothing
|
|
131
168
|
* pushes a template's later grants into it. When a template gains a door - operato-plant's nine
|
|
@@ -133,41 +170,70 @@ function allRoleTemplates() {
|
|
|
133
170
|
* file's header gives: the template's current grants against the role's current privileges,
|
|
134
171
|
* computed, never stored. The screen shows it and the administrator adds it; nothing adds it for them.
|
|
135
172
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* `
|
|
173
|
+
* `extra` is the other side: what the role holds that the template does not grant. It is not a fault —
|
|
174
|
+
* a site may have widened the role on purpose — and nothing takes it away; the screen says "wider than
|
|
175
|
+
* the template". Only privileges this installation declares count on either side: a grant it does not
|
|
176
|
+
* declare is `missingCount` on the template, and a held privilege nothing declares is struck through
|
|
177
|
+
* on the role screen already.
|
|
140
178
|
*/
|
|
141
|
-
function
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const missing = template.grants
|
|
179
|
+
function templateGapAgainst(template, held, declared = process['PRIVILEGES'] || {}) {
|
|
180
|
+
const grants = templateGrantsOf(template);
|
|
181
|
+
const granted = new Set(grants.flatMap(grant => grant.axes.map(axis => (0, role_task_js_1.privilegeKey)(grant.category, axis))));
|
|
182
|
+
const missing = grants
|
|
146
183
|
.map(grant => ({
|
|
147
184
|
category: grant.category,
|
|
148
|
-
axes: grant.axes.filter(axis => declared[
|
|
185
|
+
axes: grant.axes.filter(axis => declared[(0, role_task_js_1.privilegeKey)(grant.category, axis)] && !held.has((0, role_task_js_1.privilegeKey)(grant.category, axis)))
|
|
149
186
|
}))
|
|
150
187
|
.filter(grant => grant.axes.length > 0);
|
|
188
|
+
const extra = (0, role_task_js_1.mergeGrants)([
|
|
189
|
+
[...held]
|
|
190
|
+
.filter(key => declared[key] && !granted.has(key))
|
|
191
|
+
.sort()
|
|
192
|
+
.map(key => {
|
|
193
|
+
const [category, axis] = key.split(' ');
|
|
194
|
+
return { category, axes: [axis] };
|
|
195
|
+
})
|
|
196
|
+
]);
|
|
151
197
|
return {
|
|
152
198
|
templateId: template.id,
|
|
153
199
|
templateName: template.name,
|
|
154
200
|
missing,
|
|
155
|
-
missingCount: missing.reduce((count, grant) => count + grant.axes.length, 0)
|
|
201
|
+
missingCount: missing.reduce((count, grant) => count + grant.axes.length, 0),
|
|
202
|
+
extra,
|
|
203
|
+
extraCount: extra.reduce((count, grant) => count + grant.axes.length, 0)
|
|
156
204
|
};
|
|
157
205
|
}
|
|
158
206
|
/**
|
|
159
|
-
*
|
|
207
|
+
* The gap against the template of this role's name — the pair the role list shows unasked.
|
|
208
|
+
*
|
|
209
|
+
* Paired by name, because provenance is not stored. A role whose name no template carries — renamed by its
|
|
210
|
+
* administrator, or named in the site's own words as operato-office's roles are — has no pair here; the role
|
|
211
|
+
* screen lets the administrator pick the template to lay it against (`templateGapAgainst`). The role is never
|
|
212
|
+
* renamed to make a pair: approval lines hold the role, and the name is the site's.
|
|
213
|
+
* `null` when no template carries this name; a gap of 0 missing is a role that is up to date.
|
|
214
|
+
*/
|
|
215
|
+
function templateGapOf(roleName, held, templates = allRoleTemplates(), declared = process['PRIVILEGES'] || {}) {
|
|
216
|
+
const template = templates.find(one => one.name === roleName);
|
|
217
|
+
return template ? templateGapAgainst(template, held, declared) : null;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Names a template's own grants mention that this installation does not declare.
|
|
160
221
|
*
|
|
161
222
|
* A template that survives a category rename becomes a role that grants nothing, and that
|
|
162
223
|
* is the quietest way this design can fail — the administrator picks "quality inspector",
|
|
163
224
|
* gets a role, and it opens no doors. operato-plant's seed already does this for its own
|
|
164
225
|
* table (`seed-demo-org.ts` warns about grants it could not attach); this is the same
|
|
165
|
-
* check moved to where templates are declared.
|
|
226
|
+
* check moved to where templates are declared. A task's privileges are checked as
|
|
227
|
+
* definition errors instead (`templateDefinitionErrors`).
|
|
166
228
|
*/
|
|
167
229
|
function unknownTemplateGrants(template) {
|
|
168
230
|
const declared = process['PRIVILEGES'] || {};
|
|
169
|
-
return template.grants.flatMap(grant => grant.axes.filter(axis => !declared[
|
|
231
|
+
return (template.grants || []).flatMap(grant => grant.axes.filter(axis => !declared[(0, role_task_js_1.privilegeKey)(grant.category, axis)]).map(axis => `${grant.category}:${axis}`));
|
|
170
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Boot's one look at what templates and tasks name — after every module has loaded and `@privilege` has filled
|
|
235
|
+
* `process['PRIVILEGES']`, the first moment both registries and the declarations are complete.
|
|
236
|
+
*/
|
|
171
237
|
function reportUnknownTemplateGrants() {
|
|
172
238
|
const unknown = roleTemplates().flatMap(template => unknownTemplateGrants(template).map(grant => `${template.id} → ${grant}`));
|
|
173
239
|
if (unknown.length) {
|
|
@@ -175,5 +241,13 @@ function reportUnknownTemplateGrants() {
|
|
|
175
241
|
`${unknown.join(', ')}. Privilege definitions come from @privilege at boot, so either the query ` +
|
|
176
242
|
'is not installed here or the name has moved. A role seeded from such a template opens nothing.');
|
|
177
243
|
}
|
|
244
|
+
const broken = [
|
|
245
|
+
...(0, role_task_js_1.roleTasks)().flatMap(task => (0, role_task_js_1.undeclaredTaskGrants)(task).map(grant => `task "${task.id}" names ${grant}`)),
|
|
246
|
+
...roleTemplates().flatMap(template => (template.tasks || []).filter(id => !(0, role_task_js_1.roleTaskById)(id)).map(id => `${template.id} → task "${id}" is not registered`))
|
|
247
|
+
];
|
|
248
|
+
if (broken.length) {
|
|
249
|
+
env_1.logger.error(`[role-task] ${broken.length} definition errors — ${broken.join(', ')}. ` +
|
|
250
|
+
'A template that names any of these is refused when applied, so no seat is stood up short of the work it says it does.');
|
|
251
|
+
}
|
|
178
252
|
}
|
|
179
253
|
//# sourceMappingURL=role-template.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"role-template.js","sourceRoot":"","sources":["../../../server/service/role-template/role-template.ts"],"names":[],"mappings":";;AAqGA,oDAwCC;AAED,sCAEC;AAED,4CAEC;AAUD,sDA2DC;AAGD,4CAEC;AAwBD,sCAsBC;AAWD,sDAMC;AAED,kEAYC;AA5SD,6CAA4C;AAE5C,sEAAmF;AAoFnF,MAAM,QAAQ,GAAG,gBAAgB,CAAA;AAEjC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAA;AAEtB,SAAS,QAAQ;IACf,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAA;AAC1B,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAC,QAAsB;IACzD,IAAI,CAAC,QAAQ,EAAE,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAA;IACjE,CAAC;IAED,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CACb,kBAAkB,QAAQ,CAAC,EAAE,iEAAiE;YAC5F,4FAA4F;YAC5F,wCAAwC,CAC3C,CAAA;IACH,CAAC;IAED,KAAK,MAAM,KAAK,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;QACpC,IAAI,CAAC,KAAK,EAAE,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9E,MAAM,IAAI,KAAK,CACb,kBAAkB,QAAQ,CAAC,EAAE,oDAAoD;gBAC/E,oCAAoC,CACvC,CAAA;Q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AAI,EAAE,CAAA;IAE5C,OAAO,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CACrC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC,CAC3G,CAAA;AACH,CAAC;AAED,SAAgB,2BAA2B;IACzC,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CACjD,qBAAqB,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,EAAE,MAAM,KAAK,EAAE,CAAC,CAC1E,CAAA;IAED,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,YAAM,CAAC,IAAI,CACT,mBAAmB,OAAO,CAAC,MAAM,+DAA+D;YAC9F,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,4EAA4E;YACjG,gGAAgG,CACnG,CAAA;IACH,CAAC;AACH,CAAC","sourcesContent":["import { logger } from '@things-factory/env'\n\nimport { readOnlyAxes, readOnlyAxesAreKnown } from '../privilege/privilege-axis.js'\n\n/**\n * Role templates — the seeds an application offers so a new domain does not start with\n * zero roles.\n *\n * ── Why this is a boot registry and not a table ───────────────────────────────\n * A template is a declaration. Copying it into rows per domain gives 100 domains x 3\n * templates = 300 rows, and once the code changes the template there is nowhere to ask\n * which of the two is true: the declaration, or the row that was written from an older\n * one. `@privilege` lives in the boot registry for the same reason.\n *\n * ── What a template is not ───────────────────────────────────────────────────\n * It is a seed, not a role. Once it has been used the resulting role belongs to the\n * administrator: the template is never re-applied and never overwrites. Nothing is written\n * onto the role to remember where it came from — what an administrator would want from\n * that (\"the template gained two privileges, here they are\") is the template's current\n * grants against the role's current privileges, and that is computed rather than stored.\n *\n * ── A role is one thing seen from three sides ────────────────────────────────\n * operato-plant uses roles as privilege bundles; operato-twin uses them as the seat an\n * approval line points at (`worklist` resolves an approver of type `Role` by id). Those\n * are not two concepts. A role is a seat in the organisation: what it opens, who fills it,\n * and whether approvals route to it. Splitting them would put two QUALITY-INSPECTORs in\n * one plant and force every person into both.\n */\n\n/** One module and the axes this template grants on it. Axes are always enumerated. */\nexport type RoleTemplateGrant = {\n /** `@privilege(category:)` — what is guarded, not the package name. */\n category: string\n /**\n * `query` / `mutation` / ... — listed one by one.\n *\n * There is deliberately no `'all'`. A template written as \"everything\" would widen on\n * its own the day a new axis is declared, with nobody having touched it. The single\n * place that may widen is the framework-computed administrator template, which means\n * \"whatever exists now\" by definition.\n */\n axes: string[]\n}\n\nexport type RoleTemplate = {\n /** Declaration key, namespaced by the declaring app: `plant.quality-inspector`. */\n id: string\n /**\n * The role name created in the domain: `QUALITY-INSPECTOR`.\n *\n * Two apps may declare the same name on purpose — that is one seat, and the privileges\n * become the union. The name is also a stored identifier: approval lines hold the role's\n * id, so renaming here creates a second role and leaves the approval line pointing at\n * the first. Renaming is a migration, not a template edit.\n */\n name: string\n /**\n * Plain text, not an i18n key.\n *\n * `createRoleFromTemplate` copies this onto the role it seeds, and the role list draws\n * the role's description as a value — so a key written here reaches the screen as\n * `label.role-template.administrator`. That is exactly what the framework's own two\n * templates did until 2026-09-11.\n *\n * operato-plant had it right from the start: \"검사 기준과 처분을 정하는 자리\".\n */\n description?: string\n /**\n * Required, and may be empty.\n *\n * `[]` is a statement: this seat opens nothing, it exists for approval routing. A\n * missing `grants` is a different thing — someone forgot — and is rejected here, so the\n * screen can tell \"opens nothing on purpose\" from \"nothing was written down\".\n */\n grants: RoleTemplateGrant[]\n}\n\n/**\n * Registered form of a template.\n *\n * Identical to the declaration today. It stays a separate name because the registry is\n * what every reader goes through, and giving it its own type keeps a later addition from\n * rippling into every call site.\n */\nexport type RegisteredRoleTemplate = RoleTemplate\n\nconst REGISTRY = 'ROLE_TEMPLATES'\n\nprocess[REGISTRY] = {}\n\nfunction registry(): Record<string, RegisteredRoleTemplate> {\n return process[REGISTRY]\n}\n\n/**\n * Declares a role template. Call it from the application's server entry.\n *\n * Categories are not checked here: registration runs while modules load, before the\n * schema is built and therefore before `process['PRIVILEGES']` exists. The name check\n * happens at boot instead (`reportUnknownTemplateGrants`).\n */\nexport function registerRoleTemplate(template: RoleTemplate): void {\n if (!template?.id || !template?.name) {\n throw new Error('A role template needs both an id and a name.')\n }\n\n if (!Array.isArray(template.grants)) {\n throw new Error(\n `Role template \"${template.id}\" has no grants. Write \\`grants: []\\` if this seat is meant to ` +\n 'open nothing — leaving it out is indistinguishable from forgetting, and the screen cannot ' +\n 'tell an administrator which one it is.'\n )\n }\n\n for (const grant of template.grants) {\n if (!grant?.category || !Array.isArray(grant.axes) || grant.axes.length === 0) {\n throw new Error(\n `Role template \"${template.id}\" has a grant without a category or with no axes. ` +\n 'Axes are always listed one by one.'\n )\n }\n\n /*\n * Across the nine roles operato-plant declares, no role is granted `mutation` on a\n * category without `query` on the same category — you cannot fix what you cannot see.\n * A template that does so is almost certainly a slip, so say it out loud rather than\n * silently granting a half-usable seat.\n */\n if (grant.axes.includes('mutation') && !grant.axes.includes('query')) {\n logger.warn(\n `[role-template] \"${template.id}\" grants ${grant.category}:mutation without ${grant.category}:query. ` +\n 'Someone who may change a thing but not see it can rarely use either.'\n )\n }\n }\n\n if (registry()[template.id]) {\n throw new Error(`Role template \"${template.id}\" is declared twice.`)\n }\n\n registry()[template.id] = { ...template }\n}\n\nexport function roleTemplates(): RegisteredRoleTemplate[] {\n return Object.values(registry())\n}\n\nexport function roleTemplateById(id: string): RegisteredRoleTemplate | undefined {\n return registry()[id]\n}\n\n/**\n * The two templates the framework can build without knowing a single category name.\n *\n * Everything else has to be declared by an application: a job title spans modules\n * (a production lead uses worklist *and* order *and* ops-master) and no one package knows\n * that. These two are different — they are defined over whatever is declared, so they can\n * be computed from the registry `@privilege` fills.\n */\nexport function computedRoleTemplates(): RegisteredRoleTemplate[] {\n const declared: [string, string][] = Object.values(process['PRIVILEGES'] || {})\n const axesOf = new Map<string, Set<string>>()\n\n for (const [category, axis] of declared) {\n if (!axesOf.has(category)) {\n axesOf.set(category, new Set())\n }\n axesOf.get(category)!.add(axis)\n }\n\n const categories = [...axesOf.keys()].sort()\n\n /*\n * No description on either of these.\n *\n * They carried i18n keys, which the seeding path stored verbatim onto the role and the\n * list then drew as the description. And a static sentence would be worse than what the\n * screen already computes from `grants` — \"opens 30 things across 30 modules\" is exact\n * and moves when the installation does.\n */\n /*\n * VIEWER is only offered when someone has said which axes are read-only.\n *\n * This used to match the literal word `query`, which is right in this house and wrong in\n * dssp, where the read axis is spelled `read` and `kpi` has no `query` at all — VIEWER\n * would have quietly dropped that installation's main module.\n *\n * And when nobody has said, the template is **not offered at all** rather than offered\n * empty. An empty VIEWER is the quiet failure this whole design is built to avoid: an\n * administrator picks it, gets a role, no door opens, and the list counts the zero\n * correctly. \"We do not know\" is not \"there are none\".\n */\n const readOnly = readOnlyAxes()\n\n const viewer: RoleTemplate | undefined = readOnlyAxesAreKnown()\n ? {\n id: 'framework.viewer',\n name: 'VIEWER',\n grants: categories\n .map(category => ({ category, axes: [...axesOf.get(category)!].filter(axis => readOnly.has(axis)).sort() }))\n .filter(grant => grant.axes.length > 0)\n }\n : undefined\n\n /*\n * The one place that widens on purpose.\n *\n * Everywhere else an unenumerated axis is a defect waiting for the day a new axis is\n * declared. Here \"everything that exists\" is the meaning of the template, so picking up\n * a newly declared axis is correct rather than accidental.\n */\n const administrator: RoleTemplate = {\n id: 'framework.administrator',\n name: 'ADMINISTRATOR',\n grants: categories.map(category => ({ category, axes: [...axesOf.get(category)!].sort() }))\n }\n\n return viewer ? [viewer, administrator] : [administrator]\n}\n\n/** Every template this installation offers — declared by apps, plus the computed two. */\nexport function allRoleTemplates(): RegisteredRoleTemplate[] {\n return [...computedRoleTemplates(), ...roleTemplates()]\n}\n\n/** What a role lacks against the template of its name - see `templateGapOf`. */\nexport type RoleTemplateGap = {\n templateId: string\n templateName: string\n missing: { category: string; axes: string[] }[]\n missingCount: number\n}\n\n/**\n * **What the template of this role's name grants here that the role does not hold** (ADR-0075 decision 1).\n *\n * A template is a seed: the role it made belongs to the administrator afterwards, and nothing\n * pushes a template's later grants into it. When a template gains a door - operato-plant's nine\n * gained ai-assistant - the roles already standing fall quietly behind. The answer is the one this\n * file's header gives: the template's current grants against the role's current privileges,\n * computed, never stored. The screen shows it and the administrator adds it; nothing adds it for them.\n *\n * Paired by name, because provenance is not stored: a role renamed by its administrator has no pair,\n * and that is the administrator making the role their own. Only privileges this installation\n * declares count - a grant it does not declare is `missingCount` on the template, not a gap here.\n * `null` when no template carries this name; a gap of 0 is a role that is up to date.\n */\nexport function templateGapOf(\n roleName: string,\n held: ReadonlySet<string>,\n templates: RegisteredRoleTemplate[] = allRoleTemplates(),\n declared: Record<string, unknown> = process['PRIVILEGES'] || {}\n): RoleTemplateGap | null {\n const template = templates.find(one => one.name === roleName)\n if (!template) return null\n\n const missing = template.grants\n .map(grant => ({\n category: grant.category,\n axes: grant.axes.filter(axis => declared[`${grant.category} ${axis}`] && !held.has(`${grant.category} ${axis}`))\n }))\n .filter(grant => grant.axes.length > 0)\n\n return {\n templateId: template.id,\n templateName: template.name,\n missing,\n missingCount: missing.reduce((count, grant) => count + grant.axes.length, 0)\n }\n}\n\n/**\n * Names a template mentions that this installation does not declare.\n *\n * A template that survives a category rename becomes a role that grants nothing, and that\n * is the quietest way this design can fail — the administrator picks \"quality inspector\",\n * gets a role, and it opens no doors. operato-plant's seed already does this for its own\n * table (`seed-demo-org.ts` warns about grants it could not attach); this is the same\n * check moved to where templates are declared.\n */\nexport function unknownTemplateGrants(template: RoleTemplate): string[] {\n const declared = process['PRIVILEGES'] || {}\n\n return template.grants.flatMap(grant =>\n grant.axes.filter(axis => !declared[`${grant.category} ${axis}`]).map(axis => `${grant.category}:${axis}`)\n )\n}\n\nexport function reportUnknownTemplateGrants(): void {\n const unknown = roleTemplates().flatMap(template =>\n unknownTemplateGrants(template).map(grant => `${template.id} → ${grant}`)\n )\n\n if (unknown.length) {\n logger.warn(\n `[role-template] ${unknown.length} grants name privileges this installation does not declare — ` +\n `${unknown.join(', ')}. Privilege definitions come from @privilege at boot, so either the query ` +\n 'is not installed here or the name has moved. A role seeded from such a template opens nothing.'\n )\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"role-template.js","sourceRoot":"","sources":["../../../server/service/role-template/role-template.ts"],"names":[],"mappings":";;AAiHA,oDA4CC;AAED,sCAEC;AAED,4CAEC;AAUD,sDA2DC;AAGD,4CAEC;AASD,4CAIC;AAaD,4DAMC;AA4BD,gDAiCC;AAWD,sCAQC;AAYD,sDAMC;AAMD,kEA0BC;AAjZD,6CAA4C;AAE5C,sEAAmF;AACnF,iDAAyG;AA+FzG,MAAM,QAAQ,GAAG,gBAAgB,CAAA;AAEjC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAA;AAEtB,SAAS,QAAQ;IACf,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAA;AAC1B,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAC,QAAsB;IACzD,IAAI,CAAC,QAAQ,EAAE,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAA;IACjE,CAAC;IAED,IAAI,QAAQ,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC;QACvI,MAAM,IAAI,KAAK,CAAC,kBAAkB,QAAQ,CAAC,EAAE,8CA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O,CAAC,QAAQ,CAAC,EAAE,CACpC,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,IAAA,2BAAY,EAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,EAAE,YAAY,EAAE,qBAAqB,CAAC,CACpH;KACF,CAAA;IAED,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAClB,YAAM,CAAC,KAAK,CACV,eAAe,MAAM,CAAC,MAAM,wBAAwB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI;YACvE,uHAAuH,CAC1H,CAAA;IACH,CAAC;AACH,CAAC","sourcesContent":["import { logger } from '@things-factory/env'\n\nimport { readOnlyAxes, readOnlyAxesAreKnown } from '../privilege/privilege-axis.js'\nimport { mergeGrants, privilegeKey, roleTaskById, roleTasks, undeclaredTaskGrants } from './role-task.js'\n\n/**\n * Role templates — the seeds an application offers so a new domain does not start with\n * zero roles.\n *\n * ── Why this is a boot registry and not a table ───────────────────────────────\n * A template is a declaration. Copying it into rows per domain gives 100 domains x 3\n * templates = 300 rows, and once the code changes the template there is nowhere to ask\n * which of the two is true: the declaration, or the row that was written from an older\n * one. `@privilege` lives in the boot registry for the same reason.\n *\n * ── What a template is not ───────────────────────────────────────────────────\n * It is a seed, not a role. Once it has been used the resulting role belongs to the\n * administrator: the template is never re-applied and never overwrites. Nothing is written\n * onto the role to remember where it came from — what an administrator would want from\n * that (\"the template gained two privileges, here they are\") is the template's current\n * grants against the role's current privileges, and that is computed rather than stored.\n *\n * ── A role is one thing seen from three sides ────────────────────────────────\n * operato-plant uses roles as privilege bundles; operato-twin uses them as the seat an\n * approval line points at (`worklist` resolves an approver of type `Role` by id). Those\n * are not two concepts. A role is a seat in the organisation: what it opens, who fills it,\n * and whether approvals route to it. Splitting them would put two QUALITY-INSPECTORs in\n * one plant and force every person into both.\n */\n\n/** One module and the axes this template grants on it. Axes are always enumerated. */\nexport type RoleTemplateGrant = {\n /** `@privilege(category:)` — what is guarded, not the package name. */\n category: string\n /**\n * `query` / `mutation` / ... — listed one by one.\n *\n * There is deliberately no `'all'`. A template written as \"everything\" would widen on\n * its own the day a new axis is declared, with nobody having touched it. The single\n * place that may widen is the framework-computed administrator template, which means\n * \"whatever exists now\" by definition.\n */\n axes: string[]\n}\n\nexport type RoleTemplate = {\n /** Declaration key, namespaced by the declaring app: `plant.quality-inspector`. */\n id: string\n /**\n * The role name created in the domain: `QUALITY-INSPECTOR`.\n *\n * Two apps may declare the same name on purpose — that is one seat, and the privileges\n * become the union. The name is also a stored identifier: approval lines hold the role's\n * id, so renaming here creates a second role and leaves the approval line pointing at\n * the first. Renaming is a migration, not a template edit.\n */\n name: string\n /**\n * Plain text, not an i18n key.\n *\n * `createRoleFromTemplate` copies this onto the role it seeds, and the role list draws\n * the role's description as a value — so a key written here reaches the screen as\n * `label.role-template.administrator`. That is exactly what the framework's own two\n * templates did until 2026-09-11.\n *\n * operato-plant had it right from the start: \"검사 기준과 처분을 정하는 자리\".\n */\n description?: string\n /**\n * Required unless `tasks` is written, and may be empty.\n *\n * `[]` is a statement: this seat opens nothing, it exists for approval routing. A\n * missing `grants` is a different thing — someone forgot — and is rejected here, so the\n * screen can tell \"opens nothing on purpose\" from \"nothing was written down\".\n *\n * Read a template's privileges through `templateGrantsOf`, never from here: with `tasks`\n * this is only the part written directly.\n */\n grants?: RoleTemplateGrant[]\n /**\n * The work this seat does, by task id (`registerRoleTask`, ADR-0139 decision 11).\n *\n * The seat holds every privilege of every task here, plus `grants`, each privilege once.\n * A task id nobody registered is a definition error: boot reports it and the template is\n * not applied at all, rather than applied without that work.\n */\n tasks?: string[]\n}\n\n/**\n * Registered form of a template.\n *\n * Identical to the declaration today. It stays a separate name because the registry is\n * what every reader goes through, and giving it its own type keeps a later addition from\n * rippling into every call site.\n */\nexport type RegisteredRoleTemplate = RoleTemplate\n\nconst REGISTRY = 'ROLE_TEMPLATES'\n\nprocess[REGISTRY] = {}\n\nfunction registry(): Record<string, RegisteredRoleTemplate> {\n return process[REGISTRY]\n}\n\n/**\n * Declares a role template. Call it from the application's server entry.\n *\n * Categories are not checked here: registration runs while modules load, before the\n * schema is built and therefore before `process['PRIVILEGES']` exists. The name check\n * happens at boot instead (`reportUnknownTemplateGrants`).\n */\nexport function registerRoleTemplate(template: RoleTemplate): void {\n if (!template?.id || !template?.name) {\n throw new Error('A role template needs both an id and a name.')\n }\n\n if (template.tasks !== undefined && (!Array.isArray(template.tasks) || template.tasks.some(task => !task || typeof task !== 'string'))) {\n throw new Error(`Role template \"${template.id}\" has tasks that are not a list of task ids.`)\n }\n\n if (!Array.isArray(template.grants) && !template.tasks?.length) {\n throw new Error(\n `Role template \"${template.id}\" has no grants. Write \\`grants: []\\` if this seat is meant to ` +\n 'open nothing — leaving it out is indistinguishable from forgetting, and the screen cannot ' +\n 'tell an administrator which one it is.'\n )\n }\n\n for (const grant of template.grants || []) {\n if (!grant?.category || !Array.isArray(grant.axes) || grant.axes.length === 0) {\n throw new Error(\n `Role template \"${template.id}\" has a grant without a category or with no axes. ` +\n 'Axes are always listed one by one.'\n )\n }\n\n /*\n * Across the nine roles operato-plant declares, no role is granted `mutation` on a\n * category without `query` on the same category — you cannot fix what you cannot see.\n * A template that does so is almost certainly a slip, so say it out loud rather than\n * silently granting a half-usable seat.\n */\n if (grant.axes.includes('mutation') && !grant.axes.includes('query')) {\n logger.warn(\n `[role-template] \"${template.id}\" grants ${grant.category}:mutation without ${grant.category}:query. ` +\n 'Someone who may change a thing but not see it can rarely use either.'\n )\n }\n }\n\n if (registry()[template.id]) {\n throw new Error(`Role template \"${template.id}\" is declared twice.`)\n }\n\n registry()[template.id] = { ...template, grants: template.grants || [] }\n}\n\nexport function roleTemplates(): RegisteredRoleTemplate[] {\n return Object.values(registry())\n}\n\nexport function roleTemplateById(id: string): RegisteredRoleTemplate | undefined {\n return registry()[id]\n}\n\n/**\n * The two templates the framework can build without knowing a single category name.\n *\n * Everything else has to be declared by an application: a job title spans modules\n * (a production lead uses worklist *and* order *and* ops-master) and no one package knows\n * that. These two are different — they are defined over whatever is declared, so they can\n * be computed from the registry `@privilege` fills.\n */\nexport function computedRoleTemplates(): RegisteredRoleTemplate[] {\n const declared: [string, string][] = Object.values(process['PRIVILEGES'] || {})\n const axesOf = new Map<string, Set<string>>()\n\n for (const [category, axis] of declared) {\n if (!axesOf.has(category)) {\n axesOf.set(category, new Set())\n }\n axesOf.get(category)!.add(axis)\n }\n\n const categories = [...axesOf.keys()].sort()\n\n /*\n * No description on either of these.\n *\n * They carried i18n keys, which the seeding path stored verbatim onto the role and the\n * list then drew as the description. And a static sentence would be worse than what the\n * screen already computes from `grants` — \"opens 30 things across 30 modules\" is exact\n * and moves when the installation does.\n */\n /*\n * VIEWER is only offered when someone has said which axes are read-only.\n *\n * This used to match the literal word `query`, which is right in this house and wrong in\n * dssp, where the read axis is spelled `read` and `kpi` has no `query` at all — VIEWER\n * would have quietly dropped that installation's main module.\n *\n * And when nobody has said, the template is **not offered at all** rather than offered\n * empty. An empty VIEWER is the quiet failure this whole design is built to avoid: an\n * administrator picks it, gets a role, no door opens, and the list counts the zero\n * correctly. \"We do not know\" is not \"there are none\".\n */\n const readOnly = readOnlyAxes()\n\n const viewer: RoleTemplate | undefined = readOnlyAxesAreKnown()\n ? {\n id: 'framework.viewer',\n name: 'VIEWER',\n grants: categories\n .map(category => ({ category, axes: [...axesOf.get(category)!].filter(axis => readOnly.has(axis)).sort() }))\n .filter(grant => grant.axes.length > 0)\n }\n : undefined\n\n /*\n * The one place that widens on purpose.\n *\n * Everywhere else an unenumerated axis is a defect waiting for the day a new axis is\n * declared. Here \"everything that exists\" is the meaning of the template, so picking up\n * a newly declared axis is correct rather than accidental.\n */\n const administrator: RoleTemplate = {\n id: 'framework.administrator',\n name: 'ADMINISTRATOR',\n grants: categories.map(category => ({ category, axes: [...axesOf.get(category)!].sort() }))\n }\n\n return viewer ? [viewer, administrator] : [administrator]\n}\n\n/** Every template this installation offers — declared by apps, plus the computed two. */\nexport function allRoleTemplates(): RegisteredRoleTemplate[] {\n return [...computedRoleTemplates(), ...roleTemplates()]\n}\n\n/**\n * **A template's privileges** — its tasks' privileges and its own grants, each privilege once.\n *\n * Every reader of a template's privileges goes through this: the role screen's gap, applying it, the view the\n * template picker draws. A task id nobody registered adds nothing here; `templateDefinitionErrors` says so, and\n * applying the template refuses.\n */\nexport function templateGrantsOf(template: RoleTemplate): RoleTemplateGrant[] {\n const tasks = (template.tasks || []).map(id => roleTaskById(id)?.grants || [])\n\n return mergeGrants([...tasks, template.grants || []])\n}\n\n/**\n * What is wrong with a template as declared, before anything is applied (ADR-0139 decision 11).\n *\n * A task id nobody registered, and a privilege a task names that this installation does not declare. A task is\n * registered only by a module that is installed, so either one is a slip in code — a task id mistyped, a module the\n * application names but does not install, a category renamed under a task — and applying the template anyway would\n * give a seat that is short of work it says it does. So these refuse the template.\n *\n * A template's own `grants` naming an undeclared privilege is not here: an application may write grants for modules\n * one installation has and another does not, and those are skipped as before (`unknownTemplateGrants`).\n */\nexport function templateDefinitionErrors(template: RoleTemplate, declared: Record<string, unknown> = process['PRIVILEGES'] || {}): string[] {\n return (template.tasks || []).flatMap(id => {\n const task = roleTaskById(id)\n if (!task) return [`task \"${id}\" is not registered`]\n return undeclaredTaskGrants(task, declared).map(grant => `task \"${id}\" names ${grant}, which this installation does not declare`)\n })\n}\n\n/** What a role lacks against a template, and what it holds beyond it - see `templateGapAgainst`. */\nexport type RoleTemplateGap = {\n templateId: string\n templateName: string\n missing: { category: string; axes: string[] }[]\n missingCount: number\n /** Declared privileges the role holds that the template does not grant — wider than the template, not wrong. */\n extra: { category: string; axes: string[] }[]\n extraCount: number\n}\n\n/**\n * **A role laid against a template** (ADR-0075 decision 1, ADR-0139 decision 11).\n *\n * A template is a seed: the role it made belongs to the administrator afterwards, and nothing\n * pushes a template's later grants into it. When a template gains a door - operato-plant's nine\n * gained ai-assistant - the roles already standing fall quietly behind. The answer is the one this\n * file's header gives: the template's current grants against the role's current privileges,\n * computed, never stored. The screen shows it and the administrator adds it; nothing adds it for them.\n *\n * `extra` is the other side: what the role holds that the template does not grant. It is not a fault —\n * a site may have widened the role on purpose — and nothing takes it away; the screen says \"wider than\n * the template\". Only privileges this installation declares count on either side: a grant it does not\n * declare is `missingCount` on the template, and a held privilege nothing declares is struck through\n * on the role screen already.\n */\nexport function templateGapAgainst(\n template: RoleTemplate,\n held: ReadonlySet<string>,\n declared: Record<string, unknown> = process['PRIVILEGES'] || {}\n): RoleTemplateGap {\n const grants = templateGrantsOf(template)\n const granted = new Set(grants.flatMap(grant => grant.axes.map(axis => privilegeKey(grant.category, axis))))\n\n const missing = grants\n .map(grant => ({\n category: grant.category,\n axes: grant.axes.filter(axis => declared[privilegeKey(grant.category, axis)] && !held.has(privilegeKey(grant.category, axis)))\n }))\n .filter(grant => grant.axes.length > 0)\n\n const extra = mergeGrants([\n [...held]\n .filter(key => declared[key] && !granted.has(key))\n .sort()\n .map(key => {\n const [category, axis] = key.split(' ')\n return { category, axes: [axis] }\n })\n ])\n\n return {\n templateId: template.id,\n templateName: template.name,\n missing,\n missingCount: missing.reduce((count, grant) => count + grant.axes.length, 0),\n extra,\n extraCount: extra.reduce((count, grant) => count + grant.axes.length, 0)\n }\n}\n\n/**\n * The gap against the template of this role's name — the pair the role list shows unasked.\n *\n * Paired by name, because provenance is not stored. A role whose name no template carries — renamed by its\n * administrator, or named in the site's own words as operato-office's roles are — has no pair here; the role\n * screen lets the administrator pick the template to lay it against (`templateGapAgainst`). The role is never\n * renamed to make a pair: approval lines hold the role, and the name is the site's.\n * `null` when no template carries this name; a gap of 0 missing is a role that is up to date.\n */\nexport function templateGapOf(\n roleName: string,\n held: ReadonlySet<string>,\n templates: RegisteredRoleTemplate[] = allRoleTemplates(),\n declared: Record<string, unknown> = process['PRIVILEGES'] || {}\n): RoleTemplateGap | null {\n const template = templates.find(one => one.name === roleName)\n return template ? templateGapAgainst(template, held, declared) : null\n}\n\n/**\n * Names a template's own grants mention that this installation does not declare.\n *\n * A template that survives a category rename becomes a role that grants nothing, and that\n * is the quietest way this design can fail — the administrator picks \"quality inspector\",\n * gets a role, and it opens no doors. operato-plant's seed already does this for its own\n * table (`seed-demo-org.ts` warns about grants it could not attach); this is the same\n * check moved to where templates are declared. A task's privileges are checked as\n * definition errors instead (`templateDefinitionErrors`).\n */\nexport function unknownTemplateGrants(template: RoleTemplate): string[] {\n const declared = process['PRIVILEGES'] || {}\n\n return (template.grants || []).flatMap(grant =>\n grant.axes.filter(axis => !declared[privilegeKey(grant.category, axis)]).map(axis => `${grant.category}:${axis}`)\n )\n}\n\n/**\n * Boot's one look at what templates and tasks name — after every module has loaded and `@privilege` has filled\n * `process['PRIVILEGES']`, the first moment both registries and the declarations are complete.\n */\nexport function reportUnknownTemplateGrants(): void {\n const unknown = roleTemplates().flatMap(template =>\n unknownTemplateGrants(template).map(grant => `${template.id} → ${grant}`)\n )\n\n if (unknown.length) {\n logger.warn(\n `[role-template] ${unknown.length} grants name privileges this installation does not declare — ` +\n `${unknown.join(', ')}. Privilege definitions come from @privilege at boot, so either the query ` +\n 'is not installed here or the name has moved. A role seeded from such a template opens nothing.'\n )\n }\n\n const broken = [\n ...roleTasks().flatMap(task => undeclaredTaskGrants(task).map(grant => `task \"${task.id}\" names ${grant}`)),\n ...roleTemplates().flatMap(template =>\n (template.tasks || []).filter(id => !roleTaskById(id)).map(id => `${template.id} → task \"${id}\" is not registered`)\n )\n ]\n\n if (broken.length) {\n logger.error(\n `[role-task] ${broken.length} definition errors — ${broken.join(', ')}. ` +\n 'A template that names any of these is refused when applied, so no seat is stood up short of the work it says it does.'\n )\n }\n}\n"]}
|