what

Authorization

Control who can create, read, update, and delete each collection (with record ownership, tenant scoping, and field-level rules) from a few lines of what.toml. No policy code, no middleware.

Declare every collection your site writes

In production (run-what dev --production, or a bundled app), a collection takes writes only when what.toml has a [collections.<name>] section for it. A create, update, delete, publish or upload aimed at any other name is refused before anything is stored. An enhanced request (a form or button the What client sends, X-Requested-With: What) gets 403; a plain HTML form is redirected back (303) and shows the flash error “You don't have permission to change that.” Either way, no table, record or file is created, and existing data stays as it was. Reads are not affected. In development (run-what dev) an undeclared collection still takes writes, so you can sketch first and declare before you ship.

The default: owner-protected

The smallest declaration is an empty section, [collections.notes]. It gets this policy:

create = "all"      # anyone may create
update = "owner"    # only the record's owner may update
delete = "owner"    # only the record's owner may delete
read   = "all"      # everyone may read

When a record is created, the framework stamps an _owner onto it: user:<id> if the visitor is authenticated, otherwise session:<id>. Update and delete then verify ownership server-side, so a visitor cannot edit or delete someone else's record even by guessing its id. This is real enforcement, not a display filter.

Upgrading from an earlier version? Records with no _owner (created before 1.4, or written straight into the database) give nobody an ownership claim. In production, once the collection is declared, owner rules refuse their update and delete; in development an undeclared collection still lets anyone edit them. To edit such records, grant an editor role (update = "owner, editor") or backfill _owner from a source you trust. Apps where different visitors intentionally edit shared records should opt out. See shared collections.

Declaring a policy

Add a [collections.<name>] section to what.toml:

[collections.notes]
create = "all"
update = "owner"
delete = "owner, admin"     # the owner OR anyone with the admin role
read   = "owner"            # each visitor sees only their own notes

Each rule is a comma-separated list of terms, matched as OR:

TermMatches
allAnyone, including anonymous visitors
userAny authenticated user
ownerThe record's owner (not valid for create)
noneNo one
<role>A user holding that JWT role, e.g. admin

Reserved words (all, user, owner, none) can't be combined with other terms or used as role names, and mistakes fail loud when the server starts.

Read scoping

When read is anything other than all, the framework forces a WHERE clause onto every fetch of that collection, so you don't filter manually. read = "owner" restricts results to the current visitor's records:

<what>
fetch.notes = "local:notes"
</what>

<loop data="#notes#" as="n">#n.title#</loop>   <!-- only YOUR notes -->

This works across SQLite, Cloudflare D1, and Supabase, and it also removes the collection from the whole-store context so it can't leak through an un-directed loop. On a static build (no server, no session) a scoped collection renders empty.

Ownership in templates

#user.owner# holds the current visitor's owner key, so you can show controls only to the owner:

<if note._owner == user.owner>
  <form method="post" action="/w-action/notes/#note.id#?w-action=delete">
    <button>Delete</button>
  </form>
</if>

Multi-tenant scoping

A filter scopes a collection by any field, resolved per request from #user.*# or #session.*#. It's AND-ed into every read and checked on every update/delete by id, so one tenant can neither see nor modify another's rows:

[collections.orders]
filter = "org_id=#user.org_id#"

If the variable can't be resolved (no logged-in user), the collection fails closed: it returns nothing rather than everything.

Field rules

[collections.products]
fields.readonly = ["price"]     # ignored if sent by the client
fields.private  = ["cost"]      # never rendered into templates

readonly fields are stripped from create/update input (the server owns their value); private fields are stripped from records before they reach any template.

They're independent. readonly means unwritable; private means unreadable. A private field can still be set by a client form. It's only hidden from output. A field that must be both server-controlled and hidden (e.g. role, credits) must be listed in both readonly and private.

Shared collections

For a collaborative board, a wiki, or a guestbook where anyone edits anyone's entries, opt out of ownership:

[collections.wiki]
update = "all"
delete = "all"
# or turn ownership off entirely:
# owner = "none"

State mutations

Shared state written with w-set can be role-gated too. A [role] bracket on a data.wired or data.application declaration restricts who may write the key (not just who receives updates):

<!-- application.what -->
data.wired = ["revenue [admin]"]

<!-- only an admin's w-set succeeds; others get 403 -->
<button w-set="wired.revenue += 100">Book revenue</button>

app.revenue and wired.revenue are one stored value, so the scope gates writes under both names. If two application.what files give one key different scopes, nobody may write it or receive it live until they agree, and the log names both declarations.