diff --git a/docs/access.md b/docs/access.md index b09b99d..ac066bb 100644 --- a/docs/access.md +++ b/docs/access.md @@ -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 diff --git a/src/modules/access/rolesField.ts b/src/modules/access/rolesField.ts index 1434b5d..d9621c1 100644 --- a/src/modules/access/rolesField.ts +++ b/src/modules/access/rolesField.ts @@ -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 }, diff --git a/test/accessControl.test.ts b/test/accessControl.test.ts index f07ce17..b348064 100644 --- a/test/accessControl.test.ts +++ b/test/accessControl.test.ts @@ -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']) + }) })