@things-factory/auth-base 10.1.6 → 10.1.7

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.
Files changed (35) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist-server/index.js +8 -0
  3. package/dist-server/index.js.map +1 -1
  4. package/dist-server/service/index.d.ts +3 -1
  5. package/dist-server/service/index.js +15 -11
  6. package/dist-server/service/index.js.map +1 -1
  7. package/dist-server/service/privilege/privilege-directive.d.ts +10 -41
  8. package/dist-server/service/privilege/privilege-directive.js +14 -74
  9. package/dist-server/service/privilege/privilege-directive.js.map +1 -1
  10. package/dist-server/service/role/role-mutation.js +2 -2
  11. package/dist-server/service/role/role-mutation.js.map +1 -1
  12. package/dist-server/service/role/role-query.d.ts +43 -1
  13. package/dist-server/service/role/role-query.js +76 -33
  14. package/dist-server/service/role/role-query.js.map +1 -1
  15. package/dist-server/service/role-template/index.d.ts +4 -0
  16. package/dist-server/service/role-template/index.js +9 -0
  17. package/dist-server/service/role-template/index.js.map +1 -0
  18. package/dist-server/service/role-template/role-template-mutation.d.ts +39 -0
  19. package/dist-server/service/role-template/role-template-mutation.js +124 -0
  20. package/dist-server/service/role-template/role-template-mutation.js.map +1 -0
  21. package/dist-server/service/role-template/role-template-query.d.ts +11 -0
  22. package/dist-server/service/role-template/role-template-query.js +79 -0
  23. package/dist-server/service/role-template/role-template-query.js.map +1 -0
  24. package/dist-server/service/role-template/role-template-types.d.ts +51 -0
  25. package/dist-server/service/role-template/role-template-types.js +71 -0
  26. package/dist-server/service/role-template/role-template-types.js.map +1 -0
  27. package/dist-server/service/role-template/role-template.d.ts +110 -0
  28. package/dist-server/service/role-template/role-template.js +129 -0
  29. package/dist-server/service/role-template/role-template.js.map +1 -0
  30. package/dist-server/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +4 -4
  32. package/tests/privilege-directive.test.ts +1 -91
  33. package/tests/role-privileges-db.test.ts +98 -0
  34. package/tests/role-template-seed-db.test.ts +179 -0
  35. package/tests/role-template.test.ts +126 -0
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RoleTemplateView = exports.RoleTemplateGrantView = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const type_graphql_1 = require("type-graphql");
6
+ /**
7
+ * What a template looks like *to this installation*.
8
+ *
9
+ * The declared template and the offered template are not the same thing: an app declares
10
+ * grants across modules it expects to be installed, and a given installation has a subset.
11
+ * operato-plant declares 14 categories; the plant app running on :4000 sees 33 of the 47
12
+ * categories declared across the repository. So the answer has to say which parts of the
13
+ * template this installation can actually honour.
14
+ */
15
+ let RoleTemplateGrantView = class RoleTemplateGrantView {
16
+ };
17
+ exports.RoleTemplateGrantView = RoleTemplateGrantView;
18
+ tslib_1.__decorate([
19
+ (0, type_graphql_1.Field)({ description: 'The privilege category — what is guarded.' }),
20
+ tslib_1.__metadata("design:type", String)
21
+ ], RoleTemplateGrantView.prototype, "category", void 0);
22
+ tslib_1.__decorate([
23
+ (0, type_graphql_1.Field)(type => [String], { description: 'The privilege axes granted on that category.' }),
24
+ tslib_1.__metadata("design:type", Array)
25
+ ], RoleTemplateGrantView.prototype, "axes", void 0);
26
+ tslib_1.__decorate([
27
+ (0, type_graphql_1.Field)(type => [String], { description: 'Axes named by the template that this installation does not declare.' }),
28
+ tslib_1.__metadata("design:type", Array)
29
+ ], RoleTemplateGrantView.prototype, "missingAxes", void 0);
30
+ exports.RoleTemplateGrantView = RoleTemplateGrantView = tslib_1.__decorate([
31
+ (0, type_graphql_1.ObjectType)({ description: 'One module and the axes a role template grants on it.' })
32
+ ], RoleTemplateGrantView);
33
+ let RoleTemplateView = class RoleTemplateView {
34
+ };
35
+ exports.RoleTemplateView = RoleTemplateView;
36
+ tslib_1.__decorate([
37
+ (0, type_graphql_1.Field)({ description: 'Declaration key, namespaced by the declaring application.' }),
38
+ tslib_1.__metadata("design:type", String)
39
+ ], RoleTemplateView.prototype, "id", void 0);
40
+ tslib_1.__decorate([
41
+ (0, type_graphql_1.Field)({ description: 'The role name this template creates in a domain.' }),
42
+ tslib_1.__metadata("design:type", String)
43
+ ], RoleTemplateView.prototype, "name", void 0);
44
+ tslib_1.__decorate([
45
+ (0, type_graphql_1.Field)({ nullable: true, description: 'A description of the seat this template creates.' }),
46
+ tslib_1.__metadata("design:type", String)
47
+ ], RoleTemplateView.prototype, "description", void 0);
48
+ tslib_1.__decorate([
49
+ (0, type_graphql_1.Field)(type => [RoleTemplateGrantView], { description: 'The modules and axes this template grants.' }),
50
+ tslib_1.__metadata("design:type", Array)
51
+ ], RoleTemplateView.prototype, "grants", void 0);
52
+ tslib_1.__decorate([
53
+ (0, type_graphql_1.Field)(type => type_graphql_1.Int, { description: 'How many privileges this template would actually grant here.' }),
54
+ tslib_1.__metadata("design:type", Number)
55
+ ], RoleTemplateView.prototype, "privilegeCount", void 0);
56
+ tslib_1.__decorate([
57
+ (0, type_graphql_1.Field)(type => type_graphql_1.Int, { description: 'How many privileges the template names that this installation does not have.' }),
58
+ tslib_1.__metadata("design:type", Number)
59
+ ], RoleTemplateView.prototype, "missingCount", void 0);
60
+ tslib_1.__decorate([
61
+ (0, type_graphql_1.Field)({ description: "Whether a role already carries this template's default name in the current domain." }),
62
+ tslib_1.__metadata("design:type", Boolean)
63
+ ], RoleTemplateView.prototype, "defaultNameTaken", void 0);
64
+ tslib_1.__decorate([
65
+ (0, type_graphql_1.Field)({ description: 'Whether this template grants nothing on purpose, rather than by omission.' }),
66
+ tslib_1.__metadata("design:type", Boolean)
67
+ ], RoleTemplateView.prototype, "grantsNothingDeliberately", void 0);
68
+ exports.RoleTemplateView = RoleTemplateView = tslib_1.__decorate([
69
+ (0, type_graphql_1.ObjectType)({ description: 'A role template offered by this installation.' })
70
+ ], RoleTemplateView);
71
+ //# sourceMappingURL=role-template-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"role-template-types.js","sourceRoot":"","sources":["../../../server/service/role-template/role-template-types.ts"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,EAAE,CAAC;;sDAChG;AAepB;IADC,IAAA,oBAAK,EAAC,EAAE,WAAW,EAAE,oFAAoF,EAAE,CAAC;;0DACpF;AAWzB;IADC,IAAA,oBAAK,EAAC,EAAE,WAAW,EAAE,2EAA2E,EAAE,CAAC;;mEAClE;2BA3CvB,gBAAgB;IAD5B,IAAA,yBAAU,EAAC,EAAE,WAAW,EAAE,+CAA+C,EAAE,CAAC;GAChE,gBAAgB,CA4C5B","sourcesContent":["import { Field, Int, ObjectType } from 'type-graphql'\n\n/**\n * What a template looks like *to this installation*.\n *\n * The declared template and the offered template are not the same thing: an app declares\n * grants across modules it expects to be installed, and a given installation has a subset.\n * operato-plant declares 14 categories; the plant app running on :4000 sees 33 of the 47\n * categories declared across the repository. So the answer has to say which parts of the\n * template this installation can actually honour.\n */\n@ObjectType({ description: 'One module and the axes a role template grants on it.' })\nexport class RoleTemplateGrantView {\n @Field({ description: 'The privilege category — what is guarded.' })\n category: string\n\n @Field(type => [String], { description: 'The privilege axes granted on that category.' })\n axes: string[]\n\n /**\n * Axes this installation does not declare.\n *\n * Dropping them quietly would hand the administrator a role that does not match the\n * template's name, with no way to tell. They are reported instead, and the screen says\n * \"not present in this installation\".\n */\n @Field(type => [String], { description: 'Axes named by the template that this installation does not declare.' })\n missingAxes: string[]\n}\n\n@ObjectType({ description: 'A role template offered by this installation.' })\nexport class RoleTemplateView {\n @Field({ description: 'Declaration key, namespaced by the declaring application.' })\n id: string\n\n @Field({ description: 'The role name this template creates in a domain.' })\n name: string\n\n @Field({ nullable: true, description: 'A description of the seat this template creates.' })\n description?: string\n\n @Field(type => [RoleTemplateGrantView], { description: 'The modules and axes this template grants.' })\n grants: RoleTemplateGrantView[]\n\n @Field(type => Int, { description: 'How many privileges this template would actually grant here.' })\n privilegeCount: number\n\n @Field(type => Int, { description: 'How many privileges the template names that this installation does not have.' })\n missingCount: number\n\n /**\n * Whether a role already carries this template's default name in the current domain.\n *\n * The screen warns before creating one, because seeding onto an existing name does not\n * make a second role — approval lines hold a role's id, so a duplicate would leave the\n * approval routed to the role nobody edits. The existing role is used as it stands and\n * the template's privileges are added to it.\n *\n * It says \"default name\" rather than \"seeded\" because the administrator may give the\n * role a name of their own, and then nothing here can tell whether this template was\n * used. That is deliberate: provenance is not stored.\n */\n @Field({ description: \"Whether a role already carries this template's default name in the current domain.\" })\n defaultNameTaken: boolean\n\n /**\n * True when the template deliberately opens nothing.\n *\n * A seat that only receives approvals is legitimate — operato-twin declares seven roles\n * with no privileges at all, used as the target an approval line points at. The screen\n * must not draw that as \"0 privileges, something is wrong\", so the distinction is\n * carried here rather than inferred from an empty list.\n */\n @Field({ description: 'Whether this template grants nothing on purpose, rather than by omission.' })\n grantsNothingDeliberately: boolean\n}\n"]}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Role templates — the seeds an application offers so a new domain does not start with
3
+ * zero roles.
4
+ *
5
+ * ── Why this is a boot registry and not a table ───────────────────────────────
6
+ * A template is a declaration. Copying it into rows per domain gives 100 domains x 3
7
+ * templates = 300 rows, and once the code changes the template there is nowhere to ask
8
+ * which of the two is true: the declaration, or the row that was written from an older
9
+ * one. `@privilege` lives in the boot registry for the same reason.
10
+ *
11
+ * ── What a template is not ───────────────────────────────────────────────────
12
+ * It is a seed, not a role. Once it has been used the resulting role belongs to the
13
+ * administrator: the template is never re-applied and never overwrites. Nothing is written
14
+ * onto the role to remember where it came from — what an administrator would want from
15
+ * that ("the template gained two privileges, here they are") is the template's current
16
+ * grants against the role's current privileges, and that is computed rather than stored.
17
+ *
18
+ * ── A role is one thing seen from three sides ────────────────────────────────
19
+ * operato-plant uses roles as privilege bundles; operato-twin uses them as the seat an
20
+ * approval line points at (`worklist` resolves an approver of type `Role` by id). Those
21
+ * are not two concepts. A role is a seat in the organisation: what it opens, who fills it,
22
+ * and whether approvals route to it. Splitting them would put two QUALITY-INSPECTORs in
23
+ * one plant and force every person into both.
24
+ */
25
+ /** One module and the axes this template grants on it. Axes are always enumerated. */
26
+ export type RoleTemplateGrant = {
27
+ /** `@privilege(category:)` — what is guarded, not the package name. */
28
+ category: string;
29
+ /**
30
+ * `query` / `mutation` / ... — listed one by one.
31
+ *
32
+ * There is deliberately no `'all'`. A template written as "everything" would widen on
33
+ * its own the day a new axis is declared, with nobody having touched it. The single
34
+ * place that may widen is the framework-computed administrator template, which means
35
+ * "whatever exists now" by definition.
36
+ */
37
+ axes: string[];
38
+ };
39
+ export type RoleTemplate = {
40
+ /** Declaration key, namespaced by the declaring app: `plant.quality-inspector`. */
41
+ id: string;
42
+ /**
43
+ * The role name created in the domain: `QUALITY-INSPECTOR`.
44
+ *
45
+ * Two apps may declare the same name on purpose — that is one seat, and the privileges
46
+ * become the union. The name is also a stored identifier: approval lines hold the role's
47
+ * id, so renaming here creates a second role and leaves the approval line pointing at
48
+ * the first. Renaming is a migration, not a template edit.
49
+ */
50
+ name: string;
51
+ /**
52
+ * Plain text, not an i18n key.
53
+ *
54
+ * `createRoleFromTemplate` copies this onto the role it seeds, and the role list draws
55
+ * the role's description as a value — so a key written here reaches the screen as
56
+ * `label.role-template.administrator`. That is exactly what the framework's own two
57
+ * templates did until 2026-09-11.
58
+ *
59
+ * operato-plant had it right from the start: "검사 기준과 처분을 정하는 자리".
60
+ */
61
+ description?: string;
62
+ /**
63
+ * Required, and may be empty.
64
+ *
65
+ * `[]` is a statement: this seat opens nothing, it exists for approval routing. A
66
+ * missing `grants` is a different thing — someone forgot — and is rejected here, so the
67
+ * screen can tell "opens nothing on purpose" from "nothing was written down".
68
+ */
69
+ grants: RoleTemplateGrant[];
70
+ };
71
+ /**
72
+ * Registered form of a template.
73
+ *
74
+ * Identical to the declaration today. It stays a separate name because the registry is
75
+ * what every reader goes through, and giving it its own type keeps a later addition from
76
+ * rippling into every call site.
77
+ */
78
+ export type RegisteredRoleTemplate = RoleTemplate;
79
+ /**
80
+ * Declares a role template. Call it from the application's server entry.
81
+ *
82
+ * Categories are not checked here: registration runs while modules load, before the
83
+ * schema is built and therefore before `process['PRIVILEGES']` exists. The name check
84
+ * happens at boot instead (`reportUnknownTemplateGrants`).
85
+ */
86
+ export declare function registerRoleTemplate(template: RoleTemplate): void;
87
+ export declare function roleTemplates(): RegisteredRoleTemplate[];
88
+ export declare function roleTemplateById(id: string): RegisteredRoleTemplate | undefined;
89
+ /**
90
+ * The two templates the framework can build without knowing a single category name.
91
+ *
92
+ * Everything else has to be declared by an application: a job title spans modules
93
+ * (a production lead uses worklist *and* order *and* ops-master) and no one package knows
94
+ * that. These two are different — they are defined over whatever is declared, so they can
95
+ * be computed from the registry `@privilege` fills.
96
+ */
97
+ export declare function computedRoleTemplates(): RegisteredRoleTemplate[];
98
+ /** Every template this installation offers — declared by apps, plus the computed two. */
99
+ export declare function allRoleTemplates(): RegisteredRoleTemplate[];
100
+ /**
101
+ * Names a template mentions that this installation does not declare.
102
+ *
103
+ * A template that survives a category rename becomes a role that grants nothing, and that
104
+ * is the quietest way this design can fail — the administrator picks "quality inspector",
105
+ * gets a role, and it opens no doors. operato-plant's seed already does this for its own
106
+ * table (`seed-demo-org.ts` warns about grants it could not attach); this is the same
107
+ * check moved to where templates are declared.
108
+ */
109
+ export declare function unknownTemplateGrants(template: RoleTemplate): string[];
110
+ export declare function reportUnknownTemplateGrants(): void;
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerRoleTemplate = registerRoleTemplate;
4
+ exports.roleTemplates = roleTemplates;
5
+ exports.roleTemplateById = roleTemplateById;
6
+ exports.computedRoleTemplates = computedRoleTemplates;
7
+ exports.allRoleTemplates = allRoleTemplates;
8
+ exports.unknownTemplateGrants = unknownTemplateGrants;
9
+ exports.reportUnknownTemplateGrants = reportUnknownTemplateGrants;
10
+ const env_1 = require("@things-factory/env");
11
+ const REGISTRY = 'ROLE_TEMPLATES';
12
+ process[REGISTRY] = {};
13
+ function registry() {
14
+ return process[REGISTRY];
15
+ }
16
+ /**
17
+ * Declares a role template. Call it from the application's server entry.
18
+ *
19
+ * Categories are not checked here: registration runs while modules load, before the
20
+ * schema is built and therefore before `process['PRIVILEGES']` exists. The name check
21
+ * happens at boot instead (`reportUnknownTemplateGrants`).
22
+ */
23
+ function registerRoleTemplate(template) {
24
+ if (!template?.id || !template?.name) {
25
+ throw new Error('A role template needs both an id and a name.');
26
+ }
27
+ if (!Array.isArray(template.grants)) {
28
+ throw new Error(`Role template "${template.id}" has no grants. Write \`grants: []\` if this seat is meant to ` +
29
+ 'open nothing — leaving it out is indistinguishable from forgetting, and the screen cannot ' +
30
+ 'tell an administrator which one it is.');
31
+ }
32
+ for (const grant of template.grants) {
33
+ if (!grant?.category || !Array.isArray(grant.axes) || grant.axes.length === 0) {
34
+ throw new Error(`Role template "${template.id}" has a grant without a category or with no axes. ` +
35
+ 'Axes are always listed one by one.');
36
+ }
37
+ /*
38
+ * Across the nine roles operato-plant declares, no role is granted `mutation` on a
39
+ * category without `query` on the same category — you cannot fix what you cannot see.
40
+ * A template that does so is almost certainly a slip, so say it out loud rather than
41
+ * silently granting a half-usable seat.
42
+ */
43
+ if (grant.axes.includes('mutation') && !grant.axes.includes('query')) {
44
+ env_1.logger.warn(`[role-template] "${template.id}" grants ${grant.category}:mutation without ${grant.category}:query. ` +
45
+ 'Someone who may change a thing but not see it can rarely use either.');
46
+ }
47
+ }
48
+ if (registry()[template.id]) {
49
+ throw new Error(`Role template "${template.id}" is declared twice.`);
50
+ }
51
+ registry()[template.id] = { ...template };
52
+ }
53
+ function roleTemplates() {
54
+ return Object.values(registry());
55
+ }
56
+ function roleTemplateById(id) {
57
+ return registry()[id];
58
+ }
59
+ /**
60
+ * The two templates the framework can build without knowing a single category name.
61
+ *
62
+ * Everything else has to be declared by an application: a job title spans modules
63
+ * (a production lead uses worklist *and* order *and* ops-master) and no one package knows
64
+ * that. These two are different — they are defined over whatever is declared, so they can
65
+ * be computed from the registry `@privilege` fills.
66
+ */
67
+ function computedRoleTemplates() {
68
+ const declared = Object.values(process['PRIVILEGES'] || {});
69
+ const axesOf = new Map();
70
+ for (const [category, axis] of declared) {
71
+ if (!axesOf.has(category)) {
72
+ axesOf.set(category, new Set());
73
+ }
74
+ axesOf.get(category).add(axis);
75
+ }
76
+ const categories = [...axesOf.keys()].sort();
77
+ /*
78
+ * No description on either of these.
79
+ *
80
+ * They carried i18n keys, which the seeding path stored verbatim onto the role and the
81
+ * list then drew as the description. And a static sentence would be worse than what the
82
+ * screen already computes from `grants` — "opens 30 things across 30 modules" is exact
83
+ * and moves when the installation does.
84
+ */
85
+ const viewer = {
86
+ id: 'framework.viewer',
87
+ name: 'VIEWER',
88
+ grants: categories.filter(category => axesOf.get(category).has('query')).map(category => ({ category, axes: ['query'] }))
89
+ };
90
+ /*
91
+ * The one place that widens on purpose.
92
+ *
93
+ * Everywhere else an unenumerated axis is a defect waiting for the day a new axis is
94
+ * declared. Here "everything that exists" is the meaning of the template, so picking up
95
+ * a newly declared axis is correct rather than accidental.
96
+ */
97
+ const administrator = {
98
+ id: 'framework.administrator',
99
+ name: 'ADMINISTRATOR',
100
+ grants: categories.map(category => ({ category, axes: [...axesOf.get(category)].sort() }))
101
+ };
102
+ return [viewer, administrator];
103
+ }
104
+ /** Every template this installation offers — declared by apps, plus the computed two. */
105
+ function allRoleTemplates() {
106
+ return [...computedRoleTemplates(), ...roleTemplates()];
107
+ }
108
+ /**
109
+ * Names a template mentions that this installation does not declare.
110
+ *
111
+ * A template that survives a category rename becomes a role that grants nothing, and that
112
+ * is the quietest way this design can fail — the administrator picks "quality inspector",
113
+ * gets a role, and it opens no doors. operato-plant's seed already does this for its own
114
+ * table (`seed-demo-org.ts` warns about grants it could not attach); this is the same
115
+ * check moved to where templates are declared.
116
+ */
117
+ function unknownTemplateGrants(template) {
118
+ const declared = process['PRIVILEGES'] || {};
119
+ return template.grants.flatMap(grant => grant.axes.filter(axis => !declared[`${grant.category} ${axis}`]).map(axis => `${grant.category}:${axis}`));
120
+ }
121
+ function reportUnknownTemplateGrants() {
122
+ const unknown = roleTemplates().flatMap(template => unknownTemplateGrants(template).map(grant => `${template.id} → ${grant}`));
123
+ if (unknown.length) {
124
+ env_1.logger.warn(`[role-template] ${unknown.length} grants name privileges this installation does not declare — ` +
125
+ `${unknown.join(', ')}. Privilege definitions come from @privilege at boot, so either the query ` +
126
+ 'is not installed here or the name has moved. A role seeded from such a template opens nothing.');
127
+ }
128
+ }
129
+ //# sourceMappingURL=role-template.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"role-template.js","sourceRoot":"","sources":["../../../server/service/role-template/role-template.ts"],"names":[],"mappings":";;AAmGA,oDAwCC;AAED,sCAEC;AAED,4CAEC;AAUD,sDAyCC;AAGD,4CAEC;AAWD,sDAMC;AAED,kEAYC;AA1OD,6CAA4C;AAoF5C,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;Y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,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\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 const viewer: RoleTemplate = {\n id: 'framework.viewer',\n name: 'VIEWER',\n grants: categories.filter(category => axesOf.get(category)!.has('query')).map(category => ({ category, axes: ['query'] }))\n }\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, 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 * 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"]}