12 unstable releases (3 breaking)

0.4.2 Sep 12, 2020
0.4.0 Aug 31, 2020
0.3.0 Jul 29, 2020
Download history 22/week @ 2021-09-28 50/week @ 2021-10-05 65/week @ 2021-10-12 43/week @ 2021-10-19 33/week @ 2021-10-26 27/week @ 2021-11-02 26/week @ 2021-11-09 23/week @ 2021-11-16 20/week @ 2021-11-23 50/week @ 2021-11-30 94/week @ 2021-12-07 67/week @ 2021-12-14 50/week @ 2021-12-21 2/week @ 2021-12-28 52/week @ 2022-01-04 37/week @ 2022-01-11

160 downloads per month

Apache-2.0

15KB
221 lines

Actix Casbin

Crates.io Docs Auto Build codecov

Casbin intergration for actix framework

Install

Add it to Cargo.toml

actix-casbin = "0.4.2"
actix-rt = "1.1.1"

Example

  1. Using actix-casbin as actor alone
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(())
}
  1. Use actix-casbin with casbin actix middleware actix-casbin-auth
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(())
}

License

This project is licensed under

Dependencies

~26MB
~585K SLoC