After "Scan cookies" or a site check, the new Approve cookies view (/admin/cookie-approval) lists what waits, grouped by consent category, with provider, purpose and duration and New / Changed / Needs description markers. One "Approve and add" puts every ticked cookie into Cookie banner -> Cookie list in every locale. That list feeds the cookie policy table and the detailed settings window. - Never overwrites editor text or categories, never adds a second row (a pattern and a concrete name are the same cookie), never deletes. - Unknown cookies are approved into a category that needs consent (approval.unknownCategory, default marketing) and flagged Needs description; writing a purpose clears the flag. - Cookies a scan or crawl no longer finds are flagged No longer found, for review only. - The dashboard and the crawl e-mail say how many cookies wait and link to the view; nothing is added without approval. - Optional: cookieScanner.approval false keeps per-entry review. - Migration: cookie-review gains change and needsDescription. Co-Authored-By: Claude Opus 5.5 <[email protected]>
219 lines
8.6 KiB
TypeScript
219 lines
8.6 KiB
TypeScript
import type { CollectionAfterChangeHook, CollectionBeforeChangeHook, CollectionConfig, Field } 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}`
|
|
}
|
|
// An editor who writes the purpose has described the cookie.
|
|
if (typeof data.purpose === 'string' && data.purpose.trim() && (data.needsDescription ?? originalDoc?.needsDescription)) {
|
|
data.needsDescription = false
|
|
}
|
|
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)
|
|
const crawl = Boolean(options.crawl)
|
|
// Only with `crawl`: what the latest browser crawl saw before consent (reset on every crawl).
|
|
const crawlFields: Field[] = crawl
|
|
? [
|
|
{
|
|
name: 'violation',
|
|
type: 'checkbox',
|
|
admin: { description: L('violationDescription'), position: 'sidebar', readOnly: true },
|
|
defaultValue: false,
|
|
index: true,
|
|
label: L('violation'),
|
|
},
|
|
{ name: 'violationDetail', type: 'text', admin: { position: 'sidebar', readOnly: true }, label: L('violationDetail') },
|
|
{ name: 'seenBeforeConsent', type: 'checkbox', admin: { position: 'sidebar', readOnly: true }, defaultValue: false, index: true, label: L('seenBeforeConsent') },
|
|
]
|
|
: []
|
|
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: { approval: options.approval !== false, crawl, labels }, path: '@intecion/ipal-kit/admin#CookieScanButton' }],
|
|
},
|
|
defaultColumns: ['name', 'vendor', 'category', 'status', 'change', 'listed', ...(crawl ? ['violation'] : [])],
|
|
description: L('collectionDescription'),
|
|
group: { en: 'Privacy', pl: 'Prywatność' },
|
|
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: 'change',
|
|
type: 'select',
|
|
admin: { description: L('changeDescription'), position: 'sidebar', readOnly: true },
|
|
index: true,
|
|
label: L('change'),
|
|
options: [
|
|
{ label: L('changeNew'), value: 'new' },
|
|
{ label: L('changeChanged'), value: 'changed' },
|
|
{ label: L('changeGone'), value: 'gone' },
|
|
],
|
|
},
|
|
{
|
|
name: 'needsDescription',
|
|
type: 'checkbox',
|
|
admin: { description: L('needsDescriptionDescription'), position: 'sidebar' },
|
|
defaultValue: false,
|
|
label: L('needsDescription'),
|
|
},
|
|
{ name: 'lastScannedAt', type: 'date', admin: { position: 'sidebar', readOnly: true }, label: L('lastScannedAt') },
|
|
...crawlFields,
|
|
// `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,
|
|
}
|
|
}
|