Syntax for Models
-
A model configuration (CONF) requires at minimum four sections:
[request_definition],[policy_definition],[policy_effect], and[matchers]. -
Models implementing Role-Based Access Control (RBAC) must additionally include a
[role_definition]section. -
Models requiring policy invariant enforcement for RBAC may optionally include a
[constraint_definition]section. -
Model configuration (CONF) files support comments. The
#symbol initiates a comment, treating all subsequent text on that line as commentary.
Định nghĩa yêu cầu
The [request_definition] section specifies the parameters passed to the e.Enforce(...) function.
[request_definition]
r = sub, obj, act
Here, sub, obj, and act represent the traditional access control triple: subject (requesting entity), object (target resource), and action (operation type). This format is customizable—use sub, act when resources needn't be specified, or sub, sub2, obj, act when two requesting entities are involved.
Định nghĩa Chính sách
The [policy_definition] describes policy structure and semantics. Consider this model:
[policy_definition]
p = sub, obj, act
p2 = sub, act
With this corresponding policy (from a policy file):
p, alice, data1, read
p2, bob, write-all-objects
Policy rules are organized as lines in the policy file. Each rule begins with a policy type identifier (like p or p2) that matches one of your policy definitions when multiple exist. The policy above creates these bindings for matcher use:
(alice, data1, read) -> (p.sub, p.obj, p.act)
(bob, write-all-objects) -> (p2.sub, p2.act)
Policy rule elements are always interpreted as strings. For questions about this behavior, consult the discussion at: https://github.com/casbin/casbin/issues/113
Hiệu ứng Chính sách
The [policy_effect] section determines request approval when multiple matching policies exist—for instance, when one permits and another denies.
[policy_effect]
e = some(where (p.eft == allow))
This policy effect implements "allow-override": when any matched policy grants allow, the final decision is allow. The p.eft field contains a policy's effect, taking values of either allow or deny. This field is optional and defaults to allow—since we omitted it above, the default applies.
Here's an alternative policy effect:
[policy_effect]
e = !some(where (p.eft == deny))
This implements "deny-override": the final effect is allow when no deny policies match. Here, some indicates at least one matching policy exists, while any means all policies match (though not used in this example). Policy effects can combine using logical expressions:
[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
This requires at least one allow policy and zero deny policies. Both allow and deny authorizations are supported, with deny taking priority over allow.
While we designed the policy effect syntax shown above, current implementations use hard-coded policy effects. We discovered limited need for this level of customization. Consequently, you must select from the built-in policy effects rather than defining custom ones.
Available built-in policy effects:
| Hiệu ứng Chính sách | Ý nghĩa | Ví dụ |
|---|---|---|
| some(where (p.eft == allow)) | cho phép-ghi đè | ACL, RBAC, v.v. |
| !some(where (p.eft == deny)) | từ chối-ghi đè | Từ chối-ghi đè |
| some(where (p.eft == allow)) && !some(where (p.eft == deny)) | cho phép-và-từ chối | Cho phép-và-từ chối |
| ưu tiên(p.eft) || từ chối | ưu tiên | Ưu tiên |
| ưu tiên chủ thể(p.eft) | ưu tiên dựa trên vai trò | Chủ đề-Ưu tiên |
Constraint Definition
The [constraint_definition] section establishes policy invariants for RBAC systems. Constraints maintain role assignment validity by verifying rules whenever policies change. This optional section requires RBAC enablement (necessitating [role_definition]).
[constraint_definition]
c = sod("finance_requester", "finance_approver")
c2 = sodMax(["payroll_view", "payroll_edit", "payroll_approve"], 1)
c3 = roleMax("superadmin", 2)
c4 = rolePre("db_admin", "security_trained")
Constraint Types
Separation of Duties (sod) - Blocks users from simultaneously holding conflicting roles. When Alice receives the finance_requester role, the system prevents assigning her finance_approver.
c = sod("finance_requester", "finance_approver")
Separation of Duties Max (sodMax) - Restricts the number of roles from a set that each user may hold. Setting sodMax to 1 for payroll operations means a user can view, edit, or approve—never possessing more than one of these roles concurrently.
c2 = sodMax(["payroll_view", "payroll_edit", "payroll_approve"], 1)
Role Cardinality (roleMax) - Limits how many users can possess a specific role. Setting a limit of 2 for superadmin restricts that role to two people organization-wide.
c3 = roleMax("superadmin", 2)
Prerequisite Role (rolePre) - Requires having a prerequisite role before granting another role. Users cannot receive db_admin access without first possessing the security_trained role.
c4 = rolePre("db_admin", "security_trained")
How Constraints Work
Constraints perform automatic validation during grouping policy modifications via methods like AddGroupingPolicy() or RemoveGroupingPolicy(). When changes violate constraints, the operations fail and return constraint violation errors, leaving policies unchanged.
At model initialization, the system verifies all existing constraints against current policies. Invalid constraints (incorrect syntax, missing RBAC configuration, or violations of existing data) prevent model loading and generate descriptive errors.