Rules

Use read and write rules to control which documents each end user can see and change, and field rules to hide individual properties.

Rules are expressions stored on the bucket, in its acl field. They let you implement row-level security for your end users, for example "users can only read their own orders" or "only published posts are public".

JSON
{
  "acl": {
    "read": "document.status == \"published\" || document.owner == auth._id",
    "write": "document.owner == auth._id"
  }
}

When rules run

CallerPolicies checkedRules applied
USER tokenYesYes
IDENTITY tokenYesNo
APIKEYYesNo

Rules are an additional layer for end users. A user still needs a policy that allows the endpoint, such as bucket:data:index for the bucket. Identities and API keys see and write every document their policies allow.

Read rules

The read rule is evaluated for every document a user lists, reads or receives over realtime. Documents for which it evaluates to false are left out of the result.

Text
document.status == "published"

Write rules

The write rule is evaluated for inserts, replaces, patches and deletes made by users. When it evaluates to false, the request fails with status 401 and the message ACL rules has rejected this operation. The rule sees the document as it will be stored.

Text
document.owner == auth._id

To make a bucket read-only for users, deny all writes:

Text
false == true

Available variables

VariableContents
documentThe document being read or written. Relations can be followed, for example document.project.owner.
authThe authenticated user: _id, username, policies, lastLogin and other fields of the user record.

On PostgreSQL the user's id is auth.id.

The rule language is the same as expression filters, including functions such as some, has and regex.

Examples

Users see their own documents

JSON
{
  "acl": {
    "read": "document.owner == auth._id",
    "write": "document.owner == auth._id"
  }
}

Set owner from a function or validate it with the write rule so users can't create documents for someone else.

Public content with drafts

JSON
{
  "acl": {
    "read": "document.status == \"published\"",
    "write": "false == true"
  }
}

Editors work with identities in the panel, which ignore rules. Users only ever see published entries.

Team membership through a relation

JSON
{
  "acl": {
    "read": "some(document.project.members, [auth._id])",
    "write": "document.project.owner == auth._id"
  }
}

Field rules

A property can have its own acl expression. When it evaluates to false for a user, the property is removed from that user's read responses:

JSON
{
  "email": {
    "type": "string",
    "title": "Email",
    "acl": "document._id == auth._id"
  }
}

Testing rules

  1. Create a test user and attach a policy that allows the data endpoints of the bucket.
  2. Log in as that user with POST /passport/login.
  3. Call the data API with Authorization: USER <token> and check what comes back.
Terminal
curl -X POST https://api.example.com/api/passport/login \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "password": "readerPass1"}'

curl https://api.example.com/api/bucket/<bucketId>/data \
  -H "Authorization: USER <token>"