Skip to content

Commit

Permalink
Merge pull request #3 from hackerchai/master
Browse files Browse the repository at this point in the history
Version 0.1.0
  • Loading branch information
GopherJ authored Apr 25, 2020
2 parents 190b7c5 + 24782b4 commit f3880d2
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 18 deletions.
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: rust
sudo: required
dist: trusty
addons:
apt:
packages:
- libssl-dev
cache: cargo
rust:
- stable
- nightly
matrix:
allow_failures:
- rust: nightly
before_script:
- rustup component add rustfmt
- rustup component add clippy
script:
- cargo clean
- cargo build
- cargo test --no-default-features --features runtime-tokio
- cargo test --no-default-features --features runtime-async-std
- cargo clippy -- -D warnings
- cargo fmt --all -- --check

after_success: |
if [[ "$TRAVIS_RUST_VERSION" == stable ]]; then
# upload report to codecov
docker run --security-opt seccomp=unconfined -v "$PWD:/volume" xd009642/tarpaulin sh -c "cargo tarpaulin --out Xml"
bash <(curl -s https://codecov.io/bash)
fi
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "src/lib.rs"
[dependencies]
actix = "0.9.0"
casbin = { version = "0.6.0", default-features = false, optional = true }
tokio = { version = "0.2.18", default-features = false, optional = true }
tokio = { version = "0.2.19", default-features = false, optional = true }
async-std = { version = "1.5.0", default-features = false, optional = true }

[features]
Expand All @@ -25,5 +25,6 @@ runtime-tokio = ["casbin/runtime-tokio", "tokio/sync"]
runtime-async-std = ["casbin/runtime-async-std", "async-std/std"]

[dev-dependencies]
tokio = { version = "0.2.18", features = [ "full" ] }
tokio = { version = "0.2.19", features = [ "full" ] }
async-std = { version = "1.5.0", features = [ "attributes" ] }
actix-rt = "1.1.0"
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# actix-casbin
casbin as an actor
# Actix casbin

[![Crates.io](https://img.shields.io/crates/v/actix-casbin.svg)](https://crates.io/crates/actix-casbin)
[![Docs](https://docs.rs/actix-casbin/badge.svg)](https://docs.rs/actix-casbin)
[![Build Status](https://travis-ci.org/casbin-rs/actix-casbin.svg?branch=master)](https://travis-ci.org/casbin-rs/actix-casbin)
[![codecov](https://codecov.io/gh/casbin-rs/actix-casbin/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin-rs/actix-casbin)

[Casbin](https://github.com/casbin/casbin-rs) intergration for [actix](https://github.com/actix/actix) framework

## Install

Add it to `Cargo.toml`

```rust
casbin = "0.6.0"
actix-casbin = "0.1.0"
actix-rt = "1.1.0"
```


# Example

```rust
use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult};
use casbin::prelude::*;

#[actix_rt::main]
async fn main() -> Result<()> {
let m = DefaultModel::from_file("examples/rbac_model.conf")
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_policy.csv");

let addr = CasbinActor::new(m, a).await.unwrap();

if let CasbinResult::Enforce(test_enforce) = addr
.send(CasbinCmd::Enforce(
vec!["alice", "data1", "read"]
.iter()
.map(|s| s.to_string())
.collect(),
))
.await
.unwrap()
.unwrap()
{
if test_enforce {
println!("Enforce Pass");
} else {
println!("Enforce Fail");
}
Ok(())
} else {
panic!("Actor Error");
}
}
```

## License

This project is licensed under

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
10 changes: 10 additions & 0 deletions examples/rbac_with_hierarchy_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
p, alice, data1, read
p, bob, data2, write
p, data1_admin, data1, read
p, data1_admin, data1, write
p, data2_admin, data2, read
p, data2_admin, data2, write

g, alice, admin
g, admin, data1_admin
g, admin, data2_admin
30 changes: 16 additions & 14 deletions src/casbin_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,27 @@ impl Handler<CasbinCmd> for CasbinActor {
Box::new(
async move {
let mut lock = cloned_enforcer.write().await;
let result = match msg {
CasbinCmd::Enforce(str) => lock.enforce(&str).await.map(CasbinResult::Enforce),
CasbinCmd::AddPolicy(str) => {
lock.add_policy(str).await.map(CasbinResult::AddPolicy)
match msg {
CasbinCmd::Enforce(policy) => {
lock.enforce(&policy).await.map(CasbinResult::Enforce)
}
CasbinCmd::AddPolicies(str) => {
lock.add_policies(str).await.map(CasbinResult::AddPolicies)
CasbinCmd::AddPolicy(policy) => {
lock.add_policy(policy).await.map(CasbinResult::AddPolicy)
}
CasbinCmd::RemovePolicy(str) => lock
.remove_policy(str)
CasbinCmd::AddPolicies(policy) => lock
.add_policies(policy)
.await
.map(CasbinResult::AddPolicies),
CasbinCmd::RemovePolicy(policy) => lock
.remove_policy(policy)
.await
.map(CasbinResult::RemovePolicy),
CasbinCmd::RemovePolicies(str) => lock
.remove_policies(str)
CasbinCmd::RemovePolicies(policy) => lock
.remove_policies(policy)
.await
.map(CasbinResult::RemovePolicies),
CasbinCmd::RemoveFilteredPolicy(idx, str) => lock
.remove_filtered_policy(idx, str)
CasbinCmd::RemoveFilteredPolicy(idx, policy) => lock
.remove_filtered_policy(idx, policy)
.await
.map(CasbinResult::RemoveFilteredPolicy),
CasbinCmd::AddRoleForUser(user, roles, domain) => lock
Expand Down Expand Up @@ -130,8 +133,7 @@ impl Handler<CasbinCmd> for CasbinActor {
lock.get_implicit_permissions_for_user(&name, domain.as_deref()),
))
}
};
result
}
}
.into_actor(self)
.map(|res, _act, _ctx| res),
Expand Down
Loading

0 comments on commit f3880d2

Please sign in to comment.