@happyvertical/smrt-users 0.60.9 → 0.60.11

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/README.md CHANGED
@@ -1783,6 +1783,26 @@ for authentication, deduplication, and status semantics.
1783
1783
  | `TenantHierarchyError` | Thrown on a missing parent, a cycle, or exceeding the hierarchy depth limit (`code`) |
1784
1784
  | `TenantHierarchyMaterializationError` | Thrown by `materializeTenantHierarchy` when any tenant's parent chain is broken; lists `problems` |
1785
1785
 
1786
+ `RoleCollection.seedSystemRoles()` seeds these global system identities:
1787
+
1788
+ | Role | UUID |
1789
+ | --- | --- |
1790
+ | Owner | `566aef94-1328-4876-b291-b7cf18d4df01` |
1791
+ | Administrator | `566aef94-1328-4876-b291-b7cf18d4df02` |
1792
+ | Member | `566aef94-1328-4876-b291-b7cf18d4df03` |
1793
+ | Viewer | `566aef94-1328-4876-b291-b7cf18d4df04` |
1794
+
1795
+ Repeat seeding requires each built-in role to have the documented UUID, global
1796
+ scope, and system flag. A conflicting global slug or occupied reserved UUID
1797
+ fails seeding without rewriting role ownership or memberships. Tenant roles
1798
+ can share these slugs under their own identities.
1799
+
1800
+ Trusted provisioning can pass `reservedIdentity` to
1801
+ `TenantService.createTenantWithOwnership()` on its transaction-bound service.
1802
+ The service applies `maxTenants`, resolves the global owner role, and inserts
1803
+ the tenant and membership with the supplied IDs. A collision aborts
1804
+ provisioning. Keep these IDs under server control.
1805
+
1786
1806
  ## Dependencies
1787
1807
 
1788
1808
  - `@happyvertical/smrt-core` -- ORM, `@smrt()` decorator, SmrtObject/SmrtCollection
@@ -1,4 +1,4 @@
1
- import { a as MembershipStatus, c as TenantPermissionEffect, l as TenantStatus, n as DEFAULT_ROLES, o as OverrideEffect, r as DEFAULT_ROLE_SLUGS, s as SessionStatus, u as UserStatus } from "./types-DFqeph0R.js";
1
+ import { a as MembershipStatus, c as TenantPermissionEffect, l as TenantStatus, n as DEFAULT_ROLES, o as OverrideEffect, r as DEFAULT_ROLE_SLUGS, s as SessionStatus, u as UserStatus } from "./types-DqUZxluX.js";
2
2
  import { IN_LIST_CHUNK_SIZE, ObjectRegistry, SmrtCollection, SmrtObject, bumpChangeFeed, chunkArray, crossPackageRef, field, findManifestEntryByQualifiedName, foreignKey, inheritedCollectionSurface, isEmbeddedDatabase, isPostgresDatabase, resolveOperationPermissionAlias, smrt, withEmbeddedWriteTransaction } from "@happyvertical/smrt-core";
3
3
  import { getCurrentTenant, runTenantHierarchyReadScope, withSystemContext } from "@happyvertical/smrt-tenancy";
4
4
  import { AmbiguousOidcIdentityError, CanonicalPersonProfileError, OidcIdentity, OidcIdentityCollection, ProfileCollection, normalizeIdentityEmail } from "@happyvertical/smrt-profiles";
@@ -1990,11 +1990,13 @@ var RoleCollection = class RoleCollection extends SmrtCollection {
1990
1990
  }
1991
1991
  /** Find a built-in system role only; tenant slug collisions never match. */
