@aglyn/aglyn 1.12.0 → 1.13.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 (54) hide show
  1. package/package.json +11 -11
  2. package/src/lib/app-utils/docs-help-sections.generated.js +8 -2
  3. package/src/lib/app-utils/docs-help-sections.generated.js.map +1 -1
  4. package/src/lib/app-utils/docs-help.generated.d.ts +1 -1
  5. package/src/lib/app-utils/docs-help.generated.js +7 -0
  6. package/src/lib/app-utils/docs-help.generated.js.map +1 -1
  7. package/src/lib/app-utils/docs-index.generated.js +44 -30
  8. package/src/lib/app-utils/docs-index.generated.js.map +1 -1
  9. package/src/lib/app-utils/host-permissions.d.ts +12 -0
  10. package/src/lib/app-utils/host-permissions.js +26 -1
  11. package/src/lib/app-utils/host-permissions.js.map +1 -1
  12. package/src/lib/app-utils/media-asset-facts.d.ts +2 -2
  13. package/src/lib/app-utils/media-asset-facts.js +4 -3
  14. package/src/lib/app-utils/media-asset-facts.js.map +1 -1
  15. package/src/lib/app-utils/media-cdn-scope.d.ts +19 -4
  16. package/src/lib/app-utils/media-cdn-scope.js +19 -5
  17. package/src/lib/app-utils/media-cdn-scope.js.map +1 -1
  18. package/src/lib/app-utils/media-ref.d.ts +10 -6
  19. package/src/lib/app-utils/media-ref.js +34 -8
  20. package/src/lib/app-utils/media-ref.js.map +1 -1
  21. package/src/lib/app-utils/org-permissions.d.ts +7 -0
  22. package/src/lib/app-utils/org-permissions.js +2 -0
  23. package/src/lib/app-utils/org-permissions.js.map +1 -1
  24. package/src/lib/app-utils/org-role-grants.d.ts +7 -0
  25. package/src/lib/app-utils/org-role-grants.js +7 -5
  26. package/src/lib/app-utils/org-role-grants.js.map +1 -1
  27. package/src/lib/app-utils/scope-tokens.d.ts +132 -3
  28. package/src/lib/app-utils/scope-tokens.js +276 -3
  29. package/src/lib/app-utils/scope-tokens.js.map +1 -1
  30. package/src/lib/app-utils/site-placement.d.ts +56 -0
  31. package/src/lib/app-utils/site-placement.js +88 -0
  32. package/src/lib/app-utils/site-placement.js.map +1 -0
  33. package/src/lib/app-utils/transfer-launcher-context.d.ts +17 -1
  34. package/src/lib/app-utils/transfer-launcher-context.js.map +1 -1
  35. package/src/lib/data-transfer/access.d.ts +10 -18
  36. package/src/lib/data-transfer/access.js +21 -25
  37. package/src/lib/data-transfer/access.js.map +1 -1
  38. package/src/lib/data-transfer/resource.d.ts +13 -14
  39. package/src/lib/data-transfer/resource.js +9 -10
  40. package/src/lib/data-transfer/resource.js.map +1 -1
  41. package/src/lib/plugin-manager/first-party-plugins.generated.js +3 -4
  42. package/src/lib/plugin-manager/first-party-plugins.generated.js.map +1 -1
  43. package/src/lib/plugin-manager/plugin-entitlements.d.ts +12 -0
  44. package/src/lib/plugin-manager/plugin-entitlements.js +3 -1
  45. package/src/lib/plugin-manager/plugin-entitlements.js.map +1 -1
  46. package/src/lib/plugin-manager/plugin-scope-sharing.d.ts +52 -0
  47. package/src/lib/plugin-manager/plugin-scope-sharing.generated.d.ts +10 -0
  48. package/src/lib/plugin-manager/plugin-scope-sharing.generated.js +82 -0
  49. package/src/lib/plugin-manager/plugin-scope-sharing.generated.js.map +1 -0
  50. package/src/lib/plugin-manager/plugin-scope-sharing.js +85 -0
  51. package/src/lib/plugin-manager/plugin-scope-sharing.js.map +1 -0
  52. package/src/lib/plugin-manager/plugin-transfer-resources.d.ts +56 -1
  53. package/src/lib/plugin-manager/plugin-transfer-resources.js +13 -0
  54. package/src/lib/plugin-manager/plugin-transfer-resources.js.map +1 -1
