Permissions

Role-based access control defined in code.

Roles and permissions are defined in your cms.config.ts — not stored in the database. This keeps them version-controlled, reviewable, and deployable.

Defining roles

          import { role } from "@cms/config";

// Full admin — bypasses all permission checks
const admin = role("admin", {
  isAdmin: true,
});

// Authenticated role with granular permissions
const editor = role("editor", {
  permissions: {
    posts: {
      create: true,
      read: true,
      update: {
        filter: { author: "$CURRENT_USER" },
        fields: { exclude: ["status"] },
      },
      delete: false,
    },
    _image: {
      create: true,
      read: true,
      update: true,
      delete: false,
    },
    _video: {
      create: true,
      read: true,
      update: true,
      delete: false,
    },
    _file: {
      create: true,
      read: true,
      update: true,
      delete: false,
    },
  },
});

// Public role — unauthenticated API access
const publicRole = role("public", {
  public: true,
  permissions: {
    posts: {
      read: {
        filter: { status: "published" },
        fields: { exclude: ["internalNotes"] },
      },
    },
    pages: {
      read: {
        filter: { status: "published" },
      },
    },
    // Without these, image(), video() and file() relations resolve to bare
    // { id, _entity } references for this role.
    _image: { read: true },
    _video: { read: true },
    _file: { read: true },
  },
});
        

Assets are documents of _image, _video and _file, read with the requester’s role like any other entity: a role that can’t read them gets asset relations back unresolved. See Reading assets through a relation.

The running server, cms dev included, keeps the roles it started with. Restart it after changing one.

Permission rules

Each CRUD operation can be:

  • true — allowed with no restrictions
  • false — denied
  • An object with filter and/or fields — allowed with constraints
          {
  create?: boolean | PermissionRule;
  read?: boolean | PermissionRule;
  update?: boolean | PermissionRule;
  delete?: boolean | PermissionRule;
}
        

Item-level filters

Filters restrict which records the role can access. They’re appended as WHERE clauses automatically:

          update: {
  filter: { author: "$CURRENT_USER" },
}
        

A public request to GET /api/posts with filter: { status: "published" } automatically adds WHERE status = 'published'.

On writes, the filter scopes the result too. A create, update or draft save whose written document doesn’t match the role’s filter returns 404 and writes nothing, so an editor with create: { filter: { author: "$CURRENT_USER" } } can’t create a post authored by someone else, and with the same update filter can’t hand their post to another author. Bulk writes apply this per row, and one out-of-scope row rolls back the whole batch. A draft save on a published document doesn’t change the live document, so its result isn’t checked until that draft is published.

A role’s read filter also applies when another entity follows a relation to this one. Both filtering (?filter[author.name]=…) and sorting (?sort=author.name) AND the target’s read filter into their subquery, so a relation can never surface rows the role couldn’t read directly — and following a relation to an entity the role can’t read at all is rejected.

Field-level restrictions

Control which fields are readable or writable:

          read: {
  fields: { exclude: ["internalNotes", "draftContent"] },
}
        

Excluded fields are stripped from API responses and dropped from create/update input. A restore of an older version leaves them alone too: they keep their current value, not the version’s. The exclusion follows the entity wherever its documents appear: a relation resolved from another entity (?resolve[author]=*) and the slug in _translations leave them out too.

A request can’t filter or sort on an excluded field, directly or through a relation (?filter[author.email]=…): it gets the same 400 as an unknown field, since the matching rows and their order would reveal the value. The role’s own filter may still reference fields it excludes.

Variables

Use variables in permission filters for dynamic values:

Variable Resolves to
$CURRENT_USER The authenticated user’s ID
$CURRENT_ROLE The authenticated user’s role name
$NOW Current ISO 8601 timestamp
          // Only allow editing posts you authored
update: {
  filter: { author: "$CURRENT_USER" },
}

// Only show published items or items where the user is the author
read: {
  filter: {
    $or: [
      { status: "published" },
      { author: "$CURRENT_USER" },
    ],
  },
}
        

How permissions are enforced

Permissions are applied at the query layer — every API request is scoped by the user’s role:

  1. CRUD booleans gate the entire operation. If delete: false, the DELETE endpoint returns 403.
  2. Item-level filters are merged into the query’s WHERE clause, and every write’s result is checked against them. The user never sees, reaches or produces records outside their filter scope.
  3. Field-level restrictions control which columns appear in SELECT and which are accepted in INSERT/UPDATE.

