@astram/identity 0.3.1 → 0.5.0

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 (118) hide show
  1. package/dist/company-membership.object.d.ts.map +1 -1
  2. package/dist/company-membership.object.js +3 -2
  3. package/dist/company.object.d.ts.map +1 -1
  4. package/dist/company.object.js +8 -3
  5. package/dist/features/access/index.d.ts.map +1 -1
  6. package/dist/features/access/index.js +1 -5
  7. package/dist/features/audit/audit-read.service.d.ts.map +1 -1
  8. package/dist/features/audit/audit-read.service.js +13 -6
  9. package/dist/features/audit/audit.controller.d.ts.map +1 -1
  10. package/dist/features/audit/audit.controller.js +2 -0
  11. package/dist/features/audit/mariadb-audit-reader.d.ts.map +1 -1
  12. package/dist/features/authz/access-bundle-reconcile.job.d.ts.map +1 -1
  13. package/dist/features/authz/catalog-reconcile.job.d.ts.map +1 -1
  14. package/dist/features/authz/catalog-reconcile.job.js +1 -3
  15. package/dist/features/authz/db-permission-facts.adapter.d.ts +24 -4
  16. package/dist/features/authz/db-permission-facts.adapter.d.ts.map +1 -1
  17. package/dist/features/authz/db-permission-facts.adapter.js +92 -5
  18. package/dist/features/authz/grant-admin.controller.d.ts +8 -5
  19. package/dist/features/authz/grant-admin.controller.d.ts.map +1 -1
  20. package/dist/features/authz/grant-admin.controller.js +22 -15
  21. package/dist/features/authz/grant-admin.service.d.ts +57 -11
  22. package/dist/features/authz/grant-admin.service.d.ts.map +1 -1
  23. package/dist/features/authz/grant-admin.service.js +163 -85
  24. package/dist/features/authz/grant-ceiling.d.ts +111 -0
  25. package/dist/features/authz/grant-ceiling.d.ts.map +1 -0
  26. package/dist/features/authz/grant-ceiling.js +248 -0
  27. package/dist/features/authz/mariadb-catalog-store.d.ts.map +1 -1
  28. package/dist/features/authz/mariadb-catalog-store.js +15 -3
  29. package/dist/features/authz/permission-version.bumper.d.ts +45 -1
  30. package/dist/features/authz/permission-version.bumper.d.ts.map +1 -1
  31. package/dist/features/authz/permission-version.bumper.js +106 -16
  32. package/dist/features/authz/record-authorization.d.ts.map +1 -1
  33. package/dist/features/authz/role-binding.d.ts +36 -0
  34. package/dist/features/authz/role-binding.d.ts.map +1 -0
  35. package/dist/features/authz/role-binding.js +53 -0
  36. package/dist/features/company/company-hierarchy.service.d.ts +46 -0
  37. package/dist/features/company/company-hierarchy.service.d.ts.map +1 -0
  38. package/dist/features/company/company-hierarchy.service.js +100 -0
  39. package/dist/features/company/company.controller.d.ts +23 -5
  40. package/dist/features/company/company.controller.d.ts.map +1 -1
  41. package/dist/features/company/company.controller.js +47 -12
  42. package/dist/features/company/company.service.d.ts +28 -12
  43. package/dist/features/company/company.service.d.ts.map +1 -1
  44. package/dist/features/company/company.service.js +61 -14
  45. package/dist/features/cross-tenant/cross-tenant-grant.controller.d.ts.map +1 -1
  46. package/dist/features/cross-tenant/cross-tenant-grant.controller.js +2 -0
  47. package/dist/features/identity-feature.module.d.ts.map +1 -1
  48. package/dist/features/identity-feature.module.js +123 -6
  49. package/dist/features/membership/membership.controller.d.ts +6 -5
  50. package/dist/features/membership/membership.controller.d.ts.map +1 -1
  51. package/dist/features/membership/membership.controller.js +34 -22
  52. package/dist/features/membership/membership.service.d.ts +20 -14
  53. package/dist/features/membership/membership.service.d.ts.map +1 -1
  54. package/dist/features/membership/membership.service.js +62 -29
  55. package/dist/features/membership/posting-version-bump.d.ts +5 -0
  56. package/dist/features/membership/posting-version-bump.d.ts.map +1 -1
  57. package/dist/features/membership/posting.service.d.ts +20 -24
  58. package/dist/features/membership/posting.service.d.ts.map +1 -1
  59. package/dist/features/membership/posting.service.js +101 -64
  60. package/dist/features/org-structure/org-catalog.controller.d.ts.map +1 -1
  61. package/dist/features/org-structure/org-catalog.controller.js +2 -0
  62. package/dist/features/org-structure/org-catalog.service.d.ts.map +1 -1
  63. package/dist/features/org-structure/org-catalog.service.js +4 -1
  64. package/dist/features/policy/policy.controller.d.ts.map +1 -1
  65. package/dist/features/policy/policy.controller.js +2 -0
  66. package/dist/features/role/role.controller.d.ts +3 -2
  67. package/dist/features/role/role.controller.d.ts.map +1 -1
  68. package/dist/features/role/role.controller.js +15 -10
  69. package/dist/features/role/role.service.d.ts +16 -8
  70. package/dist/features/role/role.service.d.ts.map +1 -1
  71. package/dist/features/role/role.service.js +24 -10
  72. package/dist/features/scope/scope-closure.service.d.ts +44 -4
  73. package/dist/features/scope/scope-closure.service.d.ts.map +1 -1
  74. package/dist/features/scope/scope-closure.service.js +215 -82
  75. package/dist/features/service-account/service-account.controller.d.ts +63 -0
  76. package/dist/features/service-account/service-account.controller.d.ts.map +1 -0
  77. package/dist/features/service-account/service-account.controller.js +295 -0
  78. package/dist/features/service-account/service-account.service.d.ts +209 -0
  79. package/dist/features/service-account/service-account.service.d.ts.map +1 -0
  80. package/dist/features/service-account/service-account.service.js +528 -0
  81. package/dist/features/service-account/service-account.store.d.ts +91 -0
  82. package/dist/features/service-account/service-account.store.d.ts.map +1 -0
  83. package/dist/features/service-account/service-account.store.js +158 -0
  84. package/dist/features/share/share.controller.d.ts.map +1 -1
  85. package/dist/features/share/share.controller.js +2 -0
  86. package/dist/features/share/share.service.d.ts.map +1 -1
  87. package/dist/features/shared/identity-error.interceptor.d.ts +24 -0
  88. package/dist/features/shared/identity-error.interceptor.d.ts.map +1 -0
  89. package/dist/features/shared/identity-error.interceptor.js +82 -0
  90. package/dist/features/shared/identity-errors.d.ts +23 -0
  91. package/dist/features/shared/identity-errors.d.ts.map +1 -0
  92. package/dist/features/shared/identity-errors.js +41 -0
  93. package/dist/features/user/session-revocation-check.d.ts +11 -0
  94. package/dist/features/user/session-revocation-check.d.ts.map +1 -0
  95. package/dist/features/user/session-revocation-check.js +21 -0
  96. package/dist/features/user/user.controller.d.ts +11 -6
  97. package/dist/features/user/user.controller.d.ts.map +1 -1
  98. package/dist/features/user/user.controller.js +35 -17
  99. package/dist/features/user/user.service.d.ts +55 -14
  100. package/dist/features/user/user.service.d.ts.map +1 -1
  101. package/dist/features/user/user.service.js +143 -16
  102. package/dist/index.d.ts +1 -0
  103. package/dist/index.d.ts.map +1 -1
  104. package/dist/index.js +3 -1
  105. package/dist/membership-posting.object.d.ts.map +1 -1
  106. package/dist/membership-posting.object.js +4 -2
  107. package/dist/objects.d.ts.map +1 -1
  108. package/dist/objects.js +14 -3
  109. package/dist/role.object.d.ts.map +1 -1
  110. package/dist/role.object.js +3 -0
  111. package/dist/service-account.object.d.ts +31 -0
  112. package/dist/service-account.object.d.ts.map +1 -0
  113. package/dist/service-account.object.js +75 -0
  114. package/dist/user.object.d.ts.map +1 -1
  115. package/dist/user.object.js +3 -0
  116. package/migrations/1710000000018-CreateServiceAccount.cjs +53 -0
  117. package/migrations/1710000000019-AddSessionMustReenroll.cjs +35 -0
  118. package/package.json +4 -4
