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:
Radosław Smoliński
2026-10-22 17:17:42 +02:00
co-authored by Claude Opus 5.5
parent 4c199acdfd
commit e5d86b8afe
3 changed files with 16 additions and 9 deletions
+3 -2
View File
@@ -19,8 +19,9 @@ Wstrzykuje pole `roles` (select hasMany, `saveToJWT`, tylko admin może
nadawać i zmieniać role — także przy tworzeniu konta, więc otwarta rejestracja nie
przepuści `roles: ['admin']`; nie-admin dostaje `defaultRole`) do wskazanej kolekcji.
Ekran pierwszego użytkownika i Local API (seed) działają z `overrideAccess`, więc
pierwszego admina nadal da się utworzyć. **Pierwsze konto w kolekcji zawsze dostaje
`roles: ['admin']`** (hook pola), nawet gdy ekran pierwszego użytkownika nie pokazuje pola ról. Kolekcja należy do Ciebie
pierwszego admina nadal da się utworzyć. **Konto z ekranu „Create first user”
(`…/first-register` na pustej kolekcji) zawsze dostaje `roles: ['admin']`** (hook pola), nawet gdy ekran
nie pokazuje pola ról. Otwarta rejestracja (zwykłe `create`) na pustej kolekcji dostaje `defaultRole`. Kolekcja należy do Ciebie
(create-payload-app) — plugin tylko dokłada pole.
## Co plugin zamyka sam
+6 -4
View File
@@ -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
},
+7 -3
View File
@@ -118,8 +118,8 @@ describe('forms collection: public read, editor+ write, Emails hidden from plain
})
})
describe('roles field: the first account becomes admin', () => {
const hook = async (totalDocs: number, value: string[]) => {
describe('roles field: the first-register account becomes admin', () => {
const hook = async (totalDocs: number, value: string[], pathname = '/api/users/first-register') => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
const users = config.collections?.find((c) => c.slug === 'users') as CollectionConfig
const roles = users.fields.find((f) => 'name' in f && f.name === 'roles') as { hooks?: { beforeChange?: ((a: unknown) => unknown)[] } }
@@ -127,7 +127,7 @@ describe('roles field: the first account becomes admin', () => {
return roles.hooks?.beforeChange?.[0]?.({
collection: { slug: 'users' },
operation: 'create',
req: { payload: { count } },
req: { pathname, payload: { count } },
value,
})
}
@@ -139,4 +139,8 @@ describe('roles field: the first account becomes admin', () => {
test('later accounts keep what access allowed', async () => {
expect(await hook(1, ['user'])).toEqual(['user'])
})
test('an open sign-up on an empty collection is not promoted', async () => {
expect(await hook(0, ['user'], '/api/users')).toEqual(['user'])
})
})