The panel opens for editor and above by default: Payload's access.admin on the collection that carries the roles. An account that holds only `user` sees "Unauthorized" there; the API is not touched. The rule looks at the role, not at the second factor, so the 2FA screens stay reachable. accounts.panelRole: 'user' | false (everyone, as before) | 'admin'; accounts: false turns the account rules off; the project's own access.admin wins. At start the log says how many accounts the rule keeps out, and is an error when it keeps everyone out. Co-Authored-By: Claude Fable 5.2 <[email protected]>
251 lines
11 KiB
TypeScript
251 lines
11 KiB
TypeScript
import type { Access, CollectionConfig, Config, Field, GlobalConfig } from 'payload'
|
|
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
|
|
|
import { ipalKit } from '../src/plugin.js'
|
|
import { withTotpParts } from './helpers.js'
|
|
|
|
const i18n: I18nConfig = {
|
|
defaultLocale: 'pl',
|
|
locales: [
|
|
{ code: 'pl', label: 'Polski' },
|
|
{ code: 'en', label: 'English' },
|
|
],
|
|
}
|
|
|
|
const empty = (extra: Partial<Config> = {}): Config =>
|
|
({ collections: [], secret: 'x', ...extra }) as unknown as Config
|
|
|
|
const admin = { id: 1, collection: 'users', roles: ['admin'] }
|
|
const editor = { id: 2, collection: 'users', roles: ['editor'] }
|
|
const user = { id: 3, collection: 'users', roles: ['user'] }
|
|
|
|
/** Runs a collection/global access function the way Payload does. */
|
|
const run = (fn: Access | undefined, u: unknown) =>
|
|
fn ? fn({ req: { user: u } } as never) : 'payload-default'
|
|
|
|
describe('injected auth collection', () => {
|
|
test('with roles: a plain user can only see and edit itself, never create or delete', async () => {
|
|
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
|
|
const access = config.collections?.find((c) => c.slug === 'users')?.access ?? {}
|
|
|
|
expect(run(access.update, user)).toEqual({ id: { equals: 3 } })
|
|
expect(run(access.read, user)).toEqual({ id: { equals: 3 } })
|
|
expect(run(access.delete, user)).toBe(false)
|
|
expect(run(access.create, user)).toBe(false)
|
|
expect(run(access.delete, editor)).toBe(false)
|
|
expect(run(access.update, null)).toBe(false)
|
|
|
|
expect(run(access.update, admin)).toBe(true)
|
|
expect(run(access.delete, admin)).toBe(true)
|
|
expect(run(access.create, admin)).toBe(true)
|
|
})
|
|
|
|
test("the project's own auth collection keeps its own access", async () => {
|
|
const own = { slug: 'users', access: { read: () => true }, auth: true, fields: [] }
|
|
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(
|
|
empty({ collections: [own] as CollectionConfig[] }),
|
|
)
|
|
const access = config.collections?.find((c) => c.slug === 'users')?.access
|
|
// what the project wrote stays its own…
|
|
expect(access?.read).toBe(own.access.read)
|
|
// …and the account rules fill in only what it left out (accounts.*; `accounts: false` = nothing added)
|
|
expect(access?.admin).toBeTypeOf('function')
|
|
const untouched = await ipalKit({ access: { authCollection: 'users' }, accounts: false, i18n, twoFactor: false })(
|
|
empty({ collections: [own] as CollectionConfig[] }),
|
|
)
|
|
expect(untouched.collections?.find((c) => c.slug === 'users')?.access).toBe(own.access)
|
|
})
|
|
})
|
|
|
|
describe('2FA wraps the auth collection people log in with', () => {
|
|
const totp = () => vi.fn((o: { collection: string }) => (c: Config) => withTotpParts(c, o.collection))
|
|
|
|
test('defaults to access.authCollection, not a literal "users"', async () => {
|
|
const plugin = totp()
|
|
await ipalKit({ access: { authCollection: 'admins' }, i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })(
|
|
empty(),
|
|
)
|
|
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' }))
|
|
})
|
|
|
|
test('defaults to admin.user when access is not configured', async () => {
|
|
const plugin = totp()
|
|
await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })(
|
|
empty({ admin: { user: 'staff' } } as Partial<Config>),
|
|
)
|
|
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'staff' }))
|
|
})
|
|
|
|
const twoAuth = () =>
|
|
empty({
|
|
admin: { user: 'admins' },
|
|
collections: [
|
|
{ slug: 'admins', auth: true, fields: [] },
|
|
{ slug: 'users', auth: true, fields: [] },
|
|
{ slug: 'pages', fields: [] },
|
|
],
|
|
} as Partial<Config>)
|
|
|
|
test('admin.user wins over access.authCollection as the default target', async () => {
|
|
const plugin = totp()
|
|
await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })(twoAuth())
|
|
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' }))
|
|
})
|
|
|
|
test('a target other than the panel login collection fails at init…', async () => {
|
|
const plugin = totp()
|
|
await expect(
|
|
ipalKit({ i18n, twoFactor: { collectionSlug: 'users', issuer: 'A', plugin: plugin as never } })(twoAuth()),
|
|
).rejects.toThrow(/admin panel logs in with "admins"/)
|
|
})
|
|
|
|
test('…unless allowCollectionMismatch says it is deliberate', async () => {
|
|
const plugin = totp()
|
|
await ipalKit({
|
|
i18n,
|
|
twoFactor: { allowCollectionMismatch: true, collectionSlug: 'users', issuer: 'A', plugin: plugin as never },
|
|
})(twoAuth())
|
|
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'users' }))
|
|
})
|
|
|
|
test('a target that is not an auth collection always fails', async () => {
|
|
const plugin = totp()
|
|
await expect(
|
|
ipalKit({
|
|
i18n,
|
|
twoFactor: { allowCollectionMismatch: true, collectionSlug: 'pages', issuer: 'A', plugin: plugin as never },
|
|
})(twoAuth()),
|
|
).rejects.toThrow(/"pages" is not an auth collection/)
|
|
})
|
|
})
|
|
|
|
describe('globals: public read, editor+ write', () => {
|
|
test.each(['site-settings', 'cookie-settings', 'notifications'])('%s', async (slug) => {
|
|
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
|
|
const access = (config.globals?.find((g) => g.slug === slug) as GlobalConfig).access ?? {}
|
|
expect(run(access.read, null)).toBe(true)
|
|
expect(run(access.update, user)).toBe(false)
|
|
expect(run(access.update, null)).toBe(false)
|
|
expect(run(access.update, editor)).toBe(true)
|
|
expect(run(access.update, admin)).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('forms collection: public read, editor+ write, Emails hidden from plain users', () => {
|
|
const build = async () => {
|
|
const config = await ipalKit({ access: { authCollection: 'users' }, forms: {}, i18n, twoFactor: false })(empty())
|
|
return config.collections?.find((c) => c.slug === 'forms') as CollectionConfig
|
|
}
|
|
|
|
test('create / update / delete need editor', async () => {
|
|
const { access = {} } = await build()
|
|
expect(run(access.read, null)).toBe(true)
|
|
for (const op of ['create', 'update', 'delete'] as const) {
|
|
expect(run(access[op], user)).toBe(false)
|
|
expect(run(access[op], editor)).toBe(true)
|
|
}
|
|
})
|
|
|
|
test('the Emails field (recipients, BCC) is unreadable for a plain user', async () => {
|
|
const forms = await build()
|
|
const emails = forms.fields.find((f: Field) => 'name' in f && f.name === 'emails') as {
|
|
access?: { read?: (a: unknown) => boolean }
|
|
}
|
|
expect(emails.access?.read?.({ req: { user } })).toBe(false)
|
|
expect(emails.access?.read?.({ req: { user: editor } })).toBe(true)
|
|
})
|
|
})
|
|
|
|
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)[] } }
|
|
const count = vi.fn(async () => ({ totalDocs }))
|
|
return roles.hooks?.beforeChange?.[0]?.({
|
|
collection: { slug: 'users' },
|
|
operation: 'create',
|
|
req: { pathname, 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'])
|
|
})
|
|
|
|
test('an open sign-up on an empty collection is not promoted', async () => {
|
|
expect(await hook(0, ['user'], '/api/users')).toEqual(['user'])
|
|
})
|
|
})
|
|
|
|
describe('every access default can be overridden by the project', () => {
|
|
test('injected users: unlock is admin-only by default; access.authAccess keys win', async () => {
|
|
const base = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty())
|
|
const def = base.collections?.find((c) => c.slug === 'users')?.access ?? {}
|
|
expect(run(def.unlock, user)).toBe(false)
|
|
expect(run(def.unlock, admin)).toBe(true)
|
|
|
|
const config = await ipalKit({
|
|
access: { authAccess: { read: () => true }, authCollection: 'users' },
|
|
i18n,
|
|
twoFactor: false,
|
|
})(empty())
|
|
const access = config.collections?.find((c) => c.slug === 'users')?.access ?? {}
|
|
expect(run(access.read, user)).toBe(true)
|
|
expect(run(access.delete, user)).toBe(false) // the rest keeps the plugin default
|
|
})
|
|
|
|
test('access.firstUserAdmin: false keeps what the first-register form sent', async () => {
|
|
const config = await ipalKit({ access: { authCollection: 'users', firstUserAdmin: false }, 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?: unknown }
|
|
expect(roles.hooks).toBeUndefined()
|
|
})
|
|
|
|
test('globalOverrides: an object merges access (project wins), a function takes over', async () => {
|
|
const config = await ipalKit({
|
|
access: { authCollection: 'users' },
|
|
globalOverrides: {
|
|
'cookie-settings': (g) => ({ ...g, label: 'Cookies' }),
|
|
notifications: { access: { update: ({ req }) => Boolean(req.user) } },
|
|
},
|
|
i18n,
|
|
twoFactor: false,
|
|
})(empty())
|
|
const notifications = config.globals?.find((g) => g.slug === 'notifications') as GlobalConfig
|
|
expect(run(notifications.access?.update, user)).toBe(true)
|
|
expect(run(notifications.access?.read, null)).toBe(true)
|
|
expect(config.globals?.find((g) => g.slug === 'cookie-settings')?.label).toBe('Cookies')
|
|
// untouched globals keep the secure default
|
|
expect(run((config.globals?.find((g) => g.slug === 'site-settings') as GlobalConfig).access?.update, user)).toBe(false)
|
|
})
|
|
|
|
test('forms: formOverrides.access wins; a read rule on the Emails field wins', async () => {
|
|
const config = await ipalKit({
|
|
access: { authCollection: 'users' },
|
|
forms: {
|
|
formOverrides: {
|
|
access: { update: () => true },
|
|
fields: ({ defaultFields }) =>
|
|
defaultFields.map((f) => ('name' in f && f.name === 'emails' ? ({ ...f, access: { read: () => true } } as Field) : f)),
|
|
},
|
|
},
|
|
i18n,
|
|
twoFactor: false,
|
|
})(empty())
|
|
const forms = config.collections?.find((c) => c.slug === 'forms') as CollectionConfig
|
|
expect(run(forms.access?.update, user)).toBe(true)
|
|
expect(run(forms.access?.delete, user)).toBe(false)
|
|
const emails = forms.fields.find((f: Field) => 'name' in f && f.name === 'emails') as { access?: { read?: (a: unknown) => boolean } }
|
|
expect(emails.access?.read?.({ req: { user } })).toBe(true)
|
|
})
|
|
})
|