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".
{
"acl": {
"read": "document.status == \"published\" || document.owner == auth._id",
"write": "document.owner == auth._id"
}
}When rules run
| Caller | Policies checked | Rules applied |
|---|---|---|
USER token | Yes | Yes |
IDENTITY token | Yes | No |
APIKEY | Yes | No |
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.
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.
document.owner == auth._idTo make a bucket read-only for users, deny all writes:
false == trueAvailable variables
| Variable | Contents |
|---|---|
document | The document being read or written. Relations can be followed, for example document.project.owner. |
auth | The 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
{
"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
{
"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
{
"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:
{
"email": {
"type": "string",
"title": "Email",
"acl": "document._id == auth._id"
}
}Testing rules
- Create a test user and attach a policy that allows the data endpoints of the bucket.
- Log in as that user with
POST /passport/login. - Call the data API with
Authorization: USER <token>and check what comes back.
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>"