Copied staff collections: the plugin's rule goes in front, nothing is replaced (a rule that cannot widen)

A plugin listed after ipalKit that copies a staff collection under a new slug, or reuses its access object, brings
rules the plugin wrote for the ORIGINAL. On 1.16.7 the copy also carried the boolean 'seen' marker and was left
alone: 'the account itself' then meant an account of the original — with numeric ids the original's account with
the same id changed the copy's account (17 of 40 measured shapes).
Design: the plugin never replaces and never rewrites a rule on such a collection. It puts its rule for the copy's
own slug in front, and both must allow: on a copy named as staff in front of every rule; on a copy that is not
staff or keeps its own rules (custom.ipal.accountRules: false) in front of the rules marked as the plugin's for
another collection. The roles field of every copy is guarded. The mark on the plugin's functions says only
{ slug, self }; custom.ipalAccounts says { slug, staff } (a collection named as staff during next dev is handled
on the reload). The plugin's rules are marked before its 2FA guard, which carries the mark.
Accepted cost: a copy's own account cannot edit itself through a copied rule (as on 1.16.7); the copy writes
access.update — one documented line.
Proof: test/accountCopyTableSuite.ts — 40 shapes x accounts x operations, each row not wider than the measurement
taken on 1.16.7 (test/fixtures/copyRulesBaseline.json) and not wider than the plugin's rule; 2FA off and on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 03:48:09 +01:00
co-authored by Claude Fable 5.2
parent 4c7b722aed
commit 8dcc123b99
14 changed files with 2745 additions and 62 deletions
+31 -2
View File
@@ -10,7 +10,7 @@ import type { Config, Plugin } from 'payload'
export const E2E_LATE_STAFF = process.env.E2E_LATE_STAFF === '1'
/** Slugs the fixture names as staff (payload.config.ts → `access.staffCollections`). */
export const E2E_STAFF_SLUGS = ['late-staff', 'e2e-crew', 'e2e-late-text']
export const E2E_STAFF_SLUGS = ['late-staff', 'e2e-crew', 'e2e-late-text', 'e2e-staff', 'e2e-team', 'e2e-copy', 'e2e-copy-own-rules', 'e2e-copy-cross-op', 'e2e-copy-values', 'e2e-copy-team-admin', 'e2e-copy-opt-out']
/**
* Listed BEFORE ipalKit: a staff collection of the project's own, without rules, whose roles are the plugin's values
@@ -18,15 +18,44 @@ export const E2E_STAFF_SLUGS = ['late-staff', 'e2e-crew', 'e2e-late-text']
*/
export const e2eEarlyStaff: Plugin = (config: Config) =>
E2E_LATE_STAFF
? { ...config, collections: [...(config.collections ?? []), { slug: 'e2e-crew', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin', 'boss'], saveToJWT: true }] }] }
? {
...config,
collections: [
...(config.collections ?? []),
{ slug: 'e2e-crew', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin', 'boss'], saveToJWT: true }] },
// the two originals the later plugin copies: the plugin's roles, and roles of the project's own
{ slug: 'e2e-staff', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin'], saveToJWT: true }] },
{ slug: 'e2e-team', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['lead', 'member'], saveToJWT: true }] },
],
}
: config
/**
* Copies of staff collections under new slugs — the shapes in which a rule could become WIDER when the plugin
* rewrote copied rules. It never replaces a rule now: its rule for the copy's slug goes in front of what came along.
*/
const copies = (config: Config): NonNullable<Config['collections']> => {
const of = (slug: string) => (config.collections ?? []).find((c) => c.slug === slug)!
const staff = of('e2e-staff')
const roles = (options: string[]) => [{ name: 'roles', type: 'select' as const, hasMany: true as const, options, saveToJWT: true }]
return [
{ ...staff, slug: 'e2e-copy' }, // plain: the original's namesake is not "the account itself" here
{ ...staff, slug: 'e2e-copy-own-rules', access: { ...staff.access, create: () => false, delete: () => false, read: () => false, update: () => false } }, // "nobody" stays "nobody"
{ ...staff, slug: 'e2e-copy-cross-op', access: { ...staff.access, read: staff.access?.create, update: staff.access?.create } }, // the admin-only create rule used for read and update
{ ...staff, slug: 'e2e-copy-values', fields: roles(['user', 'editor', 'admin', 'boss']) }, // one more role value
{ ...of('e2e-team'), slug: 'e2e-copy-team-admin', fields: roles(['user', 'editor', 'admin']) }, // "an account of the collection" copied onto a collection with `admin`
{ ...staff, slug: 'e2e-copy-opt-out', custom: { ...staff.custom, ipal: { accountRules: false } }, fields: roles(['user', 'editor', 'admin']) }, // keeps its own rules, brings its own roles field
{ ...staff, slug: 'e2e-copy-not-staff' }, // not named as a staff collection
]
}
export const e2eLateStaff: Plugin = (config: Config) =>
E2E_LATE_STAFF
? {
...config,
collections: [
...(config.collections ?? []),
...copies(config),
// no access rules at all: Payload's defaults ("any logged-in user") are what ipalKit must replace
{ slug: 'late-staff', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin'], saveToJWT: true }], versions: true },
// a late staff collection whose `roles` is a text field: any value can be written there, `admin` included
+39
View File
@@ -96,6 +96,45 @@ test.describe('a late staff collection on a built site', () => {
await api.dispose()
}
// Copies of staff collections (e2e/late-staff-fixture.ts → copies): the lowest account of each copy, and an account
// of the ORIGINAL with the role `user`, get nothing that a plugin rule or the copy's own rule refuses — whatever
// role values the copy has and wherever it uses a copied rule. (Each of these shapes was open in some version that
// REWROTE copied rules; the plugin now only ever puts its rule in front.)
const original = await admin.post('/api/e2e-staff', { data: { email: `o-${tag}@staff.example.com`, password: PASSWORD, roles: ['user'] } })
expect(original.status()).toBe(201)
const fromOriginal = await context(playwright, baseURL!)
expect((await fromOriginal.post('/api/e2e-staff/login', { data: { email: `o-${tag}@staff.example.com`, password: PASSWORD } })).status()).toBe(200)
for (const slug of ['e2e-copy', 'e2e-copy-cross-op', 'e2e-copy-values', 'e2e-copy-team-admin', 'e2e-copy-opt-out', 'e2e-copy-not-staff']) {
const top = await admin.post(`/api/${slug}`, { data: { email: `a-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })
const lowest = await admin.post(`/api/${slug}`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD, roles: ['user'] } })
expect([slug, top.status(), lowest.status()]).toEqual([slug, 201, 201])
const topId = (await top.json()).doc.id as string
const lowestId = (await lowest.json()).doc.id as string
const own = await context(playwright, baseURL!)
expect((await own.post(`/api/${slug}/login`, { data: { email: `u-${tag}@${slug}.example.com`, password: PASSWORD } })).status()).toBe(200)
for (const [who, api] of [['its own lowest account', own], ['an account of the original', fromOriginal], ['a visitor', visitor]] as [string, APIRequestContext][]) {
expect([slug, who, {
create: (await api.post(`/api/${slug}`, { data: { email: `made-${tag}-${who.length}@${slug}.example.com`, password: PASSWORD, roles: ['admin'] } })).status(),
integrations: (await api.get('/api/globals/site-integrations')).status(),
patchLowest: who === 'its own lowest account' ? 'itself' : (await api.patch(`/api/${slug}/${lowestId}`, { data: { password: `taken-${PASSWORD}` } })).status(),
patchTop: (await api.patch(`/api/${slug}/${topId}`, { data: { password: `taken-${PASSWORD}` } })).status(),
zDelete: (await api.delete(`/api/${slug}/${topId}`)).status(),
}]).toEqual([slug, who, { create: 403, integrations: 403, patchLowest: who === 'its own lowest account' ? 'itself' : 403, patchTop: 403, zDelete: 403 }])
}
await own.patch(`/api/${slug}/${lowestId}`, { data: { roles: ['admin'] } })
expect((await (await admin.get(`/api/${slug}/${lowestId}`)).json()).roles, `${slug}: its own roles`).toEqual(['user'])
for (const id of [topId, lowestId]) {expect((await admin.delete(`/api/${slug}/${id}`)).status(), `${slug}: an admin cleans up`).toBe(200)}
await own.dispose()
}
// a copy with rules of its own ("nobody"): an admin is refused too — the plugin's rule went in front, it replaced nothing
expect((await admin.post('/api/e2e-copy-own-rules', { data: { email: `x-${tag}@own.example.com`, password: PASSWORD, roles: ['admin'] } })).status(), 'own rules: create').toBe(403)
expect((await admin.get('/api/e2e-copy-own-rules')).status(), 'own rules: read').toBe(403)
// the cross-op copy: read is "an admin" there (the create rule), also for the copy's own accounts — checked above through
// create / patch; and the list itself:
expect((await fromOriginal.get('/api/e2e-copy-cross-op')).status()).toBe(403)
expect((await admin.get('/api/e2e-copy-cross-op')).status()).toBe(200)
await fromOriginal.dispose()
// an admin: everything, and it cleans up
expect((await admin.get('/api/late-staff/versions')).status()).toBe(200)
expect((await admin.post('/api/late-staff/unlock', { data: { email: `low-${tag}@example.com` } })).status()).toBe(200)