A staff collection whose accounts can hold editor or admin gets the admin rules (H-2, J-3)

In the released 1.16.7. The plugin's checks go by the value on the account, but a project's own staff collection
without rules got the admin rules only when ALL its role values were the plugin's; otherwise 'an account of this
collection' could create, change and delete accounts. Measured on bc0fa65 (SQLite; the first shape also on Postgres
and MongoDB, 2FA off and on): with roles user/editor/admin/boss, with a text and with a json roles field, config-time
and added by a later plugin, an account with the role user creates an admin (201) who reads Site integrations, changes
another staff collection's roles and lists the panel's accounts (200 / 200 / 200), takes the admin over and deletes it;
with viewer/editor a viewer creates an editor who gets past the access rule of Site settings.
canHoldStaffRole(collection): a roles select with editor or admin among its values, or a roles field whose values
cannot be listed. Then: create / delete / unlock an admin, update an admin or the account itself, roles only by an
admin. A project's own rules win. A warning at start when no staff account is an admin and such a collection exists.
The built-site fixture gets a config-time and a late collection of that kind; nothing here touches copied collections.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 02:59:44 +01:00
co-authored by Claude Fable 5.2
parent 01219b69d3
commit be2e6d52d9
10 changed files with 318 additions and 10 deletions
+58 -4
View File
@@ -96,11 +96,35 @@ function findRolesField(fields: FieldLike[] | undefined): FieldLike | undefined
return undefined
}
/** The values of the collection's `roles` select (at the top level of the data, also inside a row or an unnamed tab), or null when it has none. */
const roleValues = (collection: Staffish): null | unknown[] => {
const field = findRolesField(collection.fields as FieldLike[]) as { options?: unknown[]; type?: string } | undefined
if (!field || field.type !== 'select' || !Array.isArray(field.options) || !field.options.length) {return null}
return field.options.map((option) => (typeof option === 'string' ? option : (option as { value?: unknown })?.value))
}
/** True when the collection carries the plugin's roles: a `roles` select at the top level of the data (also inside a row or an unnamed tab) whose values are all the plugin's. */
export function hasPluginRoles(collection: Staffish): boolean {
const field = findRolesField(collection.fields as FieldLike[]) as { options?: unknown[]; type?: string } | undefined
if (!field || field.type !== 'select' || !Array.isArray(field.options) || !field.options.length) {return false}
return field.options.every((option) => ROLE_HIERARCHY.includes((typeof option === 'string' ? option : (option as { value?: unknown })?.value) as never))
return roleValues(collection)?.every((value) => ROLE_HIERARCHY.includes(value as never)) ?? false
}
/**
* True when an account of the collection can hold one of the plugin's roles that carry rights — `editor` or
* `admin` — whatever else its `roles` field offers.
*
* The plugin's predicates go by the VALUE on the account (`isAdmin`, `isEditor`, `userMeetsRole`): an account of
* a staff collection whose roles hold `admin` is a plugin admin, also when the select offers `boss` next to it; one
* that holds `editor` edits the site's settings. So the accounts of such a collection must not be able to hand
* those values out to each other, and the admin rules apply:
* - a `roles` select with `editor` or `admin` among its options (`user, editor, admin, boss`; `viewer, editor`);
* - a `roles` field whose values cannot be listed — a text or json field, a select without options: anything
* can be written there, `admin` included.
* False without a `roles` field, and for a select whose options hold neither value (`lead, member`).
*/
export function canHoldStaffRole(collection: Staffish): boolean {
if (!findRolesField(collection.fields as FieldLike[])) {return false}
const values = roleValues(collection)
return values === null || values.some((value) => value === 'editor' || value === 'admin')
}
/** What the plugin put on a staff collection (`custom.ipalAccountRules`) — for the doctor and for late collections. */
@@ -147,7 +171,8 @@ export type StaffAccountAccessOptions = {
*/
function ruleStaffCollection(collection: CollectionConfig, { create = true, pluginRoles = [], read = true, write = true }: StaffAccountAccessOptions, late = false): StaffAccountRules {
const slug = collection.slug
const ours = pluginRoles.includes(slug) || hasPluginRoles(collection)
// 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)
const other = sameStaffCollectionAccess(slug)
const access = (collection.access ?? {}) as Record<string, unknown>
const applied: string[] = []
@@ -322,6 +347,35 @@ export function applyFirstUserRoute(config: Config, option: FirstUserRouteOption
type LivePayload = { collections: Record<string, { config: { access?: { create?: Access; delete?: Access; read?: Access; readVersions?: Access; unlock?: Access; update?: Access }; auth?: unknown; slug: string } }>; config?: unknown }
/**
* For the start-up line: staff collections that run on the plugin's ADMIN rules (other than `except`, the collection
* with the plugin's own roles) while NO account of any staff collection is a plugin admin — then nobody can create,
* delete or unlock accounts there. Returns their slugs; `[]` when an admin exists, when there is no such
* collection, or when the accounts cannot be counted (a `roles` field that cannot be queried).
*/
export async function staffCollectionsNobodyManages(
payload: { collections: Record<string, { config: CollectionConfig }>; count: (args: { collection: string; overrideAccess: boolean; where?: unknown }) => Promise<{ totalDocs: number }> },
staffSlugs: string[],
except?: string,
): Promise<string[]> {
const staff = staffSlugs.map((slug) => payload.collections?.[slug]?.config).filter((c): c is CollectionConfig => Boolean(c?.auth))
const ruled = staff.filter((c) => {
const rules = (c.custom as { ipalAccountRules?: StaffAccountRules } | undefined)?.ipalAccountRules
return c.slug !== except && rules && !rules.ownRoles && rules.applied.includes('create') && Boolean(findRolesField(c.fields as FieldLike[]))
})
if (!ruled.length) {return []}
try {
for (const c of staff) {
// without a `roles` field every account of a staff collection is an admin to the plugin
const where = findRolesField(c.fields as FieldLike[]) ? { roles: { in: ['admin'] } } : undefined
if ((await payload.count({ collection: c.slug, overrideAccess: true, where })).totalDocs > 0) {return []}
}
} catch {
return [] // cannot tell: say nothing
}
return ruled.map((c) => c.slug).sort()
}
/**
* Auth collections whose `create` rule lets an anonymous request through — open sign-up.
* For the doctor (slugs only). A rule that throws, or needs more than `{ req }`, counts as closed.