From e83d80ebb0cf88ee004ad4c6121119e2c3785d74 Mon Sep 17 00:00:00 2001 From: HGZ-20 Date: Fri, 1 Dec 2023 04:13:27 +0800 Subject: [PATCH 1/2] feat: add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter. fix: #304 add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter. And does not automatically load the policy when using the filter adapter. --- src/adapter/file_adapter.rs | 12 ++++++++++-- src/adapter/memory_adapter.rs | 3 ++- src/adapter/mod.rs | 2 +- src/adapter/null_adapter.rs | 2 +- src/enforcer.rs | 9 +++++++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/adapter/file_adapter.rs b/src/adapter/file_adapter.rs index 1671772c..e4c65418 100644 --- a/src/adapter/file_adapter.rs +++ b/src/adapter/file_adapter.rs @@ -51,8 +51,15 @@ where } } + pub fn new_filtered_adapter(p: P) -> FileAdapter

{ + FileAdapter { + file_path: p, + is_filtered: true, + } + } + async fn load_policy_file( - &self, + &mut self, m: &mut dyn Model, handler: LoadPolicyFileHandler, ) -> Result<()> { @@ -110,7 +117,8 @@ impl

Adapter for FileAdapter

where P: AsRef + Send + Sync, { - async fn load_policy(&self, m: &mut dyn Model) -> Result<()> { + async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> { + self.is_filtered = false; self.load_policy_file(m, load_policy_line).await?; Ok(()) } diff --git a/src/adapter/memory_adapter.rs b/src/adapter/memory_adapter.rs index 6b0dab07..4d28eb99 100644 --- a/src/adapter/memory_adapter.rs +++ b/src/adapter/memory_adapter.rs @@ -15,7 +15,8 @@ pub struct MemoryAdapter { #[async_trait] impl Adapter for MemoryAdapter { - async fn load_policy(&self, m: &mut dyn Model) -> Result<()> { + async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> { + self.is_filtered = false; for line in self.policy.iter() { let sec = &line[0]; let ptype = &line[1]; diff --git a/src/adapter/mod.rs b/src/adapter/mod.rs index 36a0095d..ad27afd2 100644 --- a/src/adapter/mod.rs +++ b/src/adapter/mod.rs @@ -20,7 +20,7 @@ pub struct Filter<'a> { #[async_trait] pub trait Adapter: Send + Sync { - async fn load_policy(&self, m: &mut dyn Model) -> Result<()>; + async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()>; async fn load_filtered_policy<'a>( &mut self, m: &mut dyn Model, diff --git a/src/adapter/null_adapter.rs b/src/adapter/null_adapter.rs index bd4e655d..054cbe3e 100644 --- a/src/adapter/null_adapter.rs +++ b/src/adapter/null_adapter.rs @@ -10,7 +10,7 @@ pub struct NullAdapter; #[async_trait] impl Adapter for NullAdapter { - async fn load_policy(&self, _m: &mut dyn Model) -> Result<()> { + async fn load_policy(&mut self, _m: &mut dyn Model) -> Result<()> { Ok(()) } diff --git a/src/enforcer.rs b/src/enforcer.rs index 8bf4a13a..8a53231c 100644 --- a/src/enforcer.rs +++ b/src/enforcer.rs @@ -416,7 +416,11 @@ impl CoreApi for Enforcer { a: A, ) -> Result { let mut e = Self::new_raw(m, a).await?; - e.load_policy().await?; + + // Do not initialize the full policy when using a filtered adapter + if !e.adapter.is_filtered() { + e.load_policy().await?; + } Ok(e) } @@ -1373,9 +1377,10 @@ mod tests { tokio::test )] async fn test_filtered_file_adapter() { + let adapter = FileAdapter::new_filtered_adapter("examples/rbac_with_domains_policy.csv"); let mut e = Enforcer::new( "examples/rbac_with_domains_model.conf", - "examples/rbac_with_domains_policy.csv", + adapter ) .await .unwrap(); From 023e5251cbba4a8eee32c1d2e588fcc555f3d35a Mon Sep 17 00:00:00 2001 From: HGZ-20 Date: Fri, 1 Dec 2023 14:31:55 +0800 Subject: [PATCH 2/2] feat: add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter. fix: #304 add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter. And does not automatically load the policy when using the filter adapter. --- src/enforcer.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/enforcer.rs b/src/enforcer.rs index 8a53231c..ddcbd589 100644 --- a/src/enforcer.rs +++ b/src/enforcer.rs @@ -416,7 +416,7 @@ impl CoreApi for Enforcer { a: A, ) -> Result { let mut e = Self::new_raw(m, a).await?; - + // Do not initialize the full policy when using a filtered adapter if !e.adapter.is_filtered() { e.load_policy().await?; @@ -1377,13 +1377,13 @@ mod tests { tokio::test )] async fn test_filtered_file_adapter() { - let adapter = FileAdapter::new_filtered_adapter("examples/rbac_with_domains_policy.csv"); - let mut e = Enforcer::new( - "examples/rbac_with_domains_model.conf", - adapter - ) - .await - .unwrap(); + let adapter = FileAdapter::new_filtered_adapter( + "examples/rbac_with_domains_policy.csv", + ); + let mut e = + Enforcer::new("examples/rbac_with_domains_model.conf", adapter) + .await + .unwrap(); let filter = Filter { p: vec!["", "domain1"],