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:
Radosław Smoliński
2026-10-22 18:33:35 +02:00
co-authored by Claude Opus 5.5
parent cff76c2a69
commit 4911da7112
24 changed files with 167 additions and 68 deletions
+8 -4
View File
@@ -63,18 +63,22 @@ The project's own auth collection keeps its own `access` — the plugin only add
of another account and unlock any account. A collection injected **without** `access` (no roles)
keeps Payload's default rules — without roles all accounts are equal.
> **Without `access` there is no `roles` field**, so `isAdmin` always returns `false`: the Site
> Integrations global and the `/api/ipal/test-email` endpoint then work only through the Local API
> (`overrideAccess`). The seed / first user must have `roles: ['admin']`.
> **Without `access` there is no `roles` field**, so `isAdmin` always returns `false`. The plugin's own
> staff features therefore check with `userMeetsRole(req, minimum)`: when the user's auth collection has no
> `roles` field, every logged-in account passes (as in Payload's default — without roles all accounts are
> equal). That keeps Site Integrations, the settings globals, `/api/ipal/test-email`, stats, consent records
> and form submissions reachable. To narrow them without roles: `globalOverrides` for the globals,
> `forms.formSubmissionOverrides` for submissions, or add `access` to get roles.
## Predicates (front end / hooks / access)
```ts
import { isAdmin, isEditor, hasMinimumRole } from '@intecion/ipal-kit'
import { isAdmin, isEditor, hasMinimumRole, userMeetsRole } from '@intecion/ipal-kit'
isAdmin(user) // admin?
isEditor(user) // editor or admin (hierarchy)
hasMinimumRole(user, 'editor') // at least editor
userMeetsRole(req, 'admin') // admin — or any logged-in user when the auth collection has no roles field
```
They take a loosely typed user (like `req.user`) and read `roles` defensively.
+1 -1
View File
@@ -45,7 +45,7 @@ through this procedure. Ask only when you are missing something you cannot get y
| `slug.join is not a function` | `[slug]` instead of `[[...slug]]` | rename the folder |
| `Missing <html> and <body>` | `[locale]` layout without `<html>` after the root layout was removed | `<html lang>` and `<body>` in `[locale]/layout.tsx` |
| `Export fileTypeFromFile doesn't exist in target module` (Turbopack, dev start) | `instrumentation.ts` imports from the package root → Payload in the Edge bundle | import from `@intecion/ipal-kit/env` + `NEXT_RUNTIME === 'nodejs'` (env.md) |
| `R2_PUBLIC_URL must be a valid URL (got "/media")` | old version of the rule / production with a relative path | dev: `/media` allowed since the new version; production: full URL of the media domain, or leave it empty (media through the app) and make the rule optional (env.md) |
| `R2_PUBLIC_URL must be a valid URL (got "/media")` | old version of the rule / production with a relative path | dev: `/media` allowed since the new version; production: full URL of the media domain, or leave it empty (media through the app; optional since `ipalEnvRules` makes it so by default, env.md) |
| `server-only` in the client | import from `@intecion/ipal-kit/server` in `'use client'` | call it through a server action |
### Admin panel (`/admin`)
+4 -4
View File
@@ -61,7 +61,7 @@ URLs point at that domain. Outside production, `R2_PUBLIC_URL` alone → read-on
| `DATABASE_URI` | ✅ | infra | database connection (read by the project config, not the plugin) |
| `PAYLOAD_SECRET` | ✅ | infra | Payload secret (as above) |
| `NEXT_PUBLIC_SERVER_URL` | ✅ | infra | public URL (canonical, OG, sitemap, IndexNow) |
| `NODE_ENV` | ⬜ | infra | `production` tightens `ipalEnvRules` (R2, `IPAL_IP_HEADER`) and turns off read-only media mode |
| `NODE_ENV` | ⬜ | infra | `production` tightens `ipalEnvRules` (R2 keys, `IPAL_IP_HEADER`) and turns off read-only media mode |
| `GRAPH_TENANT_ID` | ⬜ | email | Azure tenant (Graph) |
| `GRAPH_CLIENT_ID` | ⬜ | email | Azure app id |
| `GRAPH_CLIENT_SECRET` | ⬜ | email | Azure secret |
@@ -70,7 +70,7 @@ URLs point at that domain. Outside production, `R2_PUBLIC_URL` alone → read-on
| `R2_ENDPOINT` | ⬜ (✅ prod) | storage | R2 S3 endpoint |
| `R2_ACCESS_KEY_ID` | ⬜ (✅ prod) | storage | R2 key |
| `R2_SECRET_ACCESS_KEY` | ⬜ (✅ prod) | storage | R2 secret |
| `R2_PUBLIC_URL` | ⬜ (✅ prod in `ipalEnvRules`) | storage | media domain attached to the bucket; empty = media through the app (§6 — then relax the rule, env.md); dev: also the `/media` path |
| `R2_PUBLIC_URL` | ⬜ (✅ prod only with `storage: { requirePublicUrl: true }`) | storage | media domain attached to the bucket; empty = media through the app (§6); dev: also the `/media` path |
| `IPAL_IP_HEADER` | ⬜ (✅ prod) | forms | header with the real IP for the rate limit (`cf-connecting-ip`, `x-forwarded-for`); without it, a warning in the log |
| `NEXT_PUBLIC_TURNSTILE_SITE_KEY`, `TURNSTILE_SECRET_KEY` | ⬜ | forms | fallback when the Turnstile keys are not in Site Integrations |
| `INDEXNOW_KEY` | ⬜ | SEO | IndexNow key (takes precedence over the key from the panel) |
@@ -438,8 +438,8 @@ be attached. Options, in order of preference:
1. **Serve media through the app (default)** — leave `R2_PUBLIC_URL` empty. Files are stored in R2, Payload serves
them at `/api/media/file/<name>` on the site's own domain: no DNS record, works on any zone. Cost: media traffic
passes through the app (next/image caches optimized variants); relax the `R2_PUBLIC_URL` rule in
`ipalEnvRules` (env.md). Details: storage.md.
passes through the app (next/image caches optimized variants); `ipalEnvRules({ storage: true })` accepts an
empty `R2_PUBLIC_URL` (env.md). Details: storage.md.
2. **A dedicated media domain on Cloudflare** — a separate domain whose zone is in our Cloudflare account (e.g.
`klient-media.com`), attached as the bucket's Custom Domain → `R2_PUBLIC_URL=https://<that domain>`. CDN and
cache rules included; for media-heavy sites.
+11 -13
View File
@@ -59,7 +59,7 @@ use).
```ts
ipalEnvRules({
storage?: boolean, // R2_* (R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_ENDPOINT, R2_PUBLIC_URL)
storage?: boolean | { requirePublicUrl?: boolean }, // R2_* (R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_ENDPOINT; R2_PUBLIC_URL optional)
emailGraph?: boolean, // GRAPH_* (GRAPH_TENANT_ID, GRAPH_CLIENT_ID, GRAPH_CLIENT_SECRET, GRAPH_SENDER — e-mail)
ipHeader?: boolean, // IPAL_IP_HEADER — required in production, optional outside it (header name [a-z0-9-])
turnstile?: boolean | { siteKeyVar?: string; secretVar?: string }, // NEXT_PUBLIC_TURNSTILE_SITE_KEY, TURNSTILE_SECRET_KEY
@@ -75,11 +75,13 @@ Always (core): `DATABASE_URI`, `PAYLOAD_SECRET`, `NEXT_PUBLIC_SERVER_URL` (URL),
read; DB/secret are Payload variables — their names can be overridden.
**`storage: true` — when R2_* are required** (evaluated when `ipalEnvRules` is called):
- `NODE_ENV=production` → all 5 (`R2_PUBLIC_URL` as an absolute URL);
- outside production, when any of the 4 keys is set (`R2_BUCKET`, `R2_ACCESS_KEY_ID`,
`R2_SECRET_ACCESS_KEY`, `R2_ENDPOINT`) → all 5 (all or none);
- `NODE_ENV=production` → the 4 keys (`R2_BUCKET`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_ENDPOINT`);
- outside production, when any of the 4 keys is set → all 4 (all or none);
- outside production without keys → none required (local disk); `R2_PUBLIC_URL` alone passes
(read-only mode, storage.md).
- `R2_PUBLIC_URL` is **optional**: empty = media served by the app. When set, it must be an absolute URL in
production. `storage: { requirePublicUrl: true }` requires it wherever the 4 keys are required (a project
that always uses a media domain).
**`turnstile: true`** makes both Turnstile variables **required**, even though the plugin takes the keys
from the panel first (Site Integrations) and env is only a fallback — turn it on only when you keep the keys
@@ -154,17 +156,13 @@ import type { EnvRule, EnvKind, AssertEnvOptions, IpalEnvFeatures } from '@intec
## `R2_PUBLIC_URL` — production vs dev
`ipalEnvRules({ storage: true })` requires a full URL in **production**
(`https://media.klient-media.com`). Our default, though, is media served through the app with
`R2_PUBLIC_URL` **empty** (the client zones are not on Cloudflare — storage.md, deployment.md §6) — then make the
rule optional (the URL format is still checked when set):
`R2_PUBLIC_URL` is optional in `ipalEnvRules({ storage: true })`: our default is media served through the app
with `R2_PUBLIC_URL` **empty** (the client zones are not on Cloudflare — storage.md, deployment.md §6). When it is
set, **production** requires a full URL (`https://media.klient-media.com`). A project that always uses a media
domain requires it:
```ts
assertEnv(
ipalEnvRules({ storage: true, ipHeader: true }).map((r) =>
r.name === 'R2_PUBLIC_URL' ? { ...r, required: false } : r,
),
)
assertEnv(ipalEnvRules({ storage: { requirePublicUrl: true }, ipHeader: true }))
```
Outside production it allows a relative path
+1
View File
@@ -29,6 +29,7 @@ ipalKit({
// autoFill: false, // turn off auto-fill
// descriptionCheck: { settingsSlug: 'site-settings', siteDescriptionField: 'siteDescription' },
// descriptionCheck: false, // no hint under the description
// uploadsCollection: 'images', // meta image upload collection (default 'media')
},
})
```
+2 -9
View File
@@ -98,15 +98,8 @@ Requirements and consequences:
- **Public read on the upload collection** — the file route checks the collection's `read` access, and Payload's
default is "logged-in users only" → anonymous visitors (and next/image) get 403. Add
`access: { read: () => true }` to `Media` (and every other public upload collection).
- **Env validation** — `ipalEnvRules({ storage: true })` requires `R2_PUBLIC_URL` in production; make it optional:
```ts
assertEnv(
ipalEnvRules({ storage: true, ipHeader: true }).map((r) =>
r.name === 'R2_PUBLIC_URL' ? { ...r, required: false } : r,
),
)
```
- **Env validation** — nothing to change: `ipalEnvRules({ storage: true })` treats `R2_PUBLIC_URL` as optional
(`storage: { requirePublicUrl: true }` makes it required, for projects that always use a media domain).
- next/image treats the URLs as local images; CSP `img-src 'self'` already covers them; `MediaPreconnect` renders
nothing (same origin); the plugin's sitemap / JSON-LD helpers make relative image URLs absolute.
- Cost: every original file request passes through the app (bandwidth/CPU). Page images go through `/_next/image`,