Access defaults overridable; unlock admin-only; 2FA target validated
- access.authAccess merges over the injected auth collection's access; unlock is admin-only by default - access.firstUserAdmin (default true) makes the first-register promotion optional - globalOverrides: object merges access/admin/hooks, function takes over - form Emails protection is applied before formOverrides.fields, so a project can change it there - 2FA defaults to admin.user and fails at init when the target is not an auth collection or differs from admin.user (twoFactor.allowCollectionMismatch) - buildRolesField, injectRoles, applyGlobalOverrides exported Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
14fd2c497f
commit
a87742621a
+100
-5
@@ -70,14 +70,46 @@ describe('2FA wraps the auth collection people log in with', () => {
|
||||
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'staff' }))
|
||||
})
|
||||
|
||||
test('an explicit collectionSlug still wins', async () => {
|
||||
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({
|
||||
access: { authCollection: 'users' },
|
||||
i18n,
|
||||
twoFactor: { collectionSlug: 'admins', issuer: 'A', plugin: plugin as never },
|
||||
})(empty())
|
||||
expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' }))
|
||||
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/)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -144,3 +176,66 @@ describe('roles field: the first-register account becomes admin', () => {
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user