@simmalugnt-se/payload-consent 0.3.0 → 0.4.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,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ - `access.manage` says who works with cookies in Admin: the texts and services, the consent log,
6
+ policies, scans and evidence, starting scans and adding their findings (the `/start`,
7
+ `/:id/status` and `/classify` endpoints answer 403 to others), and the dashboard widget, which
8
+ others no longer see. Signed-in users by default, as before.
9
+
3
10
  ## 0.3.0
4
11
 
5
12
  Defaults and behaviour reviewed against the EDPB's guidelines and PTS's cookie decisions; the
package/README.md CHANGED
@@ -264,7 +264,18 @@ banner's own consent storage first: `c15t`, a cookie and a localStorage key, and
264
264
  `c15t-policy` with the policy version the visitor chose under, all kept for `consentMonths`.
265
265
 
266
266
  The global and the services are public to read by default (`access.read`), since the site shows
267
- them to everyone. `overrideGlobal` and `overrideServices` change them, for example to add hooks
267
+ them to everyone. `access.manage` says who works with cookies in Admin: changing the texts and
268
+ services, reading the consent log, the policies, the scans and their evidence, starting a scan and
269
+ adding its findings, and seeing the dashboard widget. It defaults to signed-in users; with roles,
270
+ pass the project's:
271
+
272
+ ```ts
273
+ consentPlugin({ access: { manage: ({ req }) => req.user?.role === "admin" } });
274
+ ```
275
+
276
+ `access.update` overrides `manage` for changing the global and the services only. The global
277
+ stays in the navigation for everyone who can read it; hide it with `overrideGlobal` and
278
+ `admin.hidden`. `overrideGlobal` and `overrideServices` change them, for example to add hooks
268
279
  that revalidate the site's cache.
269
280
 
270
281
  ## Look
@@ -0,0 +1,6 @@
1
+ import type { Access, PayloadRequest } from "payload";
2
+ import type { ConsentPluginOptions } from "./types.ts";
3
+ /** Who works with cookies in Admin (`access.manage`); signed-in users unless the host says. */
4
+ export declare function manageAccess(options: ConsentPluginOptions): Access;
5
+ /** Whether `manage` lets this request through; a query result counts as yes. */
6
+ export declare function canManage(options: ConsentPluginOptions, req: PayloadRequest): Promise<boolean>;
package/dist/access.js ADDED
@@ -0,0 +1,9 @@
1
+ const signedIn = ({ req }) => Boolean(req.user);
2
+ /** Who works with cookies in Admin (`access.manage`); signed-in users unless the host says. */
3
+ export function manageAccess(options) {
4
+ return options.access?.manage ?? signedIn;
5
+ }
6
+ /** Whether `manage` lets this request through; a query result counts as yes. */
7
+ export async function canManage(options, req) {
8
+ return Boolean(req.user && (await manageAccess(options)({ req })));
9
+ }
package/dist/evidence.js CHANGED
@@ -1,7 +1,7 @@
1
+ import { manageAccess } from "./access.js";
1
2
  import { CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, DEFAULT_KEEP_EVIDENCE, } from "./types.js";
2
3
  /** What the scanner sends: `<state>.pdf`, `<state>-2.pdf` for a later batch, `<state>.har.gz`. */
3
4
  const FILE_NAME = /^([a-z0-9-]{1,40}?)(?:-(\d+))?\.(pdf|har\.gz)$/;
