@7365admin1/layer-common 4.21.4-staging.344 → 4.21.4-staging.346

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.
@@ -433,7 +433,10 @@ const {
433
433
  bulkPhysicalAccessCard: _bulkPhysicalAccessCard,
434
434
  getBlockLevelUnitList: _getBlockLevelUnitList,
435
435
  } = useAccessManagement();
436
- const { getBySiteId: _getEntryPassSettings } = useSiteEntryPassSettings();
436
+ const {
437
+ getBySiteId: _getEntryPassSettings,
438
+ getEncryptedAcmUrl: _getEncryptedAcmUrl,
439
+ } = useSiteEntryPassSettings();
437
440
  const { getFileUrl } = useFile();
438
441
 
439
442
  const config = useRuntimeConfig();
@@ -588,9 +591,7 @@ watch(uploadMode, (mode) => {
588
591
 
589
592
  onMounted(async () => {
590
593
  try {
591
- const { encrypted: acmUrl } = await $fetch<{ encrypted: string }>(
592
- "/api/encrypt-acm-url"
593
- );
594
+ const acmUrl = await _getEncryptedAcmUrl(siteId);
594
595
  encryptedAcmUrl.value = acmUrl;
595
596
  const [doorLevels, liftLevels, groups] = await Promise.all([
596
597
  _getDoorAccessLevels(acmUrl),
@@ -203,6 +203,7 @@ const {
203
203
  getBlockLevelUnitList: _getBlockLevelUnitList,
204
204
  } = useAccessManagement();
205
205
  const { requiredRule } = useUtils();
206
+ const { getEncryptedAcmUrl } = useSiteEntryPassSettings();
206
207
 
207
208
  const route = useRoute();
208
209
  const siteId = computed(() => props.siteId || (route.params.site as string));
@@ -294,7 +295,7 @@ onMounted(async () => {
294
295
 
295
296
  const [blockLevelResult, acmResult] = await Promise.allSettled([
296
297
  _getBlockLevelUnitList(siteId.value),
297
- $fetch<{ encrypted: string }>("/api/encrypt-acm-url"),
298
+ getEncryptedAcmUrl(siteId.value),
298
299
  ]);
299
300
 
300
301
  // Blocks / Levels / Units
@@ -309,7 +310,7 @@ onMounted(async () => {
309
310
 
310
311
  // ACM levels (door + lift)
311
312
  if (acmResult.status === "fulfilled") {
312
- const acmUrl = acmResult.value.encrypted;
313
+ const acmUrl = acmResult.value;
313
314
  try {
314
315
  const [doorLevels, liftLevels] = await Promise.all([
315
316
  _getDoorAccessLevels(acmUrl),
@@ -403,6 +403,7 @@ const emit = defineEmits<{
403
403
  }>();
404
404
 
405
405
  const { getResidents, assignUser } = useAccessManagement();
406
+ const { getEncryptedAcmUrl } = useSiteEntryPassSettings();
406
407
 
407
408
  const historyDialog = ref(false);
408
409
  const showAssignForm = ref(false);
@@ -475,12 +476,12 @@ async function openAssignForm() {
475
476
  if (!props.unit?._id || !props.orgId || !props.siteId) return;
476
477
  peopleLoading.value = true;
477
478
  try {
478
- const [residentsRes, acmRes] = await Promise.all([
479
+ const [residentsRes, acmUrl] = await Promise.all([
479
480
  getResidents({ orgId: props.orgId, siteId: props.siteId, unitId: props.unit._id }),
480
- $fetch<{ encrypted: string }>("/api/encrypt-acm-url"),
481
+ getEncryptedAcmUrl(props.siteId),
481
482
  ]);
482
483
  peopleItems.value = residentsRes?.data ?? [];
483
- encryptedAcmUrl.value = acmRes?.encrypted ?? "";
484
+ encryptedAcmUrl.value = acmUrl ?? "";
484
485
  } catch {
485
486
  peopleItems.value = [];
486
487
  } finally {
@@ -27,9 +27,35 @@ export default function useSiteEntryPassSettings() {
27
27
  );
28
28
  }
29
29
 
30
+ /**
31
+ * The encrypted ACM URL the access-card screens hand to API-core, which
32
+ * decrypts it and sends the ACM commands to that host.
33
+ *
34
+ * Resolves the site's own "Enter entrypass URL" setting first, the way
35
+ * `VisitorForm` does for contractor passes. The access-card forms used to
36
+ * call `/api/encrypt-acm-url` with no `url`, so every site's door/lift
37
+ * levels and access groups came from the route's hardcoded default host
38
+ * whatever the site had configured. A site with no URL set, or whose
39
+ * settings cannot be read, still gets that default.
40
+ */
41
+ async function getEncryptedAcmUrl(siteId: string): Promise<string> {
42
+ let rawUrl = "";
43
+ try {
44
+ const settings: any = await getBySiteId(siteId);
45
+ rawUrl = String(settings?.data?.settings?.url ?? "").trim();
46
+ } catch {
47
+ // No entry pass settings for this site yet - the default host applies.
48
+ }
49
+ const { encrypted } = await $fetch<{ encrypted: string }>(
50
+ `/api/encrypt-acm-url${rawUrl ? `?url=${encodeURIComponent(rawUrl)}` : ""}`,
51
+ );
52
+ return encrypted;
53
+ }
54
+
30
55
  return {
31
56
  getBySiteId,
32
57
  getById,
33
58
  save,
59
+ getEncryptedAcmUrl,
34
60
  };
35
61
  }
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.21.4-staging.344",
5
+ "version": "4.21.4-staging.346",
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.",
package/plugins/API.ts CHANGED
@@ -1,4 +1,4 @@
1
- export default defineNuxtPlugin(() => {
1
+ export default defineNuxtPlugin((nuxtApp) => {
2
2
  const { cookieConfig } = useRuntimeConfig().public;
3
3
 
4
4
  const api = $fetch.create({
@@ -10,6 +10,21 @@ export default defineNuxtPlugin(() => {
10
10
  const sid = useCookie("sid", cookieConfig).value ?? "";
11
11
  options.headers.set("Authorization", sid);
12
12
  },
13
+ // "You do not have permission to ..." - the role changed since the page
14
+ // read it. Re-read it in the background (plugins/secure-member.client.ts)
15
+ // so the screen stops offering what the server refuses. The request itself
16
+ // still fails as before; nothing here retries it.
17
+ onResponseError({ response }) {
18
+ if (response?.status !== 403) return;
19
+ if ((response as any)?._data?.code !== "PERMISSION_DENIED") return;
20
+ if (!import.meta.client) return;
21
+ const refresh = (nuxtApp as any).$refreshAppRole;
22
+ if (typeof refresh === "function") {
23
+ // Outside a component the Nuxt context is gone by now; run in it so the
24
+ // refresh can read the session cookie.
25
+ void nuxtApp.runWithContext(() => refresh());
26
+ }
27
+ },
13
28
  });
14
29
 
15
30
  // Expose to useNuxtApp().$api
@@ -240,4 +240,48 @@ export default defineNuxtPlugin(() => {
240
240
  // Cosmetic only, and it never blocked navigation before either.
241
241
  }
242
242
  });
243
+
244
+ /*
245
+ * PERMISSIONS CHANGED WHILE THE PAGE IS OPEN.
246
+ *
247
+ * The role is read on page load and on a site change. If an administrator
248
+ * unticks a right afterwards, the screen still offers it and the SERVER
249
+ * refuses: 403 with `code: "PERMISSION_DENIED"`. `plugins/API.ts` calls this
250
+ * on that refusal - the role is read again in the background, the same way
251
+ * the page load reads it, so menus, tabs and buttons catch up without a
252
+ * reload. Single-flight, and at most once every few seconds, so a burst of
253
+ * refusals makes one refresh and nothing can loop. A failed read keeps the
254
+ * role on screen (see `run`).
255
+ */
256
+ let refreshing: Promise<void> | null = null;
257
+ let lastRefreshAt = 0;
258
+ const REFRESH_COOLDOWN_MS = 5000;
259
+
260
+ function refreshAppRole(): Promise<void> {
261
+ const userId = useCookie("user").value ?? "";
262
+ if (!userId) return Promise.resolve();
263
+ if (refreshing) return refreshing;
264
+ if (inFlight || Date.now() - lastRefreshAt < REFRESH_COOLDOWN_MS) {
265
+ return Promise.resolve();
266
+ }
267
+ lastRefreshAt = Date.now();
268
+ const site = idParam(router.currentRoute.value.params.site);
269
+
270
+ refreshing = (async () => {
271
+ await run(userId, site);
272
+ deniedFor = null;
273
+ await resolveDenied(userId, site);
274
+ })()
275
+ .catch((error) => console.warn("[permissions] refresh after 403 failed", error))
276
+ .finally(() => {
277
+ refreshing = null;
278
+ });
279
+ return refreshing;
280
+ }
281
+
282
+ return {
283
+ provide: {
284
+ refreshAppRole,
285
+ },
286
+ };
243
287
  });