Skip to content

Entity filters and policies

If you need to set permissions per entity, you can do so by adding filters to an operation configuration. These filters act as WHERE clause in the SQL query that retrieves the entities for read, update, and delete operations.

accountant.bake.json
[{
"resource_entity": "roles",
"resource": {
"module": "roles",
"fields": {
"name": "Accountant",
"slug": "accountant",
"modules": {
"invoices": true,
"products": {
"create": false,
"read": {
"filter": [
[
{
"field": "price",
"operator": ">",
"value": 100
},
{
"field": "price",
"operator": "<",
"recipe": "user().max_price"
}
]
]
},
"update": false,
"delete": false,
"history": true,
"fields": true
},
"customers": {
"create": false,
"read": false,
"update": false,
"delete": false,
"history": false,
"fields": true
}
}
}
}
}]

In the example above, users with the Accountant role can only read products with a price between 100 and the value of the max_price field in their user entity.

The following variables are available in recipes with the permissions configuration context:

  • role: The current role entity
  • module: The current module
  • action: The current action (read, create, update, delete, history)
  • user: The current user entity. It is also available as the known user() function.

You can also set permissions per entity for a whole module by setting the filters key in the role’s module configuration:

accountant.bake.json
[{
"resource_entity": "roles",
"resource": {
"module": "roles",
"fields": {
"name": "Accountant",
"slug": "accountant",
"modules": {
"invoices": true,
"products": {
"create": false,
"read": true,
"update": false,
"delete": false,
"history": true,
"fields": true,
"filter": [
[
{
"field": "price",
"operator": ">",
"value": 100
}
]
]
},
"customers": {
"create": false,
"read": false,
"update": false,
"delete": false,
"history": false,
"fields": true
}
}
}
}
}]

Copying and pasting the same filters for multiple roles can be cumbersome. Instead, you can define a policy that contains the filters and reference it in the role configuration:

accountant.bake.json
[{
"resource_entity": "roles",
"resource": {
"module": "roles",
"fields": {
"name": "Accountant",
"slug": "accountant",
"modules": {
"invoices": true,
"products": {
"create": false,
"read": {
"policies": ["product_price_policy"]
},
"update": false,
"delete": false,
"history": true,
"fields": true
},
"customers": {
"create": false,
"read": false,
"update": false,
"delete": false,
"history": false,
"fields": true
}
}
}
}
}]

In order for policies to work, you must first have a policies module defined in your configuration.

policies.bake.json
[{
"resource_module": "policies",
"resource": {
"identifier": "policies",
"type": "policies",
"icon": "lock",
"title": "name"
}
}]

Then, you can define the policy like this:

policies.bake.json
[{
"resource_entity": "policies",
"resource": {
"module": "policies",
"fields": {
"name": "product_price_policy",
"policy": [
[
{
"field": "price",
"operator": ">",
"value": 100
}
]
]
}
}
}]

You can also set per-entity permissions using workflows. Especially in complex systems, workflows can be more flexible and powerful than roles.

To configure a workflow-based policy, you must create a new workflow and add one or more policy events to it:

  • event/policy: This event is triggered when the system checks permissions for a user. You can use this event to add filters to the permission check.
  • event/policyRead: This event is triggered when the system checks permissions for a user to read an entity.
  • event/policyUpdate: This event is triggered when the system checks permissions for a user to update an entity.
  • event/policyDelete: This event is triggered when the system checks permissions for a user to delete an entity.

These events output a query object that you can modify to add filters, using query/where.

Clauses accept field, operator and a literal value, a recipe, or user (a dot path or array path on the current user). user overrides the clause value after recipe evaluation. Recipes can read role, module, action and user; entity policy evaluation also supplies client and named-policy evaluation supplies policy.

{"field":"owner.id","operator":"=","user":"id"}

Role filters and policies use DNF: outer groups OR, clauses within a group AND. Module-level, action-level and named-policy filters combine with AND, including inherited filters. For Bakery resource_policy, every clause requires recipe rather than a literal value; see policy resources. event/fieldPolicy additionally handles field authorization.