Skip to content

Commit

Permalink
Merge pull request #13 from hackerchai/master
Browse files Browse the repository at this point in the history
Feat: feat bump version 0.3.3
  • Loading branch information
hackerchai authored Aug 30, 2020
2 parents e83e4ed + 9e48c51 commit fc5771d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 46 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-casbin"
version = "0.3.2"
version = "0.3.3"
authors = ["Eason Chai <[email protected]>","Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -14,10 +14,10 @@ path = "src/lib.rs"

[dependencies]
actix = "0.9.0"
casbin = { version = "1.1.2", default-features = false, features = [ "incremental" ] }
actix-casbin-auth = {version = "0.3.1", default-features = false }
tokio = { version = "0.2.21", default-features = false, optional = true }
async-std = { version = "1.6.2", default-features = false, optional = true }
casbin = { version = "1.1.3", default-features = false, features = [ "incremental", "cached"] }
actix-casbin-auth = { version = "0.3.2", default-features = false }
tokio = { version = "0.2.22", default-features = false, optional = true }
async-std = { version = "1.6.3", default-features = false, optional = true }
futures = "0.3"

[features]
Expand All @@ -27,6 +27,6 @@ runtime-tokio = ["casbin/runtime-tokio", "tokio/sync", "actix-casbin-auth/runtim
runtime-async-std = ["casbin/runtime-async-std", "async-std/std", "actix-casbin-auth/runtime-async-std"]

[dev-dependencies]
tokio = { version = "0.2.21", features = [ "full" ] }
async-std = { version = "1.6.2", features = [ "attributes" ] }
tokio = { version = "0.2.22", features = [ "full" ] }
async-std = { version = "1.6.3", features = [ "attributes" ] }
actix-rt = "1.1.1"
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Add it to `Cargo.toml`

```rust
actix-casbin = "0.3.2"
actix-casbin = "0.3.3"
actix-rt = "1.1.1"
```

Expand Down Expand Up @@ -67,10 +67,9 @@ async fn main() -> Result<()> {
let a = FileAdapter::new("examples/rbac_policy.csv");

let mut casbin_middleware = CasbinService::new(m, a).await;
let enforcer = casbin_middleware.get_enforcer().await;
let enforcer = casbin_middleware.get_enforcer();

let addr = CasbinActor::<CachedEnforcer>::set_enforcer(enforcer)
.await?;
let addr = CasbinActor::<CachedEnforcer>::set_enforcer(enforcer)?;
if let CasbinResult::Enforce(test_enforce) = addr
.send(CasbinCmd::Enforce(
vec!["alice", "data1", "read"]
Expand Down
66 changes: 55 additions & 11 deletions src/casbin_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ pub enum CasbinCmd {
AddNamedGroupingPolicies(String, Vec<Vec<String>>),
RemovePolicy(Vec<String>),
RemovePolicies(Vec<Vec<String>>),
RemoveFilteredPolicy(usize, Vec<String>),
RemoveNamedPolicy(String, Vec<String>),
RemoveNamedPolicies(String, Vec<Vec<String>>),
RemoveGroupingPolicy(Vec<String>),
RemoveGroupingPolicies(Vec<Vec<String>>),
RemoveNamedGroupingPolicy(String, Vec<String>),
RemoveNamedGroupingPolicies(String, Vec<Vec<String>>),
RemoveFilteredNamedPolicy(String, usize, Vec<String>),
RemoveFilteredNamedGroupingPolicy(String, usize, Vec<String>),
AddRoleForUser(String, String, Option<String>),
AddRolesForUser(String, Vec<String>, Option<String>),
DeleteRoleForUser(String, String, Option<String>),
Expand All @@ -43,7 +50,14 @@ pub enum CasbinResult {
AddNamedGroupingPolicies(bool),
RemovePolicy(bool),
RemovePolicies(bool),
RemoveFilteredPolicy(bool),
RemoveNamedPolicy(bool),
RemoveNamedPolicies(bool),
RemoveGroupingPolicy(bool),
RemoveGroupingPolicies(bool),
RemoveNamedGroupingPolicy(bool),
RemoveNamedGroupingPolicies(bool),
RemoveFilteredNamedPolicy(bool),
RemoveFilteredNamedGroupingPolicy(bool),
AddRoleForUser(bool),
AddRolesForUser(bool),
DeleteRoleForUser(bool),
Expand Down Expand Up @@ -71,11 +85,11 @@ impl<T: IEnforcer + 'static> CasbinActor<T> {
}))
}

pub async fn set_enforcer(e: Arc<RwLock<T>>) -> Result<Addr<CasbinActor<T>>> {
Ok(Supervisor::start(|_| CasbinActor { enforcer: Some(e) }))
pub fn set_enforcer(e: Arc<RwLock<T>>) -> Result<CasbinActor<T>> {
Ok(CasbinActor { enforcer: Some(e) })
}

pub async fn get_enforcer(&mut self) -> Option<Arc<RwLock<T>>> {
pub fn get_enforcer(&mut self) -> Option<Arc<RwLock<T>>> {
self.enforcer.as_ref().map(|x| Arc::clone(x))
}
}
Expand All @@ -99,15 +113,15 @@ impl<T: IEnforcer + 'static> Handler<CasbinCmd> for CasbinActor<T> {
None => {
return Box::new(actix::fut::err(CasbinError::IoError(Error::new(
ErrorKind::NotConnected,
"Enforcer droped!",
"Enforcer dropped!",
))))
}
};
let cloned_enforcer = Arc::clone(e);
Box::new(
async move {
let mut lock = cloned_enforcer.write().await;
match msg {
let result = match msg {
CasbinCmd::Enforce(policy) => lock.enforce(&policy).map(CasbinResult::Enforce),
CasbinCmd::AddPolicy(policy) => {
lock.add_policy(policy).await.map(CasbinResult::AddPolicy)
Expand Down Expand Up @@ -140,6 +154,30 @@ impl<T: IEnforcer + 'static> Handler<CasbinCmd> for CasbinActor<T> {
.add_named_grouping_policies(&ptype, policy)
.await
.map(CasbinResult::AddNamedGroupingPolicies),
CasbinCmd::RemoveNamedPolicy(ptype, policy) => lock
.remove_named_policy(&ptype, policy)
.await
.map(CasbinResult::RemoveNamedPolicy),
CasbinCmd::RemoveNamedPolicies(ptype, policy) => lock
.remove_named_policies(&ptype, policy)
.await
.map(CasbinResult::RemoveNamedPolicies),
CasbinCmd::RemoveGroupingPolicy(policy) => lock
.remove_grouping_policy(policy)
.await
.map(CasbinResult::RemoveGroupingPolicy),
CasbinCmd::RemoveGroupingPolicies(policy) => lock
.remove_grouping_policies(policy)
.await
.map(CasbinResult::RemoveGroupingPolicies),
CasbinCmd::RemoveNamedGroupingPolicy(ptype, policy) => lock
.remove_named_grouping_policy(&ptype, policy)
.await
.map(CasbinResult::RemoveNamedGroupingPolicy),
CasbinCmd::RemoveNamedGroupingPolicies(ptype, policy) => lock
.remove_named_grouping_policies(&ptype, policy)
.await
.map(CasbinResult::RemoveNamedGroupingPolicies),
CasbinCmd::RemovePolicy(policy) => lock
.remove_policy(policy)
.await
Expand All @@ -148,10 +186,14 @@ impl<T: IEnforcer + 'static> Handler<CasbinCmd> for CasbinActor<T> {
.remove_policies(policy)
.await
.map(CasbinResult::RemovePolicies),
CasbinCmd::RemoveFilteredPolicy(idx, policy) => lock
.remove_filtered_policy(idx, policy)
CasbinCmd::RemoveFilteredNamedPolicy(ptype, idx, policy) => lock
.remove_filtered_named_policy(&ptype, idx, policy)
.await
.map(CasbinResult::RemoveFilteredNamedPolicy),
CasbinCmd::RemoveFilteredNamedGroupingPolicy(ptype, idx, policy) => lock
.remove_filtered_named_grouping_policy(&ptype, idx, policy)
.await
.map(CasbinResult::RemoveFilteredPolicy),
.map(CasbinResult::RemoveFilteredNamedGroupingPolicy),
CasbinCmd::AddRoleForUser(user, roles, domain) => lock
.add_role_for_user(&user, &roles, domain.as_deref())
.await
Expand All @@ -178,7 +220,9 @@ impl<T: IEnforcer + 'static> Handler<CasbinCmd> for CasbinActor<T> {
lock.get_implicit_permissions_for_user(&name, domain.as_deref()),
))
}
}
};
drop(lock);
result
}
.into_actor(self)
.map(|res, _act, _ctx| res),
Expand Down
30 changes: 9 additions & 21 deletions tests/test_casbin.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult};
use casbin::prelude::*;
use std::sync::Arc;

#[cfg(feature = "runtime-tokio")]
use tokio::sync::RwLock;

#[cfg(feature = "runtime-async-std")]
use async_std::sync::RwLock;

#[actix_rt::test]
async fn test_enforcer() {
let m = DefaultModel::from_file("examples/rbac_model.conf")
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

if let CasbinResult::Enforce(test_enforce) = addr
.send(CasbinCmd::Enforce(
Expand All @@ -38,8 +30,7 @@ async fn test_enforcer_threads() {
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

for _ in 0..8 {
let clone_addr = addr.clone();
Expand Down Expand Up @@ -67,8 +58,7 @@ async fn test_policy_command() {
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

if let CasbinResult::RemovePolicy(remove_policy) = addr
.send(CasbinCmd::RemovePolicy(
Expand All @@ -84,8 +74,9 @@ async fn test_policy_command() {
assert_eq!(true, remove_policy);
}

if let CasbinResult::RemoveFilteredPolicy(remove_filtered_policy) = addr
.send(CasbinCmd::RemoveFilteredPolicy(
if let CasbinResult::RemoveFilteredNamedPolicy(remove_filtered_policy) = addr
.send(CasbinCmd::RemoveFilteredNamedPolicy(
"p".to_string(),
1,
vec!["data2"].iter().map(|s| s.to_string()).collect(),
))
Expand Down Expand Up @@ -153,8 +144,7 @@ async fn test_roles_command() {
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

if let CasbinResult::AddRoleForUser(add_role_for_user) = addr
.send(CasbinCmd::AddRoleForUser(
Expand Down Expand Up @@ -214,8 +204,7 @@ async fn test_implicit_roles_command() {
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_with_hierarchy_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

if let CasbinResult::GetImplicitRolesForUser(implicit_roles_alice) = addr
.send(CasbinCmd::GetImplicitRolesForUser(
Expand Down Expand Up @@ -248,8 +237,7 @@ async fn test_implicit_permissions_command() {
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_with_hierarchy_policy.csv");
let e = Arc::new(RwLock::new(Enforcer::new(m, a).await.unwrap()));
let addr = CasbinActor::<Enforcer>::set_enforcer(e).await.unwrap();
let addr = CasbinActor::<Enforcer>::new(m, a).await.unwrap();

if let CasbinResult::GetImplicitPermissionsForUser(implicit_permissions_alice) = addr
.send(CasbinCmd::GetImplicitPermissionsForUser(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_set_enforcer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix::Actor;
use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult};
use actix_casbin_auth::CasbinService;
use casbin::prelude::*;
Expand All @@ -10,11 +11,11 @@ async fn test_set_enforcer() {
let a = FileAdapter::new("examples/rbac_policy.csv");

let mut casbin_middleware = CasbinService::new(m, a).await;
let enforcer = casbin_middleware.get_enforcer().await;
let enforcer = casbin_middleware.get_enforcer();

let addr = CasbinActor::<CachedEnforcer>::set_enforcer(enforcer)
.await
.unwrap();
.unwrap()
.start();
if let CasbinResult::Enforce(test_enforce) = addr
.send(CasbinCmd::Enforce(
vec!["alice", "data1", "read"]
Expand Down

0 comments on commit fc5771d

Please sign in to comment.