First account of the auth collection always becomes admin
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
561a9e2bb7
commit
78f4f5c16b
@@ -14,6 +14,10 @@ import { ROLE_HIERARCHY } from './types.js'
|
||||
* A non-admin's new account gets `defaultRole`. The first-user screen and
|
||||
* 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.
|
||||
*/
|
||||
export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||
return {
|
||||
@@ -29,6 +33,15 @@ export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||
},
|
||||
defaultValue: [defaultRole],
|
||||
hasMany: true,
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
async ({ collection, operation, req, value }) => {
|
||||
if (operation !== 'create' || !collection) {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,
|
||||
|
||||
Reference in New Issue
Block a user