Promote to admin only on the first-register screen, not any first create
An open sign-up on an empty collection would otherwise become admin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4c199acdfd
commit
e5d86b8afe
@@ -15,9 +15,11 @@ import { ROLE_HIERARCHY } from './types.js'
|
||||
* Local API calls (seed scripts) run with overrideAccess, so they can still
|
||||
* create the first admin.
|
||||
*
|
||||
* The very first account of the collection always becomes admin: the
|
||||
* first-user 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.
|
||||
* The account made on Payload's "create first user" screen (POST
|
||||
* …/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`.
|
||||
*/
|
||||
export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||
return {
|
||||
@@ -36,7 +38,7 @@ export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
async ({ collection, operation, req, value }) => {
|
||||
if (operation !== 'create' || !collection) {return 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
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user