@cosmicdrift/kumiko-bundled-features 0.420.0 → 0.421.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.
@@ -134,3 +134,14 @@ export declare function seedBootstrapAdmin(db: DbConnection, options: SeedAdminO
134
134
  } | {
135
135
  skipped: "has-password" | "tenant-has-members";
136
136
  }>;
137
+ /**
138
+ * Boot top-up for the bootstrap owner: adds the roles the owner's existing membership in the tenant
139
+ * lacks. Only a membership the system user created and last wrote is touched, so a human role change since then is not reverted at boot; the mail is unique per account, so the account is the owner named in the env.
140
+ */
141
+ export declare function ensureBootstrapAdminRoles(db: DbConnection, options: {
142
+ readonly email: string;
143
+ readonly tenantId: TenantId;
144
+ readonly roles: readonly string[];
145
+ }): Promise<{
146
+ readonly added: readonly string[];
147
+ }>;
@@ -13,14 +13,14 @@
13
13
  // sub-paths themselves.
14
14
  import { configuredBlindIndexKey, configuredPiiSubjectKms, decryptPiiFieldValues, } from "@cosmicdrift/kumiko-framework/crypto";
15
15
  import { fetchOne, selectMany } from "@cosmicdrift/kumiko-framework/db";
16
- import { createSystemUser, SYSTEM_TENANT_ID, } from "@cosmicdrift/kumiko-framework/engine";
16
+ import { createSystemUser, SYSTEM_TENANT_ID, SYSTEM_USER_ID, } from "@cosmicdrift/kumiko-framework/engine";
17
17
  import { ConflictError } from "@cosmicdrift/kumiko-framework/errors";
18
18
  import { createFallbackLogger } from "@cosmicdrift/kumiko-framework/logging";
19
19
  import { hashPassword, verifyPassword } from "../shared/index.js";
20
20
  // kumiko-lint-ignore cross-feature-import bootstrap admin seed checks whether the tenant already has members
21
21
  import { tenantMembershipsTable } from "../tenant/membership-table.js";
22
22
  // kumiko-lint-ignore cross-feature-import auth-tests need user+tenant seed-helpers
23
- import { seedTenant, seedTenantMembership } from "../tenant/seeding.js";
23
+ import { addTenantMembershipRoles, seedTenant, seedTenantMembership, } from "../tenant/seeding.js";
24
24
  // kumiko-lint-ignore cross-feature-import signup create-only guard reads the user projection by email
25
25
  import { USER_STATUS, userTable } from "../user/schema/user.js";
26
26
  // kumiko-lint-ignore cross-feature-import auth-tests need user+tenant seed-helpers
@@ -350,3 +350,18 @@ export async function seedBootstrapAdmin(db, options) {
350
350
  }
351
351
  return seeded;
352
352
  }
353
+ /**
354
+ * Boot top-up for the bootstrap owner: adds the roles the owner's existing membership in the tenant
355
+ * lacks. Only a membership the system user created and last wrote is touched, so a human role change since then is not reverted at boot; the mail is unique per account, so the account is the owner named in the env.
356
+ */
357
+ export async function ensureBootstrapAdminRoles(db, options) {
358
+ const owner = await findExistingUserRowForEmail(db, options.email);
359
+ if (typeof owner?.["id"] !== "string")
360
+ return { added: [] };
361
+ return addTenantMembershipRoles(db, {
362
+ userId: owner["id"],
363
+ tenantId: options.tenantId,
364
+ roles: options.roles,
365
+ onlyIfInsertedBy: SYSTEM_USER_ID,
366
+ });
367
+ }
@@ -62,3 +62,17 @@ export declare function seedTenantMembership(db: DbRunner, options: SeedTenantMe
62
62
  id: string;
63
63
  rowMissing?: true;
64
64
  }>;
65
+ /**
66
+ * Add-only top-up for an EXISTING membership: roles the membership lacks are appended through the
67
+ * event-store executor (one update event), nothing is removed. No event when all roles are present;
68
+ * no membership is created.
69
+ */
70
+ export declare function addTenantMembershipRoles(db: DbRunner, options: {
71
+ readonly userId: string;
72
+ readonly tenantId: TenantId;
73
+ readonly roles: readonly string[];
74
+ /** Only touch a membership this id created and last wrote (boot seeds pass SYSTEM_USER_ID), so a human role change since then is not reverted. */
75
+ readonly onlyIfInsertedBy?: string;
76
+ }): Promise<{
77
+ readonly added: readonly string[];
78
+ }>;
@@ -31,6 +31,7 @@ import { createEventStoreExecutor, createTenantDb, fetchOne, } from "@cosmicdrif
31
31
  import { attachOutsideTransaction, createSystemUser, HookPhases, SYSTEM_TENANT_ID, } from "@cosmicdrift/kumiko-framework/engine";
32
32
  import { getUnscopedAggregateStreamMaxVersion } from "@cosmicdrift/kumiko-framework/event-store";
33
33
  import { createDeferringNotify, createLifecycleHooks, } from "@cosmicdrift/kumiko-framework/pipeline";
34
+ import { parseRoles } from "@cosmicdrift/kumiko-framework/utils";
34
35
  import { assertAssignableMembershipRoles } from "./membership-roles.js";
