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
+3
-2
@@ -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
|
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.
|
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
|
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
|
pierwszego admina nadal da się utworzyć. **Konto z ekranu „Create first user”
|
||||||
`roles: ['admin']`** (hook pola), nawet gdy ekran pierwszego użytkownika nie pokazuje pola ról. Kolekcja należy do Ciebie
|
(`…/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.
|
(create-payload-app) — plugin tylko dokłada pole.
|
||||||
|
|
||||||
## Co plugin zamyka sam
|
## Co plugin zamyka sam
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ import { ROLE_HIERARCHY } from './types.js'
|
|||||||
* Local API calls (seed scripts) run with overrideAccess, so they can still
|
* Local API calls (seed scripts) run with overrideAccess, so they can still
|
||||||
* create the first admin.
|
* create the first admin.
|
||||||
*
|
*
|
||||||
* The very first account of the collection always becomes admin: the
|
* The account made on Payload's "create first user" screen (POST
|
||||||
* first-user screen may not show this field, and a site whose only account is
|
* …/first-register on an empty collection) always becomes admin: that screen
|
||||||
* a plain 'user' has nobody who can manage users, integrations or roles.
|
* 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 {
|
export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||||
return {
|
return {
|
||||||
@@ -36,7 +38,7 @@ export function buildRolesField(defaultRole: Role = 'user'): Field {
|
|||||||
hooks: {
|
hooks: {
|
||||||
beforeChange: [
|
beforeChange: [
|
||||||
async ({ collection, operation, req, value }) => {
|
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 })
|
const { totalDocs } = await req.payload.count({ collection: collection.slug, overrideAccess: true, req })
|
||||||
return totalDocs === 0 ? ['admin'] : value
|
return totalDocs === 0 ? ['admin'] : value
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ describe('forms collection: public read, editor+ write, Emails hidden from plain
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('roles field: the first account becomes admin', () => {
|
describe('roles field: the first-register account becomes admin', () => {
|
||||||
const hook = async (totalDocs: number, value: string[]) => {
|
const hook = async (totalDocs: number, value: string[], pathname = '/api/users/first-register') => {
|
||||||
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
|
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
|
||||||
const users = config.collections?.find((c) => c.slug === 'users') as CollectionConfig
|
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)[] } }
|
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]?.({
|
return roles.hooks?.beforeChange?.[0]?.({
|
||||||
collection: { slug: 'users' },
|
collection: { slug: 'users' },
|
||||||
operation: 'create',
|
operation: 'create',
|
||||||
req: { payload: { count } },
|
req: { pathname, payload: { count } },
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -139,4 +139,8 @@ describe('roles field: the first account becomes admin', () => {
|
|||||||
test('later accounts keep what access allowed', async () => {
|
test('later accounts keep what access allowed', async () => {
|
||||||
expect(await hook(1, ['user'])).toEqual(['user'])
|
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'])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user