Access Control
Access control decides who may read and change what. Each user has a role, and rules on collections, globals, media and fields check that role. The server checks the rules everywhere: in the admin, on your site and in the HTTP API. Scripts and other services use API keys instead of a user.
Users and roles
Every user has one role. Two roles are built in:
| Role | Can |
|---|---|
admin | Edit content and manage users |
editor | Edit content |
- The first user is an admin. Later users are editors, unless an admin picks another role.
- Only admins see Users and API keys in the admin.
- Your rules can limit what each role does with content.
Add your own roles in roles. The label shows where an admin picks the role:
export default defineCms({
// ...
roles: {
author: { label: { en: "Author", de: "Autor" }, description: "Writes posts" },
},
});
A role name MUST use only a-z, 0-9 and -. admin and editor are built in, so do not list them.
Add users
Admins add users under Manage → Users. The + button has two options:
- Invite asks for an email and a role. The person gets a link by email. Without email, the admin shows the link to copy and send yourself.
- Set password creates the user right away, with a name, a password and a role. Pass the password on yourself.
The invite link opens a page where the person picks a name and a password, or another sign-in method. A link works once and expires after 7 days. Resend makes a new link and stops the old one.
From the command line, npx tenon users create <email> sends an invite, and --password creates the user right away. See tenon users.
Manage users
A click on a user in Users opens it. An admin can:
- change the role
- send a password reset
- sign the user out on every device
- deactivate the user. They cannot sign in until an admin reactivates them.
- remove the user. Their content stays.
The last active admin cannot lose the admin role or be removed.
Rules
access sets who may do what. Each key is an operation, and each value is a rule:
export const posts = defineCollection({
access: {
create: ["admin", "editor", "author"],
delete: ["admin", "editor"],
},
fields: {
title: { type: "text", required: true },
note: { type: "text", access: { read: ["admin"], update: ["admin"] } },
},
});
A rule is one of these:
| Rule | Who may |
|---|---|
true | Everyone, also visitors without a session |
false | No one |
| A list of roles | Signed-in users with one of these roles |
| A function | Whoever the function returns true for |
access works in four places:
| Where | Operations |
|---|---|
| A collection | read, create, update, delete |
| A global | read, update |
| The media library | read, create, update, delete |
| A field | read, create, update |
For media, set the rules in media.access of defineCms().
The default
Without a rule, everyone reads published content, and signed-in users do the rest. A rule replaces this default for its operation. So create: true accepts a create without a session, and read: ["admin"] hides the content from visitors.
["editor"] keeps admins out. Add "admin" to the list when admins need access.Rule functions
A function gets the caller and the operation, and returns true or false. This example lets visitors read published posts, and only admins read drafts:
access: {
read: ({ user, draft }) => !draft || user?.role === "admin",
},
It gets these arguments:
| Argument | Value |
|---|---|
user | The signed-in user with id and role, or null |
apiKey | The API key of the request, or undefined |
cms | The local API, to read other content |
collection, global | The name of the target |
locale | The language of the operation |
id | The document, for read, update and delete |
data | The input, for create and update |
draft | true when a read asks for drafts |
update also covers saving a draft, publishing, unpublishing and scheduling. A scheduled publish runs later as the server, so the rule is checked when it is scheduled.
Where the rules apply
The server checks the rules on every read and write, whoever asks:
| Caller | Rules |
|---|---|
| The admin | Applied |
Your pages: Astro.locals.tenon and Nuxt's useTenon() | Applied, for the visitor or editor of the request |
cms.withSession(request), as in TanStack Start | Applied, for the visitor or editor of the request |
The HTTP API and createClient() | Applied |
The cms you import, and the cms that hooks get | Skipped |
Astro.locals.tenon on a prerendered page | Skipped. The page renders at build time, without a request. |
The result is the same for every caller where the rules apply:
- A read leaves out documents the caller may not read. So do relations and links in rich text.
- A field the caller may not read is missing from the result. A filter on it answers
400. - A field the caller may not change keeps its stored value. The input for it is dropped.
- Denied without a session answers
401. Denied with one answers403. - The admin also hides what the user may not read, and shows read-only what they may not change.
withSession() returns an instance for the user of the request. Use it when your own server code acts for a visitor or an editor:
const tenon = await cms.withSession(request);
// Throws ForbiddenError when the user may not update posts.
await tenon.update("posts", id, data);
withSession() or HTTP, and MUST NOT be prerendered. The cms you import reads everything.Limit to some documents
A rule for read, update or delete can return a where instead of true. The caller then only reaches the documents that match.
This example lets authors change only their own posts. Everyone else may change all of them:
export const posts = defineCollection({
access: {
update: ({ user }) => (user?.role === "author" ? { createdBy: user.id } : true),
delete: ({ user }) => (user?.role === "author" ? { createdBy: user.id } : true),
},
fields: {
title: { type: "text", required: true },
},
});
- Reads only return matching documents. An update or delete of another document is denied.
- A
wherechecks equal values of top-level fields, and ofcreatedByandupdatedBy. - Every document stores
createdByandupdatedBy: the ids of the users who created it and changed it last. Visitors without a session do not get them.
API keys
A script or another service uses an API key instead of a user. An admin picks what each key may do, and your rules still run for it. Keys and rules shows how the two combine.
Related resources
- Authentication: sign-in methods and recovery.
- API keys: create keys and set what they may do.
- HTTP API and client: what is public, and how to call the API.
- Headless: read and write from another origin with a key.
- Hooks: run code before and after writes.