Admin roles (isAdmin: true) bypass all checks.

Unauthenticated requests use the role marked public: true. If no public role exists, unauthenticated requests get 401.

An API key or session whose role is no longer in the config (renamed or removed) gets 401, never the public role. The engine logs a warning naming the missing role.

Managing users

Users are the _user entity, so a role’s _user permissions decide which users it can see, create, change and delete. Filter on role to limit the roles it may hand out: a write can neither reach nor produce a user outside the filter.

          const manager = role("manager", {
  permissions: {
    _user: {
      read: true,
      create: { filter: { role: { $in: ["editor", "customer"] } } },
      update: { filter: { role: { $in: ["editor", "customer"] } } },
      delete: { filter: { role: "customer" } },
    },
  },
});
        

Whatever its filter, a role that isn’t an admin never creates, edits or deletes a user with an admin role: those writes return 404 and change nothing. Only admins (and the system, ctx.system) manage admins.

To let users edit their own name but not their role, scope update to themselves and pin the role in the filter, so a write that changes it falls outside:

          const customer = role("customer", {
  adminPanel: false,
  permissions: {
    _user: {
      read: { filter: { id: "$CURRENT_USER" } },
      update: { filter: { id: "$CURRENT_USER", role: "customer" } },
    },
  },
});
        

Admin access

adminPanel: false keeps a role’s users out of the admin: they can sign in there, but only see that they have no access. Use it for an app’s own users, such as customers. It changes nothing in the API, where the role’s permissions still apply.

Customer accounts

To let people create their own account, give the public role signup with the role new users get. It must be a role with adminPanel: false that isn’t an admin or public role:

          const customer = role("customer", {
  adminPanel: false,
  permissions: {
    _user: { read: { filter: { id: "$CURRENT_USER" } } },
    orders: { read: { filter: { customer: "$CURRENT_USER" } } },
  },
});

const publicRole = role("public", {
  public: true,
  permissions: { products: { read: true } },
  signup: { role: "customer" },
});
        

The site asks for a code with signup: true and verifies it as for a sign-in. A new email gets its user then, with the signup role; an email that already has a user just signs in. The client never chooses the role.

          await cms.auth.request(email, { signup: true, locale }); // locale: optional, the site's current one
const { user, created } = await cms.auth.create({ authRequestId, code, name });
        

Without the flag, an unknown email gets no code, so the admin’s login (which never sends it) can’t create accounts.

To decide who may sign up, add allow. It gets the email, lowercased, and the server context, so it can check a domain or read an invite:

          signup: {
  role: "customer",
  allow: (email) => email.endsWith("@acme.com"),
},
        

A refused email gets the same response as any unknown one, and no code. allow runs again before the user is created, so tightening it also stops codes already sent.

Customer profiles

Signup only proves the email and creates the user. Anything else about a customer (phone, address, company) goes on fields you add to _user, and the customer fills them in afterwards, with an ordinary update under their own role. Exclude the fields they mustn’t write:

          const customer = role("customer", {
  adminPanel: false,
  permissions: {
    _user: {
      read: { filter: { id: "$CURRENT_USER" } },
      update: {
        filter: { id: "$CURRENT_USER", role: "customer" },
        fields: { exclude: ["plan", "stripeCustomerId"] },
      },
    },
  },
});
        

The auth routes return who signed in (id, email, name, role), not the profile. Read the profile through _user, which applies the role’s permissions, and send the customer to an onboarding step while it’s incomplete. Which fields make a profile complete is your app’s call:

          const { user } = await cms.auth.create({ authRequestId, code });
const { data: profile } = await cms.get("_user", user.id);
if (!profile.phone) {
  // onboarding: collect the rest, then
  await cms.update("_user", user.id, { phone, address });
}
        

A value that must exist from the start, such as an organisation matched by email domain, belongs in the extension’s beforeCreate: it runs on signup, on the server, so the client can’t fake it.

Granting the public role _user create, update or delete instead lets anyone write users without proving an email; the config warns about it.

Permissions that aren’t tied to an entity

A few actions belong to no entity. They sit on the role itself, next to permissions, and are off unless granted. Admin roles hold all of them.

          const mediaManager = role("media-manager", {
  permissions: { posts: { read: true } },
  assets: { cleanup: true }, // may delete unused media, see the Assets guide
});
        

Entities without permissions

If an entity is not mentioned in a role’s permissions, that role has no access to it. Permissions are deny-by-default.

Previous

Querying

Next

Hooks