SurrealDB Adapter for Casbin
A SurrealDB adapter for casbin-rs, an authorization library that supports access control models like ACL, RBAC, ABAC for Rust projects.
Installation
Add this to your Cargo.toml:
[]
= "3.0"
= "2.20"
= "3.0"
Quick Start
1. Create an adapter and enforcer
use SurrealAdapter;
use *;
use connect;
use Root;
async
2. Define your Casbin model
Create a model.conf file with your authorization model:
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
Advanced Usage
RBAC with Domains
For multi-tenant applications with role-based access control:
[request_definition]
r = sub, dom, obj, act
[policy_definition]
p = sub, dom, obj, act
[role_definition]
g = _, _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act
// Add role assignment
enforcer.add_grouping_policy.await?;
// Add permission for role
enforcer.add_policy.await?;
// Check permission
let allowed = enforcer.enforce?;
Custom Table Name
By default the adapter uses the casbin_rule table. You can customize it:
let adapter = with_table;
adapter.create_table.await?;
Filtered Policy Loading
Load only specific policies to reduce memory usage:
use Filter;
let filter = Filter ;
enforcer.load_filtered_policy.await?;
Examples
See examples/ for more details.