# Access control in kywi.config.ts

URL: https://kywi-docs-production.up.railway.app/developing/access-control-in-kywiconfigts
Type: docPage

Type-level and field-level predicates are functions, so they live only in code — they receive the user and nothing else, and absence always means allowed.

A content type's `access` block is four optional predicates, and each one is handed the caller and nothing else — no content node, no site id, no request. That constraint is the design, not an oversight: these run before core has decided which row it is dealing with, so they can only ever answer questions about who is asking.

interface AccessControlConfig { read?: (ctx: { user: UserContext | null }) => boolean write?: (ctx: { user: UserContext | null }) => boolean publish?: (ctx: { user: UserContext | null }) => boolean delete?: (ctx: { user: UserContext | null }) => boolean }

Synchronous, boolean, and independent of each other. A rule that needs the row it is about — "only the author may edit this page" — is a per-document permission or a `beforeUpdate` hook, not one of these.

## Absence means allowed

No `access` block returns `true`. An `access` block with no function for the action being attempted returns `true`. These predicates only ever TIGHTEN: passing one drops the caller into the ordinary role check, so writing `write: () => true` grants nothing an editor did not already have, and omitting `publish` while restricting `write` leaves publishing on the role ladder alone.

defineContentType({ name: 'invoice', // … access: { // Both actions still pass the role check afterwards — this narrows, never widens. read: ({ user }) => user?.role === 'superAdmin' || user?.groups.includes('finance') === true, write: ({ user }) => user?.groups.includes('finance') === true, }, })

## kywi.config.ts

They are evaluated at the content routes, one call per action: reading a list and reading one node, the three write doors (POST, PUT, PATCH), delete, and both publish routes. A type whose `read` denies is not filtered out of a listing — the listing route itself refuses, with a 401 for an anonymous caller and a 403 for a signed-in one.

## Field-level access is a separate layer

A `FieldDefinition` carries its own `access` with `read` and `write`, evaluated per key rather than per request. On the way out, denied keys are dropped from the record silently. On the way in, they are a 403 `FORBIDDEN_FIELD` whose body names the fields the caller tried to change.

## How a denied field resolves

What each combination of field definition and predicate does to one key in a create or update payload. The last row is the one that makes the feature usable at all: an admin form posts every field back on every save, so treating a denied field's own stored value as an attempted write would make a read-only field unsaveable for everyone who can see it. Equality is type-aware — dates compare by timestamp, objects by their JSON.

## Bulk writes forgive nothing

The bulk create and update endpoints hold no stored document per item to diff against, so there is no echo to forgive: the mere presence of a write-denied field in any item refuses the entire batch. A client that round-trips whole records works against the single-node routes and fails against the bulk ones with the same payload.

## Where these sit relative to per-document permissions

Config predicates gate the TYPE. Per-document permissions gate one node, out of ACL rows in the database, and follow their own rules: superAdmin and siteAdmin bypass them; a node with no rows is open; a node with rows and an anonymous caller is denied. Mutations additionally require read on the node first — you cannot publish or delete something you are not allowed to see. The two layers compose, and neither is a substitute for the other: see [Content-level permission overrides](/governing/content-level-permission-overrides/) for the ACL half and [Roles and the capability matrix](/governing/roles-and-the-capability-matrix/) for the ladder underneath both.

## These live only in code

They are functions, so nothing serialises them. The Type Designer cannot express an access predicate, and a type created there has none — which is one of the real differences between a config type and a database type, covered in [Config types, Type Designer types, and extension attributes](/developing/config-types-type-designer-types-and-extension-attributes/).

## The validator knows only read and write

The zod schema behind `parseKywiConfig` declares `access` as `{ read?, write? }`, and zod strips unknown keys — so `publish` and `delete` disappear from a parsed config. Your running app is unaffected, because it uses the config object itself; what is affected is anything that reads a config THROUGH the parser, including the CLI and the MCP developer server. Do not conclude from a parsed config that a type has no publish rule.
