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
+1 -1
View File
@@ -12,7 +12,7 @@ import { buildRolesField } from './rolesField.js'
* stack, so no conflict handling is needed — the field is simply added.
*/
export function injectRoles(config: Config, access: AccessOption): Config {
const rolesField = buildRolesField(access.defaultRole)
const rolesField = buildRolesField(access.defaultRole, { firstUserAdmin: access.firstUserAdmin })
return {
...config,
+16 -11
View File
@@ -19,9 +19,10 @@ import { ROLE_HIERARCHY } from './types.js'
* …/first-register on an empty collection) always becomes admin: that screen
* may not show this field, and a site whose only account is a plain 'user' has
* nobody who can manage users, integrations or roles. An open sign-up (plain
* create) on an empty collection still gets `defaultRole`.
* create) on an empty collection still gets `defaultRole`. `firstUserAdmin:
* false` turns this off.
*/
export function buildRolesField(defaultRole: Role = 'user'): Field {
export function buildRolesField(defaultRole: Role = 'user', { firstUserAdmin = true }: { firstUserAdmin?: boolean } = {}): Field {
return {
name: 'roles',
type: 'select',
@@ -35,15 +36,19 @@ export function buildRolesField(defaultRole: Role = 'user'): Field {
},
defaultValue: [defaultRole],
hasMany: true,
hooks: {
beforeChange: [
async ({ collection, operation, req, value }) => {
if (operation !== 'create' || !collection || !req.pathname?.endsWith('/first-register')) {return value}
const { totalDocs } = await req.payload.count({ collection: collection.slug, overrideAccess: true, req })
return totalDocs === 0 ? ['admin'] : value
},
],
},
...(firstUserAdmin
? {
hooks: {
beforeChange: [
async ({ collection, operation, req, value }) => {
if (operation !== 'create' || !collection || !req.pathname?.endsWith('/first-register')) {return value}
const { totalDocs } = await req.payload.count({ collection: collection.slug, overrideAccess: true, req })
return totalDocs === 0 ? ['admin'] : value
},
],
},
}
: {}),
options: ROLE_HIERARCHY.map((role) => ({
label: role.charAt(0).toUpperCase() + role.slice(1),
value: role,
+16
View File
@@ -1,3 +1,5 @@
import type { CollectionConfig } from 'payload'
/**
* Role hierarchy, lowest to highest privilege.
* A higher role satisfies any requirement met by a lower one.
@@ -14,8 +16,22 @@ export type Role = (typeof ROLE_HIERARCHY)[number]
* role definition belongs to the plugin.
*/
export type AccessOption = {
/**
* Access for the auth collection the plugin INJECTS when the project has none
* (a project's own collection keeps its own access untouched). Merged over the
* plugin's defaults — admin manages everyone, others only themselves:
* `{ create: adminOnly, delete: adminOnly, read: adminOrSelf, unlock: adminOnly, update: adminOrSelf }`.
* Your keys win, e.g. `{ read: authenticated }` to let editors list accounts.
*/
authAccess?: CollectionConfig['access']
/** Slug of the client's auth collection, e.g. 'users'. */
authCollection: string
/** Role assigned to new users. Defaults to 'user'. */
defaultRole?: Role
/**
* The account made on Payload's "create first user" screen gets
* `roles: ['admin']`, whatever the form preselected. Default true;
* `false` = that account gets what was submitted (`defaultRole` by default).
*/
firstUserAdmin?: boolean
}