@@ -5,6 +5,11 @@
5
5
  */
6
6
  export interface PostingVersionBump {
7
7
  bumpTenant(tenantUuid: string): Promise<void>;
8
+ /**
9
+ * OPTIONAL (security hardening Phase 3): bump every tenant the user has a membership in — after
10
+ * the account itself was disabled or re-enabled. Returns the bumped tenants; throws on failure.
11
+ */
12
+ bumpForUser?(userUuid: string): Promise<string[]>;
8
13
  }
9
14
  export declare const POSTING_VERSION_BUMP: unique symbol;
10
15
  //# sourceMappingURL=posting-version-bump.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"posting-version-bump.d.ts","sourceRoot":"","sources":["../../../src/features/membership/posting-version-bump.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C;AAED,eAAO,MAAM,oBAAoB,eAAiC,CAAC"}
1
+ {"version":3,"file":"posting-version-bump.d.ts","sourceRoot":"","sources":["../../../src/features/membership/posting-version-bump.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9C;;;OAGG;IACH,WAAW,CAAC,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CACnD;AAED,eAAO,MAAM,oBAAoB,eAAiC,CAAC"}
@@ -6,21 +6,24 @@
6
6
  * diffs current-vs-desired and applies the minimal change — delete only what's removed,
7
7
  * create only what's added, keep the rest untouched.
8
8
  *
9
- * ── Atomicity / mid-op safety (why delete-then-create is safe here) ──────────────────
10
- * The engine's feature-facing surface (`RecordsService`) exposes NO cross-call transaction
11
- * — each of its methods (create/update/delete/list/get) runs in its OWN transaction, and
12
- * no `externalQueryRunner` seam is handed to features. So `replacePostings` cannot wrap
13
- * the whole diff in a single DB transaction. Instead it is safe BY CONSTRUCTION:
14
- * - Deletes target ONLY rows absent from `desired` (the "removed" set). A KEPT posting
15
- * is never a delete target, so it is never removed.
16
- * - Creates target ONLY rows absent from `current` (the "added" set).
17
- * Therefore the membership NEVER lands empty mid-op: even if a create fails after the
18
- * delete phase, every kept posting is still present (never deleted). Ordering is
19
- * delete-first, then create; a failure surfaces to the caller (fail-closed) with the
20
- * kept set intact. (Covered by the mid-op-safety tests in posting.service.spec.ts.)
9
+ * ── Authority (security hardening #11) ─────────────────────────────────────────────────
10
+ * A posting IS a role assignment, so before ANY write the actor (the request principal) must:
11
+ * - reach the membership (a walled `records.get` → null ⇒ 404; the engine's ADR-0034 create
12
+ * wall re-checks the posting's membership company inside the transaction);
13
+ * - for every ADDED role: the role is bound within the actor's subtree or not bound at all
14
+ * (`GrantCeiling.assertRoleAssignable` — a role also used elsewhere is never assignable),
15
+ * and the role's keys are all held by the actor (`assertCanAssignRoles`, ADR-0029 applied to
16
+ * assignment). Removals need no ceiling: self-demotion stays allowed.
17
+ *
18
+ * ── Atomicity (KI #16) ────────────────────────────────────────────────────────────────
19
+ * The diff runs inside ONE `records.transaction` (ADR-0034): creates FIRST, then deletes, so a
20
+ * failure at any point rolls back every posting write of the replace (no partial set, no
21
+ * window where a removed role is gone but its replacement was refused).
21
22
  */
22
23
  import { RecordsService, type RecordView } from '@astram/engine';
24
+ import type { Principal } from '@astram/core';
23
25
  import { type PostingVersionBump } from './posting-version-bump.js';
26
+ import { GrantCeiling } from '../authz/grant-ceiling.js';
24
27
  /**
25
28
  * The shape of one desired posting row submitted by the editor. The effective-window
26
29
  * bounds arrive from the client as a bare `YYYY-MM-DD` date string (native `<input
@@ -36,24 +39,17 @@ export interface PostingInput {
36
39
  }
37
40
  export declare class PostingService {
38
41
  private readonly records;
42
+ private readonly ceiling;
39
43
  private readonly versionBump?;
40
- constructor(records: RecordsService, versionBump?: (PostingVersionBump | null) | undefined);
44
+ constructor(records: RecordsService, ceiling: GrantCeiling, versionBump?: (PostingVersionBump | null) | undefined);
41
45
  /** List a membership's postings (tenant-scoped by the pipe). */
42
46
  listPostings(membershipUuid: string): Promise<readonly RecordView[]>;
43
47
  /**
44
48
  * Replace a membership's full posting set with `desired`, applying the minimal diff.
45
49
  * Compares current vs desired by the natural key `roleUuid|departmentUuid|branchUuid`:
46
- * deletes removed rows, creates added rows, leaves kept rows untouched. See the file
47
- * header for the atomicity / mid-op-safety reasoning (delete-then-create is safe because
48
- * kept postings are never delete targets, so the membership never lands empty).
49
- */
50
- replacePostings(membershipUuid: string, desired: readonly PostingInput[]): Promise<void>;
51
- /**
52
- * Resolve the tenant (company uuid) a membership belongs to — the tenant whose permission
53
- * version a posting change must bump. Reads the parent `company_membership` through the
54
- * pipe (tenant-scoped, no raw SQL), returning `companyUuid` or `null` if unresolvable
55
- * (then no bump — fail-safe; never guesses a tenant).
50
+ * creates added rows, deletes removed rows, leaves kept rows untouched. Every authority check
51
+ * runs BEFORE any write; the writes run in one transaction (see the file header).
56
52
  */
57
- private resolveMembershipTenant;
53
+ replacePostings(actor: Principal, membershipUuid: string, desired: readonly PostingInput[]): Promise<void>;
58
54
  }
59
55
  //# sourceMappingURL=posting.service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"posting.service.d.ts","sourceRoot":"","sources":["../../../src/features/membership/posting.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EAAE,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE1F;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACzC;AA4DD,qBACa,cAAc;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAKxB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;gBALZ,OAAO,EAAE,cAAc,EAKvB,WAAW,CAAC,GAAE,kBAAkB,GAAG,IAAI,aAAA;IAG1D,gEAAgE;IAC1D,YAAY,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC;IAK1E;;;;;;OAMG;IACG,eAAe,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA4D9F;;;;;OAKG;YACW,uBAAuB;CAStC"}
1
+ {"version":3,"file":"posting.service.d.ts","sourceRoot":"","sources":["../../../src/features/membership/posting.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAGH,OAAO,EAAE,cAAc,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC1F,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACzC;AA0ED,qBACa,cAAc;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAGxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAKxB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;gBARZ,OAAO,EAAE,cAAc,EAGvB,OAAO,EAAE,YAAY,EAKrB,WAAW,CAAC,GAAE,kBAAkB,GAAG,IAAI,aAAA;IAG1D,gEAAgE;IAC1D,YAAY,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC;IAK1E;;;;;OAKG;IACG,eAAe,CACnB,KAAK,EAAE,SAAS,EAChB,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,SAAS,YAAY,EAAE,GAC/B,OAAO,CAAC,IAAI,CAAC;CAuGjB"}
@@ -7,18 +7,19 @@
7
7
  * diffs current-vs-desired and applies the minimal change — delete only what's removed,
8
8
  * create only what's added, keep the rest untouched.
9
9
  *
10
- * ── Atomicity / mid-op safety (why delete-then-create is safe here) ──────────────────
11
- * The engine's feature-facing surface (`RecordsService`) exposes NO cross-call transaction
12
- * — each of its methods (create/update/delete/list/get) runs in its OWN transaction, and
13
- * no `externalQueryRunner` seam is handed to features. So `replacePostings` cannot wrap
14
- * the whole diff in a single DB transaction. Instead it is safe BY CONSTRUCTION:
15
- * - Deletes target ONLY rows absent from `desired` (the "removed" set). A KEPT posting
16
- * is never a delete target, so it is never removed.
17
- * - Creates target ONLY rows absent from `current` (the "added" set).
18
- * Therefore the membership NEVER lands empty mid-op: even if a create fails after the
19
- * delete phase, every kept posting is still present (never deleted). Ordering is
20
- * delete-first, then create; a failure surfaces to the caller (fail-closed) with the
21
- * kept set intact. (Covered by the mid-op-safety tests in posting.service.spec.ts.)
10
+ * ── Authority (security hardening #11) ─────────────────────────────────────────────────
11
+ * A posting IS a role assignment, so before ANY write the actor (the request principal) must:
12
+ * - reach the membership (a walled `records.get` → null ⇒ 404; the engine's ADR-0034 create
13
+ * wall re-checks the posting's membership company inside the transaction);
14
+ * - for every ADDED role: the role is bound within the actor's subtree or not bound at all
15
+ * (`GrantCeiling.assertRoleAssignable` — a role also used elsewhere is never assignable),
16
+ * and the role's keys are all held by the actor (`assertCanAssignRoles`, ADR-0029 applied to
17
+ * assignment). Removals need no ceiling: self-demotion stays allowed.
18
+ *
19
+ * ── Atomicity (KI #16) ────────────────────────────────────────────────────────────────
20
+ * The diff runs inside ONE `records.transaction` (ADR-0034): creates FIRST, then deletes, so a
21
+ * failure at any point rolls back every posting write of the replace (no partial set, no
22
+ * window where a removed role is gone but its replacement was refused).
22
23
  */
23
24
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
24
25
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -37,8 +38,18 @@ exports.PostingService = void 0;
37
38
  const common_1 = require("@nestjs/common");
38
39
  const engine_1 = require("@astram/engine");
39
40
  const posting_version_bump_js_1 = require("./posting-version-bump.js");
41
+ const grant_ceiling_js_1 = require("../authz/grant-ceiling.js");
42
+ const identity_errors_js_1 = require("../shared/identity-errors.js");
40
43
  /** The field keys whose values are effective-window epoch-ms bounds (normalized on write). */
41
44
  const WINDOW_KEYS = new Set(['effectiveFrom', 'effectiveUntil']);
45
+ /** The ONLY keys a client posting entry may set (review F4) — never `membershipUuid`. */
46
+ const POSTING_KEYS = [
47
+ 'roleUuid',
48
+ 'departmentUuid',
49
+ 'branchUuid',
50
+ 'effectiveFrom',
51
+ 'effectiveUntil',
52
+ ];
42
53
  /**
43
54
  * Normalize an effective-window bound to epoch-ms (a `number`), or `null` if empty/unparseable.
44
55
  * The membership_posting object declares these fields as `number` (BIGINT epoch-ms) so the
@@ -79,7 +90,13 @@ function naturalKey(p) {
79
90
  */
80
91
  function stripEmpty(input) {
81
92
  const out = {};
82
- for (const [k, v] of Object.entries(input)) {
93
+ // Security review F4: only the posting's own fields are taken from the client entry — a
94
+ // client `membershipUuid` (or `createdBy`, or any unknown key) never reaches the engine; the
95
+ // caller sets `membershipUuid` from the PATH.
96
+ for (const k of POSTING_KEYS) {
97
+ if (!(k in input))
98
+ continue;
99
+ const v = input[k];
83
100
  if (WINDOW_KEYS.has(k)) {
84
101
  const ms = toEpochMs(v);
85
102
  if (ms !== null)
@@ -93,9 +110,14 @@ function stripEmpty(input) {
93
110
  }
94
111
  let PostingService = class PostingService {
95
112
  records;
113
+ ceiling;
96
114
  versionBump;
97
- constructor(records, versionBump) {
115
+ constructor(records,
116
+ // The role-assignment ceiling + binding rule (security hardening #11). REQUIRED: a missing
117
+ // ceiling must fail DI at boot, never silently skip the check.
118
+ ceiling, versionBump) {
98
119
  this.records = records;
120
+ this.ceiling = ceiling;
99
121
  this.versionBump = versionBump;
100
122
  }
101
123
  /** List a membership's postings (tenant-scoped by the pipe). */
@@ -106,45 +128,75 @@ let PostingService = class PostingService {
106
128
  /**
107
129
  * Replace a membership's full posting set with `desired`, applying the minimal diff.
108
130
  * Compares current vs desired by the natural key `roleUuid|departmentUuid|branchUuid`:
109
- * deletes removed rows, creates added rows, leaves kept rows untouched. See the file
110
- * header for the atomicity / mid-op-safety reasoning (delete-then-create is safe because
111
- * kept postings are never delete targets, so the membership never lands empty).
131
+ * creates added rows, deletes removed rows, leaves kept rows untouched. Every authority check
132
+ * runs BEFORE any write; the writes run in one transaction (see the file header).
112
133
  */
113
- async replacePostings(membershipUuid, desired) {
114
- const current = (await this.records.list('membership_posting', { where: { membershipUuid } })).rows;
134
+ async replacePostings(actor, membershipUuid, desired) {
135
+ for (const d of desired) {
136
+ if (typeof d?.roleUuid !== 'string' || d.roleUuid.length === 0) {
137
+ throw new identity_errors_js_1.IdentityBadRequestError('Every assignment needs a role.');
138
+ }
139
+ }
140
+ // 1) Reach: the membership must be visible through the subtree read wall (else 404).
141
+ const membership = await this.records.get('company_membership', membershipUuid);
142
+ if (membership === null)
143
+ throw new identity_errors_js_1.IdentityNotFoundError('Membership not found.');
144
+ const tenantUuid = typeof membership.fields?.companyUuid === 'string' && membership.fields.companyUuid.length > 0
145
+ ? membership.fields.companyUuid
146
+ : null;
147
+ const current = (await this.records.list('membership_posting', { where: { membershipUuid } }))
148
+ .rows;
115
149
  const desiredKeys = new Set(desired.map(naturalKey));
116
150
  const currentKeys = new Set(current.map((r) => naturalKey(r.fields)));
117
- // Track whether the diff actually changed anything — only a real add/remove of a role
118
- // assignment need invalidate cached permission facts (a no-op replace must not bump).
119
- let changed = false;
120
- // Delete removed: rows in current whose key is not in desired. Kept rows are skipped.
121
- for (const r of current) {
122
- const key = naturalKey(r.fields);
123
- if (!desiredKeys.has(key)) {
124
- await this.records.delete('membership_posting', r.id);
125
- changed = true;
126
- }
151
+ const removed = current.filter((r) => !desiredKeys.has(naturalKey(r.fields)));
152
+ const seenAdded = new Set();
153
+ const added = desired.filter((d) => {
154
+ const k = naturalKey(d);
155
+ if (currentKeys.has(k) || seenAdded.has(k))
156
+ return false;
157
+ seenAdded.add(k);
158
+ return true;
159
+ });
160
+ // 2) Authority for every ADDED role, before any write: binding rule, then the ceiling.
161
+ const addedRoles = [...new Set(added.map((d) => d.roleUuid))];
162
+ for (const roleUuid of addedRoles) {
163
+ await this.ceiling.assertRoleAssignable(actor, roleUuid);
127
164
  }
128
- // Create added: desired rows whose key is not already present. Only real fields are
129
- // sent (undefined/null/empty optionals are dropped so the engine gets a clean field map).
130
- for (const d of desired) {
131
- if (!currentKeys.has(naturalKey(d))) {
132
- await this.records.create('membership_posting', {
133
- fields: { membershipUuid, ...stripEmpty(d) },
134
- });
135
- changed = true;
136
- }
165
+ await this.ceiling.assertCanAssignRoles(actor, addedRoles);
166
+ // Removing a PEER's postings strips their authority: the actor must dominate the member
167
+ // (security review). Removing your OWN postings (self-demotion) stays allowed.
168
+ const memberUuid = typeof membership.fields?.userUuid === 'string' ? membership.fields.userUuid : '';
169
+ if (removed.length > 0 && memberUuid !== actor.id) {
170
+ await this.ceiling.assertDominates(actor, memberUuid);
137
171
  }
172
+ if (added.length === 0 && removed.length === 0)
173
+ return; // no-op replace: no write, no bump
174
+ // 3) Write atomically: creates first, then deletes (KI #16). Only whitelisted fields are sent,
175
+ // and `membershipUuid` always comes from the path (review F4).
176
+ await this.records.transaction(async (tx) => {
177
+ // Review F2: serialise against a concurrent grant/posting of the same role. Lock each
178
+ // added role row on THIS transaction (grant administration locks the same row), then
179
+ // re-run the binding rule: a binding another tenant committed while we waited is now
180
+ // visible, and a role that became shared is refused before the INSERT (rolled back).
181
+ for (const roleUuid of addedRoles) {
182
+ await tx.lockForUpdate('role', roleUuid);
183
+ await this.ceiling.assertRoleAssignable(actor, roleUuid);
184
+ }
185
+ for (const d of added) {
186
+ await tx.create('membership_posting', { fields: { ...stripEmpty(d), membershipUuid } });
187
+ }
188
+ for (const r of removed) {
189
+ await tx.delete('membership_posting', r.id);
190
+ }
191
+ });
138
192
  // ADR-0011 STALENESS FIX: a posting add/remove changes the effective roles (→ coarse
139
193
  // grants) of the membership's user in that company, so bump the tenant's permission
140
194
  // version — every live session in that tenant then reloads its cached facts on the next
141
- // request (a role gain/loss lands within the version bound). Only when something actually
142
- // changed, and only when the bump collaborator is wired (absent in unit tests → no-op).
143
- // Best-effort + fail-safe: the posting write already succeeded, so a bump failure must
144
- // NOT surface as a 500 that hides a successful assignment change — the cache is instead
145
- // invalidated by the next legit bump / the facts' natural TTL. We log-swallow it.
146
- if (changed && this.versionBump) {
147
- const tenantUuid = await this.resolveMembershipTenant(membershipUuid);
195
+ // request (a role gain/loss lands within the version bound). Only when the bump collaborator
196
+ // is wired (absent in unit tests → no-op). Best-effort + fail-safe: the posting write already
197
+ // committed, so a bump failure must NOT surface as a 500 that hides a successful assignment
198
+ // change — the cache is instead invalidated by the next legit bump / the facts' natural TTL.
199
+ if (this.versionBump) {
148
200
  if (tenantUuid) {
149
201
  try {
150
202
  await this.versionBump.bumpTenant(tenantUuid);
@@ -155,27 +207,12 @@ let PostingService = class PostingService {
155
207
  }
156
208
  }
157
209
  }
158
- /**
159
- * Resolve the tenant (company uuid) a membership belongs to — the tenant whose permission
160
- * version a posting change must bump. Reads the parent `company_membership` through the
161
- * pipe (tenant-scoped, no raw SQL), returning `companyUuid` or `null` if unresolvable
162
- * (then no bump — fail-safe; never guesses a tenant).
163
- */
164
- async resolveMembershipTenant(membershipUuid) {
165
- try {
166
- const m = await this.records.get('company_membership', membershipUuid);
167
- const companyUuid = m?.fields?.companyUuid;
168
- return typeof companyUuid === 'string' && companyUuid.length > 0 ? companyUuid : null;
169
- }
170
- catch {
171
- return null;
172
- }
173
- }
174
210
  };
175
211
  exports.PostingService = PostingService;
176
212
  exports.PostingService = PostingService = __decorate([
177
213
  (0, common_1.Injectable)(),
178
- __param(1, (0, common_1.Optional)()),
179
- __param(1, (0, common_1.Inject)(posting_version_bump_js_1.POSTING_VERSION_BUMP)),
180
- __metadata("design:paramtypes", [engine_1.RecordsService, Object])
214
+ __param(2, (0, common_1.Optional)()),
215
+ __param(2, (0, common_1.Inject)(posting_version_bump_js_1.POSTING_VERSION_BUMP)),
216
+ __metadata("design:paramtypes", [engine_1.RecordsService,
217
+ grant_ceiling_js_1.GrantCeiling, Object])
181
218
  ], PostingService);
@@ -1 +1 @@
1
- {"version":3,"file":"org-catalog.controller.d.ts","sourceRoot":"","sources":["../../../src/features/org-structure/org-catalog.controller.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAU7D,qBACa,oBAAoB;IACnB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,iBAAiB;IAEvD,kFAAkF;IAG5E,sBAAsB,CAAuB,WAAW,EAAE,MAAM;;;IAKtE,+EAA+E;IAGzE,mBAAmB,CAAuB,WAAW,EAAE,MAAM;;;CAIpE"}
1
+ {"version":3,"file":"org-catalog.controller.d.ts","sourceRoot":"","sources":["../../../src/features/org-structure/org-catalog.controller.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAU7D,qBAEa,oBAAoB;IACnB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,iBAAiB;IAEvD,kFAAkF;IAG5E,sBAAsB,CAAuB,WAAW,EAAE,MAAM;;;IAKtE,+EAA+E;IAGzE,mBAAmB,CAAuB,WAAW,EAAE,MAAM;;;CAIpE"}
@@ -14,6 +14,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.OrgCatalogController = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
+ const identity_error_interceptor_js_1 = require("../shared/identity-error.interceptor.js");
17
18
  const guard_1 = require("@astram/guard");
18
19
  const org_catalog_service_js_1 = require("./org-catalog.service.js");
19
20
  // ── Company-scoped catalogs (dept/branch option lists for the posting editor) ──
@@ -58,5 +59,6 @@ __decorate([
58
59
  ], OrgCatalogController.prototype, "listCompanyBranches", null);
59
60
  exports.OrgCatalogController = OrgCatalogController = __decorate([
60
61
  (0, common_1.Controller)('identity'),
62
+ (0, common_1.UseInterceptors)(identity_error_interceptor_js_1.IdentityErrorInterceptor),
61
63
  __metadata("design:paramtypes", [org_catalog_service_js_1.OrgCatalogService])
62
64
  ], OrgCatalogController);
@@ -1 +1 @@
1
- {"version":3,"file":"org-catalog.service.d.ts","sourceRoot":"","sources":["../../../src/features/org-structure/org-catalog.service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,qBACa,iBAAiB;IAChB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,cAAc;IAEpD,kFAAkF;IAClF,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAInE,+EAA+E;IAC/E,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAIhE,qFAAqF;YACvE,kBAAkB;CAiBjC"}
1
+ {"version":3,"file":"org-catalog.service.d.ts","sourceRoot":"","sources":["../../../src/features/org-structure/org-catalog.service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,qBACa,iBAAiB;IAChB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,cAAc;IAEpD,kFAAkF;IAClF,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IASnE,+EAA+E;IAC/E,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAIhE,qFAAqF;YACvE,kBAAkB;CAoBjC"}
@@ -41,7 +41,10 @@ let OrgCatalogService = class OrgCatalogService {
41
41
  continue;
42
42
  const view = await this.records.get(catalogTypeKey, refUuid);
43
43
  const name = view?.fields?.name;
44
- options.push({ uuid: refUuid, name: typeof name === 'string' && name.length > 0 ? name : '(unknown)' });
44
+ options.push({
45
+ uuid: refUuid,
46
+ name: typeof name === 'string' && name.length > 0 ? name : '(unknown)',
47
+ });
45
48
  }
46
49
  return options;
47
50
  }
@@ -1 +1 @@
1
- {"version":3,"file":"policy.controller.d.ts","sourceRoot":"","sources":["../../../src/features/policy/policy.controller.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EACL,aAAa,EAId,MAAM,qBAAqB,CAAC;AAE7B,qFAAqF;AACrF,UAAU,aAAa;IACrB,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,qBACa,gBAAgB;IACf,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,aAAa;IAEpD;;;;;;;;OAQG;IAGG,MAAM,CAAmB,OAAO,CAAC,EAAE,MAAM;IAY/C;;;OAGG;IAGG,IAAI,CAAQ,GAAG,EAAE,aAAa,EAAoB,OAAO,CAAC,EAAE,MAAM;;;IAQxE;;;;;OAKG;IAGG,MAAM,CAAQ,GAAG,EAAE,aAAa,EAAU,IAAI,EAAE,YAAY;;;IAWlE;;;;OAIG;IAGG,MAAM,CAAQ,GAAG,EAAE,aAAa,EAAe,EAAE,EAAE,MAAM;;;CAShE"}
1
+ {"version":3,"file":"policy.controller.d.ts","sourceRoot":"","sources":["../../../src/features/policy/policy.controller.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EACL,aAAa,EAId,MAAM,qBAAqB,CAAC;AAE7B,qFAAqF;AACrF,UAAU,aAAa;IACrB,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,qBAEa,gBAAgB;IACf,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,aAAa;IAEpD;;;;;;;;OAQG;IAGG,MAAM,CAAmB,OAAO,CAAC,EAAE,MAAM;IAY/C;;;OAGG;IAGG,IAAI,CAAQ,GAAG,EAAE,aAAa,EAAoB,OAAO,CAAC,EAAE,MAAM;;;IAQxE;;;;;OAKG;IAGG,MAAM,CAAQ,GAAG,EAAE,aAAa,EAAU,IAAI,EAAE,YAAY;;;IAWlE;;;;OAIG;IAGG,MAAM,CAAQ,GAAG,EAAE,aAAa,EAAe,EAAE,EAAE,MAAM;;;CAShE"}
@@ -33,6 +33,7 @@ exports.PolicyController = void 0;
33
33
  */
34
34
  const common_1 = require("@nestjs/common");
35
35
  const guard_1 = require("@astram/guard");
36
+ const identity_error_interceptor_js_1 = require("../shared/identity-error.interceptor.js");
36
37
  const policy_service_js_1 = require("./policy.service.js");
37
38
  let PolicyController = class PolicyController {
38
39
  policies;
@@ -146,5 +147,6 @@ __decorate([
146
147
  ], PolicyController.prototype, "delete", null);
147
148
  exports.PolicyController = PolicyController = __decorate([
148
149
  (0, common_1.Controller)('identity'),
150
+ (0, common_1.UseInterceptors)(identity_error_interceptor_js_1.IdentityErrorInterceptor),
149
151
  __metadata("design:paramtypes", [policy_service_js_1.PolicyService])
150
152
  ], PolicyController);
@@ -1,5 +1,6 @@
1
1
  import { RoleService } from './role.service.js';
2
2
  import { type ListQueryDto } from '../shared/list-query.js';
3
+ import type { AuthedRequest } from '../shared/http.js';
3
4
  export declare class RoleController {
4
5
  private readonly roles;
5
6
  constructor(roles: RoleService);
@@ -24,13 +25,13 @@ export declare class RoleController {
24
25
  * administrator, may read roles). */
25
26
  getRole(uuid: string): Promise<import("packages/engine/dist/types.js").RecordView | null>;
26
27
  /** Update a role — gated on `identity:role:update`. */
27
- updateRole(uuid: string, body: {
28
+ updateRole(req: AuthedRequest, uuid: string, body: {
28
29
  name?: string;
29
30
  }): Promise<{
30
31
  success: boolean;
31
32
  }>;
32
33
  /** Delete a role — gated on `identity:role:delete`. */
33
- deleteRole(uuid: string): Promise<{
34
+ deleteRole(req: AuthedRequest, uuid: string): Promise<{
34
35
  success: boolean;
35
36
  }>;
36
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"role.controller.d.ts","sourceRoot":"","sources":["../../../src/features/role/role.controller.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAK5E,qBACa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,WAAW;IAE/C;;;;OAIG;IAGG,SAAS,CAAU,KAAK,EAAE,YAAY;;;;;;IAM5C,uDAAuD;IAGjD,UAAU,CAAS,IAAI,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;;;IAKhD;0CACsC;IAGtC,OAAO,CAAgB,IAAI,EAAE,MAAM;IAInC,uDAAuD;IAGjD,UAAU,CAAgB,IAAI,EAAE,MAAM,EAAU,IAAI,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;;;IAK7E,uDAAuD;IAGjD,UAAU,CAAgB,IAAI,EAAE,MAAM;;;CAI7C"}
1
+ {"version":3,"file":"role.controller.d.ts","sourceRoot":"","sources":["../../../src/features/role/role.controller.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAOvD,qBAEa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,WAAW;IAE/C;;;;OAIG;IAGG,SAAS,CAAU,KAAK,EAAE,YAAY;;;;;;IAM5C,uDAAuD;IAGjD,UAAU,CAAS,IAAI,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;;;IAKhD;0CACsC;IAGtC,OAAO,CAAgB,IAAI,EAAE,MAAM;IAInC,uDAAuD;IAGjD,UAAU,CACP,GAAG,EAAE,aAAa,EACV,IAAI,EAAE,MAAM,EACnB,IAAI,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;;;IAMjC,uDAAuD;IAGjD,UAAU,CAAQ,GAAG,EAAE,aAAa,EAAiB,IAAI,EAAE,MAAM;;;CAIxE"}
@@ -17,9 +17,11 @@ const common_1 = require("@nestjs/common");
17
17
  const guard_1 = require("@astram/guard");
18
18
  const role_service_js_1 = require("./role.service.js");
19
19
  const list_query_js_1 = require("../shared/list-query.js");
20
+ const identity_error_interceptor_js_1 = require("../shared/identity-error.interceptor.js");
20
21
  // Role management is gated on `identity:role:<action>` grants. `role` is CONTROL-plane
21
22
  // for ALL actions — including read: the capped root is the identity administrator (the
22
- // identity-administrator model), so it may list/read roles AND manage them.
23
+ // identity-administrator model), so it may list/read roles AND manage them. Update/delete
24
+ // also pass the role-binding rule (a role used outside the caller's subtree is root-only).
23
25
  let RoleController = class RoleController {
24
26
  roles;
25
27
  constructor(roles) {
@@ -46,13 +48,13 @@ let RoleController = class RoleController {
46
48
  return this.roles.getRole(uuid);
47
49
  }
48
50
  /** Update a role — gated on `identity:role:update`. */
49
- async updateRole(uuid, body) {
50
- await this.roles.updateRole(uuid, body ?? {});
51
+ async updateRole(req, uuid, body) {
52
+ await this.roles.updateRole(req.principal, uuid, { name: body?.name });
51
53
  return { success: true };
52
54
  }
53
55
  /** Delete a role — gated on `identity:role:delete`. */
54
- async deleteRole(uuid) {
55
- await this.roles.deleteRole(uuid);
56
+ async deleteRole(req, uuid) {
57
+ await this.roles.deleteRole(req.principal, uuid);
56
58
  return { success: true };
57
59
  }
58
60
  };
@@ -84,21 +86,24 @@ __decorate([
84
86
  __decorate([
85
87
  (0, common_1.Put)('roles/:uuid'),
86
88
  (0, guard_1.Classified)({ typeKey: 'role', action: 'update' }),
87
- __param(0, (0, common_1.Param)('uuid')),
88
- __param(1, (0, common_1.Body)()),
89
+ __param(0, (0, common_1.Req)()),
90
+ __param(1, (0, common_1.Param)('uuid')),
91
+ __param(2, (0, common_1.Body)()),
89
92
  __metadata("design:type", Function),
90
- __metadata("design:paramtypes", [String, Object]),
93
+ __metadata("design:paramtypes", [Object, String, Object]),
91
94
  __metadata("design:returntype", Promise)
92
95
  ], RoleController.prototype, "updateRole", null);
93
96
  __decorate([
94
97
  (0, common_1.Delete)('roles/:uuid'),
95
98
  (0, guard_1.Classified)({ typeKey: 'role', action: 'delete' }),
96
- __param(0, (0, common_1.Param)('uuid')),
99
+ __param(0, (0, common_1.Req)()),
100
+ __param(1, (0, common_1.Param)('uuid')),
97
101
  __metadata("design:type", Function),
98
- __metadata("design:paramtypes", [String]),
102
+ __metadata("design:paramtypes", [Object, String]),
99
103
  __metadata("design:returntype", Promise)
100
104
  ], RoleController.prototype, "deleteRole", null);
101
105
  exports.RoleController = RoleController = __decorate([
102
106
  (0, common_1.Controller)('identity'),
107
+ (0, common_1.UseInterceptors)(identity_error_interceptor_js_1.IdentityErrorInterceptor),
103
108
  __metadata("design:paramtypes", [role_service_js_1.RoleService])
104
109
  ], RoleController);
@@ -1,24 +1,32 @@
1
1
  import { RecordsService, type RecordId } from '@astram/engine';
2
+ import type { Principal } from '@astram/core';
2
3
  import { type ListParams } from '../shared/list-query.js';
4
+ import { GrantCeiling } from '../authz/grant-ceiling.js';
3
5
  /**
4
6
  * RoleService — role CRUD on the paved road, via the framework's RecordsService
5
- * facade. The facade supplies the acting identity (the request principal) —
6
- * this service never threads a Principal. No raw SQL, no RECORD_ENGINE.
7
+ * facade. The facade supplies the acting identity (the request principal) for the pipe;
8
+ * no raw SQL, no RECORD_ENGINE.
9
+ *
10
+ * Roles are a GLOBAL catalog, so renaming or deleting one changes every tenant that uses it (a
11
+ * delete cascades its postings). Security hardening #1 (D1 interim rule): update/delete first pass
12
+ * `GrantCeiling.assertRoleAdministrable` — the role must be bound inside the actor's subtree and
13
+ * nowhere outside it (a shared role is root-only). The engine's write wall still applies after.
7
14
  */
8
15
  export declare class RoleService {
9
16
  private readonly records;
10
- constructor(records: RecordsService);
17
+ private readonly ceiling;
18
+ constructor(records: RecordsService, ceiling: GrantCeiling);
11
19
  /** Create a role. */
12
20
  createRole(name: string): Promise<RecordId>;
13
21
  /** List roles (tenant-scoped by the pipe), with paging/search/sort. */
14
22
  listRoles(params?: ListParams): Promise<import("@astram/engine").RecordPage>;
15
23
  /** Read a single role. */
16
24
  getRole(uuid: string): Promise<import("@astram/engine").RecordView | null>;
17
- /** Update a role. */
18
- updateRole(uuid: string, patch: {
19
- name?: string;
25
+ /** Rename a role — only a role the actor may administer (binding rule). Patch whitelisted. */
26
+ updateRole(actor: Principal, uuid: string, patch: {
27
+ name?: unknown;
20
28
  }): Promise<void>;
21
- /** Delete a role. */
22
- deleteRole(uuid: string): Promise<void>;
29
+ /** Delete a role — only a role the actor may administer (binding rule). */
30
+ deleteRole(actor: Principal, uuid: string): Promise<void>;
23
31
  }
24
32
  //# sourceMappingURL=role.service.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"role.service.d.ts","sourceRoot":"","sources":["../../../src/features/role/role.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAK1E;;;;GAIG;AACH,qBACa,WAAW;IACV,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,cAAc;IAEpD,qBAAqB;IACrB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAI3C,uEAAuE;IACvE,SAAS,CAAC,MAAM,CAAC,EAAE,UAAU;IAI7B,0BAA0B;IAC1B,OAAO,CAAC,IAAI,EAAE,MAAM;IAIpB,qBAAqB;IACrB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjE,qBAAqB;IACrB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGxC"}
1
+ {"version":3,"file":"role.service.d.ts","sourceRoot":"","sources":["../../../src/features/role/role.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAKzD;;;;;;;;;GASG;AACH,qBACa,WAAW;IAEpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBADP,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,YAAY;IAGxC,qBAAqB;IACrB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAI3C,uEAAuE;IACvE,SAAS,CAAC,MAAM,CAAC,EAAE,UAAU;IAI7B,0BAA0B;IAC1B,OAAO,CAAC,IAAI,EAAE,MAAM;IAIpB,8FAA8F;IACxF,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1F,2EAA2E;IACrE,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIhE"}
@@ -13,17 +13,25 @@ exports.RoleService = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const engine_1 = require("@astram/engine");
15
15
  const list_query_js_1 = require("../shared/list-query.js");
16
+ const grant_ceiling_js_1 = require("../authz/grant-ceiling.js");
16
17
  const ROLE_SEARCH_FIELDS = ['name'];
17
18
  const ROLE_SORT_FIELDS = ['name', 'createdAt'];
18
19
  /**
19
20
  * RoleService — role CRUD on the paved road, via the framework's RecordsService
20
- * facade. The facade supplies the acting identity (the request principal) —
21
- * this service never threads a Principal. No raw SQL, no RECORD_ENGINE.
21
+ * facade. The facade supplies the acting identity (the request principal) for the pipe;
22
+ * no raw SQL, no RECORD_ENGINE.
23
+ *
24
+ * Roles are a GLOBAL catalog, so renaming or deleting one changes every tenant that uses it (a
25
+ * delete cascades its postings). Security hardening #1 (D1 interim rule): update/delete first pass
26
+ * `GrantCeiling.assertRoleAdministrable` — the role must be bound inside the actor's subtree and
27
+ * nowhere outside it (a shared role is root-only). The engine's write wall still applies after.
22
28
  */
23
29
  let RoleService = class RoleService {
24
30
  records;
25
- constructor(records) {
31
+ ceiling;
32
+ constructor(records, ceiling) {
26
33
  this.records = records;
34
+ this.ceiling = ceiling;
27
35
  }
28
36
  /** Create a role. */
29
37
  createRole(name) {
@@ -37,17 +45,23 @@ let RoleService = class RoleService {
37
45
  getRole(uuid) {
38
46
  return this.records.get('role', uuid);
39
47
  }
40
- /** Update a role. */
41
- updateRole(uuid, patch) {
42
- return this.records.update('role', uuid, { fields: patch });
48
+ /** Rename a role — only a role the actor may administer (binding rule). Patch whitelisted. */
49
+ async updateRole(actor, uuid, patch) {
50
+ await this.ceiling.assertRoleAdministrable(actor, uuid);
51
+ const fields = {};
52
+ if (typeof patch?.name === 'string')
53
+ fields.name = patch.name;
54
+ await this.records.update('role', uuid, { fields });
43
55
  }
44
- /** Delete a role. */
45
- deleteRole(uuid) {
46
- return this.records.delete('role', uuid);
56
+ /** Delete a role — only a role the actor may administer (binding rule). */
57
+ async deleteRole(actor, uuid) {
58
+ await this.ceiling.assertRoleAdministrable(actor, uuid);
59
+ await this.records.delete('role', uuid);
47
60
  }
48
61
  };
49
62
  exports.RoleService = RoleService;
50
63
  exports.RoleService = RoleService = __decorate([
51
64
  (0, common_1.Injectable)(),
52
- __metadata("design:paramtypes", [engine_1.RecordsService])
65
+ __metadata("design:paramtypes", [engine_1.RecordsService,
66
+ grant_ceiling_js_1.GrantCeiling])
53
67
  ], RoleService);