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:
co-authored by
Claude Fable 5.2
parent
463fd5d664
commit
cd60b73685
@@ -2,6 +2,7 @@ import type { BasePayload, Config, PayloadRequest, Plugin } from 'payload'
|
||||
|
||||
import { deepMergeSimple } from 'payload/shared'
|
||||
|
||||
import type { AccountsOptions, Role } from './modules/access/types.js'
|
||||
import type { ResolvedAdminMeta } from './modules/branding/adminMeta.js'
|
||||
import type { IpalOptions, TotpPluginFactory } from './types.js'
|
||||
|
||||
@@ -12,6 +13,7 @@ import { applyGlobalOverrides } from './globals/overrides.js'
|
||||
import { buildSiteIntegrations } from './globals/SiteIntegrations/index.js'
|
||||
import { buildSiteSettings } from './globals/SiteSettings/index.js'
|
||||
import { adminOnly, adminOrSelfIn, injectRoles, registerStaffCollections, staffCollectionSlugs } from './modules/access/index.js'
|
||||
import { applyPanelRole, countBelowPanelRole, resolvePanelRole } from './modules/access/panelRole.js'
|
||||
import { applyAdminI18n } from './modules/admin-i18n/apply.js'
|
||||
import { buildAdminImageEndpoints } from './modules/branding/adminImages.js'
|
||||
import { buildAdminMeta, DEFAULT_AGENCY, syncAdminMeta } from './modules/branding/adminMeta.js'
|
||||
@@ -194,6 +196,10 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
/** 2FA collection when "setup required" is on — its existing accounts get their setup window in onInit. */
|
||||
let setupRequiredFor: null | string = null
|
||||
let setupWindowHours = 0
|
||||
/** accounts.panelRole in force: the collection and the role, for the start-up line. */
|
||||
let panelRoleFor: { role: Role; slug: string } | null = null
|
||||
/** `accounts: false` turns every account rule off. */
|
||||
const accounts: AccountsOptions | null = options.accounts === false ? null : (options.accounts ?? {})
|
||||
/** requireSetup.setupNotice / statusField (on unless switched off). */
|
||||
let setupNotice = true
|
||||
let statusField = false
|
||||
@@ -550,6 +556,14 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
// for accounts of a staff collection (staffCollectionSlugs).
|
||||
const staff = [...new Set([options.access.authCollection, ...(options.access.staffCollections ?? [])])]
|
||||
config.custom = { ...config.custom, ipal: { ...config.custom?.ipal, staffCollections: staff } }
|
||||
// Account rules (accounts.*): the lowest role stays out of the panel.
|
||||
const panelRole = accounts ? resolvePanelRole(accounts.panelRole) : null
|
||||
const rolesSlug = options.access.authCollection
|
||||
// the project's own access.admin (or access.authAccess.admin) wins
|
||||
if (panelRole && !(config.collections ?? []).find((c) => c.slug === rolesSlug)?.access?.admin) {
|
||||
config = applyPanelRole(config, rolesSlug, panelRole)
|
||||
panelRoleFor = { slug: rolesSlug, role: panelRole }
|
||||
}
|
||||
}
|
||||
// For predicates called without `req.payload` (isAdmin(req.user) in a hook): what this config
|
||||
// says now, under a token; the Payload instance takes the entry over when the config goes live
|
||||
@@ -836,6 +850,25 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
||||
)
|
||||
}
|
||||
}
|
||||
// The panel role: tell a site how many of its accounts the rule keeps out of the panel.
|
||||
if (panelRoleFor) {
|
||||
try {
|
||||
const { all, below } = await countBelowPanelRole(payload, panelRoleFor.slug, panelRoleFor.role)
|
||||
if (all && below === all) {
|
||||
payload.logger.error(
|
||||
`[ipal] No account of "${panelRoleFor.slug}" has the role "${panelRoleFor.role}" or higher, so NOBODY can open the admin panel ` +
|
||||
'(accounts.panelRole). Give an account the role with a script (payload.update, Local API), or set accounts: { panelRole: false } and redeploy (docs/access.md "Account rules").',
|
||||
)
|
||||
} else if (below) {
|
||||
payload.logger.warn(
|
||||
`[ipal] ${below} account(s) of "${panelRoleFor.slug}" are below the role "${panelRoleFor.role}" and cannot open the admin panel ` +
|
||||
'(accounts.panelRole). Give them a higher role, or set accounts: { panelRole: \'user\' } (docs/access.md "Account rules").',
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
/* a database that is not ready must not stop the start */
|
||||
}
|
||||
}
|
||||
// Accounts from before "setup required": their window starts now, not at their creation date.
|
||||
if (setupRequiredFor) {
|
||||
const started = await backfillSetupFrom(payload, setupRequiredFor)
|
||||
|
||||
Reference in New Issue
Block a user