M1 (9): dashboard alerts by role
IntegrationAlerts reads the admin-only Site integrations global with access
control off and showed its error texts to everyone who opened the dashboard.
Now: an admin sees what is failing and the link, an editor a neutral "an
integration needs attention — ask an administrator", anyone else nothing.
CookieCrawlAlert is shown to editors and above (the role that may read the crawl
result). Both use userMeetsRole, so a session that still owes its second factor
sees neither. dashboardAlerts: { integrations: 'admin' | 'editor' | 'all',
cookies: 'editor' | 'all' } loosens it.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
5d35b2c3f4
commit
0b4c4e8295
@@ -4,6 +4,7 @@ import { defineTexts } from '../define.js'
|
||||
export const integrationsTexts = defineTexts({
|
||||
en: {
|
||||
// Dashboard alert (IntegrationAlerts)
|
||||
alertsAskAdmin: 'Ask an administrator to look at it.',
|
||||
alertsHeading: 'An integration needs attention. The site still works.',
|
||||
alertsLink: 'Go to Site integrations',
|
||||
alertsUnknownError: 'Unknown error.',
|
||||
@@ -62,6 +63,7 @@ export const integrationsTexts = defineTexts({
|
||||
testEmailWithFile: 'Attach a 10 MB test file',
|
||||
},
|
||||
pl: {
|
||||
alertsAskAdmin: 'Poproś administratora, aby to sprawdził.',
|
||||
alertsHeading: 'Integracja wymaga uwagi. Witryna nadal działa.',
|
||||
alertsLink: 'Przejdź do integracji',
|
||||
alertsUnknownError: 'Nieznany błąd.',
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Payload } from 'payload'
|
||||
|
||||
import type { CookieScanLabels } from '../labels.js'
|
||||
|
||||
import { userMeetsRole } from '../../../access/predicates.js'
|
||||
import { COOKIE_APPROVAL_VIEW_PATH, countPendingCookies } from '../approval.js'
|
||||
import { COOKIE_REVIEW_SLUG } from '../collection.js'
|
||||
import { COOKIE_SCAN_LABELS, labelFor } from '../labels.js'
|
||||
@@ -11,12 +12,19 @@ type Props = {
|
||||
adminRoute?: string
|
||||
/** Cookie approval is on: remind while cookies wait, link to the approval view. Default true. */
|
||||
approval?: boolean
|
||||
/**
|
||||
* Who sees the alert: `'editor'` (default — the role that may read the crawl result and the
|
||||
* cookie review) or `'all'` (everyone who opens the dashboard, as before).
|
||||
*/
|
||||
audience?: 'all' | 'editor'
|
||||
/** The browser crawl is on: report violations and failed crawls. Default true. */
|
||||
crawl?: boolean
|
||||
/** Injected by Payload into admin server components. */
|
||||
i18n?: { language?: string }
|
||||
labels?: Record<string, CookieScanLabels>
|
||||
payload?: Payload
|
||||
/** Injected by Payload into admin server components — the account looking at the dashboard. */
|
||||
user?: null | Record<string, unknown>
|
||||
}
|
||||
|
||||
type CrawlDoc = { error?: null | string; status?: null | string; summary?: { newUnknown?: null | number; violations?: null | number } | null }
|
||||
@@ -29,8 +37,11 @@ const fill = (template: string, values: Record<string, number | string>) =>
|
||||
* link to the approval view), and — with the crawl — violations or a failed
|
||||
* crawl. Renders nothing when nothing needs attention, and never throws.
|
||||
*/
|
||||
export async function CookieCrawlAlert({ adminRoute = '/admin', approval = true, crawl = true, i18n, labels = COOKIE_SCAN_LABELS, payload }: Props) {
|
||||
export async function CookieCrawlAlert({ adminRoute = '/admin', approval = true, audience = 'editor', crawl = true, i18n, labels = COOKIE_SCAN_LABELS, payload, user }: Props) {
|
||||
if (!payload) {return null}
|
||||
// The crawl result and the review list are for editors and above; the widget reads them with
|
||||
// access control off, so it asks the same question itself (the second factor included).
|
||||
if (audience !== 'all' && !(user && userMeetsRole({ payload, user }, 'editor'))) {return null}
|
||||
let doc: CrawlDoc = {}
|
||||
if (crawl) {
|
||||
try {
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Payload } from 'payload'
|
||||
|
||||
import type { IntegrationStatus } from './status.js'
|
||||
|
||||
import { userMeetsRole } from '../access/predicates.js'
|
||||
import { turnstileMissingKey } from '../turnstile/config.js'
|
||||
import { type IntegrationLabel, resolveLabel, serverText, STATUS_CODE_TEXTS } from './texts.js'
|
||||
|
||||
@@ -10,10 +11,18 @@ type Props = {
|
||||
adminRoute?: string
|
||||
/** Status groups to check: field name → label (one string, or one per language). */
|
||||
checks?: Record<string, IntegrationLabel>
|
||||
/**
|
||||
* Who sees WHAT is failing (the error text and the link to Site integrations): `'admin'`
|
||||
* (default — the role that may read that global), `'editor'`, or `'all'` (everyone who opens
|
||||
* the dashboard, as before). Below that, an editor sees a neutral line; anyone else nothing.
|
||||
*/
|
||||
details?: 'admin' | 'all' | 'editor'
|
||||
/** Injected by Payload into admin server components. */
|
||||
i18n?: { language?: string; t?: (key: never, vars?: Record<string, unknown>) => string }
|
||||
/** Injected by Payload into admin server components. */
|
||||
payload?: Payload
|
||||
/** Injected by Payload into admin server components — the account looking at the dashboard. */
|
||||
user?: null | Record<string, unknown>
|
||||
}
|
||||
|
||||
const DEFAULT_CHECKS: Record<string, IntegrationLabel> = {
|
||||
@@ -25,9 +34,16 @@ const DEFAULT_CHECKS: Record<string, IntegrationLabel> = {
|
||||
* Dashboard alert (admin.components.beforeDashboard): lists integrations that
|
||||
* are currently failing, with a link to Site integrations. Renders nothing when
|
||||
* everything is fine — and never throws (DB not ready → nothing).
|
||||
*
|
||||
* The texts come from the Site integrations global, which only admins may read: the widget
|
||||
* reads it with access control off, so it decides itself who gets them (`details`). A
|
||||
* session that still owes its second factor gets nothing (`userMeetsRole` includes it).
|
||||
*/
|
||||
export async function IntegrationAlerts({ adminRoute = '/admin', checks = DEFAULT_CHECKS, i18n, payload }: Props) {
|
||||
export async function IntegrationAlerts({ adminRoute = '/admin', checks = DEFAULT_CHECKS, details = 'admin', i18n, payload, user }: Props) {
|
||||
if (!payload) {return null}
|
||||
const sees = (role: 'admin' | 'editor') => Boolean(user) && userMeetsRole({ payload, user }, role)
|
||||
const full = details === 'all' || sees(details)
|
||||
if (!full && !sees('editor')) {return null}
|
||||
let doc: Record<string, unknown>
|
||||
try {
|
||||
doc = (await payload.findGlobal({ slug: 'site-integrations' as never, depth: 0 })) as Record<string, unknown>
|
||||
@@ -57,8 +73,18 @@ export async function IntegrationAlerts({ adminRoute = '/admin', checks = DEFAUL
|
||||
}
|
||||
if (!failing.length) {return null}
|
||||
|
||||
// Not allowed the detail: that something needs attention, and whom to ask — nothing of what or why.
|
||||
if (!full) {
|
||||
return (
|
||||
<div data-ipal="integration-alerts" role="status" style={{ border: '1px solid var(--theme-elevation-200, #ddd)', borderRadius: 4, marginBottom: '1.5rem', padding: '1rem 1.25rem' }}>
|
||||
<strong>{serverText(i18nLike, 'alertsHeading')}</strong> {serverText(i18nLike, 'alertsAskAdmin')}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
data-ipal="integration-alerts"
|
||||
role="alert"
|
||||
style={{
|
||||
background: 'var(--theme-error-50, rgba(229, 72, 77, 0.08))',
|
||||
|
||||
Reference in New Issue
Block a user