Gap fixes: top-level searchSettings, public shouldLoadGoogle, consent/stats collection overrides

- ipalKit({ searchSettings }): switches off / reshapes Site Settings → Search & AI
  without `seo`; read as options.searchSettings ?? options.seo?.searchSettings.
- shouldLoadGoogle moved to a pure module (analytics/shouldLoadGoogle.ts) and
  exported from @intecion/ipal-kit and /client.
- consentLog.collectionOverrides and stats.collectionOverrides (object merges
  access/admin/hooks key by key, function takes over), via the new exported
  applyCollectionOverride / CollectionOverride. The stats view and dashboard
  chart now follow the stats-visits read access, so overrides narrow them too.

All additive; defaults unchanged. Docs: AGENT.md, ipal-reference, consent,
stats, seo, analytics, ipal-setup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 18:54:08 +02:00
co-authored by Claude Opus 5.5
parent 4911da7112
commit 5a07c42f76
22 changed files with 235 additions and 54 deletions
+4 -4
View File
@@ -5,8 +5,8 @@ import { Gutter } from '@payloadcms/ui'
import type { StatsVisit } from '../collection.js'
import { userMeetsRole } from '../../access/predicates.js'
import { VISITOR_ID_PATTERN } from '../parse.js'
import { canReadStats } from './canReadStats.js'
import { loadRecentVisitors, loadStats, loadVisitor } from './loadStats.js'
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
@@ -77,7 +77,7 @@ function VisitorHistory({ base, timeZone, visitorId, visits }: { base: string; t
* Admin view at /admin/stats (ipalKit({ stats: true })): visits per day for the
* last 30 days, top pages, entry pages, referrers, devices, and — for visitors
* who accepted analytics cookies — a per-visitor history (?visitor=<id>).
* Admins only.
* For users the stats-visits collection lets read (admins by default).
*/
export async function StatsView(props: Props) {
const { adminRoute = '/admin', initPageResult, searchParams, timeZone = 'Europe/Warsaw' } = props
@@ -88,8 +88,8 @@ export async function StatsView(props: Props) {
const visitorId = visitorParam && VISITOR_ID_PATTERN.test(visitorParam) ? visitorParam : undefined
let content: React.ReactNode
if (!userMeetsRole(req, 'admin')) {
content = <p>Statistics are available to administrators only.</p>
if (!(await canReadStats(req.payload, req))) {
content = <p>You do not have access to the statistics.</p>
} else if (visitorId) {
content = <VisitorHistory base={base} timeZone={timeZone} visitorId={visitorId} visits={await loadVisitor(req.payload, visitorId)} />
} else {
+4 -3
View File
@@ -1,6 +1,6 @@
import type { Payload, PayloadRequest } from 'payload'
import { userMeetsRole } from '../../access/predicates.js'
import { canReadStats } from './canReadStats.js'
import { loadStats } from './loadStats.js'
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
@@ -15,11 +15,12 @@ type Props = {
/**
* Dashboard card (admin.components.beforeDashboard): visits per day for the
* last 30 days with a link to the full view. Admins only; renders nothing on
* last 30 days with a link to the full view. For users the stats-visits
* collection lets read (admins by default); renders nothing on
* any error (DB not ready, collection missing).
*/
export async function StatsWidget({ adminRoute = '/admin', payload, req, timeZone = 'Europe/Warsaw', user }: Props) {
if (!payload || !userMeetsRole({ payload, user: req?.user ?? user }, 'admin')) {return null}
if (!payload || !(await canReadStats(payload, req, user))) {return null}
let stats
try {
stats = await loadStats(payload, { timeZone })
+19
View File
@@ -0,0 +1,19 @@
import type { Payload, PayloadRequest } from 'payload'
import { STATS_SLUG } from '../collection.js'
/**
* Whether the user may see the stats view and dashboard chart: the
* `stats-visits` collection's own `access.read` decides, so
* `stats.collectionOverrides` narrows the panel too. A `Where` result counts
* as allowed (the view shows totals, not single records).
*/
export async function canReadStats(payload: Payload, req: Partial<PayloadRequest> | undefined, user?: unknown): Promise<boolean> {
const read = payload.collections[STATS_SLUG as never]?.config.access?.read
if (!read) {return false}
try {
return Boolean(await read({ req: { ...req, payload, user: (req?.user ?? user) as never } as PayloadRequest }))
} catch {
return false
}
}