4
- const signedIn = ({ req }) => Boolean(req.user);
5
5
  /**
6
6
  * WEC's evidence per scan: its PDF report and the network traffic (HAR, gzipped) for each consent
7
7
  * state. On a host without a persistent disk, such as Vercel, add this collection to the storage
@@ -20,7 +20,12 @@ export function evidenceCollection(options) {
20
20
  group: false,
21
21
  defaultColumns: ["filename", "scan", "state", "kind", "createdAt"],
22
22
  },
23
- access: { read: signedIn, create: () => false, update: () => false, delete: signedIn },
23
+ access: {
24
+ read: manageAccess(options),
25
+ create: () => false,
26
+ update: () => false,
27
+ delete: manageAccess(options),
28
+ },
24
29
  upload: { mimeTypes: ["application/pdf", "application/gzip", "application/x-gzip"] },
25
30
  timestamps: true,
26
31
  fields: [
package/dist/global.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { manageAccess } from "./access.js";
1
2
  import { defaultTexts, isFormerSuggestion, OPTIONAL_CATEGORIES, } from "./defaults.js";
2
3
  import { CONSENT_GLOBAL } from "./types.js";
3
4
  export const categoryLabels = {
@@ -54,7 +55,10 @@ export function consentGlobal(options, { usersSlug, schedule }) {
54
55
  slug: CONSENT_GLOBAL,
55
56
  label: { en: "Cookies", sv: "Cookies" },
56
57
  admin: { group: options.group ?? { en: "Settings", sv: "Inställningar" } },
57
- access: { read: () => true, ...options.access },
58
+ access: {
59
+ read: options.access?.read ?? (() => true),
60
+ update: options.access?.update ?? manageAccess(options),
61
+ },
58
62
  fields: [
59
63
  {
60
64
  type: "tabs",
package/dist/records.js CHANGED
@@ -1,7 +1,7 @@
1
+ import { manageAccess } from "./access.js";
1
2
  import { CATEGORIES } from "./defaults.js";
2
3
  import { readConsent, toConsentConfig } from "./site-config.js";
3
4
  import { CONSENT_POLICIES, CONSENT_RECORDS, DEFAULT_RETENTION_DAYS, } from "./types.js";
4
- const signedIn = ({ req }) => Boolean(req.user);
5
5
  const nobody = () => false;
6
6
  /** Checks a posted record; returns why it is refused, or the record. */
7
7
  export function parseRecord(body) {
@@ -132,7 +132,7 @@ export function recordsCollection(options) {
132
132
  sv: `Varje val en besökare har sparat, med policyn det gavs för. Identifieras bara med det slumpade ID:t i besökarens c15t-cookie; ingen IP-adress eller webbläsarinformation. Sparas i ${retention} dagar.`,
133
133
  },
134
134
  },
135
- access: { read: signedIn, create: nobody, update: nobody, delete: nobody },
135
+ access: { read: manageAccess(options), create: nobody, update: nobody, delete: nobody },
136
136
  endpoints: [recordEndpoint(options)],
137
137
  timestamps: true,
138
138
  fields: [
@@ -180,7 +180,7 @@ export function policiesCollection(options) {
180
180
  sv: "Det besökarna fick se, per policyversion och språk: kategorier, tjänster och texter. Sparas med det första valet för varje version.",
181
181
  },
182
182
  },
183
- access: { read: signedIn, create: nobody, update: nobody, delete: nobody },
183
+ access: { read: manageAccess(options), create: nobody, update: nobody, delete: nobody },
184
184
  timestamps: true,
