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:
Radosław Smoliński
2026-12-01 10:39:41 +01:00
co-authored by Claude Opus 5.5
parent fc70a34f3e
commit 4b17254135
10 changed files with 252 additions and 12 deletions
+38 -4
View File
@@ -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
})
}
/**
+3
View File
@@ -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'