Management API
The Management API provides comprehensive support for Casbin policy management operations.
Filtered API
Filtered API methods share a common parameter signature: (fieldIndex int, fieldValues ...string). The fieldIndex parameter specifies the starting index for matching, while fieldValues contains the expected values. Empty strings in fieldValues act as wildcards, matching any value.
Example:
p, alice, book, read
p, bob, book, read
p, bob, book, write
p, alice, pen, get
p, bob, pen ,get
e.GetFilteredPolicy(1, "book") // returns: [["alice", "book", "read"], ["bob", "book", "read"], ["bob", "book", "write"]]
e.GetFilteredPolicy(1, "book", "read") // returns: [["alice", "book", "read"], ["bob", "book", "read"]]
e.GetFilteredPolicy(0, "alice", "", "read") // returns: [[alice book read]]
e.GetFilteredPolicy(0, "alice") // returns: [["alice", "book", "read"], ["alice", "pen", "get"]]
API Reference
Throughout this reference, the variable e represents an Enforcer instance.
e, err := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv')
$e = new Enforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
e = casbin.Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
var e = new Enforcer("path/to/model.conf", "path/to/policy.csv");
let mut e = Enforce::new("examples/rbac_model.conf", "examples/rbac_policy.csv").await?;
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
Enforce()
Enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).
For example:
ok, err := e.Enforce(request)
const ok = await e.enforce(request);
$ok = $e->enforcer($request);
ok = e.enforcer(request)
var ok = e.Enforce(request);
// or async
var ok = await e.EnforceAsync(request);
boolean ok = e.enforce(request);
EnforceWithMatcher()
EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
For example:
ok, err := e.EnforceWithMatcher(matcher, request)
$ok = $e->enforceWithMatcher($matcher, $request);
ok = e.enforce_with_matcher(matcher, request)
var ok = e.EnforceWithMatcher(matcher, request);
// or async
var ok = await e.EnforceWithMatcherAsync(matcher, request);
boolean ok = e.enforceWithMatcher(matcher, request);
EnforceEx()
EnforceEx explain enforcement by informing matched rules.
For example:
ok, reason, err := e.EnforceEx(request)
const ok = await e.enforceEx(request);
list($ok, $reason) = $e->enforceEx($request);
var (ok, explain) = e.EnforceEx(request);
// or async
var (ok, explain) = await e.EnforceExAsync(request);
ok, reason = e.enforce_ex(request)
EnforceExWithMatcher()
EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules.
For example:
ok, reason, err := e.EnforceExWithMatcher(matcher, request)
var (ok, explain) = e.EnforceExWithMatcher(matcher, request);
// or async
var (ok, explain) = await e.EnforceExWithMatcherAsync(matcher, request);