readPublished on version history: readPublishedVersions, and the documented form works (1.17.2)
readVersions: readPublished(), as access.md showed, answered every version read with 500 on MongoDB, Postgres and SQLite: the rule returns the document's `_status` path, a version query runs on the versions collection where the status is `version._status` (SQL: "Cannot find field for path at _status"; MongoDB: TypeError in sanitizeQueryValue). Reported by a client project upgrading to 1.17.1. - readPublishedVersions(minimum): the version form, for readVersions. - readPublished() carries a symbol mark (READ_PUBLISHED); iapl() swaps it on readVersions of the collections and globals it sees (withVersionReadRules, exported for plugins listed after it). - Tests on the three adapters: the version list (REST, depth 0/2, Local API, GraphQL), one version by id, the count, a global, an explicit role; the 1.17.1 HTTP test now checks the status too. - access.md says where each rule goes; AGENT.md, iapl-collection and iapl-audit skills. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fc70a34f3e
commit
4b17254135
@@ -15,12 +15,15 @@ export {
|
||||
injectRoles,
|
||||
isAdmin,
|
||||
isEditor,
|
||||
READ_PUBLISHED,
|
||||
readPublished,
|
||||
readPublishedVersions,
|
||||
requireRole,
|
||||
requireRoleField,
|
||||
ROLE_HIERARCHY,
|
||||
staffCollectionSlugs,
|
||||
userMeetsRole,
|
||||
withVersionReadRules,
|
||||
} from './modules/access/index.js'
|
||||
export { applyPanelRole, countBelowPanelRole, DEFAULT_PANEL_ROLE, panelAccess, resolvePanelRole } from './modules/access/panelRole.js'
|
||||
export { applyPasswordRule, buildPasswordRuleHook, checkPassword, DEFAULT_PASSWORD_MIN_LENGTH, resolvePasswordRule } from './modules/access/passwordRule.js'
|
||||
|
||||
@@ -21,17 +21,51 @@ export const requireRole = (minimum: Role): Access => {
|
||||
return ({ req }) => hasMinimumRole(req.user, minimum, req.payload)
|
||||
}
|
||||
|
||||
/** Marks a `readPublished()` rule, so the plugin can give `readVersions` the version form of it (a symbol, not a name). */
|
||||
export const READ_PUBLISHED = Symbol.for('iapl.readPublished')
|
||||
|
||||
/**
|
||||
* Read rule for a collection with drafts: staff of `minimum` role or higher (default editor) read everything,
|
||||
* drafts and versions included; everybody else (visitors, plain `user` accounts, a session that still owes its
|
||||
* Read rule for a collection (or a global) with drafts: staff of `minimum` role or higher (default editor) read
|
||||
* everything, drafts included; everybody else (visitors, plain `user` accounts, a session that still owes its
|
||||
* second factor) reads published documents only — by id, in lists, with `?draft=true` and over GraphQL. Payload's
|
||||
* open `read: () => true` answers `?draft=true` with the draft itself (IAWL idea 23).
|
||||
*
|
||||
* access: { read: readPublished(), readVersions: readPublished() }
|
||||
* For `read`. Version history (`readVersions`) asks for the status at another path (`version._status`): use
|
||||
* `readPublishedVersions()` there. Given to `readVersions` of a collection or global that iapl() sees, this rule
|
||||
* is swapped for `readPublishedVersions()` with the same role (1.17.2; in 1.17.1 it answered 500 there).
|
||||
*
|
||||
* access: { read: readPublished(), readVersions: readPublishedVersions() }
|
||||
*/
|
||||
export const readPublished = (minimum: Role = 'editor'): Access => {
|
||||
assertKnownRole('readPublished', minimum)
|
||||
return ({ req }) => (hasMinimumRole(req.user, minimum, req.payload) ? true : { _status: { equals: 'published' } })
|
||||
const rule: Access = ({ req }) => (hasMinimumRole(req.user, minimum, req.payload) ? true : { _status: { equals: 'published' } })
|
||||
return Object.assign(rule, { [READ_PUBLISHED]: minimum })
|
||||
}
|
||||
|
||||
/**
|
||||
* The version-history form of `readPublished()`, for `readVersions`: staff of `minimum` role or higher read every
|
||||
* version; everybody else reads published versions only — the list, one version by id, the count, REST, GraphQL,
|
||||
* the Local API with access control. A version query runs on the versions collection, where the status is
|
||||
* `version._status` (a `_status` condition there is an unknown path: 500 on every database adapter).
|
||||
*/
|
||||
export const readPublishedVersions = (minimum: Role = 'editor'): Access => {
|
||||
assertKnownRole('readPublishedVersions', minimum)
|
||||
return ({ req }) => (hasMinimumRole(req.user, minimum, req.payload) ? true : { 'version._status': { equals: 'published' } })
|
||||
}
|
||||
|
||||
type WithAccess = { access?: { readVersions?: unknown } }
|
||||
|
||||
/**
|
||||
* Gives every `readVersions: readPublished()` the version form (`readPublishedVersions()` with the same role).
|
||||
* iapl() runs it on the collections and globals it sees; a plugin listed after iapl() uses
|
||||
* `readPublishedVersions()` itself, or runs this on its own entities.
|
||||
*/
|
||||
export function withVersionReadRules<T extends WithAccess>(entities: T[] | undefined): T[] | undefined {
|
||||
return entities?.map((entity) => {
|
||||
const rule = entity.access?.readVersions as (object & Partial<Record<typeof READ_PUBLISHED, Role>>) | undefined
|
||||
const minimum = rule && typeof rule === 'function' ? rule[READ_PUBLISHED] : undefined
|
||||
return minimum ? { ...entity, access: { ...entity.access, readVersions: readPublishedVersions(minimum) } } : entity
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,9 +6,12 @@ export {
|
||||
adminOrSelf,
|
||||
adminOrSelfIn,
|
||||
authenticated,
|
||||
READ_PUBLISHED,
|
||||
readPublished,
|
||||
readPublishedVersions,
|
||||
requireRole,
|
||||
requireRoleField,
|
||||
withVersionReadRules,
|
||||
} from './access.js'
|
||||
export { injectRoles } from './injectRoles.js'
|
||||
export { clearStaffRegistry, hasMinimumRole, isAdmin, isEditor, registerStaffCollections, staffCollectionSlugs, userMeetsRole } from './predicates.js'
|
||||
|
||||
+5
-1
@@ -12,7 +12,7 @@ import { buildNotifications } from './globals/Notifications/index.js'
|
||||
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 { adminOnly, adminOrSelfIn, injectRoles, registerStaffCollections, staffCollectionSlugs, withVersionReadRules } from './modules/access/index.js'
|
||||
import { applyPanelRole, countBelowPanelRole, resolvePanelRole } from './modules/access/panelRole.js'
|
||||
import { applyPasswordRule, buildPasswordRuleHook, resolvePasswordRule } from './modules/access/passwordRule.js'
|
||||
import { adoptLateAuthCollections, applyFirstUserRoute, applyStaffAccountAccess, markSeenAuthCollections, markStaffRules, staffCollectionsNobodyManages, unknownStaffCollections } from './modules/access/selfRegistration.js'
|
||||
@@ -1093,6 +1093,10 @@ export const iapl = (options: IaplOptions): Plugin => {
|
||||
payload.logger.info('[iapl] Plugin initialized.')
|
||||
}
|
||||
|
||||
// readVersions: readPublished() asks for `version._status` on version history (readPublishedVersions).
|
||||
config.collections = withVersionReadRules(config.collections)
|
||||
config.globals = withVersionReadRules(config.globals)
|
||||
|
||||
// Every auth collection of this config has been seen: what appears later is a late one (adoptConfig).
|
||||
if (accounts) {
|
||||
const panel = config.admin?.user ?? (config.collections ?? []).find((c) => c.auth)?.slug
|
||||
|
||||
Reference in New Issue
Block a user