Users and Access

Access Control

Roles, rules and API keys.

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:

RoleCan
adminEdit content and manage users
editorEdit 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:

tenon.config.ts
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:

cms/collections/posts.ts
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:

RuleWho may
trueEveryone, also visitors without a session
falseNo one
A list of rolesSigned-in users with one of these roles
A functionWhoever the function returns true for

access works in four places:

WhereOperations
A collectionread, create, update, delete
A globalread, update
The media libraryread, create, update, delete
A fieldread, 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.

A list of roles has no exception for admins. ["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:

ArgumentValue
userThe signed-in user with id and role, or null
apiKeyThe API key of the request, or undefined
cmsThe local API, to read other content
collection, globalThe name of the target
localeThe language of the operation
idThe document, for read, update and delete
dataThe input, for create and update
drafttrue 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:

CallerRules
The adminApplied
Your pages: Astro.locals.tenon and Nuxt's useTenon()Applied, for the visitor or editor of the request
cms.withSession(request), as in TanStack StartApplied, for the visitor or editor of the request
The HTTP API and createClient()Applied
The cms you import, and the cms that hooks getSkipped
Astro.locals.tenon on a prerendered pageSkipped. 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 answers 403.
  • 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);
A page that shows content your rules hide from visitors MUST read through 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:

cms/collections/posts.ts
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 where checks equal values of top-level fields, and of createdBy and updatedBy.
  • Every document stores createdBy and updatedBy: 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.