From 443b3a7552d6816f1a5b79bf09ee89441c09fafd Mon Sep 17 00:00:00 2001 From: Hackerchai Date: Sun, 30 Aug 2020 17:58:46 +0800 Subject: [PATCH 1/2] Feat: add remove relation functions --- Cargo.toml | 12 +++---- README.md | 5 ++- src/casbin_actor.rs | 66 +++++++++++++++++++++++++++++++------- tests/test_casbin.rs | 30 ++++++----------- tests/test_set_enforcer.rs | 7 ++-- 5 files changed, 76 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da8843a..35145a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -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" diff --git a/README.md b/README.md index 1f67470..aa94aff 100644 --- a/README.md +++ b/README.md @@ -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::::set_enforcer(enforcer) - .await?; + let addr = CasbinActor::::set_enforcer(enforcer)?; if let CasbinResult::Enforce(test_enforce) = addr .send(CasbinCmd::Enforce( vec!["alice", "data1", "read"] diff --git a/src/casbin_actor.rs b/src/casbin_actor.rs index a237388..c082cde 100644 --- a/src/casbin_actor.rs +++ b/src/casbin_actor.rs @@ -22,7 +22,14 @@ pub enum CasbinCmd { AddNamedGroupingPolicies(String, Vec>), RemovePolicy(Vec), RemovePolicies(Vec>), - RemoveFilteredPolicy(usize, Vec), + RemoveNamedPolicy(String, Vec), + RemoveNamedPolicies(String, Vec>), + RemoveGroupingPolicy(Vec), + RemoveGroupingPolicies(Vec>), + RemoveNamedGroupingPolicy(String, Vec), + RemoveNamedGroupingPolicies(String, Vec>), + RemoveFilteredNamedPolicy(String, usize, Vec), + RemoveFilteredNamedGroupingPolicy(String, usize, Vec), AddRoleForUser(String, String, Option), AddRolesForUser(String, Vec, Option), DeleteRoleForUser(String, String, Option), @@ -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), @@ -71,11 +85,11 @@ impl CasbinActor { })) } - pub async fn set_enforcer(e: Arc>) -> Result>> { - Ok(Supervisor::start(|_| CasbinActor { enforcer: Some(e) })) + pub fn set_enforcer(e: Arc>) -> Result> { + Ok(CasbinActor { enforcer: Some(e) }) } - pub async fn get_enforcer(&mut self) -> Option>> { + pub fn get_enforcer(&mut self) -> Option>> { self.enforcer.as_ref().map(|x| Arc::clone(x)) } } @@ -99,7 +113,7 @@ impl Handler for CasbinActor { None => { return Box::new(actix::fut::err(CasbinError::IoError(Error::new( ErrorKind::NotConnected, - "Enforcer droped!", + "Enforcer dropped!", )))) } }; @@ -107,7 +121,7 @@ impl Handler for CasbinActor { 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) @@ -140,6 +154,30 @@ impl Handler for CasbinActor { .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 @@ -148,10 +186,14 @@ impl Handler for CasbinActor { .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 @@ -178,7 +220,9 @@ impl Handler for CasbinActor { lock.get_implicit_permissions_for_user(&name, domain.as_deref()), )) } - } + }; + drop(lock); + result } .into_actor(self) .map(|res, _act, _ctx| res), diff --git a/tests/test_casbin.rs b/tests/test_casbin.rs index 87efbac..b100168 100644 --- a/tests/test_casbin.rs +++ b/tests/test_casbin.rs @@ -1,12 +1,5 @@ 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() { @@ -14,8 +7,7 @@ async fn test_enforcer() { .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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); if let CasbinResult::Enforce(test_enforce) = addr .send(CasbinCmd::Enforce( @@ -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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); for _ in 0..8 { let clone_addr = addr.clone(); @@ -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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); if let CasbinResult::RemovePolicy(remove_policy) = addr .send(CasbinCmd::RemovePolicy( @@ -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(), )) @@ -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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); if let CasbinResult::AddRoleForUser(add_role_for_user) = addr .send(CasbinCmd::AddRoleForUser( @@ -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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); if let CasbinResult::GetImplicitRolesForUser(implicit_roles_alice) = addr .send(CasbinCmd::GetImplicitRolesForUser( @@ -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::::set_enforcer(e).await.unwrap(); + let addr = CasbinActor::::new(m, a).await.unwrap(); if let CasbinResult::GetImplicitPermissionsForUser(implicit_permissions_alice) = addr .send(CasbinCmd::GetImplicitPermissionsForUser( diff --git a/tests/test_set_enforcer.rs b/tests/test_set_enforcer.rs index 8502309..0d1aef1 100644 --- a/tests/test_set_enforcer.rs +++ b/tests/test_set_enforcer.rs @@ -1,3 +1,4 @@ +use actix::Actor; use actix_casbin::{CasbinActor, CasbinCmd, CasbinResult}; use actix_casbin_auth::CasbinService; use casbin::prelude::*; @@ -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::::set_enforcer(enforcer) - .await - .unwrap(); + .unwrap() + .start(); if let CasbinResult::Enforce(test_enforce) = addr .send(CasbinCmd::Enforce( vec!["alice", "data1", "read"] From 9e48c51a2fc8feb1a9ef21b4ee58ee18bd9ecdfe Mon Sep 17 00:00:00 2001 From: Hackerchai Date: Sun, 30 Aug 2020 17:59:19 +0800 Subject: [PATCH 2/2] Feat: feat bump version 0.3.3 --- Cargo.toml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 35145a0..44f1bd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-casbin" -version = "0.3.2" +version = "0.3.3" authors = ["Eason Chai ","Cheng JIANG "] edition = "2018" license = "Apache-2.0" diff --git a/README.md b/README.md index aa94aff..a53478d 100644 --- a/README.md +++ b/README.md @@ -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" ```