Small fixes: optional R2_PUBLIC_URL, seo.uploadsCollection, staff features without roles
- ipalEnvRules({ storage: true }): R2_PUBLIC_URL optional (media served by
the app); storage: { requirePublicUrl: true } requires it again.
- seo.uploadsCollection: upload collection of the meta image (default media).
- userMeetsRole(req, minimum): the plugin's admin/editor features (Site
Integrations, settings globals, test e-mail, stats, consent records, form
submissions) stay reachable when `access` is omitted — without a roles
field every logged-in account passes, as in Payload's default.
- IndexNow skip for seed writes is covered by test/seed.test.ts.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cff76c2a69
commit
4911da7112
@@ -1,6 +1,6 @@
|
||||
import type { GlobalConfig } from 'payload'
|
||||
|
||||
import { adminOrEditor } from '../../modules/access/index.js'
|
||||
import { userMeetsRole } from '../../modules/access/index.js'
|
||||
import { cookieSettingsFields } from './fields.js'
|
||||
|
||||
/**
|
||||
@@ -12,7 +12,7 @@ export function buildCookieSettings(): GlobalConfig {
|
||||
slug: 'cookie-settings',
|
||||
access: {
|
||||
read: () => true,
|
||||
update: adminOrEditor, // consent texts are legal copy — not for plain 'user' accounts
|
||||
update: ({ req }) => userMeetsRole(req, 'editor'), // consent texts are legal copy — not for plain 'user' accounts
|
||||
},
|
||||
admin: {
|
||||
group: 'Settings',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { GlobalConfig } from 'payload'
|
||||
|
||||
import { adminOrEditor } from '../../modules/access/index.js'
|
||||
import { userMeetsRole } from '../../modules/access/index.js'
|
||||
import { notificationsFields } from './fields.js'
|
||||
|
||||
/**
|
||||
@@ -13,7 +13,7 @@ export function buildNotifications(): GlobalConfig {
|
||||
slug: 'notifications',
|
||||
access: {
|
||||
read: () => true, // texts are public-facing (shown to end users)
|
||||
update: adminOrEditor,
|
||||
update: ({ req }) => userMeetsRole(req, 'editor'),
|
||||
},
|
||||
admin: { group: 'Settings' },
|
||||
fields: notificationsFields,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Field, GlobalConfig } from 'payload'
|
||||
|
||||
import { isAdmin } from '../../modules/access/index.js'
|
||||
import { userMeetsRole } from '../../modules/access/index.js'
|
||||
import { indexNowFields } from '../../modules/seo/indexNow.js'
|
||||
import { analyticsFields } from './fields/analytics.js'
|
||||
import { smtpFields } from './fields/smtp.js'
|
||||
@@ -31,8 +31,9 @@ export function buildSiteIntegrations({
|
||||
access: {
|
||||
// Admin-only — secrets live here. Anonymous and non-admin users get
|
||||
// nothing through the API; admins read/edit in the panel and via Local API.
|
||||
read: ({ req: { user } }) => isAdmin(user),
|
||||
update: ({ req: { user } }) => isAdmin(user),
|
||||
// Without roles (no `access` option) every logged-in account is admin.
|
||||
read: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
update: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
},
|
||||
admin: {
|
||||
group: 'Settings',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Field, GlobalConfig } from 'payload'
|
||||
|
||||
import { adminOrEditor } from '../../modules/access/index.js'
|
||||
import { userMeetsRole } from '../../modules/access/index.js'
|
||||
import { generalFields } from './fields/general.js'
|
||||
import { themeFields } from './fields/theme.js'
|
||||
|
||||
@@ -48,7 +48,7 @@ export function buildSiteSettings({
|
||||
read: () => true,
|
||||
// Editors and admins only — a plain 'user' account (open sign-up) must
|
||||
// not rewrite the brand, homepage or construction mode.
|
||||
update: adminOrEditor,
|
||||
update: ({ req }) => userMeetsRole(req, 'editor'),
|
||||
},
|
||||
admin: {
|
||||
group: 'Settings',
|
||||
|
||||
@@ -16,6 +16,7 @@ export {
|
||||
requireRole,
|
||||
requireRoleField,
|
||||
ROLE_HIERARCHY,
|
||||
userMeetsRole,
|
||||
} from './modules/access/index.js'
|
||||
// Concrete files, not module barrels: a barrel that also re-exports 'use client'
|
||||
// components would load React UI into payload.config / the CLI (test/entryGraph).
|
||||
|
||||
@@ -9,7 +9,7 @@ export {
|
||||
requireRoleField,
|
||||
} from './access.js'
|
||||
export { injectRoles } from './injectRoles.js'
|
||||
export { hasMinimumRole, isAdmin, isEditor } from './predicates.js'
|
||||
export { hasMinimumRole, isAdmin, isEditor, userMeetsRole } from './predicates.js'
|
||||
export { buildRolesField } from './rolesField.js'
|
||||
export type { AccessOption, Role } from './types.js'
|
||||
export { ROLE_HIERARCHY } from './types.js'
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { Role } from './types.js'
|
||||
|
||||
import { ROLE_HIERARCHY } from './types.js'
|
||||
@@ -42,3 +44,24 @@ export function isAdmin(user: MaybeUser): boolean {
|
||||
export function isEditor(user: MaybeUser): boolean {
|
||||
return hasMinimumRole(user, 'editor')
|
||||
}
|
||||
|
||||
/**
|
||||
* Role check for the plugin's own staff features (Site Integrations, the
|
||||
* e-mail test, stats, consent records, form submissions, the settings
|
||||
* globals): true when the user holds at least `minimum` — or, when the
|
||||
* user's auth collection has no `roles` field (the project omitted `access`),
|
||||
* for every logged-in user. Without roles all accounts are equal, as in
|
||||
* Payload's own default; nobody being "admin" would lock those features away.
|
||||
*
|
||||
* Pass `req` (it has `payload` and `user`). An unknown collection counts as
|
||||
* having roles — the strict side. A project with its own role scheme in
|
||||
* another field narrows access through globalOverrides / collection overrides.
|
||||
*/
|
||||
export function userMeetsRole({ payload, user }: { payload?: BasePayload; user?: unknown }, minimum: Role): boolean {
|
||||
if (!user || typeof user !== 'object') {return false}
|
||||
if (hasMinimumRole(user as MaybeUser, minimum)) {return true}
|
||||
const slug = (user as { collection?: unknown }).collection
|
||||
const fields = typeof slug === 'string' ? payload?.collections?.[slug as never]?.config.fields : undefined
|
||||
if (!fields) {return false}
|
||||
return !fields.some((f) => 'name' in f && f.name === 'roles')
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CollectionConfig, Endpoint, PayloadRequest } from 'payload'
|
||||
|
||||
import { isAdmin } from '../access/predicates.js'
|
||||
import { userMeetsRole } from '../access/predicates.js'
|
||||
import { createDailyPurge } from '../shared/purge.js'
|
||||
import { clientIp, consumeRateLimit } from '../shared/rateLimit.js'
|
||||
import { CONSENT_CATEGORIES } from './categories.js'
|
||||
@@ -30,8 +30,8 @@ export function buildConsentLogCollection(): CollectionConfig {
|
||||
access: {
|
||||
// Written only by the endpoint (Local API); read and pruned by admins.
|
||||
create: () => false,
|
||||
delete: ({ req: { user } }) => isAdmin(user),
|
||||
read: ({ req: { user } }) => isAdmin(user),
|
||||
delete: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
read: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
update: () => false,
|
||||
},
|
||||
admin: {
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Endpoint, PayloadRequest } from 'payload'
|
||||
|
||||
import { addDataAndFileToRequest } from 'payload'
|
||||
|
||||
import { isAdmin } from '../../access/predicates.js'
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
|
||||
/**
|
||||
* Custom endpoint: send a test email to a given address through whatever
|
||||
@@ -24,7 +24,7 @@ export const testEmailEndpoint: Endpoint = {
|
||||
if (!req.user) {
|
||||
return Response.json({ error: 'Unauthorized', ok: false }, { status: 401 })
|
||||
}
|
||||
if (!isAdmin(req.user)) {
|
||||
if (!userMeetsRole(req, 'admin')) {
|
||||
return Response.json({ error: 'Forbidden', ok: false }, { status: 403 })
|
||||
}
|
||||
|
||||
|
||||
Vendored
+12
-7
@@ -17,12 +17,16 @@ export type IpalEnvFeatures = {
|
||||
/** Override the site-URL var. Default 'NEXT_PUBLIC_SERVER_URL'. */
|
||||
serverUrlVar?: string
|
||||
/**
|
||||
* R2 object storage (buildR2Storage). Adds the R2_* vars — required in
|
||||
* production (media on a container disk vanish on redeploy). Outside
|
||||
* production they're optional (media go to the local disk), but a partial
|
||||
* set is an error: buildR2Storage needs all of them.
|
||||
* R2 object storage (buildR2Storage). Adds the R2_* vars — the credentials
|
||||
* are required in production (media on a container disk vanish on redeploy).
|
||||
* Outside production they're optional (media go to the local disk), but a
|
||||
* partial set is an error: buildR2Storage needs all of them.
|
||||
*
|
||||
* R2_PUBLIC_URL is optional: empty = media served by the app
|
||||
* (/api/media/file/…). `{ requirePublicUrl: true }` requires it wherever the
|
||||
* credentials are required (a media domain on Cloudflare).
|
||||
*/
|
||||
storage?: boolean
|
||||
storage?: { requirePublicUrl?: boolean } | boolean
|
||||
/**
|
||||
* Cloudflare Turnstile. true uses the common var names
|
||||
* (NEXT_PUBLIC_TURNSTILE_SITE_KEY + TURNSTILE_SECRET_KEY); pass an object to
|
||||
@@ -74,6 +78,7 @@ export function ipalEnvRules(features: IpalEnvFeatures = {}): EnvRule[] {
|
||||
]
|
||||
|
||||
if (storage) {
|
||||
const requirePublicUrl = typeof storage === 'object' && storage.requirePublicUrl === true
|
||||
const r2Credentials = ['R2_BUCKET', 'R2_ACCESS_KEY_ID', 'R2_SECRET_ACCESS_KEY', 'R2_ENDPOINT']
|
||||
// Production: always. Development: once any credential is set (all or nothing).
|
||||
// R2_PUBLIC_URL alone is the read-only media mode (buildR2Storage) — allowed in dev.
|
||||
@@ -86,8 +91,8 @@ export function ipalEnvRules(features: IpalEnvFeatures = {}): EnvRule[] {
|
||||
{ name: 'R2_ENDPOINT', description: 'R2 S3 API endpoint URL', kind: 'url', required: r2Required },
|
||||
{
|
||||
name: 'R2_PUBLIC_URL',
|
||||
description: 'Public base URL media is served from (custom domain of the bucket)',
|
||||
required: r2Required,
|
||||
description: 'Public base URL media is served from (custom domain of the bucket); empty = served by the app',
|
||||
required: requirePublicUrl && r2Required,
|
||||
// Production: absolute URL (https://media.example.com). Outside production a
|
||||
// site-relative path ('/media') is accepted — buildR2Storage prefixes it with
|
||||
// NEXT_PUBLIC_SERVER_URL (e.g. a dev rewrite /media → bucket).
|
||||
|
||||
@@ -5,7 +5,7 @@ import { fields as builtinBlocks, formBuilderPlugin } from '@payloadcms/plugin-f
|
||||
|
||||
import type { FormsOption } from './types.js'
|
||||
|
||||
import { hasMinimumRole, isAdmin } from '../access/predicates.js'
|
||||
import { userMeetsRole } from '../access/predicates.js'
|
||||
import { checkboxGroupBlock, stepBlock, timeBlock, withPlaceholder, withShowWhen } from './builderBlocks.js'
|
||||
import { buildEmailLayoutHook } from './emailLayout.js'
|
||||
|
||||
@@ -24,13 +24,13 @@ const submissionsCreateAccess = ({ req }: AccessArgs) => Boolean(req.user)
|
||||
* form-builder default lets ANY account of the admin collection read them —
|
||||
* including a plain 'user' from an open sign-up.
|
||||
*/
|
||||
const submissionsReadAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, 'editor')
|
||||
const submissionsDeleteAccess = ({ req }: AccessArgs) => isAdmin(req.user as never)
|
||||
const submissionsReadAccess = ({ req }: AccessArgs) => userMeetsRole(req, 'editor')
|
||||
const submissionsDeleteAccess = ({ req }: AccessArgs) => userMeetsRole(req, 'admin')
|
||||
/**
|
||||
* Forms carry recipient addresses (Emails → To/BCC): a plain 'user' account
|
||||
* editing a form could BCC every submission to itself. Editors and admins only.
|
||||
*/
|
||||
const staffAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, 'editor')
|
||||
const staffAccess = ({ req }: AccessArgs) => userMeetsRole(req, 'editor')
|
||||
|
||||
/**
|
||||
* Forms stay publicly readable (their fields render the form), but the Emails
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { SeoOption } from './types.js'
|
||||
|
||||
type BuildSeoPluginArgs = {
|
||||
seo: SeoOption
|
||||
/** Upload collection slug for the meta image (typically 'media'). */
|
||||
/** Upload collection slug for the meta image. Default `seo.uploadsCollection`, then 'media'. */
|
||||
uploadsCollection?: string
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ function afterDescription(fields: Field[], extra: Field | null): Field[] {
|
||||
* slug) already sit in the collection, leaving an empty SEO tab. Instead the
|
||||
* SEO UI fields are injected into a controlled tab by injectSeoTabs.
|
||||
*/
|
||||
export function buildSeoPlugin({ seo, uploadsCollection = 'media' }: BuildSeoPluginArgs): Plugin {
|
||||
export function buildSeoPlugin({ seo, uploadsCollection = seo.uploadsCollection ?? 'media' }: BuildSeoPluginArgs): Plugin {
|
||||
return seoPlugin({
|
||||
collections: seo.collections,
|
||||
fields: ({ defaultFields }) => [
|
||||
|
||||
@@ -45,6 +45,8 @@ export type SeoOption = {
|
||||
* aiCrawlers, llmsIntro) or read your own fields in your routes.
|
||||
*/
|
||||
searchSettings?: ((defaults: Field[]) => Field[]) | false
|
||||
/** Upload collection the meta image (`meta.image`) relates to. Default 'media'. */
|
||||
uploadsCollection?: string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Gutter } from '@payloadcms/ui'
|
||||
|
||||
import type { StatsVisit } from '../collection.js'
|
||||
|
||||
import { isAdmin } from '../../access/predicates.js'
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { VISITOR_ID_PATTERN } from '../parse.js'
|
||||
import { loadRecentVisitors, loadStats, loadVisitor } from './loadStats.js'
|
||||
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
|
||||
@@ -88,7 +88,7 @@ export async function StatsView(props: Props) {
|
||||
const visitorId = visitorParam && VISITOR_ID_PATTERN.test(visitorParam) ? visitorParam : undefined
|
||||
|
||||
let content: React.ReactNode
|
||||
if (!isAdmin(req.user)) {
|
||||
if (!userMeetsRole(req, 'admin')) {
|
||||
content = <p>Statistics are available to administrators only.</p>
|
||||
} else if (visitorId) {
|
||||
content = <VisitorHistory base={base} timeZone={timeZone} visitorId={visitorId} visits={await loadVisitor(req.payload, visitorId)} />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Payload, PayloadRequest } from 'payload'
|
||||
|
||||
import { isAdmin } from '../../access/predicates.js'
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { loadStats } from './loadStats.js'
|
||||
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
|
||||
|
||||
@@ -19,7 +19,7 @@ type Props = {
|
||||
* any error (DB not ready, collection missing).
|
||||
*/
|
||||
export async function StatsWidget({ adminRoute = '/admin', payload, req, timeZone = 'Europe/Warsaw', user }: Props) {
|
||||
if (!payload || !isAdmin((req?.user ?? user) as never)) {return null}
|
||||
if (!payload || !userMeetsRole({ payload, user: req?.user ?? user }, 'admin')) {return null}
|
||||
let stats
|
||||
try {
|
||||
stats = await loadStats(payload, { timeZone })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
|
||||
import { isAdmin } from '../access/predicates.js'
|
||||
import { userMeetsRole } from '../access/predicates.js'
|
||||
|
||||
/**
|
||||
* One document per visit (a run of page views with gaps under 30 minutes).
|
||||
@@ -48,8 +48,8 @@ export function buildStatsCollection(): CollectionConfig {
|
||||
// Written only by the hit endpoint (Local API); read and deleted by admins
|
||||
// (deleting is how a visitor's erasure request is handled).
|
||||
create: () => false,
|
||||
delete: ({ req: { user } }) => isAdmin(user),
|
||||
read: ({ req: { user } }) => isAdmin(user),
|
||||
delete: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
read: ({ req }) => userMeetsRole(req, 'admin'),
|
||||
update: () => false,
|
||||
},
|
||||
admin: {
|
||||
|
||||
Reference in New Issue
Block a user