@7365admin1/layer-common 4.12.3 → 4.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 4.13.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ec3c570: Invite Client: for a new client, Seven365 now sets the owner's role permissions per application next to the modules (all permissions of the ticked modules ticked by default, only those offered) and sends them as `permissions`. Role editors (`RolePermissionMain`) offer only what that owner role allows for its application; new `ownerGrant` / `catalogueForOwnerGrant` in `utils/org-modules`. The form's site value is now actually sent.
8
+
3
9
  ## 4.12.3
4
10
 
5
11
  ### Patch Changes
@@ -1101,11 +1107,11 @@
1101
1107
  `utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
1102
1108
  own rule from two endpoints the console already calls, unprojected:
1103
1109
 
1104
- GET /api/members/user/:user/app/admin the Seven365 staff membership
1105
- GET /api/roles/id/:role that membership's role document
1110
+ GET /api/members/user/:user/app/admin the Seven365 staff membership
1111
+ GET /api/roles/id/:role that membership's role document
1106
1112
 
1107
- owner = member.type === "admin" && role.type === "admin" && role.default === true
1108
- staff = member.type === "admin" && role.type === "admin"
1113
+ owner = member.type === "admin" && role.type === "admin" && role.default === true
1114
+ staff = member.type === "admin" && role.type === "admin"
1109
1115
 
1110
1116
  `role.default` is the marker because it is the only property of a platform
1111
1117
  staff role no API caller can set - `role.controller.ts` validates create and
@@ -78,9 +78,8 @@
78
78
  <div v-for="app in invite.app" :key="`modules-${app}`" class="mb-4">
79
79
  <InputLabel :title="`Modules for ${appTitle(app)}`" required />
80
80
  <p class="text-caption text-medium-emphasis mb-2">
81
- The client's owner gets full access to the modules ticked here,
82
- and nothing else. You can change this later on the client's
83
- details.
81
+ Only the modules ticked here are available to this client. You
82
+ can change this later on the client's details.
84
83
  </p>
85
84
  <div v-for="group in inviteModuleGroups(app)" :key="`${app}-${group.key}`" class="mb-2">
86
85
  <div class="text-subtitle-2">{{ group.label }}</div>
@@ -107,6 +106,49 @@
107
106
  </v-col>
108
107
  </v-row>
109
108
  </div>
109
+
110
+ <!--
111
+ THE OWNER ROLE (owner, 2026-09-15): Seven365 chooses the
112
+ client owner's permissions with the modules. Only permissions
113
+ of ticked modules are offered, all ticked to start.
114
+ -->
115
+ <div class="mt-4" data-test="invite-owner-role">
116
+ <InputLabel :title="`Owner role for ${appTitle(app)}`" required />
117
+ <p class="text-caption text-medium-emphasis mb-2">
118
+ The client's owner gets exactly the permissions ticked here in
119
+ {{ appTitle(app) }}. They can only give their own staff roles
120
+ within these. Unticking a module above removes its permissions.
121
+ </p>
122
+ <v-expansion-panels multiple variant="accordion">
123
+ <v-expansion-panel
124
+ v-for="(actions, resource) in roleCatalogue(app)"
125
+ :key="`${app}-role-${resource}`"
126
+ >
127
+ <v-expansion-panel-title>
128
+ <span class="text-body-2">{{ formatResourceLabel(String(resource)) }}</span>
129
+ <v-spacer />
130
+ <span class="text-caption text-medium-emphasis mr-2">
131
+ {{ heldCount(app, String(resource)) }} / {{ Object.keys(actions || {}).length }}
132
+ </span>
133
+ </v-expansion-panel-title>
134
+ <v-expansion-panel-text>
135
+ <v-checkbox
136
+ v-for="(meta, action) in actions"
137
+ :key="`${app}-${resource}-${action}`"
138
+ :model-value="isHeld(app, `${resource}:${action}`)"
139
+ :label="meta?.description || formatResourceLabel(String(action))"
140
+ density="compact"
141
+ hide-details
142
+ color="primary"
143
+ @update:model-value="setHeld(app, `${resource}:${action}`, !!$event)"
144
+ />
145
+ </v-expansion-panel-text>
146
+ </v-expansion-panel>
147
+ </v-expansion-panels>
148
+ <div v-if="!ownerPermissions(app).length" class="text-caption text-error mt-2">
149
+ Tick at least one permission for the owner.
150
+ </div>
151
+ </div>
110
152
  </div>
111
153
  </v-col>
112
154
 
@@ -168,7 +210,7 @@
168
210
  block
169
211
  variant="flat"
170
212
  class="text-none screen-btn-primary"
171
- :disabled="!validForm"
213
+ :disabled="!validForm || !ownerRoleComplete"
172
214
  :loading="loading.submittingForm"
173
215
  @click="submit"
174
216
  >
@@ -189,7 +231,7 @@ import useUser from '../composables/useUser'
189
231
  import useUtils from '../composables/useUtils'
190
232
  import useOrg from '../composables/useOrg'
191
233
  import useMember from '../composables/useMember'
192
- import { inviteModuleGroups } from '../utils/client-invite-modules'
234
+ import { catalogueStrings, inviteModuleGroups, inviteRoleCatalogue } from '../utils/client-invite-modules'
193
235
  import { ALWAYS_ALLOWED_MODULES } from '../utils/org-modules'
194
236
  import { formatResourceLabel } from '../utils/permission-labels'
195
237
 
@@ -369,6 +411,61 @@ function syncModules(chosen: string[]) {
369
411
 
370
412
  syncModules(invite.value.app)
371
413
 
414
+ /* ================= NEW CLIENT: OWNER ROLE ================= */
415
+ /*
416
+ * What Seven365 UNTICKED, per application. The owner's permissions are every
417
+ * permission the ticked modules offer minus these, so ticking a module back on
418
+ * brings its permissions back ticked, and unticking one drops them.
419
+ */
420
+ const uncheckedByApp = ref<Record<string, string[]>>({})
421
+
422
+ // Built once per change of the chosen apps or modules, not once per checkbox.
423
+ const roleCatalogues = computed(() =>
424
+ Object.fromEntries(
425
+ invite.value.app.map(app => [
426
+ app,
427
+ inviteRoleCatalogue(app, [...ALWAYS_ALLOWED_MODULES, ...(modulesByApp.value[app] ?? [])]),
428
+ ])
429
+ )
430
+ )
431
+
432
+ function roleCatalogue(app: string) {
433
+ return roleCatalogues.value[app] ?? {}
434
+ }
435
+
436
+ const ownerPermissionsByApp = computed(() =>
437
+ Object.fromEntries(
438
+ invite.value.app.map(app => {
439
+ const unchecked = uncheckedByApp.value[app] ?? []
440
+ return [app, catalogueStrings(roleCatalogue(app)).filter(p => !unchecked.includes(p))]
441
+ })
442
+ )
443
+ )
444
+
445
+ function ownerPermissions(app: string): string[] {
446
+ return ownerPermissionsByApp.value[app] ?? []
447
+ }
448
+
449
+ function isHeld(app: string, permission: string) {
450
+ return !(uncheckedByApp.value[app] ?? []).includes(permission)
451
+ }
452
+
453
+ function heldCount(app: string, resource: string) {
454
+ return ownerPermissions(app).filter(p => p.startsWith(`${resource}:`)).length
455
+ }
456
+
457
+ function setHeld(app: string, permission: string, held: boolean) {
458
+ const unchecked = new Set(uncheckedByApp.value[app] ?? [])
459
+ if (held) unchecked.delete(permission)
460
+ else unchecked.add(permission)
461
+ uncheckedByApp.value = { ...uncheckedByApp.value, [app]: [...unchecked] }
462
+ }
463
+
464
+ // An empty list means EVERYTHING to the permission model, so none ticked cannot be sent.
465
+ const ownerRoleComplete = computed(() =>
466
+ !newClient.value || invite.value.app.every(app => ownerPermissions(app).length > 0)
467
+ )
468
+
372
469
  /* ================= EVENTS ================= */
373
470
  async function handleUpdateApp(value: string[]) {
374
471
  invite.value.role = ""
@@ -464,12 +561,14 @@ async function submit() {
464
561
  ...(modulesByApp.value[app] ?? inviteModuleGroups(app).flatMap(group => group.modules)),
465
562
  ]),
466
563
  ],
564
+ permissions: ownerPermissions(app),
467
565
  }
468
566
  : { role: invite.value.role }),
469
567
 
470
568
  org: org.value?._id,
471
569
 
472
- siteId: invite.value.site,
570
+ // `inviteUserClient` reads `site`; `siteId` was silently dropped.
571
+ site: invite.value.site,
473
572
  siteName: invite.value.siteName,
474
573
  })
475
574
  }
@@ -205,7 +205,9 @@ import { showSiteField } from "../utils/role";
205
205
  import { holdsPermission } from "../utils/permission-spellings";
206
206
  import {
207
207
  catalogueForOrg,
208
+ catalogueForOwnerGrant,
208
209
  effectiveModules,
210
+ ownerGrant,
209
211
  siteModulesForOrg,
210
212
  } from "../utils/org-modules";
211
213
 
@@ -360,15 +362,41 @@ const { data: siteForModules } = useLazyAsyncData(
360
362
  { watch: [() => props.siteId] }
361
363
  );
362
364
 
365
+ /*
366
+ * THE OWNER ROLE SEVEN365 CHOSE (owner, 2026-09-15). A client invited with a
367
+ * chosen owner role can only give its own application's roles within it - the
368
+ * server refuses anything wider (core `boundRoleToOwnerGrant`), so the editor
369
+ * offers only that. Fetched only when the organisation records such a role and
370
+ * this editor edits roles of that application; fails open like the lists above.
371
+ */
372
+ const grantRoleId = computed(() => {
373
+ const org = orgForModules.value as any;
374
+ return org?.invitedRole && org?.nature === props.type ? String(org.invitedRole) : "";
375
+ });
376
+
377
+ const { getRoleById: _getGrantRole } = useRole();
378
+
379
+ const { data: grantRole } = useLazyAsyncData(
380
+ `owner-grant-${props.orgId}-${props.type}`,
381
+ () =>
382
+ grantRoleId.value
383
+ ? _getGrantRole(grantRoleId.value).catch(() => null)
384
+ : Promise.resolve(null),
385
+ { watch: [grantRoleId] }
386
+ );
387
+
363
388
  /** `props.permissions`, narrowed to what this client - and this site - was given. */
364
389
  const offeredPermissions = computed(() =>
365
- catalogueForOrg(
366
- props.permissions ?? {},
367
- effectiveModules(
368
- (orgForModules.value as any)?.modules,
369
- // Only the site's OWN organisation is narrowed by it (owner Q1).
370
- siteModulesForOrg(siteForModules.value as any, props.orgId)
371
- )
390
+ catalogueForOwnerGrant(
391
+ catalogueForOrg(
392
+ props.permissions ?? {},
393
+ effectiveModules(
394
+ (orgForModules.value as any)?.modules,
395
+ // Only the site's OWN organisation is narrowed by it (owner Q1).
396
+ siteModulesForOrg(siteForModules.value as any, props.orgId)
397
+ )
398
+ ),
399
+ ownerGrant(orgForModules.value as any, grantRole.value as any, props.type)
372
400
  )
373
401
  );
374
402
 
@@ -22,7 +22,8 @@ export default function useUser() {
22
22
  org = '',
23
23
  site = '',
24
24
  siteName = '',
25
- modules
25
+ modules,
26
+ permissions
26
27
  }: {
27
28
  email?: string
28
29
  app?: string[]
@@ -33,6 +34,8 @@ export default function useUser() {
33
34
  siteName?: string
34
35
  /** A new client's modules (Seven365 staff only). */
35
36
  modules?: string[]
37
+ /** That client owner's permissions in the application (with modules only). */
38
+ permissions?: string[]
36
39
  } = {}) {
37
40
  return useNuxtApp().$api<Record<string, any>>('/api/auth/invite/user', {
38
41
  method: 'POST',
@@ -44,7 +47,8 @@ export default function useUser() {
44
47
  org,
45
48
  ...(site && { siteId: site }),
46
49
  ...(siteName && { siteName }),
47
- ...(modules && { modules })
50
+ ...(modules && { modules }),
51
+ ...(permissions && { permissions })
48
52
  }
49
53
  })
50
54
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.12.3",
5
+ "version": "4.13.0",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
@@ -36,7 +36,7 @@
36
36
  "build": "nuxt build .playground",
37
37
  "generate": "nuxt generate .playground",
38
38
  "preview": "nuxt preview .playground",
39
- "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
39
+ "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
40
40
  "test:units": "node --experimental-strip-types --test \"utils/*.test.ts\"",
41
41
  "release": "yarn run build && changeset publish"
42
42
  },
@@ -1,5 +1,6 @@
1
1
  import { PERMISSIONS } from "../constants/permissions";
2
- import { applicationModules, groupModulesByApplication } from "./module-applications";
2
+ import { APPLICATIONS, applicationModules, groupModulesByApplication } from "./module-applications";
3
+ import { catalogueForOrg } from "./org-modules";
3
4
 
4
5
  /**
5
6
  * THE MODULES A NEW CLIENT IS OFFERED ON "INVITE CLIENT".
@@ -54,3 +55,43 @@ export function inviteModuleGroups(nature: string): ModuleGroup[] {
54
55
  ? [...shown, { key: "other", label: "Not tied to a service application", modules: ungrouped }]
55
56
  : shown;
56
57
  }
58
+
59
+ /** `{ resource: { action: { description } } }` - every `use*Permission` shape. */
60
+ export type RoleCatalogue = Record<string, Record<string, { description?: string }>>;
61
+
62
+ /**
63
+ * THE CLIENT OWNER'S ROLE, CHOSEN WITH THE MODULES (owner, 2026-09-15).
64
+ *
65
+ * *"There is no option for Seven365 to choose the modules, applications and
66
+ * roles that they will access."* The permissions offered are the application's
67
+ * own role catalogue - the one its role editor uses - narrowed to the ticked
68
+ * modules exactly as the client's own editor narrows it (`catalogueForOrg`), so
69
+ * the owner can never be given, or later give, a permission of a module the
70
+ * client does not have. The server re-checks the same rule.
71
+ */
72
+ export function inviteRoleCatalogue(nature: string, modules: readonly string[]): RoleCatalogue {
73
+ const applications = Object.hasOwn(NATURE_APPLICATIONS, nature) ? NATURE_APPLICATIONS[nature] : null;
74
+ const merged: RoleCatalogue = {};
75
+
76
+ for (const application of APPLICATIONS) {
77
+ if (applications && !applications.includes(application.key)) continue;
78
+ let permissions: RoleCatalogue = {};
79
+ try {
80
+ permissions = ((application.composable as () => { permissions?: RoleCatalogue })().permissions ?? {}) as RoleCatalogue;
81
+ } catch {
82
+ continue;
83
+ }
84
+ for (const [resource, actions] of Object.entries(permissions)) {
85
+ merged[resource] = { ...merged[resource], ...actions };
86
+ }
87
+ }
88
+
89
+ return catalogueForOrg(merged, modules);
90
+ }
91
+
92
+ /** Every `resource:action` string a catalogue offers. */
93
+ export function catalogueStrings(catalogue: RoleCatalogue): string[] {
94
+ return Object.entries(catalogue).flatMap(([resource, actions]) =>
95
+ Object.keys(actions ?? {}).map((action) => `${resource}:${action}`),
96
+ );
97
+ }
@@ -216,3 +216,59 @@ export function siteModulesForOrg(
216
216
  function isKey(value: unknown): value is string {
217
217
  return typeof value === "string" && value.trim().length > 0;
218
218
  }
219
+
220
+ /**
221
+ * TIER ONE-AND-A-HALF: THE PERMISSIONS SEVEN365 CHOSE FOR THE CLIENT'S OWNER.
222
+ *
223
+ * Owner, 2026-09-15: Seven365 chooses the modules AND the owner's role on Invite
224
+ * Client, and the client gives staff only within that. The client half of core
225
+ * `module-ceiling.util ownerGrantCeiling` / `org-modules.util boundToOwnerGrant`,
226
+ * same shape test: the organisation's `invitedRole` is its OWN default role of
227
+ * its own nature holding an enumerated list, and only roles of that nature are
228
+ * capped. Anything else - every client invited before this - returns `null`.
229
+ */
230
+ export function ownerGrant(
231
+ org: { _id?: unknown; nature?: unknown; invitedRole?: unknown } | null | undefined,
232
+ grantRole: { _id?: unknown; org?: unknown; type?: unknown; default?: unknown; permissions?: unknown } | null | undefined,
233
+ type: string | null | undefined,
234
+ ): string[] | null {
235
+ if (!org?.invitedRole || !grantRole || !type || org.nature !== type) return null;
236
+ const permissions = grantRole.permissions;
237
+ const chosen =
238
+ String(grantRole._id ?? "") === String(org.invitedRole) &&
239
+ String(grantRole.org ?? "") === String(org._id ?? "") &&
240
+ grantRole.default === true &&
241
+ grantRole.type === org.nature &&
242
+ Array.isArray(permissions) &&
243
+ permissions.length > 0 &&
244
+ !permissions.includes("*");
245
+ return chosen ? (permissions as unknown[]).map(String) : null;
246
+ }
247
+
248
+ /**
249
+ * Narrow a `{ resource: { action } }` catalogue to that grant. Running the
250
+ * organisation (`ALWAYS_ALLOWED_MODULES`) is never narrowed, matching the
251
+ * server. No grant: the catalogue is returned unchanged.
252
+ */
253
+ export function catalogueForOwnerGrant<T extends Record<string, unknown>>(
254
+ catalogue: T,
255
+ grant: readonly string[] | null | undefined,
256
+ ): T {
257
+ if (!grant || !catalogue || typeof catalogue !== "object") return catalogue;
258
+
259
+ const narrowed: Record<string, unknown> = {};
260
+ for (const [resource, actions] of Object.entries(catalogue)) {
261
+ if (ALWAYS_ALLOWED_MODULES.includes(resource)) {
262
+ narrowed[resource] = actions;
263
+ continue;
264
+ }
265
+ if (!actions || typeof actions !== "object") continue;
266
+ const kept = Object.fromEntries(
267
+ Object.entries(actions as Record<string, unknown>).filter(([action]) =>
268
+ grant.includes(`${resource}:${action}`),
269
+ ),
270
+ );
271
+ if (Object.keys(kept).length) narrowed[resource] = kept;
272
+ }
273
+ return narrowed as T;
274
+ }