@@ -85,6 +85,13 @@ export interface OrgPermissionDefinition {
85
85
  * default for it. `narrowPermissionsToSite` applies it. Only a plugin's key.
86
86
  */
87
87
  narrowsPerSite?: Readonly<Record<OrgRole, boolean>>;
88
+ /**
89
+ * A site can raise the key (AGL-4047): wider access to one site than the
90
+ * member's workspace role reads `hostRoleDefaults` there, unless a custom
91
+ * role or their own permissions decide it or the key it follows.
92
+ * `resolveSiteRaisedPermissions` decides it. Only a plugin's key.
93
+ */
94
+ raisesPerSite?: true;
88
95
  /**
89
96
  * The key's preset is the member's role ON THE SITE a request names, for
90
97
  * every member (AGL-4047): `hostRoleDefaults` read for the access they hold
@@ -156,6 +156,8 @@ function replaceEntries(target, entries) {
156
156
  follows: declared.follows
157
157
  } : {}, declared.narrowsPerSite ? {
158
158
  narrowsPerSite: _extends({}, declared.narrowsPerSite)
159
+ } : {}, declared.raisesPerSite ? {
160
+ raisesPerSite: true
159
161
  } : {}, declared.presetFromSiteRole && declared.hostRoleDefaults ? {
160
162
  presetFromSiteRole: true
161
163
  } : {}, declared.presetFromSiteRole && declared.hostRoleDefaults && declared.workspaceMembersOnly ? {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/app-utils/org-permissions.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { AglynOrgMember, OrgRole } from '../foundation'\nimport {\n listPluginCatalogPermissions,\n subscribePluginEntitlements,\n} from '../plugin-manager/plugin-entitlements'\nimport {\n permissionRefinementOf,\n refinePermissionMap,\n subscribePluginPermissions,\n type PermissionRefinement,\n} from '../plugin-manager/plugin-permissions'\nimport { ORG_ROLE_PERMISSION_KEYS } from './org-roles'\nimport {\n CORE_ORG_PERMISSION_ENTRIES,\n CORE_ORG_PERMISSION_GROUPS,\n CORE_ORG_ROLE_GRANTS,\n CORE_SITE_ROLE_PRESETS,\n type OrgPermissionGroup,\n type OrgPermissionScope,\n} from './org-role-grants'\n\nexport type { OrgPermissionGroup, OrgPermissionScope }\n\n/**\n * Granular org permissions (AGL-243) layered on the 4 org roles: each\n * role maps to a default permission set; custom roles\n * (`orgs/{orgId}/roles/{roleId}`) and per-member overrides refine it, so\n * e.g. an editor restricted to one host can also be granted billing\n * visibility — or an admin stripped of it — without inventing new roles.\n * The console hides unpermitted surfaces; the org API routes are the\n * enforcement point.\n */\nexport type CoreOrgPermission =\n | 'org.settings'\n | 'org.auditLog'\n | 'org.apiKeys'\n | 'org.privacy'\n | 'org.holds'\n | 'org.export'\n | 'org.sendingDomains'\n | 'org.businessProfile'\n | 'org.media'\n | 'org.eraseRecords'\n | 'billing.view'\n | 'billing.manage'\n | 'members.manage'\n | 'hosts.create'\n | 'hosts.delete'\n | 'data.manage'\n | 'marketplace.publish'\n | 'plugins.install'\n | 'site.edit'\n | 'site.publish'\n | 'site.sensitive'\n | 'site.manage'\n\n/**\n * A key of the catalog: a core key above, or a key a plugin declares into\n * the catalog through `registerPluginEntitlements` (AGL-2984). A declared key\n * is stored on custom roles and member overrides, layered and resolved\n * exactly like a core key.\n */\nexport type OrgPermission = CoreOrgPermission | (string & {})\n\n/**\n * Legacy boolean permission map derived from the granular `OrgPermission`\n * set (AGL-243). Kept for the console surfaces that predate the granular\n * model; lives here so relocated feature plugins can accept it as a prop\n * (AGL-395).\n */\nexport interface OrgPermissions {\n createHosts: boolean\n editHosts: boolean\n editBilling: boolean\n publishToMarketplace: boolean\n installPlugins: boolean\n manageMembers: boolean\n}\n\nexport interface OrgPermissionDefinition {\n key: OrgPermission\n label: string\n description: string\n /** The plugin that declared the key; absent for a core key. */\n pluginId?: string\n /** The `ORG_PERMISSION_GROUPS` id the role editor files it under (AGL-4047). */\n group: string\n /** Where it is decided: `org`, or `site` for a key a site collaborator holds per site. */\n scope: OrgPermissionScope\n /** Each org role's verdict when nothing refines it (`DEFAULT_ROLE_PERMISSIONS`). */\n roleDefaults: Readonly<Record<OrgRole, boolean>>\n /** Each host role's verdict, for a `site` key. */\n hostRoleDefaults?: Readonly<Record<string, boolean>>\n /**\n * The broader key this one is a finer part of (AGL-4047): never granted\n * without it, and following it where no custom role or override sets this\n * key. Only a plugin's key refines, and only a key the catalog holds.\n */\n refines?: OrgPermission\n /**\n * The key this one follows where no custom role or override sets it,\n * without needing it (AGL-4047): granted on its own, it holds while that\n * key is refused. Only a plugin's key follows, and only a key the catalog\n * holds; a key that refines does not also follow.\n */\n follows?: OrgPermission\n /**\n * Present when a site can narrow the key (AGL-4047): where a member who\n * works across the workspace has narrower access to one site than their\n * workspace role, whether the key follows that site role there is a\n * control, set per key on a custom role (`siteNarrowing`) and per member\n * (`AglynOrgMember.siteNarrowing`), and this is each built-in role's\n * default for it. `narrowPermissionsToSite` applies it. Only a plugin's key.\n */\n narrowsPerSite?: Readonly<Record<OrgRole, boolean>>\n /**\n * The key's preset is the member's role ON THE SITE a request names, for\n * every member (AGL-4047): `hostRoleDefaults` read for the access they hold\n * there, then their custom role, their own permissions and that site's\n * toggle. `roleDefaults` is the answer with no site named.\n * `resolveSiteRolePresetPermissions` decides it. The core's site keys\n * (`CORE_SITE_ROLE_PRESETS`) and a plugin's key with `hostRoleDefaults`.\n */\n presetFromSiteRole?: true\n /**\n * With `presetFromSiteRole`: the site role is the preset only for a member\n * of the whole workspace (AGL-4047). A site collaborator's preset is\n * refusal, whatever their site role, and only the site's toggle grants it\n * to them. A plugin's key that replaces a gate asking for an admin of the\n * whole workspace who is also the site's admin.\n */\n workspaceMembersOnly?: true\n}\n\n/**\n * The core keys, in display order for role editors.\n *\n * ⚠️ **EVERY KEY HERE MUST BE ENFORCED SERVER-SIDE.** A permission a customer\n * can tick that changes nothing is worse than its absence, because it implies\n * a control that does not exist — an owner unticks \"delete sites\", hands the\n * role out, and the member deletes sites while the console does not even dim\n * the button. Three of the eleven were in exactly that state (AGL-2444).\n *\n * `marketing.manage` was removed rather than wired, and the reason is worth\n * keeping: announcement bars, popups and campaigns live at\n * `hosts/{hostId}/overlays|campaigns|experiments` and are written\n * client-direct against the security rules, which gate on the HOST role.\n * There is no org-level boundary for it to sit on, and the action it named is\n * not org-scoped at all. Wiring it would have meant inventing one and\n * producing a second permission that looks enforced and is not. If a granular\n * marketing permission is wanted it belongs in the plugin-declared mechanism\n * (AGL-435), scoped to a host — a product decision, not this repair.\n *\n * `apps/console/specs/org-permissions-are-enforced.spec.ts` fails the build if\n * a key here, or a key a plugin declares, gains no server-side consumer.\n */\nconst CORE_ORG_PERMISSIONS: readonly OrgPermissionDefinition[] =\n // One table, shared with the native apps through the contracts (AGL-3950).\n CORE_ORG_PERMISSION_ENTRIES.map((entry) => ({\n ...entry,\n key: entry.key as CoreOrgPermission,\n group: entry.group ?? 'organization',\n scope: entry.scope ?? 'org',\n // A site key's preset is the member's role on the site (AGL-4047).\n ...(CORE_SITE_ROLE_PRESETS[entry.key]\n ? { hostRoleDefaults: { ...CORE_SITE_ROLE_PRESETS[entry.key] }, presetFromSiteRole: true as const }\n : {}),\n roleDefaults: {\n owner: CORE_ORG_ROLE_GRANTS.owner.includes(entry.key),\n admin: CORE_ORG_ROLE_GRANTS.admin.includes(entry.key),\n editor: CORE_ORG_ROLE_GRANTS.editor.includes(entry.key),\n viewer: CORE_ORG_ROLE_GRANTS.viewer.includes(entry.key),\n },\n }))\n\n/** The core keys: a plugin may add keys beside them and never redefine one. */\nconst CORE_ORG_PERMISSION_KEYS: ReadonlySet<string> = new Set(\n CORE_ORG_PERMISSIONS.map((definition) => definition.key),\n)\n\n/** The legacy camelCase projection's keys, which no declaration may take either. */\nconst LEGACY_PERMISSION_KEYS: ReadonlySet<string> = new Set<string>(\n ORG_ROLE_PERMISSION_KEYS,\n)\n\n/** The org roles, in the order `DEFAULT_ROLE_PERMISSIONS` lists them. */\nconst ORG_ROLES: readonly OrgRole[] = ['owner', 'admin', 'editor', 'viewer']\n\n/**\n * Every permission, in display order for role editors: the core keys, then\n * the keys plugins declare through `registerPluginEntitlements`, in the\n * plugins' catalog order (AGL-2984).\n *\n * LIVE, and kept in step in place. A plugin's declarations register at\n * module scope — at boot on the server, before the first paint in the\n * console — which can be after a module holding this binding was evaluated.\n * So this array, the key list, the role defaults and the empty map below are\n * rewritten in place on every registration instead of being computed once\n * when this module loads, and whoever holds the binding reads the declared\n * keys with the core ones.\n */\nexport const ORG_PERMISSIONS: readonly OrgPermissionDefinition[] = []\n\nexport const ORG_PERMISSION_KEYS: readonly OrgPermission[] = []\n\n/**\n * The groups the catalog's keys are filed under, in display order (AGL-4047):\n * the core groups, then one per plugin group a declaration names. Live, like\n * `ORG_PERMISSIONS`; a group with no key is left out.\n */\nexport const ORG_PERMISSION_GROUPS: readonly OrgPermissionGroup[] = []\n\n/**\n * The catalog keys that came through `registerPluginPermissions` (AGL-4047):\n * the keys the per-tier resolver (`resolveRolePermissions`) also answers,\n * which a caller holding a catalog-resolved map lays over that resolver's\n * answer so a custom role reaches them. Live.\n */\nexport const TIER_PERMISSION_KEYS: readonly OrgPermission[] = []\n\n/**\n * Every catalog key that refines a broader one, with that key (AGL-4047),\n * and every key that only follows one, marked `'follows'`. Live, like\n * `ORG_PERMISSIONS`. A resolver lays it over its map with\n * `refinePermissionMap`.\n */\nexport const ORG_PERMISSION_REFINEMENTS: readonly PermissionRefinement[] = []\n\nconst NO_PERMISSIONS = {} as Record<OrgPermission, boolean>\n\n/**\n * Role → default permission set. Owner/admin hold everything (owner-only\n * actions like org deletion stay role-checked, not permission-checked);\n * editors work on content but see no money or roster controls; viewers read\n * only. A declared key's default for each role is the one its declaration\n * names.\n */\nexport const DEFAULT_ROLE_PERMISSIONS: Record<\n OrgRole,\n Record<OrgPermission, boolean>\n> = {\n owner: {} as Record<OrgPermission, boolean>,\n admin: {} as Record<OrgPermission, boolean>,\n editor: {} as Record<OrgPermission, boolean>,\n viewer: {} as Record<OrgPermission, boolean>,\n}\n\n/** Refused declarations already logged, so a refusal is reported once. */\nconst reportedRefusals = new Set<string>()\n\nfunction replaceItems<T>(target: readonly T[], items: readonly T[]): void {\n const list = target as T[]\n list.length = 0\n list.push(...items)\n}\n\nfunction replaceEntries(\n target: Record<string, boolean>,\n entries: Record<string, boolean>,\n): void {\n for (const key of Object.keys(target)) delete target[key]\n Object.assign(target, entries)\n}\n\n/**\n * Rebuilds the live catalog: the core keys, then every key a plugin declared\n * through either declaration API (`listPluginCatalogPermissions`, AGL-4047).\n * A declaration naming a core or legacy key is refused — that key keeps its\n * own meaning and defaults — and logged once.\n */\nfunction syncCatalog(): void {\n const definitions: OrgPermissionDefinition[] = [...CORE_ORG_PERMISSIONS]\n const tierKeys: string[] = []\n const groups: OrgPermissionGroup[] = CORE_ORG_PERMISSION_GROUPS.map((group) => ({ ...group }))\n for (const declared of listPluginCatalogPermissions()) {\n if (\n CORE_ORG_PERMISSION_KEYS.has(declared.key) ||\n LEGACY_PERMISSION_KEYS.has(declared.key)\n ) {\n const refusal = `${declared.pluginId}\\x00${declared.key}`\n if (!reportedRefusals.has(refusal)) {\n reportedRefusals.add(refusal)\n console.error(\n `[plugins] refused catalog permission \"${declared.key}\" to ` +\n `\"${declared.pluginId}\": the key belongs to the core catalog`,\n )\n }\n continue\n }\n definitions.push({\n key: declared.key,\n label: declared.label,\n description: declared.description,\n pluginId: declared.pluginId,\n group: declared.group,\n scope: declared.hostRoleDefaults ? 'site' : 'org',\n roleDefaults: declared.roleDefaults,\n ...(declared.hostRoleDefaults ? { hostRoleDefaults: declared.hostRoleDefaults } : {}),\n ...(declared.refines ? { refines: declared.refines } : {}),\n ...(declared.follows && !declared.refines ? { follows: declared.follows } : {}),\n ...(declared.narrowsPerSite ? { narrowsPerSite: { ...declared.narrowsPerSite } } : {}),\n ...(declared.presetFromSiteRole && declared.hostRoleDefaults ? { presetFromSiteRole: true as const } : {}),\n ...(declared.presetFromSiteRole && declared.hostRoleDefaults && declared.workspaceMembersOnly\n ? { workspaceMembersOnly: true as const }\n : {}),\n })\n if (declared.source === 'tier') tierKeys.push(declared.key)\n if (!groups.some((group) => group.id === declared.group)) {\n groups.push({ id: declared.group, label: declared.groupLabel, pluginId: declared.pluginId })\n }\n }\n const keys = definitions.map((definition) => definition.key)\n const known = new Set<string>(keys)\n // A refinement of a key the catalog does not hold refines nothing, and is\n // dropped from the definition so no editor offers a link to nowhere.\n for (const definition of definitions) {\n if (definition.refines && (!known.has(definition.refines) || definition.refines === definition.key)) {\n delete definition.refines\n }\n if (definition.follows && (!known.has(definition.follows) || definition.follows === definition.key)) {\n delete definition.follows\n }\n }\n replaceItems(\n ORG_PERMISSION_REFINEMENTS,\n definitions.flatMap((definition) => {\n const refinement = permissionRefinementOf(definition)\n return refinement ? [refinement] : []\n }),\n )\n replaceItems(ORG_PERMISSIONS, definitions)\n replaceItems(ORG_PERMISSION_KEYS, keys)\n replaceItems(\n ORG_PERMISSION_GROUPS,\n groups.filter((group) => definitions.some((definition) => definition.group === group.id)),\n )\n replaceItems(TIER_PERMISSION_KEYS, tierKeys)\n replaceEntries(NO_PERMISSIONS, Object.fromEntries(keys.map((key) => [key, false])))\n for (const role of ORG_ROLES) {\n replaceEntries(\n DEFAULT_ROLE_PERMISSIONS[role],\n Object.fromEntries(\n definitions.map((definition) => [definition.key, definition.roleDefaults[role] === true]),\n ),\n )\n }\n CATALOG_AND_LEGACY_KEYS.clear()\n for (const key of [...keys, ...LEGACY_PERMISSION_KEYS]) {\n CATALOG_AND_LEGACY_KEYS.add(key)\n }\n}\n\n/**\n * The `registerPluginPermissions` keys of a catalog-resolved map (AGL-4047).\n *\n * Those keys are answered twice: by the per-tier resolver\n * (`resolveRolePermissions`, the role's tier default and the member's\n * override) and, now that they are catalog keys, by `resolveOrgPermissions`\n * with every layer — the custom role's included. A caller that builds its\n * map from the first spreads this over it, so the custom role a member\n * carries reaches `managePos` the way it reaches `hosts.create`.\n */\nexport function tierPermissionsOf(\n granted: Readonly<Record<string, boolean>> | null | undefined,\n): Record<string, boolean> {\n const picked: Record<string, boolean> = {}\n if (!granted) return picked\n for (const key of TIER_PERMISSION_KEYS) {\n if (typeof granted[key] === 'boolean') picked[key] = granted[key]\n }\n return picked\n}\n\n/** One catalog key by name, or undefined. */\nexport function orgPermissionDefinition(\n permission: OrgPermission,\n): OrgPermissionDefinition | undefined {\n return ORG_PERMISSIONS.find((definition) => definition.key === permission)\n}\n\n/** `orgs/{orgId}/roles/{roleId}` — a named custom permission set. */\nexport interface AglynOrgCustomRole {\n $id?: string\n name?: string\n description?: string\n /** Full permission map; missing keys read as false. */\n permissions?: Partial<Record<OrgPermission, boolean>>\n /**\n * Per key a site can narrow (AGL-4047): `true` follows the site role on a\n * site where the member's access is narrower than their workspace role,\n * `false` keeps the permission there. A key it leaves out follows the\n * built-in role's default (`OrgPermissionDefinition.narrowsPerSite`), and\n * a member's own `siteNarrowing` wins over it.\n */\n siteNarrowing?: Partial<Record<OrgPermission, boolean>>\n}\n\n/**\n * Effective permission map for a member: the org role's defaults, then\n * the custom role's map (when the member has `roleId` and the role doc\n * is supplied), then per-member overrides — later layers win key-by-key.\n */\nexport function resolveOrgPermissions(\n member:\n | (Partial<AglynOrgMember> & {\n roleId?: string\n permissions?: Partial<Record<OrgPermission, boolean>>\n })\n | null\n | undefined,\n customRole?: AglynOrgCustomRole | null,\n): Record<OrgPermission, boolean> {\n if (!member) return NO_PERMISSIONS\n const role = (member.role ?? 'viewer') as OrgRole\n const base = DEFAULT_ROLE_PERMISSIONS[role] ?? NO_PERMISSIONS\n const merged: Record<OrgPermission, boolean> = { ...base }\n if (member.roleId && customRole?.permissions) {\n for (const key of ORG_PERMISSION_KEYS) {\n const value = customRole.permissions[key]\n if (typeof value === 'boolean') merged[key] = value\n }\n }\n if (member.permissions) {\n for (const key of ORG_PERMISSION_KEYS) {\n const value = member.permissions[key]\n if (typeof value === 'boolean') merged[key] = value\n }\n }\n // A key that refines a broader one (AGL-4047) follows it where neither\n // layer set the key, and is never held without it; a key that only follows\n // one takes its verdict where neither layer set the key, and nothing more.\n return refinePermissionMap(merged, ORG_PERMISSION_REFINEMENTS, [\n member.roleId ? customRole?.permissions : undefined,\n member.permissions,\n ])\n}\n\n/**\n * The values a custom role and a per-member override set EXPLICITLY for\n * permission keys outside this catalog — the keys plugins declare through\n * `registerPluginPermissions` (AGL-2974).\n *\n * `resolveOrgPermissions` walks the catalog alone, so a plugin key stored on\n * a custom role or a member document never reached the `resolvedPermissions`\n * stamp the Firestore rules read. A rule gating on one could then honor\n * neither a grant to an editor nor a revocation from an admin. This carries\n * exactly those stored decisions, custom role first and the per-member\n * override over it, which is the precedence the resolver applies.\n *\n * Tier DEFAULTS are deliberately absent. They live in the plugin registry,\n * which is populated per process by whichever plugin bundles that process\n * loaded, and a stamp computed from it would differ by which route happened\n * to write it. A rule supplies the default itself from the member's role,\n * the way `memberResolves` is always conjoined with a role gate.\n *\n * The legacy six camelCase keys are excluded as well: they are the catalog's\n * older projection, not plugin keys, and no rule reads them.\n */\nexport function explicitPluginPermissionValues(\n member:\n | {\n roleId?: string\n permissions?: Partial<Record<string, unknown>> | null\n }\n | null\n | undefined,\n customRole?: { permissions?: Partial<Record<string, unknown>> | null } | null,\n): Record<string, boolean> {\n const values: Record<string, boolean> = {}\n if (!member) return values\n const layers = [\n member.roleId ? customRole?.permissions : undefined,\n member.permissions,\n ]\n for (const layer of layers) {\n for (const [key, value] of Object.entries(layer ?? {})) {\n if (typeof value !== 'boolean') continue\n if (CATALOG_AND_LEGACY_KEYS.has(key)) continue\n values[key] = value\n }\n }\n return values\n}\n\n/**\n * Keys {@link explicitPluginPermissionValues} leaves to the catalog: every\n * catalog key, declared ones included, and the legacy six. Kept in step by\n * `syncCatalog`.\n */\nconst CATALOG_AND_LEGACY_KEYS = new Set<string>()\n\n/**\n * The `resolvedPermissions` map stamped on a member document for the\n * Firestore rules: the catalog verdict with every layer applied, and beside\n * it the plugin keys a custom role or an override set explicitly.\n *\n * One function for every writer of the stamp, so the org-create transaction\n * and `syncOrgAuthProjections` cannot project different maps. The catalog\n * spreads last; the two key spaces do not overlap, and the order keeps it\n * true that nothing but the resolver decides a catalog key.\n */\nexport function projectMemberResolvedPermissions(\n member: Parameters<typeof resolveOrgPermissions>[0],\n customRole?: AglynOrgCustomRole | null,\n): Record<string, boolean> {\n return {\n ...explicitPluginPermissionValues(member, customRole),\n ...resolveOrgPermissions(member, customRole),\n }\n}\n\n/** Single-permission convenience over `resolveOrgPermissions`. */\nexport function hasOrgPermission(\n member: Parameters<typeof resolveOrgPermissions>[0],\n permission: OrgPermission,\n customRole?: AglynOrgCustomRole | null,\n): boolean {\n return resolveOrgPermissions(member, customRole)[permission]\n}\n\n/** The catalog label for a key — the words a refusal names — or the key itself. */\nexport function orgPermissionLabel(permission: OrgPermission): string {\n return (\n ORG_PERMISSIONS.find((definition) => definition.key === permission)?.label ??\n permission\n )\n}\n\n/**\n * The sentence a door answers when a member does not hold a key (AGL-4047):\n * it names the permission by its catalog label and who can change it, never\n * a role.\n */\nexport function permissionRefusalText(permission: OrgPermission): string {\n return `Your role does not include \"${orgPermissionLabel(permission)}\" — ask an organization admin`\n}\n\n/** The keys plugins declared into the catalog, in catalog order. */\nexport function pluginOrgPermissionKeys(): OrgPermission[] {\n return ORG_PERMISSIONS.filter((definition) => definition.pluginId).map(\n (definition) => definition.key,\n )\n}\n\n/** One declared catalog key that moved, and the direction it moved in. */\nexport interface PluginPermissionChange {\n permission: string\n granted: boolean\n}\n\n/**\n * The keys PLUGINS declared into the catalog whose value moved between two\n * maps, in the new map's order — what a member, role or collaborator write\n * raises `org.permissions.changed` for, once per key, so the plugin that owns\n * a key can record who moved it.\n *\n * A key the new map leaves unset is not a change: on a role it defers to the\n * base role, on a site toggle to the host role, and neither is a grant or a\n * revocation of its own. A key set for the first time is a change, because\n * the verdict it produces is no longer the default's. The core keys and the\n * legacy six raise nothing; their writes are recorded by the routes' own\n * activity sentences.\n */\nexport function pluginPermissionChanges(\n before: Partial<Record<string, unknown>> | null | undefined,\n after: Partial<Record<string, unknown>> | null | undefined,\n): PluginPermissionChange[] {\n const changes: PluginPermissionChange[] = []\n for (const [key, next] of Object.entries(after ?? {})) {\n if (typeof next !== 'boolean') continue\n if (CORE_ORG_PERMISSION_KEYS.has(key) || LEGACY_PERMISSION_KEYS.has(key)) continue\n if (before?.[key] === next) continue\n changes.push({ permission: key, granted: next })\n }\n return changes\n}\n\n/**\n * The granular (dotted) set projected onto the legacy boolean map\n * (AGL-2350).\n *\n * ## Why this lives here rather than in the console hook\n *\n * It was a private function in `apps/console/hooks/use-org-permissions.ts`,\n * which meant the CLIENT translated the stored permission model into the\n * legacy flags and the SERVER did not translate at all — it derived the\n * legacy flags straight from the built-in role tier and dropped custom roles\n * and per-member overrides on the floor. The two disagreed exactly on the\n * feature `custom-roles.md` sells, in both directions: a permission granted\n * by a custom role showed in the UI and 403'd on POST, and one revoked by an\n * override was hidden in the UI and still succeeded on POST.\n *\n * One exported copy is what stops them drifting again.\n *\n * ## The key spaces are NOT interchangeable, and that is the trap\n *\n * Two permission vocabularies exist. The stored one is DOTTED\n * (`plugins.install`) — `apps/console/app/api/orgs/roles/route.ts` sanitizes\n * against `ORG_PERMISSION_KEYS` before writing, and `AglynOrgMember`\n * `permissions` is typed to it. The legacy one is camelCase\n * (`installPlugins`).\n *\n * `resolveRolePermissions` in `org-roles.ts` accepts `overrides` and\n * `customRoles` arguments keyed by the CAMELCASE space. Feeding it the real\n * stored documents therefore matches no key and changes nothing — it looks\n * like wiring the feature up while doing precisely nothing. Those two\n * parameters are used by nothing but that module's own spec. Translate\n * through here instead.\n *\n * `editHosts` is derived from the ROLE, not from a dotted key: it has no\n * counterpart in the granular catalog and never had one.\n */\nexport function toLegacyPermissions(\n granted: Record<OrgPermission, boolean>,\n role: OrgRole | null | undefined,\n): OrgPermissions {\n return {\n createHosts: granted['hosts.create'],\n editHosts: role !== 'viewer',\n editBilling: granted['billing.manage'],\n publishToMarketplace: granted['marketplace.publish'],\n installPlugins: granted['plugins.install'],\n manageMembers: granted['members.manage'],\n }\n}\n\nsyncCatalog()\nsubscribePluginEntitlements(syncCatalog)\n// The second declaration API feeds the same catalog (AGL-4047).\nsubscribePluginPermissions(syncCatalog)\n"],"names":["listPluginCatalogPermissions","subscribePluginEntitlements","permissionRefinementOf","refinePermissionMap","subscribePluginPermissions","ORG_ROLE_PERMISSION_KEYS","CORE_ORG_PERMISSION_ENTRIES","CORE_ORG_PERMISSION_GROUPS","CORE_ORG_ROLE_GRANTS","CORE_SITE_ROLE_PRESETS","CORE_ORG_PERMISSIONS","map","entry","key","group","scope","hostRoleDefaults","presetFromSiteRole","roleDefaults","owner","includes","admin","editor","viewer","CORE_ORG_PERMISSION_KEYS","Set","definition","LEGACY_PERMISSION_KEYS","ORG_ROLES","ORG_PERMISSIONS","ORG_PERMISSION_KEYS","ORG_PERMISSION_GROUPS","TIER_PERMISSION_KEYS","ORG_PERMISSION_REFINEMENTS","NO_PERMISSIONS","DEFAULT_ROLE_PERMISSIONS","reportedRefusals","replaceItems","target","items","list","length","push","replaceEntries","entries","Object","keys","assign","syncCatalog","definitions","tierKeys","groups","declared","has","refusal","pluginId","add","console","error","label","description","refines","follows","narrowsPerSite","workspaceMembersOnly","source","some","id","groupLabel","known","flatMap","refinement","filter","fromEntries","role","CATALOG_AND_LEGACY_KEYS","clear","tierPermissionsOf","granted","picked","orgPermissionDefinition","permission","find","resolveOrgPermissions","member","customRole","base","merged","roleId","permissions","value","undefined","explicitPluginPermissionValues","values","layers","layer","projectMemberResolvedPermissions","hasOrgPermission","orgPermissionLabel","permissionRefusalText","pluginOrgPermissionKeys","pluginPermissionChanges","before","after","changes","next","toLegacyPermissions","createHosts","editHosts","editBilling","publishToMarketplace","installPlugins","manageMembers"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC;AAGD,SACEA,4BAA4B,EAC5BC,2BAA2B,QACtB,2CAAuC;AAC9C,SACEC,sBAAsB,EACtBC,mBAAmB,EACnBC,0BAA0B,QAErB,0CAAsC;AAC7C,SAASC,wBAAwB,QAAQ,iBAAa;AACtD,SACEC,2BAA2B,EAC3BC,0BAA0B,EAC1BC,oBAAoB,EACpBC,sBAAsB,QAGjB,uBAAmB;AAmH1B;;;;;;;;;;;;;;;;;;;;;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yBAAyB6B,GAAG,CAACxC,QAAQc,uBAAuB0B,GAAG,CAACxC,MAAM;QAC1E,IAAIyF,CAAAA,0BAAAA,MAAQ,CAACzF,IAAI,MAAK4F,MAAM;QAC5BD,QAAQ9D,IAAI,CAAC;YAAEuC,YAAYpE;YAAKiE,SAAS2B;QAAK;IAChD;IACA,OAAOD;AACT;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCC,GACD,OAAO,SAASE,oBACd5B,OAAuC,EACvCJ,IAAgC;IAEhC,OAAO;QACLiC,aAAa7B,OAAO,CAAC,eAAe;QACpC8B,WAAWlC,SAAS;QACpBmC,aAAa/B,OAAO,CAAC,iBAAiB;QACtCgC,sBAAsBhC,OAAO,CAAC,sBAAsB;QACpDiC,gBAAgBjC,OAAO,CAAC,kBAAkB;QAC1CkC,eAAelC,OAAO,CAAC,iBAAiB;IAC1C;AACF;AAEA9B;AACA/C,4BAA4B+C;AAC5B,gEAAgE;AAChE5C,2BAA2B4C"}
1
+ {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/app-utils/org-permissions.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { AglynOrgMember, OrgRole } from '../foundation'\nimport {\n listPluginCatalogPermissions,\n subscribePluginEntitlements,\n} from '../plugin-manager/plugin-entitlements'\nimport {\n permissionRefinementOf,\n refinePermissionMap,\n subscribePluginPermissions,\n type PermissionRefinement,\n} from '../plugin-manager/plugin-permissions'\nimport { ORG_ROLE_PERMISSION_KEYS } from './org-roles'\nimport {\n CORE_ORG_PERMISSION_ENTRIES,\n CORE_ORG_PERMISSION_GROUPS,\n CORE_ORG_ROLE_GRANTS,\n CORE_SITE_ROLE_PRESETS,\n type OrgPermissionGroup,\n type OrgPermissionScope,\n} from './org-role-grants'\n\nexport type { OrgPermissionGroup, OrgPermissionScope }\n\n/**\n * Granular org permissions (AGL-243) layered on the 4 org roles: each\n * role maps to a default permission set; custom roles\n * (`orgs/{orgId}/roles/{roleId}`) and per-member overrides refine it, so\n * e.g. an editor restricted to one host can also be granted billing\n * visibility — or an admin stripped of it — without inventing new roles.\n * The console hides unpermitted surfaces; the org API routes are the\n * enforcement point.\n */\nexport type CoreOrgPermission =\n | 'org.settings'\n | 'org.auditLog'\n | 'org.apiKeys'\n | 'org.privacy'\n | 'org.holds'\n | 'org.export'\n | 'org.sendingDomains'\n | 'org.businessProfile'\n | 'org.media'\n | 'org.eraseRecords'\n | 'billing.view'\n | 'billing.manage'\n | 'members.manage'\n | 'hosts.create'\n | 'hosts.delete'\n | 'data.manage'\n | 'marketplace.publish'\n | 'plugins.install'\n | 'site.edit'\n | 'site.publish'\n | 'site.sensitive'\n | 'site.manage'\n\n/**\n * A key of the catalog: a core key above, or a key a plugin declares into\n * the catalog through `registerPluginEntitlements` (AGL-2984). A declared key\n * is stored on custom roles and member overrides, layered and resolved\n * exactly like a core key.\n */\nexport type OrgPermission = CoreOrgPermission | (string & {})\n\n/**\n * Legacy boolean permission map derived from the granular `OrgPermission`\n * set (AGL-243). Kept for the console surfaces that predate the granular\n * model; lives here so relocated feature plugins can accept it as a prop\n * (AGL-395).\n */\nexport interface OrgPermissions {\n createHosts: boolean\n editHosts: boolean\n editBilling: boolean\n publishToMarketplace: boolean\n installPlugins: boolean\n manageMembers: boolean\n}\n\nexport interface OrgPermissionDefinition {\n key: OrgPermission\n label: string\n description: string\n /** The plugin that declared the key; absent for a core key. */\n pluginId?: string\n /** The `ORG_PERMISSION_GROUPS` id the role editor files it under (AGL-4047). */\n group: string\n /** Where it is decided: `org`, or `site` for a key a site collaborator holds per site. */\n scope: OrgPermissionScope\n /** Each org role's verdict when nothing refines it (`DEFAULT_ROLE_PERMISSIONS`). */\n roleDefaults: Readonly<Record<OrgRole, boolean>>\n /** Each host role's verdict, for a `site` key. */\n hostRoleDefaults?: Readonly<Record<string, boolean>>\n /**\n * The broader key this one is a finer part of (AGL-4047): never granted\n * without it, and following it where no custom role or override sets this\n * key. Only a plugin's key refines, and only a key the catalog holds.\n */\n refines?: OrgPermission\n /**\n * The key this one follows where no custom role or override sets it,\n * without needing it (AGL-4047): granted on its own, it holds while that\n * key is refused. Only a plugin's key follows, and only a key the catalog\n * holds; a key that refines does not also follow.\n */\n follows?: OrgPermission\n /**\n * Present when a site can narrow the key (AGL-4047): where a member who\n * works across the workspace has narrower access to one site than their\n * workspace role, whether the key follows that site role there is a\n * control, set per key on a custom role (`siteNarrowing`) and per member\n * (`AglynOrgMember.siteNarrowing`), and this is each built-in role's\n * default for it. `narrowPermissionsToSite` applies it. Only a plugin's key.\n */\n narrowsPerSite?: Readonly<Record<OrgRole, boolean>>\n /**\n * A site can raise the key (AGL-4047): wider access to one site than the\n * member's workspace role reads `hostRoleDefaults` there, unless a custom\n * role or their own permissions decide it or the key it follows.\n * `resolveSiteRaisedPermissions` decides it. Only a plugin's key.\n */\n raisesPerSite?: true\n /**\n * The key's preset is the member's role ON THE SITE a request names, for\n * every member (AGL-4047): `hostRoleDefaults` read for the access they hold\n * there, then their custom role, their own permissions and that site's\n * toggle. `roleDefaults` is the answer with no site named.\n * `resolveSiteRolePresetPermissions` decides it. The core's site keys\n * (`CORE_SITE_ROLE_PRESETS`) and a plugin's key with `hostRoleDefaults`.\n */\n presetFromSiteRole?: true\n /**\n * With `presetFromSiteRole`: the site role is the preset only for a member\n * of the whole workspace (AGL-4047). A site collaborator's preset is\n * refusal, whatever their site role, and only the site's toggle grants it\n * to them. A plugin's key that replaces a gate asking for an admin of the\n * whole workspace who is also the site's admin.\n */\n workspaceMembersOnly?: true\n}\n\n/**\n * The core keys, in display order for role editors.\n *\n * ⚠️ **EVERY KEY HERE MUST BE ENFORCED SERVER-SIDE.** A permission a customer\n * can tick that changes nothing is worse than its absence, because it implies\n * a control that does not exist — an owner unticks \"delete sites\", hands the\n * role out, and the member deletes sites while the console does not even dim\n * the button. Three of the eleven were in exactly that state (AGL-2444).\n *\n * `marketing.manage` was removed rather than wired, and the reason is worth\n * keeping: announcement bars, popups and campaigns live at\n * `hosts/{hostId}/overlays|campaigns|experiments` and are written\n * client-direct against the security rules, which gate on the HOST role.\n * There is no org-level boundary for it to sit on, and the action it named is\n * not org-scoped at all. Wiring it would have meant inventing one and\n * producing a second permission that looks enforced and is not. If a granular\n * marketing permission is wanted it belongs in the plugin-declared mechanism\n * (AGL-435), scoped to a host — a product decision, not this repair.\n *\n * `apps/console/specs/org-permissions-are-enforced.spec.ts` fails the build if\n * a key here, or a key a plugin declares, gains no server-side consumer.\n */\nconst CORE_ORG_PERMISSIONS: readonly OrgPermissionDefinition[] =\n // One table, shared with the native apps through the contracts (AGL-3950).\n CORE_ORG_PERMISSION_ENTRIES.map((entry) => ({\n ...entry,\n key: entry.key as CoreOrgPermission,\n group: entry.group ?? 'organization',\n scope: entry.scope ?? 'org',\n // A site key's preset is the member's role on the site (AGL-4047).\n ...(CORE_SITE_ROLE_PRESETS[entry.key]\n ? { hostRoleDefaults: { ...CORE_SITE_ROLE_PRESETS[entry.key] }, presetFromSiteRole: true as const }\n : {}),\n roleDefaults: {\n owner: CORE_ORG_ROLE_GRANTS.owner.includes(entry.key),\n admin: CORE_ORG_ROLE_GRANTS.admin.includes(entry.key),\n editor: CORE_ORG_ROLE_GRANTS.editor.includes(entry.key),\n viewer: CORE_ORG_ROLE_GRANTS.viewer.includes(entry.key),\n },\n }))\n\n/** The core keys: a plugin may add keys beside them and never redefine one. */\nconst CORE_ORG_PERMISSION_KEYS: ReadonlySet<string> = new Set(\n CORE_ORG_PERMISSIONS.map((definition) => definition.key),\n)\n\n/** The legacy camelCase projection's keys, which no declaration may take either. */\nconst LEGACY_PERMISSION_KEYS: ReadonlySet<string> = new Set<string>(\n ORG_ROLE_PERMISSION_KEYS,\n)\n\n/** The org roles, in the order `DEFAULT_ROLE_PERMISSIONS` lists them. */\nconst ORG_ROLES: readonly OrgRole[] = ['owner', 'admin', 'editor', 'viewer']\n\n/**\n * Every permission, in display order for role editors: the core keys, then\n * the keys plugins declare through `registerPluginEntitlements`, in the\n * plugins' catalog order (AGL-2984).\n *\n * LIVE, and kept in step in place. A plugin's declarations register at\n * module scope — at boot on the server, before the first paint in the\n * console — which can be after a module holding this binding was evaluated.\n * So this array, the key list, the role defaults and the empty map below are\n * rewritten in place on every registration instead of being computed once\n * when this module loads, and whoever holds the binding reads the declared\n * keys with the core ones.\n */\nexport const ORG_PERMISSIONS: readonly OrgPermissionDefinition[] = []\n\nexport const ORG_PERMISSION_KEYS: readonly OrgPermission[] = []\n\n/**\n * The groups the catalog's keys are filed under, in display order (AGL-4047):\n * the core groups, then one per plugin group a declaration names. Live, like\n * `ORG_PERMISSIONS`; a group with no key is left out.\n */\nexport const ORG_PERMISSION_GROUPS: readonly OrgPermissionGroup[] = []\n\n/**\n * The catalog keys that came through `registerPluginPermissions` (AGL-4047):\n * the keys the per-tier resolver (`resolveRolePermissions`) also answers,\n * which a caller holding a catalog-resolved map lays over that resolver's\n * answer so a custom role reaches them. Live.\n */\nexport const TIER_PERMISSION_KEYS: readonly OrgPermission[] = []\n\n/**\n * Every catalog key that refines a broader one, with that key (AGL-4047),\n * and every key that only follows one, marked `'follows'`. Live, like\n * `ORG_PERMISSIONS`. A resolver lays it over its map with\n * `refinePermissionMap`.\n */\nexport const ORG_PERMISSION_REFINEMENTS: readonly PermissionRefinement[] = []\n\nconst NO_PERMISSIONS = {} as Record<OrgPermission, boolean>\n\n/**\n * Role → default permission set. Owner/admin hold everything (owner-only\n * actions like org deletion stay role-checked, not permission-checked);\n * editors work on content but see no money or roster controls; viewers read\n * only. A declared key's default for each role is the one its declaration\n * names.\n */\nexport const DEFAULT_ROLE_PERMISSIONS: Record<\n OrgRole,\n Record<OrgPermission, boolean>\n> = {\n owner: {} as Record<OrgPermission, boolean>,\n admin: {} as Record<OrgPermission, boolean>,\n editor: {} as Record<OrgPermission, boolean>,\n viewer: {} as Record<OrgPermission, boolean>,\n}\n\n/** Refused declarations already logged, so a refusal is reported once. */\nconst reportedRefusals = new Set<string>()\n\nfunction replaceItems<T>(target: readonly T[], items: readonly T[]): void {\n const list = target as T[]\n list.length = 0\n list.push(...items)\n}\n\nfunction replaceEntries(\n target: Record<string, boolean>,\n entries: Record<string, boolean>,\n): void {\n for (const key of Object.keys(target)) delete target[key]\n Object.assign(target, entries)\n}\n\n/**\n * Rebuilds the live catalog: the core keys, then every key a plugin declared\n * through either declaration API (`listPluginCatalogPermissions`, AGL-4047).\n * A declaration naming a core or legacy key is refused — that key keeps its\n * own meaning and defaults — and logged once.\n */\nfunction syncCatalog(): void {\n const definitions: OrgPermissionDefinition[] = [...CORE_ORG_PERMISSIONS]\n const tierKeys: string[] = []\n const groups: OrgPermissionGroup[] = CORE_ORG_PERMISSION_GROUPS.map((group) => ({ ...group }))\n for (const declared of listPluginCatalogPermissions()) {\n if (\n CORE_ORG_PERMISSION_KEYS.has(declared.key) ||\n LEGACY_PERMISSION_KEYS.has(declared.key)\n ) {\n const refusal = `${declared.pluginId}\\x00${declared.key}`\n if (!reportedRefusals.has(refusal)) {\n reportedRefusals.add(refusal)\n console.error(\n `[plugins] refused catalog permission \"${declared.key}\" to ` +\n `\"${declared.pluginId}\": the key belongs to the core catalog`,\n )\n }\n continue\n }\n definitions.push({\n key: declared.key,\n label: declared.label,\n description: declared.description,\n pluginId: declared.pluginId,\n group: declared.group,\n scope: declared.hostRoleDefaults ? 'site' : 'org',\n roleDefaults: declared.roleDefaults,\n ...(declared.hostRoleDefaults ? { hostRoleDefaults: declared.hostRoleDefaults } : {}),\n ...(declared.refines ? { refines: declared.refines } : {}),\n ...(declared.follows && !declared.refines ? { follows: declared.follows } : {}),\n ...(declared.narrowsPerSite ? { narrowsPerSite: { ...declared.narrowsPerSite } } : {}),\n ...(declared.raisesPerSite ? { raisesPerSite: true as const } : {}),\n ...(declared.presetFromSiteRole && declared.hostRoleDefaults ? { presetFromSiteRole: true as const } : {}),\n ...(declared.presetFromSiteRole && declared.hostRoleDefaults && declared.workspaceMembersOnly\n ? { workspaceMembersOnly: true as const }\n : {}),\n })\n if (declared.source === 'tier') tierKeys.push(declared.key)\n if (!groups.some((group) => group.id === declared.group)) {\n groups.push({ id: declared.group, label: declared.groupLabel, pluginId: declared.pluginId })\n }\n }\n const keys = definitions.map((definition) => definition.key)\n const known = new Set<string>(keys)\n // A refinement of a key the catalog does not hold refines nothing, and is\n // dropped from the definition so no editor offers a link to nowhere.\n for (const definition of definitions) {\n if (definition.refines && (!known.has(definition.refines) || definition.refines === definition.key)) {\n delete definition.refines\n }\n if (definition.follows && (!known.has(definition.follows) || definition.follows === definition.key)) {\n delete definition.follows\n }\n }\n replaceItems(\n ORG_PERMISSION_REFINEMENTS,\n definitions.flatMap((definition) => {\n const refinement = permissionRefinementOf(definition)\n return refinement ? [refinement] : []\n }),\n )\n replaceItems(ORG_PERMISSIONS, definitions)\n replaceItems(ORG_PERMISSION_KEYS, keys)\n replaceItems(\n ORG_PERMISSION_GROUPS,\n groups.filter((group) => definitions.some((definition) => definition.group === group.id)),\n )\n replaceItems(TIER_PERMISSION_KEYS, tierKeys)\n replaceEntries(NO_PERMISSIONS, Object.fromEntries(keys.map((key) => [key, false])))\n for (const role of ORG_ROLES) {\n replaceEntries(\n DEFAULT_ROLE_PERMISSIONS[role],\n Object.fromEntries(\n definitions.map((definition) => [definition.key, definition.roleDefaults[role] === true]),\n ),\n )\n }\n CATALOG_AND_LEGACY_KEYS.clear()\n for (const key of [...keys, ...LEGACY_PERMISSION_KEYS]) {\n CATALOG_AND_LEGACY_KEYS.add(key)\n }\n}\n\n/**\n * The `registerPluginPermissions` keys of a catalog-resolved map (AGL-4047).\n *\n * Those keys are answered twice: by the per-tier resolver\n * (`resolveRolePermissions`, the role's tier default and the member's\n * override) and, now that they are catalog keys, by `resolveOrgPermissions`\n * with every layer — the custom role's included. A caller that builds its\n * map from the first spreads this over it, so the custom role a member\n * carries reaches `managePos` the way it reaches `hosts.create`.\n */\nexport function tierPermissionsOf(\n granted: Readonly<Record<string, boolean>> | null | undefined,\n): Record<string, boolean> {\n const picked: Record<string, boolean> = {}\n if (!granted) return picked\n for (const key of TIER_PERMISSION_KEYS) {\n if (typeof granted[key] === 'boolean') picked[key] = granted[key]\n }\n return picked\n}\n\n/** One catalog key by name, or undefined. */\nexport function orgPermissionDefinition(\n permission: OrgPermission,\n): OrgPermissionDefinition | undefined {\n return ORG_PERMISSIONS.find((definition) => definition.key === permission)\n}\n\n/** `orgs/{orgId}/roles/{roleId}` — a named custom permission set. */\nexport interface AglynOrgCustomRole {\n $id?: string\n name?: string\n description?: string\n /** Full permission map; missing keys read as false. */\n permissions?: Partial<Record<OrgPermission, boolean>>\n /**\n * Per key a site can narrow (AGL-4047): `true` follows the site role on a\n * site where the member's access is narrower than their workspace role,\n * `false` keeps the permission there. A key it leaves out follows the\n * built-in role's default (`OrgPermissionDefinition.narrowsPerSite`), and\n * a member's own `siteNarrowing` wins over it.\n */\n siteNarrowing?: Partial<Record<OrgPermission, boolean>>\n}\n\n/**\n * Effective permission map for a member: the org role's defaults, then\n * the custom role's map (when the member has `roleId` and the role doc\n * is supplied), then per-member overrides — later layers win key-by-key.\n */\nexport function resolveOrgPermissions(\n member:\n | (Partial<AglynOrgMember> & {\n roleId?: string\n permissions?: Partial<Record<OrgPermission, boolean>>\n })\n | null\n | undefined,\n customRole?: AglynOrgCustomRole | null,\n): Record<OrgPermission, boolean> {\n if (!member) return NO_PERMISSIONS\n const role = (member.role ?? 'viewer') as OrgRole\n const base = DEFAULT_ROLE_PERMISSIONS[role] ?? NO_PERMISSIONS\n const merged: Record<OrgPermission, boolean> = { ...base }\n if (member.roleId && customRole?.permissions) {\n for (const key of ORG_PERMISSION_KEYS) {\n const value = customRole.permissions[key]\n if (typeof value === 'boolean') merged[key] = value\n }\n }\n if (member.permissions) {\n for (const key of ORG_PERMISSION_KEYS) {\n const value = member.permissions[key]\n if (typeof value === 'boolean') merged[key] = value\n }\n }\n // A key that refines a broader one (AGL-4047) follows it where neither\n // layer set the key, and is never held without it; a key that only follows\n // one takes its verdict where neither layer set the key, and nothing more.\n return refinePermissionMap(merged, ORG_PERMISSION_REFINEMENTS, [\n member.roleId ? customRole?.permissions : undefined,\n member.permissions,\n ])\n}\n\n/**\n * The values a custom role and a per-member override set EXPLICITLY for\n * permission keys outside this catalog — the keys plugins declare through\n * `registerPluginPermissions` (AGL-2974).\n *\n * `resolveOrgPermissions` walks the catalog alone, so a plugin key stored on\n * a custom role or a member document never reached the `resolvedPermissions`\n * stamp the Firestore rules read. A rule gating on one could then honor\n * neither a grant to an editor nor a revocation from an admin. This carries\n * exactly those stored decisions, custom role first and the per-member\n * override over it, which is the precedence the resolver applies.\n *\n * Tier DEFAULTS are deliberately absent. They live in the plugin registry,\n * which is populated per process by whichever plugin bundles that process\n * loaded, and a stamp computed from it would differ by which route happened\n * to write it. A rule supplies the default itself from the member's role,\n * the way `memberResolves` is always conjoined with a role gate.\n *\n * The legacy six camelCase keys are excluded as well: they are the catalog's\n * older projection, not plugin keys, and no rule reads them.\n */\nexport function explicitPluginPermissionValues(\n member:\n | {\n roleId?: string\n permissions?: Partial<Record<string, unknown>> | null\n }\n | null\n | undefined,\n customRole?: { permissions?: Partial<Record<string, unknown>> | null } | null,\n): Record<string, boolean> {\n const values: Record<string, boolean> = {}\n if (!member) return values\n const layers = [\n member.roleId ? customRole?.permissions : undefined,\n member.permissions,\n ]\n for (const layer of layers) {\n for (const [key, value] of Object.entries(layer ?? {})) {\n if (typeof value !== 'boolean') continue\n if (CATALOG_AND_LEGACY_KEYS.has(key)) continue\n values[key] = value\n }\n }\n return values\n}\n\n/**\n * Keys {@link explicitPluginPermissionValues} leaves to the catalog: every\n * catalog key, declared ones included, and the legacy six. Kept in step by\n * `syncCatalog`.\n */\nconst CATALOG_AND_LEGACY_KEYS = new Set<string>()\n\n/**\n * The `resolvedPermissions` map stamped on a member document for the\n * Firestore rules: the catalog verdict with every layer applied, and beside\n * it the plugin keys a custom role or an override set explicitly.\n *\n * One function for every writer of the stamp, so the org-create transaction\n * and `syncOrgAuthProjections` cannot project different maps. The catalog\n * spreads last; the two key spaces do not overlap, and the order keeps it\n * true that nothing but the resolver decides a catalog key.\n */\nexport function projectMemberResolvedPermissions(\n member: Parameters<typeof resolveOrgPermissions>[0],\n customRole?: AglynOrgCustomRole | null,\n): Record<string, boolean> {\n return {\n ...explicitPluginPermissionValues(member, customRole),\n ...resolveOrgPermissions(member, customRole),\n }\n}\n\n/** Single-permission convenience over `resolveOrgPermissions`. */\nexport function hasOrgPermission(\n member: Parameters<typeof resolveOrgPermissions>[0],\n permission: OrgPermission,\n customRole?: AglynOrgCustomRole | null,\n): boolean {\n return resolveOrgPermissions(member, customRole)[permission]\n}\n\n/** The catalog label for a key — the words a refusal names — or the key itself. */\nexport function orgPermissionLabel(permission: OrgPermission): string {\n return (\n ORG_PERMISSIONS.find((definition) => definition.key === permission)?.label ??\n permission\n )\n}\n\n/**\n * The sentence a door answers when a member does not hold a key (AGL-4047):\n * it names the permission by its catalog label and who can change it, never\n * a role.\n */\nexport function permissionRefusalText(permission: OrgPermission): string {\n return `Your role does not include \"${orgPermissionLabel(permission)}\" — ask an organization admin`\n}\n\n/** The keys plugins declared into the catalog, in catalog order. */\nexport function pluginOrgPermissionKeys(): OrgPermission[] {\n return ORG_PERMISSIONS.filter((definition) => definition.pluginId).map(\n (definition) => definition.key,\n )\n}\n\n/** One declared catalog key that moved, and the direction it moved in. */\nexport interface PluginPermissionChange {\n permission: string\n granted: boolean\n}\n\n/**\n * The keys PLUGINS declared into the catalog whose value moved between two\n * maps, in the new map's order — what a member, role or collaborator write\n * raises `org.permissions.changed` for, once per key, so the plugin that owns\n * a key can record who moved it.\n *\n * A key the new map leaves unset is not a change: on a role it defers to the\n * base role, on a site toggle to the host role, and neither is a grant or a\n * revocation of its own. A key set for the first time is a change, because\n * the verdict it produces is no longer the default's. The core keys and the\n * legacy six raise nothing; their writes are recorded by the routes' own\n * activity sentences.\n */\nexport function pluginPermissionChanges(\n before: Partial<Record<string, unknown>> | null | undefined,\n after: Partial<Record<string, unknown>> | null | undefined,\n): PluginPermissionChange[] {\n const changes: PluginPermissionChange[] = []\n for (const [key, next] of Object.entries(after ?? {})) {\n if (typeof next !== 'boolean') continue\n if (CORE_ORG_PERMISSION_KEYS.has(key) || LEGACY_PERMISSION_KEYS.has(key)) continue\n if (before?.[key] === next) continue\n changes.push({ permission: key, granted: next })\n }\n return changes\n}\n\n/**\n * The granular (dotted) set projected onto the legacy boolean map\n * (AGL-2350).\n *\n * ## Why this lives here rather than in the console hook\n *\n * It was a private function in `apps/console/hooks/use-org-permissions.ts`,\n * which meant the CLIENT translated the stored permission model into the\n * legacy flags and the SERVER did not translate at all — it derived the\n * legacy flags straight from the built-in role tier and dropped custom roles\n * and per-member overrides on the floor. The two disagreed exactly on the\n * feature `custom-roles.md` sells, in both directions: a permission granted\n * by a custom role showed in the UI and 403'd on POST, and one revoked by an\n * override was hidden in the UI and still succeeded on POST.\n *\n * One exported copy is what stops them drifting again.\n *\n * ## The key spaces are NOT interchangeable, and that is the trap\n *\n * Two permission vocabularies exist. The stored one is DOTTED\n * (`plugins.install`) — `apps/console/app/api/orgs/roles/route.ts` sanitizes\n * against `ORG_PERMISSION_KEYS` before writing, and `AglynOrgMember`\n * `permissions` is typed to it. The legacy one is camelCase\n * (`installPlugins`).\n *\n * `resolveRolePermissions` in `org-roles.ts` accepts `overrides` and\n * `customRoles` arguments keyed by the CAMELCASE space. Feeding it the real\n * stored documents therefore matches no key and changes nothing — it looks\n * like wiring the feature up while doing precisely nothing. Those two\n * parameters are used by nothing but that module's own spec. Translate\n * through here instead.\n *\n * `editHosts` is derived from the ROLE, not from a dotted key: it has no\n * counterpart in the granular catalog and never had one.\n */\nexport function toLegacyPermissions(\n granted: Record<OrgPermission, boolean>,\n role: OrgRole | null | undefined,\n): OrgPermissions {\n return {\n createHosts: granted['hosts.create'],\n editHosts: role !== 'viewer',\n editBilling: granted['billing.manage'],\n publishToMarketplace: granted['marketplace.publish'],\n installPlugins: granted['plugins.install'],\n manageMembers: granted['members.manage'],\n }\n}\n\nsyncCatalog()\nsubscribePluginEntitlements(syncCatalog)\n// The second declaration API feeds the same catalog (AGL-4047).\nsubscribePluginPermissions(syncCatalog)\n"],"names":["listPluginCatalogPermissions","subscribePluginEntitlements","permissionRefinementOf","refinePermissionMap","subscribePluginPermissions","ORG_ROLE_PERMISSION_KEYS","CORE_ORG_PERMISSION_ENTRIES","CORE_ORG_PERMISSION_GROUPS","CORE_ORG_ROLE_GRANTS","CORE_SITE_ROLE_PRESETS","CORE_ORG_PERMISSIONS","map","entry","key","group","scope","hostRoleDefaults","presetFromSiteRole","roleDefaults","owner","includes","admin","editor","viewer","CORE_ORG_PERMISSION_KEYS","Set","definition","LEGACY_PERMISSION_KEYS","ORG_ROLES","ORG_PERMISSIONS","ORG_PERMISSION_KEYS","ORG_PERMISSION_GROUPS","TIER_PERMISSION_KEYS","ORG_PERMISSION_REFINEMENTS","NO_PERMISSIONS","DEFAULT_ROLE_PERMISSIONS","reportedRefusals","replaceItems","target","items","list","length","push","replaceEntries","entries","Object","keys","assign","syncCatalog","definitions","tierKeys","groups","declared","has","refusal","pluginId","add","console","error","label","description","refines","follows","narrowsPerSite","raisesPerSite","workspaceMembersOnly","source","some","id","groupLabel","known","flatMap","refinement","filter","fromEntries","role","CATALOG_AND_LEGACY_KEYS","clear","tierPermissionsOf","granted","picked","orgPermissionDefinition","permission","find","resolveOrgPermissions","member","customRole","base","merged","roleId","permissions","value","undefined","explicitPluginPermissionValues","values","layers","layer","projectMemberResolvedPermissions","hasOrgPermission","orgPermissionLabel","permissionRefusalText","pluginOrgPermissionKeys","pluginPermissionChanges","before","after","changes","next","toLegacyPermissions","createHosts","editHosts","editBilling","publishToMarketplace","installPlugins","manageMembers"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC;AAGD,SACEA,4BAA4B,EAC5BC,2BAA2B,QACtB,2CAAuC;AAC9C,SACEC,sBAAsB,EACtBC,mBAAmB,EACnBC,0BAA0B,QAErB,0CAAsC;AAC7C,SAASC,wBAAwB,QAAQ,iBAAa;AACtD,SACEC,2BAA2B,EAC3BC,0BAA0B,EAC1BC,oBAAoB,EACpBC,sBAAsB,QAGjB,uBAAmB;AA0H1B;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,MAAMC,uBACJ,2EAA2E;AAC3EJ,4BAA4BK,GAAG,CAAC,CAACC;QAGxBA,cACAA;WAJmC,aACvCA;QACHC,KAAKD,MAAMC,GAAG;QACdC,KAAK,GAAEF,eAAAA,MAAME,KAAK,YAAXF,eAAe;QACtBG,KAAK,GAAEH,eAAAA,MAAMG,KAAK,YAAXH,eAAe;OAElBH,sBAAsB,CAACG,MAAMC,GAAG,CAAC,GACjC;QAAEG,kBAAkB,aAAKP,sBAAsB,CAACG,MAAMC,GAAG,CAAC;QAAII,oBAAoB;IAAc,IAChG,CAAC;QACLC,cAAc;YACZC,OAAOX,qBAAqBW,KAAK,CAACC,QAAQ,CAACR,MAAMC,GAAG;YACpDQ,OAAOb,qBAAqBa,KAAK,CAACD,QAAQ,CAACR,MAAMC,GAAG;YACpDS,QAAQd,qBAAqBc,MAAM,CAACF,QAAQ,CAACR,MAAMC,GAAG;YACtDU,QAAQf,qBAAqBe,MAAM,CAACH,QAAQ,CAACR,MAAMC,GAAG;QACxD;;;AAGJ,6EAA6E,GAC7E,MAAMW,2BAAgD,IAAIC,IACxDf,qBAAqBC,GAAG,CAAC,CAACe,aAAeA,WAAWb,GAAG;AAGzD,kFAAkF,GAClF,MAAMc,yBAA8C,IAAIF,IACtDpB;AAGF,uEAAuE,GACvE,MAAMuB,YAAgC;IAAC;IAAS;IA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B,EAAEiB,mBAAmBjB,YAAY,6BAA6B,CAAC;AACrG;AAEA,kEAAkE,GAClE,OAAO,SAASmB;IACd,OAAOxE,gBAAgB4C,MAAM,CAAC,CAAC/C,aAAeA,WAAW6B,QAAQ,EAAE5C,GAAG,CACpE,CAACe,aAAeA,WAAWb,GAAG;AAElC;AAQA;;;;;;;;;;;;CAYC,GACD,OAAO,SAASyF,wBACdC,MAA2D,EAC3DC,KAA0D;IAE1D,MAAMC,UAAoC,EAAE;IAC5C,KAAK,MAAM,CAAC5F,KAAK6F,KAAK,IAAI7D,OAAOD,OAAO,CAAC4D,gBAAAA,QAAS,CAAC,GAAI;QACrD,IAAI,OAAOE,SAAS,WAAW;QAC/B,IAAIlF,yBAAyB6B,GAAG,CAACxC,QAAQc,uBAAuB0B,GAAG,CAACxC,MAAM;QAC1E,IAAI0F,CAAAA,0BAAAA,MAAQ,CAAC1F,IAAI,MAAK6F,MAAM;QAC5BD,QAAQ/D,IAAI,CAAC;YAAEwC,YAAYrE;YAAKkE,SAAS2B;QAAK;IAChD;IACA,OAAOD;AACT;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCC,GACD,OAAO,SAASE,oBACd5B,OAAuC,EACvCJ,IAAgC;IAEhC,OAAO;QACLiC,aAAa7B,OAAO,CAAC,eAAe;QACpC8B,WAAWlC,SAAS;QACpBmC,aAAa/B,OAAO,CAAC,iBAAiB;QACtCgC,sBAAsBhC,OAAO,CAAC,sBAAsB;QACpDiC,gBAAgBjC,OAAO,CAAC,kBAAkB;QAC1CkC,eAAelC,OAAO,CAAC,iBAAiB;IAC1C;AACF;AAEA/B;AACA/C,4BAA4B+C;AAC5B,gEAAgE;AAChE5C,2BAA2B4C"}
@@ -72,6 +72,13 @@ export interface OrgPermissionEntry {
72
72
  * for a key a site cannot narrow.
73
73
  */
74
74
  narrowable?: boolean;
75
+ /**
76
+ * For a site key, who holds it by the role they have on that site, read
77
+ * where the site has no stamp for them (`hostStampedSitePermission`, AGL-4047).
78
+ * Absent for a key with no such preset, and for one that holds for members of
79
+ * the whole workspace only, which a site's roles alone cannot answer.
80
+ */
81
+ sitePreset?: Record<string, boolean>;
75
82
  }
76
83
  /** A heading the role editor groups keys under. */
77
84
  export interface OrgPermissionGroup {
@@ -207,7 +207,7 @@ export const ORG_ROLE_NAMES = [
207
207
  key: 'data.manage',
208
208
  group: 'datasets',
209
209
  label: 'Manage data',
210
- description: 'Create, edit, and delete organization datasets, the CRM’s records, and marketing lists and topics.'
210
+ description: 'Create and edit organization datasets, add, change and delete their rows, and manage the CRM’s records and marketing lists and topics.'
211
211
  },
212
212
  // Workspace content that asked for the owner, admin or editor role
213
213
  // (AGL-4047), each a key whose default is exactly those roles.
@@ -221,7 +221,7 @@ export const ORG_ROLE_NAMES = [
221
221
  key: 'org.eraseRecords',
222
222
  group: 'datasets',
223
223
  label: 'Delete workspace records',
224
- description: 'Delete records the whole workspace shares, such as dataset rows, contacts and lists.'
224
+ description: 'Delete a whole dataset or list the workspace shares, with everything in it. Deleting one row of a dataset asks “Manage data”.'
225
225
  },
226
226
  {
227
227
  key: 'marketplace.publish',
@@ -319,9 +319,9 @@ const CORE_KEYS = CORE_ORG_PERMISSION_ENTRIES.map((entry)=>entry.key);
319
319
  * `registerPluginPermissions` keys land in one catalog (AGL-4047).
320
320
  */ export function assembleOrgRoleGrants(core, ...declarationLists) {
321
321
  var _core_groups;
322
- const permissions = [
323
- ...core.permissions
324
- ];
322
+ const permissions = core.permissions.map((entry)=>CORE_SITE_ROLE_PRESETS[entry.key] ? _extends({}, entry, {
323
+ sitePreset: _extends({}, CORE_SITE_ROLE_PRESETS[entry.key])
324
+ }) : entry);
325
325
  const groups = [
326
326
  ...(_core_groups = core.groups) != null ? _core_groups : []
327
327
  ];
@@ -354,6 +354,8 @@ const CORE_KEYS = CORE_ORG_PERMISSION_ENTRIES.map((entry)=>entry.key);
354
354
  follows: declaration.follows
355
355
  } : {}, declaration.narrowsPerSite ? {
356
356
  narrowable: true
357
+ } : {}, declaration.hostRoleDefaults && !declaration.workspaceMembersOnly ? {
358
+ sitePreset: _extends({}, declaration.hostRoleDefaults)
357
359
  } : {}));
358
360
  if (group && !groupIds.has(group)) {
359
361
  var _declaration_groupLabel;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/app-utils/org-role-grants.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * WHAT EACH ORG ROLE MAY DO, AS DATA (AGL-243, AGL-3950).\n *\n * The core permission catalog and the core keys each org role holds when\n * nothing refines it. `org-permissions.ts` builds the console's live\n * catalog from these (adding the keys plugins declare, with their own role\n * defaults), and the native apps read the same grants through the generated\n * contracts, so a native gate (`hosts.create`, `data.manage`) answers as the\n * console does for a member row that carries no `resolvedPermissions` stamp.\n *\n * `ORG_ROLE_GRANTS` in the contracts is this table with every plugin's\n * declared catalog keys added (`assembleOrgRoleGrants`, run by\n * `generate:native-contracts` over each plugin's declarations), which is\n * exactly the set the console's `DEFAULT_ROLE_PERMISSIONS` holds once the\n * plugins have registered.\n *\n * NO IMPORTS, on purpose: the native apps read it through the generated\n * contracts.\n */\n\n/** The four org roles. */\nexport type OrgRoleName = 'owner' | 'admin' | 'editor' | 'viewer'\n\n/**\n * Where a key is decided (AGL-4047). `org`: by the member's org role, custom\n * role and overrides, the same on every site. `site`: as `org` for a member\n * who reaches the whole workspace, and per site for a site collaborator,\n * from the host role they hold there and that site's toggle.\n */\nexport type OrgPermissionScope = 'org' | 'site'\n\n/** One key of the org permission catalog. */\nexport interface OrgPermissionEntry {\n key: string\n label: string\n description: string\n /** The plugin that declared it; absent for a core key. */\n pluginId?: string\n /** The group the role editor files it under (`ORG_PERMISSION_GROUPS`, or the declaring plugin's id). */\n group?: string\n /** Where it is decided; absent reads as `org`. */\n scope?: OrgPermissionScope\n /**\n * The broader key this one is a finer part of (AGL-4047): never granted\n * without it, and following it where no custom role or override sets this\n * key. Absent for a key that refines nothing.\n */\n refines?: string\n /**\n * The key this one follows where no custom role or override sets it,\n * without needing it (AGL-4047): granted on its own, it holds while that\n * key is refused. Absent for a key that follows nothing, and for one that\n * refines.\n */\n follows?: string\n /**\n * Whether a site can narrow the key (`narrowsPerSite`, AGL-4047), so the\n * role and member editors offer its \"follow the site role\" choice. Absent\n * for a key a site cannot narrow.\n */\n narrowable?: boolean\n}\n\n/** A heading the role editor groups keys under. */\nexport interface OrgPermissionGroup {\n id: string\n label: string\n /** The plugin whose keys it holds; absent for a core group. */\n pluginId?: string\n}\n\n/** The catalog and each role's default grants. */\nexport interface OrgRoleGrants {\n /** Every key, in the order the role editor lists them. */\n permissions: OrgPermissionEntry[]\n /** The keys each role holds when no custom role or per-member override sets them. */\n grants: Record<OrgRoleName, string[]>\n /** The groups the keys are filed under, in display order; absent in a catalog written before them. */\n groups?: OrgPermissionGroup[]\n}\n\n/**\n * A key a plugin declares into the catalog: `PluginOrgPermissionDeclaration`\n * with org-role defaults, or a `registerPluginPermissions` key (`managePos`),\n * whose `defaults` are per role tier, the owner holding the admin tier's.\n */\nexport interface OrgPermissionDeclaration {\n key: string\n label: string\n description?: string\n roleDefaults?: Record<OrgRoleName, boolean>\n /** The tier defaults of a `registerPluginPermissions` key, used when `roleDefaults` is absent. */\n defaults?: { admin: boolean; editor: boolean; viewer: boolean }\n /** The host-role defaults of a key a site collaborator holds per site. */\n hostRoleDefaults?: Record<string, boolean>\n /** The group it is filed under; absent: a group named by the declaring plugin's id. */\n group?: string\n /** That group's heading, for a group the core does not name. */\n groupLabel?: string\n pluginId?: string\n /** The broader key it refines (AGL-4047). */\n refines?: string\n /** The key it follows without needing it (AGL-4047). */\n follows?: string\n}\n\n/** A custom role (`orgs/{orgId}/roles/{roleId}`) or a member's overrides: explicit verdicts per key. */\nexport interface OrgPermissionLayer {\n permissions?: Record<string, boolean>\n}\n\nexport const ORG_ROLE_NAMES: readonly OrgRoleName[] = ['owner', 'admin', 'editor', 'viewer']\n\n/**\n * The core groups, in display order. A plugin's keys are filed under a group\n * named by the plugin's id unless its declaration names one of these, so no\n * core group id is a plugin id.\n */\nexport const CORE_ORG_PERMISSION_GROUPS: readonly OrgPermissionGroup[] = [\n { id: 'organization', label: 'Organization' },\n { id: 'billing', label: 'Billing' },\n { id: 'team', label: 'Team' },\n { id: 'sites', label: 'Sites' },\n { id: 'datasets', label: 'Data' },\n { id: 'extensions', label: 'Marketplace and plugins' },\n]\n\n/**\n * A declaration's verdict for each org role: its `roleDefaults`, or, for a\n * `registerPluginPermissions` key, its tier `defaults` with the owner on the\n * admin tier (the tier `ORG_ROLE_TIER` resolves the owner onto).\n */\nexport function declarationRoleDefaults(\n declaration: Pick<OrgPermissionDeclaration, 'roleDefaults' | 'defaults'>,\n): Record<OrgRoleName, boolean> {\n if (declaration.roleDefaults) {\n return {\n owner: declaration.roleDefaults.owner === true,\n admin: declaration.roleDefaults.admin === true,\n editor: declaration.roleDefaults.editor === true,\n viewer: declaration.roleDefaults.viewer === true,\n }\n }\n const tiers = declaration.defaults\n return {\n owner: tiers?.admin === true,\n admin: tiers?.admin === true,\n editor: tiers?.editor === true,\n viewer: tiers?.viewer === true,\n }\n}\n\n/**\n * The core keys, in display order for role editors. Every key here is\n * enforced server-side (`apps/console/specs/org-permissions-are-enforced.spec.ts`).\n */\nexport const CORE_ORG_PERMISSION_ENTRIES: readonly OrgPermissionEntry[] = [\n {\n key: 'org.settings',\n group: 'organization',\n label: 'Organization settings',\n description: 'Rename the organization, change its slug, transfer it.',\n },\n {\n key: 'org.auditLog',\n group: 'organization',\n label: 'Activity & audit log',\n description: 'See the organization activity feed.',\n },\n {\n key: 'org.apiKeys',\n group: 'organization',\n label: 'Manage API keys',\n description: 'Create and revoke the workspace’s REST API keys.',\n },\n {\n key: 'org.privacy',\n group: 'organization',\n label: 'Privacy requests',\n description: 'Erase a person from every site in the workspace when they ask.',\n },\n {\n key: 'org.holds',\n group: 'organization',\n label: 'Holds and reviews',\n description: 'See what is held, flagged or locked on the workspace, and request a review.',\n },\n // Workspace acts that asked for the owner or admin role (AGL-4047), each\n // a key of its own whose default is exactly those roles.\n {\n key: 'org.export',\n group: 'organization',\n label: 'Export workspace data',\n description: 'Download a copy of everything the workspace holds.',\n },\n {\n key: 'org.sendingDomains',\n group: 'organization',\n label: 'Sending domains',\n description: 'Add, verify and remove the domains the workspace’s mail is sent from.',\n },\n {\n key: 'org.businessProfile',\n group: 'organization',\n label: 'Business profile',\n description: 'Change the services, audience and tone every site starts from.',\n },\n {\n key: 'billing.view',\n group: 'billing',\n label: 'View billing',\n description: 'See the plan, usage meters, and invoices.',\n },\n {\n key: 'billing.manage',\n group: 'billing',\n label: 'Manage billing',\n description: 'Change plans, buy add-ons, update payment details.',\n },\n {\n key: 'members.manage',\n group: 'team',\n label: 'Manage members',\n description: 'Invite, remove, and re-role organization members.',\n },\n {\n key: 'hosts.create',\n group: 'sites',\n label: 'Create sites',\n description: 'Create new sites in the organization.',\n },\n {\n key: 'hosts.delete',\n group: 'sites',\n label: 'Delete sites',\n description: 'Delete sites the member can access.',\n },\n // The site keys (AGL-4047): each replaced a gate on the member's role on a\n // site, and keeps that role as its preset (`CORE_SITE_ROLE_PRESETS`).\n {\n key: 'site.edit',\n group: 'sites',\n scope: 'site',\n label: 'Edit site content',\n description: 'Create and change pages, content and everything else on a site, without making it live.',\n },\n {\n key: 'site.publish',\n group: 'sites',\n scope: 'site',\n label: 'Publish site content',\n description: 'Make pages and content live, take them down or schedule them, change redirects and change the site’s fonts.',\n },\n {\n key: 'site.sensitive',\n group: 'sites',\n scope: 'site',\n label: 'See orders and webhooks',\n description: 'Read a site’s orders, returns and sending address, and set up its webhooks.',\n },\n {\n key: 'site.manage',\n group: 'sites',\n scope: 'site',\n label: 'Manage site settings',\n description: 'Manage a site’s people, domain, address and plugins, change its plugin settings (live chat, Zapier) and security allow-lists, import, export and delete it.',\n },\n {\n key: 'data.manage',\n group: 'datasets',\n label: 'Manage data',\n description: 'Create, edit, and delete organization datasets, the CRM’s records, and marketing lists and topics.',\n },\n // Workspace content that asked for the owner, admin or editor role\n // (AGL-4047), each a key whose default is exactly those roles.\n {\n key: 'org.media',\n group: 'datasets',\n label: 'Edit the workspace media library',\n description: 'Upload, change, move and remove the files in the media library every site shares.',\n },\n {\n key: 'org.eraseRecords',\n group: 'datasets',\n label: 'Delete workspace records',\n description: 'Delete records the whole workspace shares, such as dataset rows, contacts and lists.',\n },\n {\n key: 'marketplace.publish',\n group: 'extensions',\n label: 'Publish to marketplace',\n description: 'Publish listings under the organization profile.',\n },\n {\n key: 'plugins.install',\n group: 'extensions',\n label: 'Install plugins',\n description: 'Install or remove marketplace plugins.',\n },\n]\n\nconst CORE_KEYS = CORE_ORG_PERMISSION_ENTRIES.map((entry) => entry.key)\n\n/**\n * The core keys whose preset is the member's role ON THE SITE (AGL-4047), and\n * each site role's verdict: the sets the rules' `canWriteHostContent`,\n * `canPublishHostContent`, `canReadHostSensitive` and the site-admin checks\n * asked before they asked these keys. A member's preset on a site is the\n * verdict for the role they hold there (the owner and admins are Admin on\n * every site); a custom role, their own permissions and that site's toggle\n * then grant or refuse it. Each workspace role's default below is the verdict\n * for its own site role, which is the answer with no site named.\n */\nexport const CORE_SITE_ROLE_PRESETS: Readonly<Record<string, Readonly<Record<string, boolean>>>> = {\n 'site.edit': { admin: true, editor: true, author: true, viewer: false },\n 'site.publish': { admin: true, editor: true, author: false, viewer: false },\n 'site.sensitive': { admin: true, editor: true, author: false, viewer: false },\n 'site.manage': { admin: true, editor: false, author: false, viewer: false },\n}\n\n/** The workspace editor's site keys: its own site role's preset. */\nconst EDITOR_SITE_KEYS = Object.keys(CORE_SITE_ROLE_PRESETS).filter((key) => CORE_SITE_ROLE_PRESETS[key].editor)\n\n/**\n * The core keys each org role holds when nothing refines it: owners and\n * admins hold every core key (owner-only acts like deleting the\n * organization stay role-checked, not permission-checked); editors work on\n * content and see no money or roster controls; viewers read only.\n */\nexport const CORE_ORG_ROLE_GRANTS: Readonly<Record<OrgRoleName, readonly string[]>> = {\n owner: CORE_KEYS,\n admin: CORE_KEYS,\n editor: ['data.manage', 'org.media', 'org.eraseRecords', 'marketplace.publish', 'plugins.install', ...EDITOR_SITE_KEYS],\n viewer: [],\n}\n\n/** The core's catalog and grants; the contracts' value adds the plugins' keys. */\nexport const ORG_ROLE_GRANTS: OrgRoleGrants = {\n permissions: CORE_ORG_PERMISSION_ENTRIES.map((entry) => ({ ...entry })),\n groups: CORE_ORG_PERMISSION_GROUPS.map((group) => ({ ...group })),\n grants: {\n owner: [...CORE_ORG_ROLE_GRANTS.owner],\n admin: [...CORE_ORG_ROLE_GRANTS.admin],\n editor: [...CORE_ORG_ROLE_GRANTS.editor],\n viewer: [...CORE_ORG_ROLE_GRANTS.viewer],\n },\n}\n\n/**\n * The catalog with plugins' declared keys after the core ones, each role\n * holding a declared key when its declaration's defaults say so. A\n * declaration naming a key already taken is refused, as the console's\n * catalog refuses it: the first declaration keeps its meaning and defaults.\n * Every declaration list after the first is folded in the same way, so the\n * org catalog keys (`PluginOrgPermissionDeclaration`) and the\n * `registerPluginPermissions` keys land in one catalog (AGL-4047).\n */\nexport function assembleOrgRoleGrants(\n core: OrgRoleGrants,\n ...declarationLists: ReadonlyArray<readonly OrgPermissionDeclaration[]>\n): OrgRoleGrants {\n const permissions = [...core.permissions]\n const groups = [...(core.groups ?? [])]\n const grants = Object.fromEntries(ORG_ROLE_NAMES.map((role) => [role, [...core.grants[role]]])) as Record<\n OrgRoleName,\n string[]\n >\n const taken = new Set(permissions.map((entry) => entry.key))\n const groupIds = new Set(groups.map((group) => group.id))\n for (const declaration of declarationLists.flat()) {\n if (!declaration?.key || taken.has(declaration.key)) continue\n taken.add(declaration.key)\n const group = declaration.group ?? declaration.pluginId\n permissions.push({\n key: declaration.key,\n label: declaration.label,\n description: declaration.description ?? '',\n ...(declaration.pluginId ? { pluginId: declaration.pluginId } : {}),\n ...(group ? { group } : {}),\n scope: declaration.hostRoleDefaults ? 'site' : 'org',\n ...(declaration.refines && declaration.refines !== declaration.key ? { refines: declaration.refines } : {}),\n ...(declaration.follows && declaration.follows !== declaration.key && !declaration.refines\n ? { follows: declaration.follows }\n : {}),\n // A plugin's own declaration carries `narrowsPerSite` (a flag or one per role); only whether it is set matters here.\n ...((declaration as { narrowsPerSite?: unknown }).narrowsPerSite ? { narrowable: true } : {}),\n })\n if (group && !groupIds.has(group)) {\n groupIds.add(group)\n groups.push({\n id: group,\n label: declaration.groupLabel ?? group,\n ...(declaration.pluginId ? { pluginId: declaration.pluginId } : {}),\n })\n } else if (group && declaration.pluginId) {\n // A plugin's group opened by a declaration that names no plugin (an org\n // catalog declaration, AGL-4047) is still that plugin's: the first of\n // its declarations that names one says so.\n const index = groups.findIndex((entry) => entry.id === group)\n if (index >= 0 && !groups[index].pluginId) groups[index] = { ...groups[index], pluginId: declaration.pluginId }\n }\n const defaults = declarationRoleDefaults(declaration)\n for (const role of ORG_ROLE_NAMES) {\n if (defaults[role]) grants[role].push(declaration.key)\n }\n }\n return { permissions, grants, groups }\n}\n\n/**\n * A member's effective permissions, as the console's `resolveOrgPermissions`\n * answers them: the role's defaults, then the custom role's explicit\n * verdicts (when the member has one), then the member's own overrides,\n * later layers winning key by key, over the catalog's keys only. A missing\n * role reads as viewer; a role the catalog does not know holds nothing.\n */\nexport function rolePermissions(\n catalog: OrgRoleGrants,\n role: string | null | undefined,\n customRole?: OrgPermissionLayer | null,\n overrides?: OrgPermissionLayer | null,\n): Record<string, boolean> {\n const keys = catalog.permissions.map((entry) => entry.key)\n const name = role ?? 'viewer'\n const held = new Set((catalog.grants as Record<string, string[] | undefined>)[name] ?? [])\n const out: Record<string, boolean> = Object.fromEntries(keys.map((key) => [key, held.has(key)]))\n for (const layer of [customRole, overrides]) {\n const values = layer?.permissions\n if (!values) continue\n for (const key of keys) {\n if (typeof values[key] === 'boolean') out[key] = values[key]\n }\n }\n // A key that refines a broader one (AGL-4047) is never held without it, and\n // follows it where neither layer set the key; a key that only follows one\n // takes its verdict where neither layer set the key, and nothing more.\n // Applied broader keys first, so a chain settles in one pass; a refinement\n // that loops is left alone.\n const broader = new Map<string, string>()\n const followsOnly = new Set<string>()\n for (const entry of catalog.permissions) {\n if (entry.refines && entry.refines in out) broader.set(entry.key, entry.refines)\n else if (entry.follows && entry.follows in out) {\n broader.set(entry.key, entry.follows)\n followsOnly.add(entry.key)\n }\n }\n const depth = (key: string): number => {\n const seen = new Set<string>()\n let steps = 0\n let current = key\n while (broader.has(current)) {\n if (seen.has(current)) return -1\n seen.add(current)\n current = broader.get(current) as string\n steps += 1\n }\n return steps\n }\n const ordered = [...broader.keys()]\n .map((key) => ({ key, steps: depth(key) }))\n .filter((entry) => entry.steps > 0)\n .sort((a, b) => a.steps - b.steps)\n for (const { key } of ordered) {\n let stored: boolean | undefined\n for (const layer of [customRole, overrides]) {\n const value = layer?.permissions?.[key]\n if (typeof value === 'boolean') stored = value\n }\n const held = out[broader.get(key) as string] === true\n out[key] = followsOnly.has(key) ? (stored === undefined ? held : stored) : held && (stored === undefined ? true : stored)\n }\n return out\n}\n"],"names":["ORG_ROLE_NAMES","CORE_ORG_PERMISSION_GROUPS","id","label","declarationRoleDefaults","declaration","roleDefaults","owner","admin","editor","viewer","tiers","defaults","CORE_ORG_PERMISSION_ENTRIES","key","group","description","scope","CORE_KEYS","map","entry","CORE_SITE_ROLE_PRESETS","author","EDITOR_SITE_KEYS","Object","keys","filter","CORE_ORG_ROLE_GRANTS","ORG_ROLE_GRANTS","permissions","groups","grants","assembleOrgRoleGrants","core","declarationLists","fromEntries","role","taken","Set","groupIds","flat","has","add","pluginId","push","hostRoleDefaults","refines","follows","narrowsPerSite","narrowable","groupLabel","index","findIndex","rolePermissions","catalog","customRole","overrides","name","held","out","layer","values","broader","Map","followsOnly","set","depth","seen","steps","current","get","ordered","sort","a","b","stored","value","undefined"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;CAkBC,GAED,wBAAwB;AA2FxB,OAAO,MAAMA,iBAAyC;IAAC;IAAS;IAAS;IAAU;CAAS,CAAA;AAE5F;;;;CAIC,GACD,OAAO,MAAMC,6BAA4D;IACvE;QAAEC,IAAI;QAAgBC,OAAO;IAAe;IAC5C;QAAED,IAAI;QAAWC,OAAO;IAAU;IAClC;QAAED,IAAI;QAAQC,OAAO;IAAO;IAC5B;QAAED,IAAI;QAASC,OAAO;IAAQ;IAC9B;QAAED,IAAI;QAAYC,OAAO;IAAO;IAChC;QAAED,IAAI;QAAcC,OAAO;IAA0B;CACtD,CAAA;AAED;;;;CAIC,GACD,OAAO,SAASC,wBACdC,WAAwE;IAExE,IAAIA,YAAYC,YAAY,EAAE;QAC5B,OAAO;YACLC,OAAOF,YAAYC,YAAY,CAACC,KAAK,KAAK;YAC1CC,OAAOH,YAAYC,YAAY,CAACE,KAAK,KAAK;YAC1CC,QAAQJ,YAAYC,YAAY,CAACG,MAAM,KAAK;YAC5CC,QAAQL,YAAYC,YAAY,CAACI,MAAM,KAAK;QAC9C;IACF;IACA,MAAMC,QAAQN,YAAYO,QAAQ;IAClC,OAAO;QACLL,OAAOI,CAAAA,yBAAAA,MAAOH,KAAK,MAAK;QACxBA,OAAOG,CAAAA,yBAAAA,MAAOH,KAAK,MAAK;QACxBC,QAAQE,CAAAA,yBAAAA,MAAOF,MAAM,MAAK;QAC1BC,QAAQC,CAAAA,yBAAAA,MAAOD,MAAM,MAAK;IAC5B;AACF;AAEA;;;CAGC,GACD,OAAO,MAAMG,8BAA6D;IACxE;QACEC,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,yEAAyE;IACzE,yDAAyD;IACzD;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,2EAA2E;IAC3E,sEAAsE;IACtE;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,mEAAmE;IACnE,+DAA+D;IAC/D;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAA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P,QAAQQ,GAAG,CAACD;YACtBD,SAAS;QACX;QACA,OAAOA;IACT;IACA,MAAMG,UAAU;WAAIT,QAAQrC,IAAI;KAAG,CAChCN,GAAG,CAAC,CAACL,MAAS,CAAA;YAAEA;YAAKsD,OAAOF,MAAMpD;QAAK,CAAA,GACvCY,MAAM,CAAC,CAACN,QAAUA,MAAMgD,KAAK,GAAG,GAChCI,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAEL,KAAK,GAAGM,EAAEN,KAAK;IACnC,KAAK,MAAM,EAAEtD,GAAG,EAAE,IAAIyD,QAAS;QAC7B,IAAII;QACJ,KAAK,MAAMf,SAAS;YAACL;YAAYC;SAAU,CAAE;gBAC7BI;YAAd,MAAMgB,QAAQhB,0BAAAA,qBAAAA,MAAO/B,WAAW,qBAAlB+B,kBAAoB,CAAC9C,IAAI;YACvC,IAAI,OAAO8D,UAAU,WAAWD,SAASC;QAC3C;QACA,MAAMlB,OAAOC,GAAG,CAACG,QAAQQ,GAAG,CAACxD,KAAe,KAAK;QACjD6C,GAAG,CAAC7C,IAAI,GAAGkD,YAAYvB,GAAG,CAAC3B,OAAQ6D,WAAWE,YAAYnB,OAAOiB,SAAUjB,QAASiB,CAAAA,WAAWE,YAAY,OAAOF,MAAK;IACzH;IACA,OAAOhB;AACT"}
1
+ {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/app-utils/org-role-grants.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * WHAT EACH ORG ROLE MAY DO, AS DATA (AGL-243, AGL-3950).\n *\n * The core permission catalog and the core keys each org role holds when\n * nothing refines it. `org-permissions.ts` builds the console's live\n * catalog from these (adding the keys plugins declare, with their own role\n * defaults), and the native apps read the same grants through the generated\n * contracts, so a native gate (`hosts.create`, `data.manage`) answers as the\n * console does for a member row that carries no `resolvedPermissions` stamp.\n *\n * `ORG_ROLE_GRANTS` in the contracts is this table with every plugin's\n * declared catalog keys added (`assembleOrgRoleGrants`, run by\n * `generate:native-contracts` over each plugin's declarations), which is\n * exactly the set the console's `DEFAULT_ROLE_PERMISSIONS` holds once the\n * plugins have registered.\n *\n * NO IMPORTS, on purpose: the native apps read it through the generated\n * contracts.\n */\n\n/** The four org roles. */\nexport type OrgRoleName = 'owner' | 'admin' | 'editor' | 'viewer'\n\n/**\n * Where a key is decided (AGL-4047). `org`: by the member's org role, custom\n * role and overrides, the same on every site. `site`: as `org` for a member\n * who reaches the whole workspace, and per site for a site collaborator,\n * from the host role they hold there and that site's toggle.\n */\nexport type OrgPermissionScope = 'org' | 'site'\n\n/** One key of the org permission catalog. */\nexport interface OrgPermissionEntry {\n key: string\n label: string\n description: string\n /** The plugin that declared it; absent for a core key. */\n pluginId?: string\n /** The group the role editor files it under (`ORG_PERMISSION_GROUPS`, or the declaring plugin's id). */\n group?: string\n /** Where it is decided; absent reads as `org`. */\n scope?: OrgPermissionScope\n /**\n * The broader key this one is a finer part of (AGL-4047): never granted\n * without it, and following it where no custom role or override sets this\n * key. Absent for a key that refines nothing.\n */\n refines?: string\n /**\n * The key this one follows where no custom role or override sets it,\n * without needing it (AGL-4047): granted on its own, it holds while that\n * key is refused. Absent for a key that follows nothing, and for one that\n * refines.\n */\n follows?: string\n /**\n * Whether a site can narrow the key (`narrowsPerSite`, AGL-4047), so the\n * role and member editors offer its \"follow the site role\" choice. Absent\n * for a key a site cannot narrow.\n */\n narrowable?: boolean\n /**\n * For a site key, who holds it by the role they have on that site, read\n * where the site has no stamp for them (`hostStampedSitePermission`, AGL-4047).\n * Absent for a key with no such preset, and for one that holds for members of\n * the whole workspace only, which a site's roles alone cannot answer.\n */\n sitePreset?: Record<string, boolean>\n}\n\n/** A heading the role editor groups keys under. */\nexport interface OrgPermissionGroup {\n id: string\n label: string\n /** The plugin whose keys it holds; absent for a core group. */\n pluginId?: string\n}\n\n/** The catalog and each role's default grants. */\nexport interface OrgRoleGrants {\n /** Every key, in the order the role editor lists them. */\n permissions: OrgPermissionEntry[]\n /** The keys each role holds when no custom role or per-member override sets them. */\n grants: Record<OrgRoleName, string[]>\n /** The groups the keys are filed under, in display order; absent in a catalog written before them. */\n groups?: OrgPermissionGroup[]\n}\n\n/**\n * A key a plugin declares into the catalog: `PluginOrgPermissionDeclaration`\n * with org-role defaults, or a `registerPluginPermissions` key (`managePos`),\n * whose `defaults` are per role tier, the owner holding the admin tier's.\n */\nexport interface OrgPermissionDeclaration {\n key: string\n label: string\n description?: string\n roleDefaults?: Record<OrgRoleName, boolean>\n /** The tier defaults of a `registerPluginPermissions` key, used when `roleDefaults` is absent. */\n defaults?: { admin: boolean; editor: boolean; viewer: boolean }\n /** The host-role defaults of a key a site collaborator holds per site. */\n hostRoleDefaults?: Record<string, boolean>\n /** The group it is filed under; absent: a group named by the declaring plugin's id. */\n group?: string\n /** That group's heading, for a group the core does not name. */\n groupLabel?: string\n pluginId?: string\n /** The broader key it refines (AGL-4047). */\n refines?: string\n /** The key it follows without needing it (AGL-4047). */\n follows?: string\n}\n\n/** A custom role (`orgs/{orgId}/roles/{roleId}`) or a member's overrides: explicit verdicts per key. */\nexport interface OrgPermissionLayer {\n permissions?: Record<string, boolean>\n}\n\nexport const ORG_ROLE_NAMES: readonly OrgRoleName[] = ['owner', 'admin', 'editor', 'viewer']\n\n/**\n * The core groups, in display order. A plugin's keys are filed under a group\n * named by the plugin's id unless its declaration names one of these, so no\n * core group id is a plugin id.\n */\nexport const CORE_ORG_PERMISSION_GROUPS: readonly OrgPermissionGroup[] = [\n { id: 'organization', label: 'Organization' },\n { id: 'billing', label: 'Billing' },\n { id: 'team', label: 'Team' },\n { id: 'sites', label: 'Sites' },\n { id: 'datasets', label: 'Data' },\n { id: 'extensions', label: 'Marketplace and plugins' },\n]\n\n/**\n * A declaration's verdict for each org role: its `roleDefaults`, or, for a\n * `registerPluginPermissions` key, its tier `defaults` with the owner on the\n * admin tier (the tier `ORG_ROLE_TIER` resolves the owner onto).\n */\nexport function declarationRoleDefaults(\n declaration: Pick<OrgPermissionDeclaration, 'roleDefaults' | 'defaults'>,\n): Record<OrgRoleName, boolean> {\n if (declaration.roleDefaults) {\n return {\n owner: declaration.roleDefaults.owner === true,\n admin: declaration.roleDefaults.admin === true,\n editor: declaration.roleDefaults.editor === true,\n viewer: declaration.roleDefaults.viewer === true,\n }\n }\n const tiers = declaration.defaults\n return {\n owner: tiers?.admin === true,\n admin: tiers?.admin === true,\n editor: tiers?.editor === true,\n viewer: tiers?.viewer === true,\n }\n}\n\n/**\n * The core keys, in display order for role editors. Every key here is\n * enforced server-side (`apps/console/specs/org-permissions-are-enforced.spec.ts`).\n */\nexport const CORE_ORG_PERMISSION_ENTRIES: readonly OrgPermissionEntry[] = [\n {\n key: 'org.settings',\n group: 'organization',\n label: 'Organization settings',\n description: 'Rename the organization, change its slug, transfer it.',\n },\n {\n key: 'org.auditLog',\n group: 'organization',\n label: 'Activity & audit log',\n description: 'See the organization activity feed.',\n },\n {\n key: 'org.apiKeys',\n group: 'organization',\n label: 'Manage API keys',\n description: 'Create and revoke the workspace’s REST API keys.',\n },\n {\n key: 'org.privacy',\n group: 'organization',\n label: 'Privacy requests',\n description: 'Erase a person from every site in the workspace when they ask.',\n },\n {\n key: 'org.holds',\n group: 'organization',\n label: 'Holds and reviews',\n description: 'See what is held, flagged or locked on the workspace, and request a review.',\n },\n // Workspace acts that asked for the owner or admin role (AGL-4047), each\n // a key of its own whose default is exactly those roles.\n {\n key: 'org.export',\n group: 'organization',\n label: 'Export workspace data',\n description: 'Download a copy of everything the workspace holds.',\n },\n {\n key: 'org.sendingDomains',\n group: 'organization',\n label: 'Sending domains',\n description: 'Add, verify and remove the domains the workspace’s mail is sent from.',\n },\n {\n key: 'org.businessProfile',\n group: 'organization',\n label: 'Business profile',\n description: 'Change the services, audience and tone every site starts from.',\n },\n {\n key: 'billing.view',\n group: 'billing',\n label: 'View billing',\n description: 'See the plan, usage meters, and invoices.',\n },\n {\n key: 'billing.manage',\n group: 'billing',\n label: 'Manage billing',\n description: 'Change plans, buy add-ons, update payment details.',\n },\n {\n key: 'members.manage',\n group: 'team',\n label: 'Manage members',\n description: 'Invite, remove, and re-role organization members.',\n },\n {\n key: 'hosts.create',\n group: 'sites',\n label: 'Create sites',\n description: 'Create new sites in the organization.',\n },\n {\n key: 'hosts.delete',\n group: 'sites',\n label: 'Delete sites',\n description: 'Delete sites the member can access.',\n },\n // The site keys (AGL-4047): each replaced a gate on the member's role on a\n // site, and keeps that role as its preset (`CORE_SITE_ROLE_PRESETS`).\n {\n key: 'site.edit',\n group: 'sites',\n scope: 'site',\n label: 'Edit site content',\n description: 'Create and change pages, content and everything else on a site, without making it live.',\n },\n {\n key: 'site.publish',\n group: 'sites',\n scope: 'site',\n label: 'Publish site content',\n description: 'Make pages and content live, take them down or schedule them, change redirects and change the site’s fonts.',\n },\n {\n key: 'site.sensitive',\n group: 'sites',\n scope: 'site',\n label: 'See orders and webhooks',\n description: 'Read a site’s orders, returns and sending address, and set up its webhooks.',\n },\n {\n key: 'site.manage',\n group: 'sites',\n scope: 'site',\n label: 'Manage site settings',\n description: 'Manage a site’s people, domain, address and plugins, change its plugin settings (live chat, Zapier) and security allow-lists, import, export and delete it.',\n },\n {\n key: 'data.manage',\n group: 'datasets',\n label: 'Manage data',\n description: 'Create and edit organization datasets, add, change and delete their rows, and manage the CRM’s records and marketing lists and topics.',\n },\n // Workspace content that asked for the owner, admin or editor role\n // (AGL-4047), each a key whose default is exactly those roles.\n {\n key: 'org.media',\n group: 'datasets',\n label: 'Edit the workspace media library',\n description: 'Upload, change, move and remove the files in the media library every site shares.',\n },\n {\n key: 'org.eraseRecords',\n group: 'datasets',\n label: 'Delete workspace records',\n description: 'Delete a whole dataset or list the workspace shares, with everything in it. Deleting one row of a dataset asks “Manage data”.',\n },\n {\n key: 'marketplace.publish',\n group: 'extensions',\n label: 'Publish to marketplace',\n description: 'Publish listings under the organization profile.',\n },\n {\n key: 'plugins.install',\n group: 'extensions',\n label: 'Install plugins',\n description: 'Install or remove marketplace plugins.',\n },\n]\n\nconst CORE_KEYS = CORE_ORG_PERMISSION_ENTRIES.map((entry) => entry.key)\n\n/**\n * The core keys whose preset is the member's role ON THE SITE (AGL-4047), and\n * each site role's verdict: the sets the rules' `canWriteHostContent`,\n * `canPublishHostContent`, `canReadHostSensitive` and the site-admin checks\n * asked before they asked these keys. A member's preset on a site is the\n * verdict for the role they hold there (the owner and admins are Admin on\n * every site); a custom role, their own permissions and that site's toggle\n * then grant or refuse it. Each workspace role's default below is the verdict\n * for its own site role, which is the answer with no site named.\n */\nexport const CORE_SITE_ROLE_PRESETS: Readonly<Record<string, Readonly<Record<string, boolean>>>> = {\n 'site.edit': { admin: true, editor: true, author: true, viewer: false },\n 'site.publish': { admin: true, editor: true, author: false, viewer: false },\n 'site.sensitive': { admin: true, editor: true, author: false, viewer: false },\n 'site.manage': { admin: true, editor: false, author: false, viewer: false },\n}\n\n/** The workspace editor's site keys: its own site role's preset. */\nconst EDITOR_SITE_KEYS = Object.keys(CORE_SITE_ROLE_PRESETS).filter((key) => CORE_SITE_ROLE_PRESETS[key].editor)\n\n/**\n * The core keys each org role holds when nothing refines it: owners and\n * admins hold every core key (owner-only acts like deleting the\n * organization stay role-checked, not permission-checked); editors work on\n * content and see no money or roster controls; viewers read only.\n */\nexport const CORE_ORG_ROLE_GRANTS: Readonly<Record<OrgRoleName, readonly string[]>> = {\n owner: CORE_KEYS,\n admin: CORE_KEYS,\n editor: ['data.manage', 'org.media', 'org.eraseRecords', 'marketplace.publish', 'plugins.install', ...EDITOR_SITE_KEYS],\n viewer: [],\n}\n\n/** The core's catalog and grants; the contracts' value adds the plugins' keys. */\nexport const ORG_ROLE_GRANTS: OrgRoleGrants = {\n permissions: CORE_ORG_PERMISSION_ENTRIES.map((entry) => ({ ...entry })),\n groups: CORE_ORG_PERMISSION_GROUPS.map((group) => ({ ...group })),\n grants: {\n owner: [...CORE_ORG_ROLE_GRANTS.owner],\n admin: [...CORE_ORG_ROLE_GRANTS.admin],\n editor: [...CORE_ORG_ROLE_GRANTS.editor],\n viewer: [...CORE_ORG_ROLE_GRANTS.viewer],\n },\n}\n\n/**\n * The catalog with plugins' declared keys after the core ones, each role\n * holding a declared key when its declaration's defaults say so. A\n * declaration naming a key already taken is refused, as the console's\n * catalog refuses it: the first declaration keeps its meaning and defaults.\n * Every declaration list after the first is folded in the same way, so the\n * org catalog keys (`PluginOrgPermissionDeclaration`) and the\n * `registerPluginPermissions` keys land in one catalog (AGL-4047).\n */\nexport function assembleOrgRoleGrants(\n core: OrgRoleGrants,\n ...declarationLists: ReadonlyArray<readonly OrgPermissionDeclaration[]>\n): OrgRoleGrants {\n const permissions = core.permissions.map((entry) =>\n CORE_SITE_ROLE_PRESETS[entry.key] ? { ...entry, sitePreset: { ...CORE_SITE_ROLE_PRESETS[entry.key] } } : entry,\n )\n const groups = [...(core.groups ?? [])]\n const grants = Object.fromEntries(ORG_ROLE_NAMES.map((role) => [role, [...core.grants[role]]])) as Record<\n OrgRoleName,\n string[]\n >\n const taken = new Set(permissions.map((entry) => entry.key))\n const groupIds = new Set(groups.map((group) => group.id))\n for (const declaration of declarationLists.flat()) {\n if (!declaration?.key || taken.has(declaration.key)) continue\n taken.add(declaration.key)\n const group = declaration.group ?? declaration.pluginId\n permissions.push({\n key: declaration.key,\n label: declaration.label,\n description: declaration.description ?? '',\n ...(declaration.pluginId ? { pluginId: declaration.pluginId } : {}),\n ...(group ? { group } : {}),\n scope: declaration.hostRoleDefaults ? 'site' : 'org',\n ...(declaration.refines && declaration.refines !== declaration.key ? { refines: declaration.refines } : {}),\n ...(declaration.follows && declaration.follows !== declaration.key && !declaration.refines\n ? { follows: declaration.follows }\n : {}),\n // A plugin's own declaration carries `narrowsPerSite` (a flag or one per role); only whether it is set matters here.\n ...((declaration as { narrowsPerSite?: unknown }).narrowsPerSite ? { narrowable: true } : {}),\n ...(declaration.hostRoleDefaults && !(declaration as { workspaceMembersOnly?: unknown }).workspaceMembersOnly\n ? { sitePreset: { ...declaration.hostRoleDefaults } }\n : {}),\n })\n if (group && !groupIds.has(group)) {\n groupIds.add(group)\n groups.push({\n id: group,\n label: declaration.groupLabel ?? group,\n ...(declaration.pluginId ? { pluginId: declaration.pluginId } : {}),\n })\n } else if (group && declaration.pluginId) {\n // A plugin's group opened by a declaration that names no plugin (an org\n // catalog declaration, AGL-4047) is still that plugin's: the first of\n // its declarations that names one says so.\n const index = groups.findIndex((entry) => entry.id === group)\n if (index >= 0 && !groups[index].pluginId) groups[index] = { ...groups[index], pluginId: declaration.pluginId }\n }\n const defaults = declarationRoleDefaults(declaration)\n for (const role of ORG_ROLE_NAMES) {\n if (defaults[role]) grants[role].push(declaration.key)\n }\n }\n return { permissions, grants, groups }\n}\n\n/**\n * A member's effective permissions, as the console's `resolveOrgPermissions`\n * answers them: the role's defaults, then the custom role's explicit\n * verdicts (when the member has one), then the member's own overrides,\n * later layers winning key by key, over the catalog's keys only. A missing\n * role reads as viewer; a role the catalog does not know holds nothing.\n */\nexport function rolePermissions(\n catalog: OrgRoleGrants,\n role: string | null | undefined,\n customRole?: OrgPermissionLayer | null,\n overrides?: OrgPermissionLayer | null,\n): Record<string, boolean> {\n const keys = catalog.permissions.map((entry) => entry.key)\n const name = role ?? 'viewer'\n const held = new Set((catalog.grants as Record<string, string[] | undefined>)[name] ?? [])\n const out: Record<string, boolean> = Object.fromEntries(keys.map((key) => [key, held.has(key)]))\n for (const layer of [customRole, overrides]) {\n const values = layer?.permissions\n if (!values) continue\n for (const key of keys) {\n if (typeof values[key] === 'boolean') out[key] = values[key]\n }\n }\n // A key that refines a broader one (AGL-4047) is never held without it, and\n // follows it where neither layer set the key; a key that only follows one\n // takes its verdict where neither layer set the key, and nothing more.\n // Applied broader keys first, so a chain settles in one pass; a refinement\n // that loops is left alone.\n const broader = new Map<string, string>()\n const followsOnly = new Set<string>()\n for (const entry of catalog.permissions) {\n if (entry.refines && entry.refines in out) broader.set(entry.key, entry.refines)\n else if (entry.follows && entry.follows in out) {\n broader.set(entry.key, entry.follows)\n followsOnly.add(entry.key)\n }\n }\n const depth = (key: string): number => {\n const seen = new Set<string>()\n let steps = 0\n let current = key\n while (broader.has(current)) {\n if (seen.has(current)) return -1\n seen.add(current)\n current = broader.get(current) as string\n steps += 1\n }\n return steps\n }\n const ordered = [...broader.keys()]\n .map((key) => ({ key, steps: depth(key) }))\n .filter((entry) => entry.steps > 0)\n .sort((a, b) => a.steps - b.steps)\n for (const { key } of ordered) {\n let stored: boolean | undefined\n for (const layer of [customRole, overrides]) {\n const value = layer?.permissions?.[key]\n if (typeof value === 'boolean') stored = value\n }\n const held = out[broader.get(key) as string] === true\n out[key] = followsOnly.has(key) ? (stored === undefined ? held : stored) : held && (stored === undefined ? true : stored)\n }\n return out\n}\n"],"names":["ORG_ROLE_NAMES","CORE_ORG_PERMISSION_GROUPS","id","label","declarationRoleDefaults","declaration","roleDefaults","owner","admin","editor","viewer","tiers","defaults","CORE_ORG_PERMISSION_ENTRIES","key","group","description","scope","CORE_KEYS","map","entry","CORE_SITE_ROLE_PRESETS","author","EDITOR_SITE_KEYS","Object","keys","filter","CORE_ORG_ROLE_GRANTS","ORG_ROLE_GRANTS","permissions","groups","grants","assembleOrgRoleGrants","core","declarationLists","sitePreset","fromEntries","role","taken","Set","groupIds","flat","has","add","pluginId","push","hostRoleDefaults","refines","follows","narrowsPerSite","narrowable","workspaceMembersOnly","groupLabel","index","findIndex","rolePermissions","catalog","customRole","overrides","name","held","out","layer","values","broader","Map","followsOnly","set","depth","seen","steps","current","get","ordered","sort","a","b","stored","value","undefined"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;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;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,yEAAyE;IACzE,yDAAyD;IACzD;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,2EAA2E;IAC3E,sEAAsE;IACtE;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPE,OAAO;QACPd,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA,mEAAmE;IACnE,+DAA+D;IAC/D;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;IACA;QACEF,KAAK;QACLC,OAAO;QACPZ,OAAO;QACPa,aAAa;IACf;CACD,CAAA;AAED,MAAME,YAAYL,4BAA4BM,GAAG,CAAC,CAAC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K3B,GAAG,CAAC6B,UAAU,OAAO,CAAC;YAC/BF,KAAK1B,GAAG,CAAC4B;YACTA,UAAUP,QAAQQ,GAAG,CAACD;YACtBD,SAAS;QACX;QACA,OAAOA;IACT;IACA,MAAMG,UAAU;WAAIT,QAAQvC,IAAI;KAAG,CAChCN,GAAG,CAAC,CAACL,MAAS,CAAA;YAAEA;YAAKwD,OAAOF,MAAMtD;QAAK,CAAA,GACvCY,MAAM,CAAC,CAACN,QAAUA,MAAMkD,KAAK,GAAG,GAChCI,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAEL,KAAK,GAAGM,EAAEN,KAAK;IACnC,KAAK,MAAM,EAAExD,GAAG,EAAE,IAAI2D,QAAS;QAC7B,IAAII;QACJ,KAAK,MAAMf,SAAS;YAACL;YAAYC;SAAU,CAAE;gBAC7BI;YAAd,MAAMgB,QAAQhB,0BAAAA,qBAAAA,MAAOjC,WAAW,qBAAlBiC,kBAAoB,CAAChD,IAAI;YACvC,IAAI,OAAOgE,UAAU,WAAWD,SAASC;QAC3C;QACA,MAAMlB,OAAOC,GAAG,CAACG,QAAQQ,GAAG,CAAC1D,KAAe,KAAK;QACjD+C,GAAG,CAAC/C,IAAI,GAAGoD,YAAYxB,GAAG,CAAC5B,OAAQ+D,WAAWE,YAAYnB,OAAOiB,SAAUjB,QAASiB,CAAAA,WAAWE,YAAY,OAAOF,MAAK;IACzH;IACA,OAAOhB;AACT"}
@@ -252,9 +252,9 @@ export declare function scopeCovers(target: readonly string[] | undefined | null
252
252
  * Rewrites an org media CDN path so it names the site using the asset
253
253
  * (AGL-1043/1045).
254
254
  *
255
- * `/api/media/cdn/org:{orgId}/{mediaId}` serves ORG-WIDE assets only. A
256
- * restricted asset has to be requested through the form that names the
257
- * site — `org:{orgId}:{hostId}` — because the CDN is unauthenticated and
255
+ * `/api/media/cdn/org:{orgId}/{mediaId}` serves org-wide assets, and assets
256
+ * shared with all sites except some. An asset shared with chosen sites has to
257
+ * be requested through the form that names the site — `org:{orgId}:{hostId}` — because the CDN is unauthenticated and
258
258
  * the URL is the only thing it can decide from.
259
259
  *
260
260
  * Returns the path untouched when there is nothing to do: an org-wide
@@ -289,3 +289,132 @@ export declare function heldByHost(doc: Readonly<Record<string, unknown>> | null
289
289
  * every read that predates sharing keeps exactly the rows it had.
290
290
  */
291
291
  export declare function seenOnlyThroughGrant(doc: Readonly<Record<string, unknown>> | null | undefined, hostId: string): boolean;
292
+ /** The `scopeMode` a document kept up by the sweep carries. */
293
+ export declare const SCOPE_MODE_ALL_EXCEPT: "allExcept";
294
+ /**
295
+ * What a sharing choice writes. A `null` marker field means the document must
296
+ * not carry it, which a server write spells as a field delete, so a document
297
+ * moved back to All sites or to chosen sites keeps no stale marker.
298
+ */
299
+ export interface ScopeSharingFields {
300
+ visibleTo: ScopeToken[];
301
+ scopeMode: typeof SCOPE_MODE_ALL_EXCEPT | null;
302
+ scopeExcept: ScopeToken[] | null;
303
+ }
304
+ /** How a document is shared, as a person would choose it. */
305
+ export type ScopeSharing = {
306
+ mode: 'all';
307
+ } | {
308
+ mode: 'selected';
309
+ hostIds: string[];
310
+ } | {
311
+ mode: 'allExcept';
312
+ exceptHostIds: string[];
313
+ includedHostIds: string[];
314
+ } | {
315
+ mode: 'none';
316
+ };
317
+ /** What a person picked: the input to {@link sharingFromChoice}. */
318
+ export type ScopeSharingChoice = {
319
+ mode: 'all';
320
+ } | {
321
+ mode: 'selected';
322
+ hostIds: readonly string[];
323
+ } | {
324
+ mode: 'allExcept';
325
+ exceptHostIds: readonly string[];
326
+ };
327
+ /**
328
+ * How a document is shared. Reads the marker only where it agrees with what
329
+ * readers enforce: a document carrying `'org'` is shared with all sites
330
+ * whatever a marker beside it says, and a damaged marker reads as the chosen
331
+ * sites `visibleTo` names.
332
+ */
333
+ export declare function scopeSharingOf(doc: Readonly<Record<string, unknown>> | null | undefined): ScopeSharing;
334
+ /**
335
+ * What a sharing choice writes, or why it cannot be written.
336
+ *
337
+ * `orgHostIds` is the workspace's sites as the writer read them. An exclusion
338
+ * naming a site that is no longer there is dropped, not refused: the person
339
+ * asked for that site to stay out, and it is out. Excluding nothing is
340
+ * refused, since that choice is All sites; excluding everything is refused,
341
+ * since it would hide the document from every site.
342
+ *
343
+ * An all-except list is not held to `MAX_SCOPE_HOSTS`, which limits what a
344
+ * QUERY can ask for and never what a document stores. A collection whose
345
+ * stored fields multiply by site passes its own `maxIncluded`. `keepHostIds`
346
+ * are sites that stay on the list even if excluded, such as a site that
347
+ * captured the person a record is about.
348
+ */
349
+ export declare function sharingFromChoice(choice: ScopeSharingChoice, orgHostIds: readonly string[], options?: {
350
+ maxIncluded?: number | null;
351
+ keepHostIds?: readonly string[];
352
+ }): {
353
+ fields: ScopeSharingFields;
354
+ problem: null;
355
+ } | {
356
+ fields: null;
357
+ problem: string;
358
+ };
359
+ /** What keeping an all-except document up to date came to. */
360
+ export interface AllExceptReconcile {
361
+ /**
362
+ * - `unmarked`: not an all-except document; nothing to do.
363
+ * - `current`: already says what it should.
364
+ * - `changed`: write `fields`.
365
+ * - `capped`: it should reach more sites than `maxIncluded`, so it stopped
366
+ * widening. Any `fields` only take away sites that are gone.
367
+ * - `invalid`: a marker this module cannot act on, or one beside `'org'`.
368
+ * Left alone and reported, because either guess moves who can see it.
369
+ */
370
+ status: 'unmarked' | 'current' | 'changed' | 'capped' | 'invalid';
371
+ fields: ScopeSharingFields | null;
372
+ }
373
+ /**
374
+ * What an all-except document should say, given the workspace's sites today.
375
+ *
376
+ * Pure and idempotent, which is what lets every trigger and the drift report
377
+ * ask the same question: a second run over its own answer says `current`.
378
+ *
379
+ * - A new site joins `visibleTo`, unless it is excluded.
380
+ * - A site that left the workspace leaves `visibleTo` and `scopeExcept`.
381
+ * - Once no exclusion is left the document IS All sites, so it collapses to
382
+ * `['org']` and drops its marker.
383
+ * - When every remaining site is excluded `visibleTo` is empty and the marker
384
+ * stays: no site sees it until a new one joins. The data is kept, which is
385
+ * why {@link isSiteOnlyScope} answers false for a marked document.
386
+ * - A site in `sharing.added` stays, since a person shared the document with
387
+ * it. So do `keepHostIds`. Both only while the site is in the workspace.
388
+ */
389
+ export declare function reconcileAllExcept(doc: Readonly<Record<string, unknown>> | null | undefined, orgHostIds: readonly string[], options?: {
390
+ maxIncluded?: number | null;
391
+ keepHostIds?: readonly string[];
392
+ }): AllExceptReconcile;
393
+ /**
394
+ * The sentence the console shows for how a document is shared:
395
+ * {@link describeScope}, plus the all-except wording. Sites are named only
396
+ * where `hostNames` has them, so a reader who cannot see a site's name is told
397
+ * how many sites, never an id.
398
+ */
399
+ export declare function describeSharing(doc: Readonly<Record<string, unknown>> | null | undefined, hostNames?: Readonly<Record<string, string>>): string;
400
+ /**
401
+ * Whether a document belongs to this site alone: what deciding to erase it
402
+ * with the site asks, so the three copies of that question share one answer.
403
+ *
404
+ * FALSE FOR ANY MARKED DOCUMENT, and that is the reason this exists. A site
405
+ * can be the last one an all-except document still reaches, with `visibleTo`
406
+ * naming it and nothing else, and the document is still the workspace's.
407
+ * Reading that as "this site's own" would erase the data with the site.
408
+ */
409
+ export declare function isSiteOnlyScope(doc: Readonly<Record<string, unknown>> | null | undefined, hostId: string): boolean;
410
+ /**
411
+ * {@link scopeCovers} for whole documents: whether `target` stays visible
412
+ * everywhere `source` is, which a cross-dataset reference needs.
413
+ *
414
+ * Today's tokens are not enough once either side is all-except, because the
415
+ * two lists move when a site is added: a chosen-sites target cannot cover an
416
+ * all-except source however it looks today (the next new site gets the source
417
+ * and not the target), and an all-except target covers an all-except source
418
+ * only when it leaves out no site the source includes.
419
+ */
420
+ export declare function sharingCovers(target: Readonly<Record<string, unknown>> | null | undefined, source: Readonly<Record<string, unknown>> | null | undefined): boolean;