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
+2
-1
@@ -19,7 +19,8 @@ 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ć. Kolekcja należy do Ciebie
|
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
|
||||||
(create-payload-app) — plugin tylko dokłada pole.
|
(create-payload-app) — plugin tylko dokłada pole.
|
||||||
|
|
||||||
## Co plugin zamyka sam
|
## Co plugin zamyka sam
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ import { ROLE_HIERARCHY } from './types.js'
|
|||||||
* A non-admin's new account gets `defaultRole`. The first-user screen and
|
* 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
|
* 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
|
||||||
|
* 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 {
|
export function buildRolesField(defaultRole: Role = 'user'): Field {
|
||||||
return {
|
return {
|
||||||
@@ -29,6 +33,15 @@ export function buildRolesField(defaultRole: Role = 'user'): Field {
|
|||||||
},
|
},
|
||||||
defaultValue: [defaultRole],
|
defaultValue: [defaultRole],
|
||||||
hasMany: true,
|
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) => ({
|
options: ROLE_HIERARCHY.map((role) => ({
|
||||||
label: role.charAt(0).toUpperCase() + role.slice(1),
|
label: role.charAt(0).toUpperCase() + role.slice(1),
|
||||||
value: role,
|
value: role,
|
||||||
|
|||||||
@@ -117,3 +117,26 @@ describe('forms collection: public read, editor+ write, Emails hidden from plain
|
|||||||
expect(emails.access?.read?.({ req: { user: editor } })).toBe(true)
|
expect(emails.access?.read?.({ req: { user: editor } })).toBe(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('roles field: the first account becomes admin', () => {
|
||||||
|
const hook = async (totalDocs: number, value: string[]) => {
|
||||||
|
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)[] } }
|
||||||
|
const count = vi.fn(async () => ({ totalDocs }))
|
||||||
|
return roles.hooks?.beforeChange?.[0]?.({
|
||||||
|
collection: { slug: 'users' },
|
||||||
|
operation: 'create',
|
||||||
|
req: { payload: { count } },
|
||||||
|
value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
test('empty collection → admin, whatever was sent', async () => {
|
||||||
|
expect(await hook(0, ['user'])).toEqual(['admin'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('later accounts keep what access allowed', async () => {
|
||||||
|
expect(await hook(1, ['user'])).toEqual(['user'])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user