Role rules and sets
Narrow a role per record with role_rules and principal sets, let managers grant roles, and widen rules with consent.
Permissions covers role rights and row rules. This page covers the finer tools.
Role rules
A row rule applies to everyone. A role rule applies to one role: where it fails on a record, that role's rights give nothing there. The user's other roles keep theirs.
role_rules:
- {role: Employee, rule: doc.user_id == user, rights: [read, write, create, delete, report, import, export, print, email, share]}rights lists the rights the rule scopes, every right when left out. Leaving select out lets the role still pick any record in a Link, as Frappe's Link check asks only that the record exists. A role rule only narrows. Both RecType YAML and a permission file in permissions/ take role_rules. A file may scope only roles its own app declares, or roles the owning app lists in a grants entry.
Principal sets
A set is a list of users computed per request, such as everyone below a manager in a sales hierarchy. A rule tests it with <set>().includes(...):
row_rules:
- 'has_role("System Manager") || doc.lead_owner == user || team().includes(doc.lead_owner)'Declare the set and its module (shortened):
principal_sets: [{"team", Crm.PrincipalSets.Team}]defmodule Crm.PrincipalSets.Team do
@behaviour Cardinal.PrincipalSet
@impl true
def members(ctx, user) do
with {:ok, node} when is_binary(node) <-
Cardinal.DB.get_value(ctx, "CRM Sales Hierarchy", %{"user" => user}, "name"),
{:ok, rows} <-
Cardinal.DB.get_list(ctx, "CRM Sales Hierarchy",
fields: ["user"],
filters: [["name", "descendants of (inclusive)", node]]
) do
Enum.map(rows, & &1["user"])
else
_ -> []
end
end
endmembers/2 runs on every request with a system ctx, before the user's rights are known. Keep it to one or two indexed reads and cache nothing. In lists and search the set becomes = ANY($n) in SQL. Set names are shared by all apps, and cardinal app check reports a duplicate or a rule that tests an unknown set.
Self access
self_access lets a user read and write named fields on their own record, without other rights on the RecType:
self_access:
fields: [first_name, middle_name, last_name, language, time_zone, user_image, bio, phone, mobile_no]
read_only: [roles]System Manager only
system_manager_only: true on a RecType, or trust: on a field, puts it under the System Manager write guard. Even an as_system ctx needs a System Manager in the request to write it.
Grants
grants lets a role hand out other roles without being System Manager. ERP lets an HR Manager onboard employees:
grants: [
%{
role: "HR Manager",
roles: ["Employee"],
user_permissions: ["Employee", "Company"],
user_fields: ["enabled", "first_name", "middle_name", "last_name", "user_image"]
}
]A holder of role may add or remove Has Role rows for roles, User Permission rows for user_permissions, and change user_fields on a User. System Manager, Administrator, Guest, All and Desk User are never grantable. Creating or deleting a User stays System Manager only.
user_invitation sets who may invite people with which roles. Core's is [{"System Manager", :all}].
Widening another app's rules
A file in permissions/ may only narrow a role another app declares. The owning app consents with widenable. Core lets HRMS's public Job Application form give guests select on Country:
widenable: [%{rectype: "Country", roles: ["Guest"], rights: ["select"]}]cardinal app check and install print each widened row. A row that widens without consent fails with widens_rule.