M1 (5): the lowest role stays out of the admin panel (accounts.panelRole)

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]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:12 +01:00
co-authored by Claude Fable 5.2
parent 463fd5d664
commit cd60b73685
7 changed files with 259 additions and 2 deletions
+60
View File
@@ -0,0 +1,60 @@
import type { BasePayload, CollectionConfig, Config } from 'payload'
import type { Role } from './types.js'
import { hasMinimumRole } from './predicates.js'
import { ROLE_HIERARCHY } from './types.js'
/**
* Who may open the admin panel.
*
* Payload lets every account of the login collection into the panel; what it sees there is
* decided per collection. The lowest role (`user`) is the default for a new account and has
* no business in the panel: by default the panel opens for `editor` and above
* (`accounts.panelRole`). It is Payload's own `access.admin` on the login collection, so the
* API is not touched — a `user` account still logs in over the API and gets what its role allows.
*
* The rule looks at the role only, not at the second factor: an editor who has typed the
* password must still reach the 2FA screens, which are part of the panel.
*/
export const DEFAULT_PANEL_ROLE: Role = 'editor'
/** `access.admin` for a staff collection: the account holds at least `minimum`. */
export const panelAccess =
(minimum: Role = DEFAULT_PANEL_ROLE): NonNullable<NonNullable<CollectionConfig['access']>['admin']> =>
({ req }) =>
hasMinimumRole(req.user, minimum, req.payload)
/** `accounts.panelRole` → the role to require, or null when the rule is off (`false`, or the lowest role). */
export function resolvePanelRole(option: false | Role | undefined): null | Role {
if (option === false) {return null}
const role = option ?? DEFAULT_PANEL_ROLE
if (!ROLE_HIERARCHY.includes(role)) {
throw new Error(`[ipal] accounts.panelRole must be one of ${ROLE_HIERARCHY.join(', ')} or false, got ${JSON.stringify(option)}.`)
}
return role === ROLE_HIERARCHY[0] ? null : role
}
/**
* Sets `access.admin` on the collection that carries the roles, unless the project wrote its
* own (`access.admin` on its collection, or `access.authAccess.admin` for the injected one).
*/
export function applyPanelRole(config: Config, slug: string, minimum: Role): Config {
return {
...config,
collections: (config.collections ?? []).map((collection) =>
collection.slug === slug && !collection.access?.admin ? { ...collection, access: { ...collection.access, admin: panelAccess(minimum) } } : collection,
),
}
}
/**
* How many accounts of the collection are below the panel role (no role, or only lower ones),
* and how many there are. For the start-up line that tells a site what the rule did to it.
*/
export async function countBelowPanelRole(payload: BasePayload, slug: string, minimum: Role): Promise<{ all: number; below: number }> {
const allowed = ROLE_HIERARCHY.slice(ROLE_HIERARCHY.indexOf(minimum))
const all = (await payload.count({ collection: slug as never, overrideAccess: true })).totalDocs
const able = (await payload.count({ collection: slug as never, overrideAccess: true, where: { roles: { in: [...allowed] } } as never })).totalDocs
return { all, below: Math.max(0, all - able) }
}
+17
View File
@@ -8,6 +8,23 @@ export const ROLE_HIERARCHY = ['user', 'editor', 'admin'] as const
export type Role = (typeof ROLE_HIERARCHY)[number]
/**
* `ipalKit({ accounts })` — rules for the accounts of the staff collection. On by default
* where they apply; each one has its own switch, `accounts: false` turns them all off.
*/
export type AccountsOptions = {
/**
* The lowest role that may open the admin panel. Default `'editor'`: an
* account that holds only `user` (the default role of a new account) sees
* Payload's "Unauthorized" screen instead of the panel; over the API it
* still gets what its role allows. `'user'` or `false` = every account of
* the collection may open the panel, as before. `'admin'` = admins only.
* Needs `access` (the roles). The project's own `access.admin` on the
* collection (or `access.authAccess.admin`) wins. docs/access.md.
*/
panelRole?: false | Role
}
/**
* Access-control options.
*