Crates.io | actix-casbin |
lib.rs | actix-casbin |
version | 1.1.0 |
source | src |
created_at | 2020-04-25 14:11:25.868836 |
updated_at | 2024-03-03 07:48:00.716768 |
description | An Actix actor for casbin |
homepage | https://github.com/casbin-rs/actix-casbin |
repository | |
max_upload_size | |
id | 233960 |
size | 39,815 |
Casbin intergration for actix framework
Add it to Cargo.toml
cargo add actix-casbin
cargo add actix-rt
use actix_casbin::casbin::{DefaultModel, FileAdapter, Result, Enforcer};
use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult};
#[actix_rt::main]
async fn main() -> Result<()> {
let m = DefaultModel::from_file("examples/rbac_model.conf").await?;
let a = FileAdapter::new("examples/rbac_policy.csv");
let addr = CasbinActor::<Enforcer>::new(m, a).await?;
let res = addr
.send(CasbinCmd::Enforce(
vec!["alice", "data1", "read"]
.iter()
.map(|s| (*s).to_string())
.collect(),
))
.await;
let test_enforce = match res {
Ok(Ok(CasbinResult::Enforce(result))) => result,
_ => panic!("Actor Error"),
};
if test_enforce {
println!("Enforce Pass");
} else {
println!("Enforce Fail");
}
Ok(())
}
use actix_casbin::casbin::{DefaultModel, FileAdapter, Result, CachedEnforcer};
use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult};
use actix_casbin_auth::CasbinService;
#[actix_rt::main]
async fn main() -> Result<()> {
let m = DefaultModel::from_file("examples/rbac_model.conf")
.await?;
let a = FileAdapter::new("examples/rbac_policy.csv");
let mut casbin_middleware = CasbinService::new(m, a).await;
let enforcer = casbin_middleware.get_enforcer();
let addr = CasbinActor::<CachedEnforcer>::set_enforcer(enforcer)?;
if let CasbinResult::Enforce(test_enforce) = addr
.send(CasbinCmd::Enforce(
vec!["alice", "data1", "read"]
.iter()
.map(|s| s.to_string())
.collect(),
))
.await;
let test_enforce = match res {
Ok(Ok(CasbinResult::Enforce(result))) => result,
_ => panic!("Actor Error"),
};
if test_enforce {
println!("Enforce Pass");
} else {
println!("Enforce Fail");
}
Ok(())
}
This project is licensed under