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:
co-authored by
Claude Opus 5.5
parent
14fd2c497f
commit
a87742621a
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -35,7 +35,9 @@ const staffAccess = ({ req }: AccessArgs) => hasMinimumRole(req.user as never, '
|
||||
/**
|
||||
* Forms stay publicly readable (their fields render the form), but the Emails
|
||||
* tab — recipient addresses, templates — is for editors and admins only, so
|
||||
* GET /api/forms doesn't hand out the company's inboxes to spammers.
|
||||
* GET /api/forms doesn't hand out the company's inboxes to spammers. Applied to
|
||||
* the default fields BEFORE formOverrides.fields sees them, so a project can
|
||||
* still change the rule there.
|
||||
*/
|
||||
function hideEmails(fields: Field[]): Field[] {
|
||||
return fields.map((field) =>
|
||||
@@ -117,8 +119,10 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
...forms.formOverrides?.access,
|
||||
},
|
||||
admin: { ...group, ...forms.formOverrides?.admin },
|
||||
fields: ({ defaultFields }) =>
|
||||
hideEmails(forms.formOverrides?.fields ? forms.formOverrides.fields({ defaultFields }) : defaultFields),
|
||||
fields: ({ defaultFields }) => {
|
||||
const protectedFields = hideEmails(defaultFields)
|
||||
return forms.formOverrides?.fields ? forms.formOverrides.fields({ defaultFields: protectedFields }) : protectedFields
|
||||
},
|
||||
},
|
||||
formSubmissionOverrides: {
|
||||
labels: { plural: 'Submissions', singular: 'Submission' },
|
||||
|
||||
Reference in New Issue
Block a user