1992
1992
  async findSystemRoleBySlug(slug) {
1993
+ const builtIn = DEFAULT_ROLES.find((role) => role.slug === slug);
1993
1994
  return (await this.list({
1994
1995
  where: {
1995
1996
  slug,
1996
1997
  tenantId: null,
1997
- isSystem: true
1998
+ isSystem: true,
1999
+ ...builtIn && { id: builtIn.id }
1998
2000
  },
1999
2001
  limit: 1
2000
2002
  }))[0] ?? null;
@@ -2008,9 +2010,17 @@ var RoleCollection = class RoleCollection extends SmrtCollection {
2008
2010
  const knownSlugs = new Set(DEFAULT_ROLES.map((def) => def.slug));
2009
2011
  for (const slug of inheritableSlugs) if (!knownSlugs.has(slug)) throw new Error(`Unknown system role slug '${slug}' in inheritsToDescendants. Valid slugs: ${[...knownSlugs].join(", ")}.`);
2010
2012
  const seed = async (writer) => {
2013
+ const existingRoles = /* @__PURE__ */ new Map();
2014
+ const globalRoles = await writer.findSystemRoles();
2015
+ for (const roleDef of DEFAULT_ROLES) {
2016
+ const matches = globalRoles.filter(({ slug }) => slug === roleDef.slug);
2017
+ const [existing] = matches;
2018
+ if (matches.length > 1 || existing && (!existing.isSystem || existing.id !== roleDef.id) || !existing && await writer.count({ where: { id: roleDef.id } }) > 0) throw new Error(`System role identity collision for '${roleDef.slug}'`);
2019
+ if (existing) existingRoles.set(roleDef.slug, existing);
2020
+ }
2011
2021
  for (const roleDef of DEFAULT_ROLES) {
2012
2022
  const inheritable = inheritableSlugs.has(roleDef.slug);
2013
- const existing = await writer.findBySlug(roleDef.slug);
2023
+ const existing = existingRoles.get(roleDef.slug);
2014
2024
  if (existing) {
2015
2025
  if (inheritable && !existing.inheritsToDescendants) {
2016
2026
  existing.inheritsToDescendants = true;
@@ -2020,6 +2030,8 @@ var RoleCollection = class RoleCollection extends SmrtCollection {
2020
2030
  continue;
2021
2031
  }
2022
2032
  const role = await writer.create({
2033
+ id: roleDef.id,
2034
+ _insertOnly: true,
2023
2035
  slug: roleDef.slug,
2024
2036
  name: roleDef.name,
2025
2037
  description: roleDef.description,
@@ -2030,9 +2042,11 @@ var RoleCollection = class RoleCollection extends SmrtCollection {
2030
2042
  roles.push(role);
2031
2043
  }
2032
2044
  };
2033
- if (isPostgresDatabase(this.db)) {
2034
- await withEmbeddedWriteTransaction(this.db, false, async (db) => {
2035
- await db.query("SELECT pg_advisory_xact_lock(3329, 1)");
2045
+ const postgres = isPostgresDatabase(this.db);
2046
+ const embedded = isEmbeddedDatabase(this.db);
2047
+ if (postgres || embedded) {
2048
+ await withEmbeddedWriteTransaction(this.db, embedded, async (db) => {
2049
+ if (postgres) await db.query("SELECT pg_advisory_xact_lock(3329, 1)");
2036
2050
  await seed(await RoleCollection.create({
2037
2051
  ...this.options,
2038
2052
  db
@@ -2040,7 +2054,7 @@ var RoleCollection = class RoleCollection extends SmrtCollection {
2040
2054
  }, true);
2041
2055
  roles.length = 0;
2042
2056
  for (const roleDef of DEFAULT_ROLES) {
2043
- const role = await this.findBySlug(roleDef.slug);
2057
+ const role = await this.findSystemRoleBySlug(roleDef.slug);
2044
2058
  if (role) roles.push(role);
2045
2059
  }
2046
2060
  } else await seed(this);
@@ -6180,4 +6194,4 @@ var PasswordCredentialService = class PasswordCredentialService {
6180
6194
  //#endregion
6181
6195
  export { PermissionCatalogService as $, InvalidCredentialsError as A, SESSION_DATA_KEYS as At, backfillUserEmailKeys as B, DEFAULT_LOGIN_ATTEMPT_WINDOW_SECONDS as C, UsersLoginAttempt as Ct, DEFAULT_LOGIN_MAX_ATTEMPTS as D, normalizeEmail as Dt, DEFAULT_LOGIN_LOCKOUT_MAX_SECONDS as E, isValidEmail as Et, OidcProvisioningError as F, Membership as Ft, MAX_TENANT_HIERARCHY_DEPTH as G, TenantPermissionOverride as H, UserCollection as I, __exportAll as It, assertTenantTableName as J, TenantHierarchyError as K, requireExistingOwnedPerson as L, LoginRateLimitError as M, generateSessionId as Mt, hashSecret as N, Role as Nt, DEFAULT_SOURCE_LOCKOUT_AUDIT_SECONDS as O, normalizeUsername as Ot, verifySecretHash as P, Permission as Pt, RolePermissionCollection as Q, USER_EMAIL_KEY_BACKFILL_NAME as R, normalizeAncestorReadPolicy as S, UsersLoginAttemptCollection as St, DEFAULT_LOGIN_LOCKOUT_FACTOR as T, User as Tt, TenantCollection as U, TenantPermissionOverrideCollection as V, Tenant as W, recordTenantHierarchyChanges as X, planTenantHierarchy as Y, DEFAULT_ROLE_PERMISSION_PATTERNS as Z, PermissionResolver as _, GroupMember as _t, DEFAULT_PASSWORD_RECOVERY_MAX_AGE_SECONDS as a, withoutListBounds as at, getConfiguredAncestorReadPolicy as b, UsersLoginAuditEventCollection as bt, PASSWORD_PERMISSION_DEFINITIONS as c, PermissionCollection as ct, PasswordCredentialForbiddenError as d, MembershipOverrideCollection as dt, deriveOperationPermissionCollectionName as et, PasswordCredentialService as f, MembershipOverride as ft, SessionService as g, GroupMemberCollection as gt, outOfBandProofSessionData as h, GroupRole as ht, DEFAULT_PASSWORD_MIN_LENGTH as i, syncPermissionCatalog as it, LoginAttemptLimiter as j, Session as jt, DurableLoginAuditSink as k, DEFAULT_SESSION_TTL as kt, PASSWORD_RECOVERY_MAX_AGE_CEILING_SECONDS as l, UsersPasswordCredentialCollection as lt, ensurePasswordPermissionsRegistered as m, GroupRoleCollection as mt, DEFAULT_PASSWORD_MANAGE_PERMISSION as n, normalizeOperationPermissionAction as nt, PASSWORD_LOGIN_KIND as o, RolePermission as ot, PasswordPolicyError as p, MembershipCollection as pt, applyTenantHierarchyUpdates as q, DEFAULT_PASSWORD_MAX_LENGTH as r, registerPermissionDefinitions as rt, PASSWORD_MAX_LENGTH_CEILING as s, RoleCollection as st, DEFAULT_PASSWORD_DENYLIST as t, deriveOperationPermissionSlug as tt, PasswordCredentialError as u, UsersPasswordCredential as ut, ANCESTOR_READ_ACTION as v, GroupCollection as vt, DEFAULT_LOGIN_LOCKOUT_BASE_SECONDS as w, SessionCollection as wt, isAncestorReadableSlug as x, UsersLoginAuditEvent as xt, DEFAULT_ANCESTOR_READ_MAX_DEPTH as y, Group as yt, UserEmailKeyBackfillError as z };
6182
6196
 
6183
- //# sourceMappingURL=PasswordCredentialService-DHJNKEeJ.js.map
6197
+ //# sourceMappingURL=PasswordCredentialService-BSSP_kyT.js.map