35
36
  import { findLiveMembershipStreamId } from "./membership-stream-history.js";
36
37
  import { tenantMembershipEntity, tenantMembershipsTable } from "./membership-table.js";
@@ -256,3 +257,46 @@ function extractMembershipId(data) {
256
257
  }
257
258
  throw new Error(`seedTenantMembership: executor.create returned no string id (got ${JSON.stringify(data)})`);
258
259
  }
260
+ // ponytail: checks creator and last writer only; a human demotion followed by a system write
261
+ // (invite acceptance) passes. Scan the membership's event stream for non-system actors if that matters.
262
+ function wasOnlyWrittenBy(row, actorId) {
263
+ return (row.insertedById === actorId && (row.modifiedById === null || row.modifiedById === actorId));
264
+ }
265
+ /**
266
+ * Add-only top-up for an EXISTING membership: roles the membership lacks are appended through the
267
+ * event-store executor (one update event), nothing is removed. No event when all roles are present;
268
+ * no membership is created.
269
+ */
270
+ export async function addTenantMembershipRoles(db, options) {
271
+ assertAssignableMembershipRoles(options.roles);
272
+ // One re-read on version_conflict: a concurrent boot (second pod) may have added the same roles.
273
+ for (let attempt = 0; attempt < 2; attempt++) {
274
+ const existing = await fetchOne(db, tenantMembershipsTable, { userId: options.userId, tenantId: options.tenantId });
275
+ if (!existing)
276
+ return { added: [] };
277
+ if (options.onlyIfInsertedBy !== undefined &&
278
+ !wasOnlyWrittenBy(existing, options.onlyIfInsertedBy)) {
279
+ return { added: [] };
280
+ }
281
+ const currentRoles = parseRoles(existing.roles);
282
+ const added = options.roles.filter((role) => !currentRoles.includes(role));
283
+ if (added.length === 0)
284
+ return { added };
285
+ const result = await executor.update({
286
+ id: existing.id,
287
+ version: existing.version,
288
+ changes: { roles: JSON.stringify([...currentRoles, ...added]) },
289
+ }, createSystemUser(options.tenantId), createTenantDb({
290
+ db,
291
+ tenantId: options.tenantId,
292
+ mode: "system",
293
+ reason: "boot seed tops up the owner's membership roles in the seeded tenant",
294
+ }));
295
+ if (result.isSuccess)
296
+ return { added };
297
+ if (result.error.code === "version_conflict" && attempt === 0)
298
+ continue;
299
+ throw new Error(`addTenantMembershipRoles failed: ${result.error.code} — ${JSON.stringify(result.error.details ?? {})}`);
300
+ }
301
+ return { added: [] };
302
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-bundled-features",
3
- "version": "0.420.0",
3
+ "version": "0.421.0",
4
4
  "description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
5
5
  "license": "MIT",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -646,9 +646,9 @@
646
646
  }
647
647
  },
648
648
  "dependencies": {
649
- "@cosmicdrift/kumiko-framework": "0.420.0",
650
- "@cosmicdrift/kumiko-renderer": "0.420.0",
651
- "@cosmicdrift/kumiko-types": "0.420.0",
649
+ "@cosmicdrift/kumiko-framework": "0.421.0",
650
+ "@cosmicdrift/kumiko-renderer": "0.421.0",
651
+ "@cosmicdrift/kumiko-types": "0.421.0",
652
652
  "@fontsource/arimo": "^5.3.0",
653
653
  "@fontsource/caladea": "^5.3.0",
654
654
  "@fontsource/carlito": "^5.3.0",
@@ -1323,19 +1323,19 @@
1323
1323
  "LICENSE"
1324
1324
  ],
1325
1325
  "devDependencies": {
1326
- "@cosmicdrift/kumiko-locale-de": "0.420.0",
1327
- "@cosmicdrift/kumiko-locale-es": "0.420.0",
1328
- "@cosmicdrift/kumiko-locale-fr": "0.420.0",
1329
- "@cosmicdrift/kumiko-locale-it": "0.420.0",
1330
- "@cosmicdrift/kumiko-renderer-mail-html": "0.420.0",
1331
- "@cosmicdrift/kumiko-testing": "0.420.0",
1326
+ "@cosmicdrift/kumiko-locale-de": "0.421.0",
1327
+ "@cosmicdrift/kumiko-locale-es": "0.421.0",
1328
+ "@cosmicdrift/kumiko-locale-fr": "0.421.0",
1329
+ "@cosmicdrift/kumiko-locale-it": "0.421.0",
1330
+ "@cosmicdrift/kumiko-renderer-mail-html": "0.421.0",
1331
+ "@cosmicdrift/kumiko-testing": "0.421.0",
1332
1332
  "@testing-library/user-event": "^14.6.8",
1333
1333
  "@types/sanitize-html": "^2.16.2",
1334
1334
  "jsqr": "^1.4.0",
1335
1335
  "unpdf": "^1.9.1"
1336
1336
  },
1337
1337
  "peerDependencies": {
1338
- "@cosmicdrift/kumiko-testing": "0.420.0"
1338
+ "@cosmicdrift/kumiko-testing": "0.421.0"
1339
1339
  },
1340
1340
  "peerDependenciesMeta": {
1341
1341
  "@cosmicdrift/kumiko-testing": {