L-2: a config collection that becomes staff later keeps the rules it had at config time
A collection of the config that is not named as staff and that a plugin listed after ipalKit makes the panel's (admin.user) was adopted as a late collection: the plugin's rule went in front of every rule, also the ones the project wrote. The marker now holds the operations that had a rule at config time (own); those are left as written when the collection is handled again, and an operation without one gets the plugin's. The start log tells this case from a collection a later plugin added. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
a743479276
commit
20809ba933
@@ -165,7 +165,16 @@ export type StaffAccountRules = { applied: string[]; ownRoles: boolean; slug?: s
|
||||
* (`{ ...staff, slug: 'copy' }`) and the marker travels with it; and in `next dev` a collection object can
|
||||
* outlive a reload in which it became staff.
|
||||
*/
|
||||
export type AccountsMarker = { slug: string; staff: boolean }
|
||||
export type AccountsMarker = {
|
||||
/**
|
||||
* The operations that had a rule in the project's config when ipalKit ran (before Payload and the 2FA package
|
||||
* filled the missing ones). A collection of the config that becomes staff LATER — a plugin listed after ipalKit
|
||||
* sets `admin.user` to it — keeps exactly those rules; an operation that had none gets the plugin's.
|
||||
*/
|
||||
own?: string[]
|
||||
slug: string
|
||||
staff: boolean
|
||||
}
|
||||
const markerOf = (collection: { custom?: unknown }): AccountsMarker | undefined => {
|
||||
const marker = (collection.custom as { ipalAccounts?: unknown } | undefined)?.ipalAccounts
|
||||
return marker && typeof marker === 'object' && typeof (marker as AccountsMarker).slug === 'string' ? (marker as AccountsMarker) : undefined
|
||||
@@ -223,7 +232,9 @@ const bothAllowField =
|
||||
* `mode`:
|
||||
* - `'config'` (ipalKit is running, nothing is sanitized): an operation without a rule gets the plugin's;
|
||||
* - `'late'` (a staff collection a later plugin added; the config is sanitized, so Payload's default cannot be
|
||||
* told from a written rule): the plugin's rule in front of every rule;
|
||||
* told from a written rule): the plugin's rule in front of every rule — except the operations named in `kept`:
|
||||
* the collection was in the config when ipalKit ran and had a rule of the project's for them then (the marker's
|
||||
* `own`), so they are left as a rule the project wrote, exactly as at config time;
|
||||
* - `'copied'` (a late collection that is not staff, or that keeps its own rules — `custom.ipal.accountRules:
|
||||
* false`): the plugin's rule goes in front ONLY of a rule the plugin wrote for another collection (its mark,
|
||||
* ruleMark.ts). Such a rule says "an account of the original", "the account itself" = one of the original's;
|
||||
@@ -231,7 +242,7 @@ const bothAllowField =
|
||||
* Accepted cost: where a copied rule says "itself" for the ORIGINAL's accounts, the copy's own account cannot
|
||||
* edit itself (as before this change) — the copying plugin writes the copy's `access.update`.
|
||||
*/
|
||||
function ruleStaffCollection(collection: CollectionConfig, { create = true, pluginRoles = [], read = true, write = true }: StaffAccountAccessOptions, mode: 'config' | 'copied' | 'late' = 'config'): StaffAccountRules {
|
||||
function ruleStaffCollection(collection: CollectionConfig, { create = true, pluginRoles = [], read = true, write = true }: StaffAccountAccessOptions, mode: 'config' | 'copied' | 'late' = 'config', kept: string[] = []): StaffAccountRules {
|
||||
const slug = collection.slug
|
||||
// the admin rules: where the roles are the plugin's, and wherever an account can hold `editor` or `admin` (canHoldStaffRole)
|
||||
const ours = pluginRoles.includes(slug) || hasPluginRoles(collection) || canHoldStaffRole(collection)
|
||||
@@ -263,7 +274,7 @@ function ruleStaffCollection(collection: CollectionConfig, { create = true, plug
|
||||
if (mode === 'config') {
|
||||
if (there || !enabled) {continue} // a rule the project wrote
|
||||
access[operation] = rule // marked when ipalKit finishes (markStaffRules): the 2FA package wraps it first
|
||||
} else if ((mode === 'late' && enabled) || mark) {
|
||||
} else if ((mode === 'late' && enabled && !kept.includes(operation)) || mark) {
|
||||
access[operation] = markedStaffRule(bothAllow(rule, there), slug)
|
||||
} else {continue}
|
||||
if (!applied.includes(operation)) {applied.push(operation)}
|
||||
@@ -272,7 +283,8 @@ function ruleStaffCollection(collection: CollectionConfig, { create = true, plug
|
||||
// The `roles` field. An account that may update itself would raise itself through a `roles` field without access of
|
||||
// its own. The guard (the plugin's roles: only an admin changes them; others: never one's own) is put:
|
||||
// - config: where the plugin's `update` rule is the one in force (it says "the account itself");
|
||||
// - late: on every staff collection the plugin rules;
|
||||
// - late: on every staff collection the plugin rules (as at config time, not where the project's own `update`
|
||||
// rule was kept);
|
||||
// - on every COPY, whatever the mode — also one that keeps its own rules and one that is not staff: its field came
|
||||
// without a rule of its own, and its accounts must not write their own roles because the original's could not.
|
||||
// A rule the field has is kept: the guard goes in front of it only when it is the plugin's rule for another collection.
|
||||
@@ -282,7 +294,7 @@ function ruleStaffCollection(collection: CollectionConfig, { create = true, plug
|
||||
const there = roles.access?.update
|
||||
const fieldMark = staffRuleOf(there)
|
||||
const foreign = Boolean(fieldMark) && mode !== 'config'
|
||||
const wanted = mode === 'config' ? write && applied.includes('update') : (mode === 'late' && write) || copied
|
||||
const wanted = mode === 'config' ? write && applied.includes('update') : (mode === 'late' && write && !kept.includes('update')) || copied
|
||||
if (foreign || (!there && wanted)) {
|
||||
// the field object may be shared with the collection this one was copied from — give this one its own
|
||||
if (mode !== 'config') {roles = ownCopyOfField(collection, roles)}
|
||||
@@ -316,6 +328,9 @@ function ownCopyOfField(collection: CollectionConfig, field: FieldLike): FieldLi
|
||||
return copy
|
||||
}
|
||||
|
||||
/** The operations the plugin has a rule for. */
|
||||
const OPERATIONS = ['create', 'update', 'delete', 'unlock', 'read', 'readVersions']
|
||||
|
||||
/** A copy of the field tree down to the `roles` field, so the project's own objects are not changed. */
|
||||
const copyToRoles = (fields: FieldLike[] | undefined): FieldLike[] | undefined =>
|
||||
fields?.map((field) => {
|
||||
@@ -335,7 +350,13 @@ export function applyStaffAccountAccess(config: Config, staffSlugs: string[], op
|
||||
return {
|
||||
...config,
|
||||
collections: (config.collections ?? []).map((collection) => {
|
||||
if (!collection.auth || !staffSlugs.includes(collection.slug)) {return collection}
|
||||
if (!collection.auth) {return collection}
|
||||
// Which operations have a rule of the project's NOW — later nobody can tell (Payload and the 2FA package fill
|
||||
// the missing ones). Kept in the marker for a collection that becomes staff after ipalKit ran.
|
||||
const own = OPERATIONS.filter((operation) => Boolean((collection.access as Record<string, unknown> | undefined)?.[operation]))
|
||||
if (!staffSlugs.includes(collection.slug)) {
|
||||
return { ...collection, custom: { ...collection.custom, ipalAccounts: { slug: collection.slug, own, staff: false } satisfies AccountsMarker } }
|
||||
}
|
||||
const copy = { ...collection, access: { ...collection.access }, fields: copyToRoles(collection.fields as FieldLike[]) as CollectionConfig['fields'] }
|
||||
ruleStaffCollection(copy, options, 'config')
|
||||
return copy
|
||||
@@ -344,7 +365,16 @@ export function applyStaffAccountAccess(config: Config, staffSlugs: string[], op
|
||||
}
|
||||
|
||||
/** What was put on one late auth collection: for the log, which says only what was really done. */
|
||||
export type LateAdoption = { applied: string[]; firstUser: boolean; password: boolean; slug: string }
|
||||
export type LateAdoption = {
|
||||
applied: string[]
|
||||
firstUser: boolean
|
||||
/** The operations whose rule from the project's config was left as it is (a config collection that became staff later). */
|
||||
kept: string[]
|
||||
/** True when the collection was in the config when ipalKit ran (and became staff afterwards); false when a later plugin added it. */
|
||||
known: boolean
|
||||
password: boolean
|
||||
slug: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Collections that were not in the config when ipalKit ran — a plugin listed after it added them.
|
||||
@@ -368,12 +398,16 @@ export function adoptLateAuthCollections(
|
||||
// a copy; a marker that says "not staff" on a collection that is staff now was made before it was named as one.
|
||||
const marker = markerOf(collection)
|
||||
if (marker?.slug === collection.slug && (marker.staff || !staff)) {continue}
|
||||
const done: LateAdoption = { slug: collection.slug, applied: [], firstUser: false, password: false }
|
||||
// In the config when ipalKit ran, not staff then (a later plugin made it the panel's): the rules it had then stay.
|
||||
const known = marker?.slug === collection.slug
|
||||
const kept = known && Array.isArray(marker.own) ? marker.own : []
|
||||
const done: LateAdoption = { slug: collection.slug, applied: [], firstUser: false, kept: [], known, password: false }
|
||||
// A staff collection gets the plugin's rules in front of its own. Any other late collection — not staff, or one
|
||||
// that keeps its own rules — only where it carries a rule the plugin wrote for another collection (a copy).
|
||||
const ruled = staff && access !== false && custom?.ipal?.accountRules !== false
|
||||
done.applied = ruleStaffCollection(collection, access === false || !access ? {} : access, ruled ? 'late' : 'copied').applied
|
||||
collection.custom = { ...collection.custom, ipalAccounts: { slug: collection.slug, staff } satisfies AccountsMarker }
|
||||
done.kept = ruled ? kept : []
|
||||
done.applied = ruleStaffCollection(collection, access === false || !access ? {} : access, ruled ? 'late' : 'copied', kept).applied
|
||||
collection.custom = { ...collection.custom, ipalAccounts: { slug: collection.slug, ...(kept.length ? { own: kept } : {}), staff } satisfies AccountsMarker }
|
||||
if (staff) {
|
||||
const hooks = (collection.hooks ?? {}) as { beforeOperation?: unknown[] }
|
||||
// (a copied collection brings the original's hooks along: the password rule is put once)
|
||||
@@ -422,7 +456,13 @@ export function markSeenAuthCollections(config: Config, staffSlugs: string[] = [
|
||||
const marked = markStaffRules(config) // (a no-op where the plugin marked them before its 2FA guard)
|
||||
return {
|
||||
...marked,
|
||||
collections: (marked.collections ?? []).map((c) => (c.auth ? { ...c, custom: { ...c.custom, ipalAccounts: { slug: c.slug, staff: staffSlugs.includes(c.slug) } satisfies AccountsMarker } } : c)),
|
||||
collections: (marked.collections ?? []).map((c) => {
|
||||
if (!c.auth) {return c}
|
||||
// (the rules the project wrote, noted before the 2FA package ran — applyStaffAccountAccess)
|
||||
const before = markerOf(c)
|
||||
const own = before?.slug === c.slug && Array.isArray(before.own) ? { own: before.own } : {}
|
||||
return { ...c, custom: { ...c.custom, ipalAccounts: { slug: c.slug, ...own, staff: staffSlugs.includes(c.slug) } satisfies AccountsMarker } }
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user