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
+44
View File
@@ -0,0 +1,44 @@
import type { GlobalConfig } from 'payload'
/** Slugs of the globals the plugin adds. */
export type IpalGlobalSlug = 'cookie-settings' | 'notifications' | 'site-integrations' | 'site-settings'
/**
* A project's change to one plugin global:
* - an object is merged over the plugin's: `access`, `admin` and `hooks` key by
* key (your access rules win, your hooks run after the plugin's), anything
* else (label, fields, …) replaces the plugin's value;
* - a function gets the plugin's global and returns the one to use — full
* control, e.g. dropping a plugin hook or appending fields.
*/
export type GlobalOverride = ((global: GlobalConfig) => GlobalConfig) | Partial<GlobalConfig>
export type GlobalOverrides = Partial<Record<IpalGlobalSlug, GlobalOverride>>
type Hooks = NonNullable<GlobalConfig['hooks']>
function mergeHooks(base: GlobalConfig['hooks'], extra: GlobalConfig['hooks']): GlobalConfig['hooks'] {
if (!extra) {return base}
const out: Record<string, unknown[]> = { ...(base as Record<string, unknown[]>) }
for (const [key, list] of Object.entries(extra)) {
out[key] = [...(out[key] ?? []), ...((list as unknown[]) ?? [])]
}
return out as Hooks
}
/** Applies `globalOverrides` to the plugin's globals. */
export function applyGlobalOverrides(globals: GlobalConfig[], overrides: GlobalOverrides | undefined): GlobalConfig[] {
if (!overrides) {return globals}
return globals.map((global) => {
const override = overrides[global.slug as IpalGlobalSlug]
if (!override) {return global}
if (typeof override === 'function') {return override(global)}
return {
...global,
...override,
access: { ...global.access, ...override.access },
admin: { ...global.admin, ...override.admin },
hooks: mergeHooks(global.hooks, override.hooks),
}
})
}