Cookie scanner, phase 1: declarations, database lookup, review list
ipalKit({ cookieScanner }) (opt-in) adds the cookie-review collection, a
"Scan cookies" box above its list and GET/POST /api/ipal/cookie-scan. A scan
lists the cookies of every integration the project switches on (consent,
admin login, locale, stats, GA4, GTM, Meta Pixel, Clarity, LinkedIn, TikTok,
Turnstile), the project's own declarations, and extra names looked up in the
bundled Open Cookie Database, as drafts. Editors accept (→ Cookie Settings →
Cookies, every locale, only empty text filled), edit or reject. A rescan never
deletes, never changes status and never overwrites edited text.
Everything is overridable (access, collectionOverrides, integrations,
declarations, databaseCategories, labels, syncAccepted) and the building
blocks are exported. Admin labels in en + pl. Docs: cookie-scanner.md,
consent.md, AGENT.md, ipal-reference, ipal-consent-check.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ab45cf48be
commit
a0545b6716
@@ -0,0 +1,178 @@
|
||||
import type { CollectionAfterChangeHook, CollectionBeforeChangeHook, CollectionConfig } from 'payload'
|
||||
|
||||
import type { CookieReviewValues } from './merge.js'
|
||||
import type { CookieScannerOptions } from './types.js'
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { CONSENT_CATEGORIES } from '../categories.js'
|
||||
import { type CookieScanLabels, labelFor, labelRecord, mergeCookieScanLabels } from './labels.js'
|
||||
import { addToCookieList } from './sync.js'
|
||||
|
||||
/**
|
||||
* Cookie review list — drafts from "Scan cookies" that an editor accepts,
|
||||
* edits or rejects. Accepting adds the cookie to Cookie Settings → Cookies
|
||||
* (the banner's detailed window and <CookieTable>). Opt-in:
|
||||
* `ipalKit({ cookieScanner: true })`.
|
||||
*/
|
||||
export const COOKIE_REVIEW_SLUG = 'cookie-review'
|
||||
|
||||
const setKey: CollectionBeforeChangeHook = ({ data, originalDoc }) => {
|
||||
const name = (data.name ?? originalDoc?.name)?.trim()
|
||||
const storage = data.storage ?? originalDoc?.storage ?? 'cookie'
|
||||
if (name) {
|
||||
data.name = name
|
||||
data.key = `${storage}:${name}`
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
function buildSyncHook(): CollectionAfterChangeHook {
|
||||
return async ({ context, doc, previousDoc, req }) => {
|
||||
if (context?.ipalCookieScan || doc.status !== 'accepted' || previousDoc?.status === 'accepted') {return doc}
|
||||
// Local API calls with `req` rewrite req.locale / req.fallbackLocale; the editor's own
|
||||
// save is still being read after this hook, so put them back.
|
||||
const { fallbackLocale, locale } = req
|
||||
try {
|
||||
const full = (await req.payload.findByID({
|
||||
id: doc.id,
|
||||
collection: COOKIE_REVIEW_SLUG as never,
|
||||
depth: 0,
|
||||
locale: (req.payload.config.localization ? 'all' : undefined) as never,
|
||||
overrideAccess: true,
|
||||
req,
|
||||
})) as unknown as { name: string } & CookieReviewValues
|
||||
const result = await addToCookieList({ entry: full, payload: req.payload, req })
|
||||
req.payload.logger.info(`[ipal] cookie review: "${full.name}" accepted → Cookie Settings (${result})`)
|
||||
if (!doc.listed) {
|
||||
await req.payload.update({
|
||||
id: doc.id,
|
||||
collection: COOKIE_REVIEW_SLUG as never,
|
||||
context: { ipalCookieScan: true },
|
||||
data: { listed: true } as never,
|
||||
overrideAccess: true,
|
||||
req,
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
req.locale = locale
|
||||
req.fallbackLocale = fallbackLocale
|
||||
}
|
||||
return { ...doc, listed: true }
|
||||
}
|
||||
}
|
||||
|
||||
export function buildCookieReviewCollection(options: CookieScannerOptions = {}): CollectionConfig {
|
||||
const labels = mergeCookieScanLabels(options.labels)
|
||||
const L = (key: keyof CookieScanLabels) => labelRecord(labels, key)
|
||||
return {
|
||||
slug: COOKIE_REVIEW_SLUG,
|
||||
access: {
|
||||
// Written by editors (review) and by the scan (Local API); deleting is admin-only and never automatic.
|
||||
create: ({ req }) => userMeetsRole(req, 'editor'),
|
||||
delete: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
read: ({ req }) => userMeetsRole(req, 'editor'),
|
||||
update: ({ req }) => userMeetsRole(req, 'editor'),
|
||||
},
|
||||
admin: {
|
||||
components: {
|
||||
beforeListTable: [{ clientProps: { labels }, path: '@intecion/ipal-kit/admin#CookieScanButton' }],
|
||||
},
|
||||
defaultColumns: ['name', 'vendor', 'category', 'status', 'source', 'listed'],
|
||||
description: L('collectionDescription'),
|
||||
group: 'Settings',
|
||||
listSearchableFields: ['name', 'vendor'],
|
||||
useAsTitle: 'name',
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
type: 'row',
|
||||
fields: [
|
||||
{ name: 'name', type: 'text', admin: { width: '50%' }, label: L('name'), required: true },
|
||||
{
|
||||
name: 'storage',
|
||||
type: 'select',
|
||||
admin: { width: '25%' },
|
||||
defaultValue: 'cookie',
|
||||
label: L('storage'),
|
||||
options: [
|
||||
{ label: L('cookie'), value: 'cookie' },
|
||||
{ label: L('localStorage'), value: 'localStorage' },
|
||||
{ label: L('sessionStorage'), value: 'sessionStorage' },
|
||||
],
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'category',
|
||||
type: 'select',
|
||||
admin: { width: '25%' },
|
||||
label: L('category'),
|
||||
options: CONSENT_CATEGORIES.map((c) => ({ label: L(c), value: c })),
|
||||
validate: (value: unknown, { req, siblingData }: { req: { i18n?: { language?: string } }; siblingData: { status?: string } }) =>
|
||||
siblingData?.status === 'accepted' && !value ? labelFor(labels, req.i18n?.language, 'categoryRequired') : true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'row',
|
||||
fields: [
|
||||
{ name: 'vendor', type: 'text', admin: { width: '50%' }, label: L('vendor') },
|
||||
{ name: 'domain', type: 'text', admin: { width: '50%' }, label: L('domain') },
|
||||
],
|
||||
},
|
||||
{ name: 'purpose', type: 'textarea', label: L('purpose'), localized: true },
|
||||
{ name: 'retention', type: 'text', label: L('retention'), localized: true },
|
||||
{
|
||||
name: 'setBeforeConsent',
|
||||
type: 'checkbox',
|
||||
admin: { description: L('setBeforeConsentDescription') },
|
||||
label: L('setBeforeConsent'),
|
||||
},
|
||||
{
|
||||
name: 'status',
|
||||
type: 'select',
|
||||
admin: { description: L('statusDescription'), position: 'sidebar' },
|
||||
defaultValue: 'draft',
|
||||
index: true,
|
||||
label: L('status'),
|
||||
options: [
|
||||
{ label: L('draft'), value: 'draft' },
|
||||
{ label: L('accepted'), value: 'accepted' },
|
||||
{ label: L('rejected'), value: 'rejected' },
|
||||
],
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'source',
|
||||
type: 'select',
|
||||
admin: { position: 'sidebar', readOnly: true },
|
||||
defaultValue: 'manual',
|
||||
label: L('source'),
|
||||
options: [
|
||||
{ label: L('integration'), value: 'integration' },
|
||||
{ label: L('project'), value: 'project' },
|
||||
{ label: L('database'), value: 'database' },
|
||||
{ label: L('unknown'), value: 'unknown' },
|
||||
{ label: L('manual'), value: 'manual' },
|
||||
],
|
||||
},
|
||||
{ name: 'sourceDetail', type: 'text', admin: { position: 'sidebar', readOnly: true }, label: L('sourceDetail') },
|
||||
{
|
||||
name: 'listed',
|
||||
type: 'checkbox',
|
||||
admin: { description: L('listedDescription'), position: 'sidebar', readOnly: true },
|
||||
label: L('listed'),
|
||||
},
|
||||
{ name: 'lastScannedAt', type: 'date', admin: { position: 'sidebar', readOnly: true }, label: L('lastScannedAt') },
|
||||
// `storage:name`, one entry per cookie — what a rescan matches on.
|
||||
{ name: 'key', type: 'text', admin: { hidden: true }, index: true, unique: true },
|
||||
// The last scan's suggestion: a value still equal to it was not edited and may follow the next scan.
|
||||
{ name: 'suggested', type: 'json', admin: { hidden: true } },
|
||||
],
|
||||
hooks: {
|
||||
afterChange: options.syncAccepted === false ? [] : [buildSyncHook()],
|
||||
beforeChange: [setKey],
|
||||
},
|
||||
labels: { plural: L('plural'), singular: L('singular') },
|
||||
timestamps: true,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user