Access defaults overridable; unlock admin-only; 2FA target validated

- access.authAccess merges over the injected auth collection's access;
  unlock is admin-only by default
- access.firstUserAdmin (default true) makes the first-register promotion optional
- globalOverrides: object merges access/admin/hooks, function takes over
- form Emails protection is applied before formOverrides.fields, so a
  project can change it there
- 2FA defaults to admin.user and fails at init when the target is not an
  auth collection or differs from admin.user (twoFactor.allowCollectionMismatch)
- buildRolesField, injectRoles, applyGlobalOverrides exported

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 17:23:14 +02:00
co-authored by Claude Opus 5.5
parent 14fd2c497f
commit a87742621a
11 changed files with 334 additions and 65 deletions
+42 -14
View File
@@ -8,6 +8,7 @@ import { buildNotifications } from './globals/Notifications/index.js'
import { buildSiteIntegrations } from './globals/SiteIntegrations/index.js'
import { buildSiteSettings } from './globals/SiteSettings/index.js'
import { adminOnly, adminOrSelf, injectRoles } from './modules/access/index.js'
import { applyGlobalOverrides } from './globals/overrides.js'
import { buildConsentLogCollection, buildConsentLogEndpoint } from './modules/consent/consentLog.js'
import { buildConstructionFields } from './modules/construction/fields.js'
import { buildArchiveFields } from './modules/content/index.js'
@@ -68,8 +69,9 @@ export const ipalKit = (options: IpalOptions): Plugin => {
'users'
// With roles on it (access.authCollection), the injected collection is
// locked down: Payload's default lets ANY logged-in account read, edit
// (email, password) or delete every other account — a plain 'user' could
// take over an admin. Admins manage everyone; others only themselves.
// (email, password), unlock or delete every other account — a plain 'user'
// could take over an admin. Admins manage everyone; others only themselves.
// access.authAccess overrides any of it; a project's own collection wins whole.
if (!(config.collections ?? []).some((c) => c.slug === authSlug)) {
const hasRoles = options.access?.authCollection === authSlug
config.collections = [
@@ -77,7 +79,16 @@ export const ipalKit = (options: IpalOptions): Plugin => {
{
slug: authSlug,
...(hasRoles
? { access: { create: adminOnly, delete: adminOnly, read: adminOrSelf, update: adminOrSelf } }
? {
access: {
create: adminOnly,
delete: adminOnly,
read: adminOrSelf,
unlock: adminOnly,
update: adminOrSelf,
...options.access?.authAccess,
},
}
: {}),
admin: { useAsTitle: 'email' },
auth: true,
@@ -122,12 +133,30 @@ export const ipalKit = (options: IpalOptions): Plugin => {
)
}
// Default to the resolved auth collection (access.authCollection /
// admin.user), not a literal 'users': wrapping a slug nobody logs in with
// leaves the real collection without TOTP fields, and the TOTP access
// wrapper lets users without those fields through — 2FA silently off.
// TOTP must wrap the collection people log in to the panel with
// (admin.user, else Payload's pick: the first auth collection). Wrapping
// another slug leaves the real one without TOTP fields, and the TOTP
// access wrapper lets users without those fields through — 2FA silently
// off. So: default to it, and refuse a target that isn't an auth
// collection, or differs from it unless allowCollectionMismatch says so.
const authSlugs = (config.collections ?? []).filter((c) => c.auth).map((c) => c.slug)
const adminSlug = config.admin?.user ?? authSlugs[0] ?? authSlug
const target = tf.collectionSlug ?? adminSlug
if (!authSlugs.includes(target)) {
throw new Error(
`[ipal] twoFactor: "${target}" is not an auth collection, so 2FA would protect nothing. ` +
`Auth collections: ${authSlugs.join(', ') || 'none'}. Fix twoFactor.collectionSlug.`,
)
}
if (target !== adminSlug && !tf.allowCollectionMismatch) {
throw new Error(
`[ipal] twoFactor wraps "${target}" but the admin panel logs in with "${adminSlug}" ` +
'(admin.user) — those accounts would skip 2FA. Point twoFactor.collectionSlug at ' +
`"${adminSlug}", or pass twoFactor.allowCollectionMismatch: true if that is deliberate.`,
)
}
config = await totp({
collection: tf.collectionSlug ?? authSlug,
collection: target,
forceSetup: true, // ENFORCED — every user must set up TOTP; no opt-out
totp: { issuer: tf.issuer },
})(config)
@@ -243,12 +272,11 @@ export const ipalKit = (options: IpalOptions): Plugin => {
]
// A global feeds every page's layout — revalidate the site on save.
const revalidateGlobal = options.revalidatePath ? buildGlobalRevalidateHook({ revalidatePath: options.revalidatePath }) : null
config.globals = [
...(config.globals ?? []),
...(revalidateGlobal
? pluginGlobals.map((g) => ({ ...g, hooks: { ...g.hooks, afterChange: [...(g.hooks?.afterChange ?? []), revalidateGlobal] } }))
: pluginGlobals),
]
const withRevalidate = revalidateGlobal
? pluginGlobals.map((g) => ({ ...g, hooks: { ...g.hooks, afterChange: [...(g.hooks?.afterChange ?? []), revalidateGlobal] } }))
: pluginGlobals
// The project's globalOverrides win (access, admin, hooks merged; a function = full control).
config.globals = [...(config.globals ?? []), ...applyGlobalOverrides(withRevalidate, options.globalOverrides)]
config.endpoints = [...(config.endpoints ?? []), testEmailEndpoint]