185
185
  fields: [
186
186
  {
package/dist/scans.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { timingSafeEqual } from "node:crypto";
2
+ import { canManage, manageAccess } from "./access.js";
2
3
  import { evaluateScan, } from "./evaluate.js";
3
4
  import { evidenceEndpoint } from "./evidence.js";
4
5
  import { notifyScan } from "./notify.js";
@@ -34,7 +35,6 @@ export function scanStates(config, now = Date.now()) {
34
35
  states.push(state(`only-${category}`, [category]));
35
36
  return states;
36
37
  }
37
- const signedIn = ({ req }) => Boolean(req.user);
38
38
  function sameToken(given, expected) {
39
39
  const a = Buffer.from(given);
40
40
  const b = Buffer.from(expected);
@@ -167,6 +167,8 @@ function endpoints(options) {
167
167
  handler: async (req) => {
168
168
  if (!req.user)
169
169
  return Response.json({ error: "Sign in" }, { status: 401 });
170
+ if (!(await canManage(options, req)))
171
+ return Response.json({ error: "Not allowed" }, { status: 403 });
170
172
  const body = (await req.json?.().catch(() => null));
171
173
  const started = await startScan({ payload: req.payload, options, url: body?.url, req });
172
174
  return "error" in started
@@ -236,6 +238,8 @@ function endpoints(options) {
236
238
  handler: async (req) => {
237
239
  if (!req.user)
238
240
  return Response.json({ error: "Sign in" }, { status: 401 });
241
+ if (!(await canManage(options, req)))
242
+ return Response.json({ error: "Not allowed" }, { status: 403 });
239
243
  const scan = (await req.payload.findByID({
240
244
  collection: CONSENT_SCANS,
241
245
  id: req.routeParams?.id,
@@ -274,6 +278,8 @@ function endpoints(options) {
274
278
  handler: async (req) => {
275
279
  if (!req.user)
276
280
  return Response.json({ error: "Sign in" }, { status: 401 });
281
+ if (!(await canManage(options, req)))
282
+ return Response.json({ error: "Not allowed" }, { status: 403 });
277
283
  const body = (await req.json?.().catch(() => null));
278
284
  const kind = body?.kind;
279
285
  const name = body?.name?.trim();
@@ -362,7 +368,12 @@ export function scansCollection(options) {
362
368
  sv: "Varje skanning av den publicerade webbplatsen. Starta en under Cookies → Skanning.",
363
369
  },
364
370
  },
365
- access: { read: signedIn, create: () => false, update: () => false, delete: signedIn },
371
+ access: {
372
+ read: manageAccess(options),
373
+ create: () => false,
374
+ update: () => false,
375
+ delete: manageAccess(options),
376
+ },
366
377
  endpoints: endpoints(options),
367
378
  hooks: {
368
379
  beforeDelete: [
package/dist/services.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { manageAccess } from "./access.js";
1
2
  import { CATEGORIES, OPTIONAL_CATEGORIES } from "./defaults.js";
2
3
  import { categoryLabels } from "./global.js";
3
4
  import { INTEGRATIONS } from "./integrations.js";
@@ -59,7 +60,12 @@ export function servicesCollection(options) {
59
60
  sv: "Varje tjänst som sparar något i besökarens webbläsare eller som webbplatsen kontaktar. Bannerns kategorier och cookietabellen följer listan.",
60
61
  },
61
62
  },
62
- access: { read: () => true, ...options.access },
63
+ access: {
64
+ read: options.access?.read ?? (() => true),
65
+ create: manageAccess(options),
66
+ update: options.access?.update ?? manageAccess(options),
67
+ delete: manageAccess(options),
68
+ },
63
69
  hooks: {
64
70
  afterChange: [afterChange],
65
71
  afterDelete: [async ({ req }) => syncCategories(req)],
package/dist/types.d.ts CHANGED
@@ -11,10 +11,17 @@ export type ConsentPluginOptions = {
11
11
  collection: CollectionSlug;
12
12
  url: (doc: Record<string, unknown>, locale: string | undefined) => string;
13
13
  };
14
- /** Read access from outside Admin, such as the site's own API. Defaults to public. */
14
+ /**
15
+ * `read`: the global's and services' reads from outside Admin, such as the site's own API;
16
+ * public by default. `manage`: who works with cookies in Admin: changes the texts and services,
17
+ * reads the consent log, policies, scans and their evidence, starts scans and adds their
18
+ * findings, and sees the dashboard widget. Signed-in users by default. `update` overrides
19
+ * `manage` for changing the global and services only.
20
+ */
15
21
  access?: {
16
22
  read?: Access;
17
23
  update?: Access;
24
+ manage?: Access;
18
25
  };
19
26
  /** Change the global, for example to add hooks that revalidate the site's cache. */
20
27
  overrideGlobal?: (global: GlobalConfig) => GlobalConfig;
@@ -5,4 +5,4 @@ import "../styles/admin.css";
5
5
  * the Cookies global. A panel of our own like content health's: Payload's `Card` is a link to one
6
6
  * collection, not a status.
7
7
  */
8
- export declare function ConsentScanWidget({ req }: WidgetServerProps): Promise<import("react").JSX.Element>;
8
+ export declare function ConsentScanWidget({ req }: WidgetServerProps): Promise<import("react").JSX.Element | null>;
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Button, Pill } from "@payloadcms/ui";
3
+ import { canManage } from "../access.js";
3
4
  import { nextScan } from "../schedule.js";
4
5
  import { CONSENT_GLOBAL, CONSENT_SCANS } from "../types.js";
5
6
  import "../styles/admin.css";
@@ -41,6 +42,10 @@ const COPY = {
41
42
  * collection, not a status.
42
43
  */
43
44
  export async function ConsentScanWidget({ req }) {
45
+ // Only for those who work with cookies: the scans are theirs to read.
46
+ const options = (req.payload.config.custom?.consent ?? {});
47
+ if (!(await canManage(options, req)))
48
+ return null;
44
49
  const language = req.i18n?.language === "sv" ? "sv" : "en";
45
50
  const copy = COPY[language];
46
51
  const [{ docs }, global] = await Promise.all([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simmalugnt-se/payload-consent",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",