import { type Access, type AccessArgs, type AuthUser, isStaff, type Where } from './access.js' import { QueryError } from './errors.js' import { type Field, hasRows, rowFields } from './fields.js' import { blockFields, blockNodes, blockOf, hasBlocks } from './rich-text-blocks.js' type Data = Record /** Runs an access function; a missing one means "logged-in users only" (FR-ACL-03). */ export async function evaluateAccess( access: Access | undefined, args: AccessArgs, ): Promise { const result = await (access ?? isStaff)(args) if (typeof result === 'boolean') return result if (typeof result === 'object' && result !== null) return result throw new QueryError('Access functions must return a boolean or a where query') } /** Combines a query with an access constraint. */ export function andWhere(where: Where | undefined, constraint: true | Where): Where | undefined { if (constraint === true) return where return where === undefined ? constraint : { and: [where, constraint] } } /** * Field-level access, evaluated once per field for one operation. * `null` user means the request is not logged in. */ export class FieldAccessChecker { private readonly cache = new Map() /** `rules`: the user's role's rules for top-level fields (`auth.rbac`), on top of `access`. */ constructor( private readonly kind: 'read' | 'create' | 'update', private readonly args: AccessArgs, private readonly rules?: ReadonlyMap, ) {} async allows(field: Field): Promise { const rule = this.rules?.get(field) if (rule === 'hidden' || (rule === 'read' && this.kind !== 'read')) return false const fn = this.kind === 'create' ? (field.access?.create ?? field.access?.update) : field.access?.[this.kind] if (!fn) return true let allowed = this.cache.get(field) if (allowed === undefined) { allowed = (await fn(this.args)) === true this.cache.set(field, allowed) } return allowed } } /** A rich text document with each block's fields passed through `map` (#328); a copy. */ async function mapBlocks( field: Field & { type: 'richText' }, doc: unknown, map: (fields: readonly Field[], data: Data) => Promise, ): Promise { const copy = structuredClone(doc) for (const node of blockNodes(copy)) { const block = blockOf(field, node) if (block) node.attrs = { ...node.attrs, fields: await map(block.fields, blockFields(node)) } } return copy } /** * Removes fields the caller may not see: hidden fields always, * fields whose `access.read` denies when a checker is given. */ export async function stripFields( fields: readonly Field[], doc: Data, read?: FieldAccessChecker, ): Promise { const result: Data = { ...doc } for (const field of fields) { if (!(field.name in result)) continue if (field.hidden || (read && !(await read.allows(field)))) { delete result[field.name] continue } const value = result[field.name] if (field.type === 'group' && value && typeof value === 'object') { result[field.name] = await stripFields(field.fields, value as Data, read) } else if (field.type === 'richText' && hasBlocks(field, value)) { result[field.name] = await mapBlocks(field, value, (f, d) => stripFields(f, d, read)) } else if (hasRows(field) && Array.isArray(value)) { result[field.name] = await Promise.all( value.map((row) => { const fields = rowFields(field, row) return fields && row && typeof row === 'object' ? stripFields(fields, row as Data, read) : row }), ) } } return result } /** * Removes input the caller may not set: hidden fields always, * fields whose `access.update` denies when a checker is given. */ export async function filterInput( fields: readonly Field[], data: Data, update?: FieldAccessChecker, ): Promise { const result: Data = { ...data } for (const field of fields) { if (!(field.name in result)) continue if (field.hidden || (update && !(await update.allows(field)))) { delete result[field.name] continue } const value = result[field.name] if (field.type === 'group' && value && typeof value === 'object' && !Array.isArray(value)) { result[field.name] = await filterInput(field.fields, value as Data, update) } else if (field.type === 'richText' && hasBlocks(field, value)) { result[field.name] = await mapBlocks(field, value, (f, d) => filterInput(f, d, update)) } else if (hasRows(field) && Array.isArray(value)) { result[field.name] = await Promise.all( value.map((row) => { const fields = rowFields(field, row) return fields && row && typeof row === 'object' ? filterInput(fields, row as Data, update) : row }), ) } } return result } export type { AuthUser }