diff --git a/Cargo.toml b/Cargo.toml index a0ad94e1..3b765041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "casbin" -version = "0.7.4" +version = "0.7.5" authors = ["Joey ", "Cheng JIANG "] edition = "2018" license = "Apache-2.0" diff --git a/README.md b/README.md index c22e5f2f..6ddb7685 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate ```toml [dependencies] -casbin = { version = "0.7.4", default-features = false, features = ["runtime-async-std", "logging"] } +casbin = { version = "0.7.5", default-features = false, features = ["runtime-async-std", "logging"] } async-std = { version = "1.5.0", features = ["attributes"] } env_logger = "0.7.1" ``` diff --git a/src/cached_enforcer.rs b/src/cached_enforcer.rs index ad0b04a8..9013bebe 100644 --- a/src/cached_enforcer.rs +++ b/src/cached_enforcer.rs @@ -274,6 +274,11 @@ impl CoreApi for CachedEnforcer { fn has_auto_notify_watcher_enabled(&self) -> bool { self.enforcer.has_auto_notify_watcher_enabled() } + + #[inline] + fn has_auto_build_role_links_enabled(&self) -> bool { + self.enforcer.has_auto_build_role_links_enabled() + } } impl CachedApi for CachedEnforcer { diff --git a/src/core_api.rs b/src/core_api.rs index 9355b436..c7a07300 100644 --- a/src/core_api.rs +++ b/src/core_api.rs @@ -57,4 +57,5 @@ pub trait CoreApi: Sized + Send + Sync { fn has_auto_save_enabled(&self) -> bool; #[cfg(feature = "watcher")] fn has_auto_notify_watcher_enabled(&self) -> bool; + fn has_auto_build_role_links_enabled(&self) -> bool; } diff --git a/src/enforcer.rs b/src/enforcer.rs index 7a854904..947b207a 100644 --- a/src/enforcer.rs +++ b/src/enforcer.rs @@ -389,7 +389,11 @@ impl CoreApi for Enforcer { fn add_matching_fn(&mut self, f: fn(String, String) -> bool) -> Result<()> { self.rm.write().unwrap().add_matching_fn(f); - self.build_role_links() + if self.auto_build_role_links { + self.build_role_links()?; + } + + Ok(()) } async fn set_model(&mut self, m: M) -> Result<()> { @@ -559,6 +563,11 @@ impl CoreApi for Enforcer { fn has_auto_notify_watcher_enabled(&self) -> bool { self.auto_notify_watcher } + + #[inline] + fn has_auto_build_role_links_enabled(&self) -> bool { + self.auto_build_role_links + } } #[cfg(test)] diff --git a/src/internal_api.rs b/src/internal_api.rs index 3ab1bbc4..95dfd47a 100644 --- a/src/internal_api.rs +++ b/src/internal_api.rs @@ -5,7 +5,12 @@ use crate::{ Result, }; -#[cfg(any(feature = "watcher", feature = "cached", feature = "logging"))] +#[cfg(any( + feature = "watcher", + feature = "cached", + feature = "logging", + feature = "incremental" +))] use crate::emitter::EventData; #[cfg(feature = "cached")] @@ -66,18 +71,31 @@ impl InternalApi for Enforcer { } let rule_added = self.get_mut_model().add_policy(sec, ptype, { - #[cfg(any(feature = "watcher", feature = "logging"))] + #[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))] { rule.clone() } - #[cfg(all(not(feature = "watcher"), not(feature = "logging")))] + #[cfg(all( + not(feature = "watcher"), + not(feature = "logging"), + not(feature = "incremental") + ))] { rule } }); #[cfg(any(feature = "watcher", feature = "logging"))] { - let event_data = EventData::AddPolicy(sec.to_owned(), ptype.to_owned(), rule); + let event_data = EventData::AddPolicy(sec.to_owned(), ptype.to_owned(), { + #[cfg(feature = "incremental")] + { + rule.clone() + } + #[cfg(not(feature = "incremental"))] + { + rule + } + }); #[cfg(feature = "watcher")] { if rule_added && self.has_auto_notify_watcher_enabled() { @@ -91,6 +109,21 @@ impl InternalApi for Enforcer { } } } + if sec != "g" || !self.has_auto_build_role_links_enabled() { + return Ok(rule_added); + } + #[cfg(not(feature = "incremental"))] + { + self.build_role_links()?; + } + #[cfg(feature = "incremental")] + { + self.build_incremental_role_links(EventData::AddPolicy( + sec.to_owned(), + ptype.to_owned(), + rule, + ))?; + } Ok(rule_added) } @@ -111,18 +144,31 @@ impl InternalApi for Enforcer { } let rules_added = self.get_mut_model().add_policies(sec, ptype, { - #[cfg(any(feature = "watcher", feature = "logging"))] + #[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))] { rules.clone() } - #[cfg(all(not(feature = "watcher"), not(feature = "logging")))] + #[cfg(all( + not(feature = "watcher"), + not(feature = "logging"), + not(feature = "incremental") + ))] { rules } }); #[cfg(any(feature = "watcher", feature = "logging"))] { - let event_data = EventData::AddPolicies(sec.to_owned(), ptype.to_owned(), rules); + let event_data = EventData::AddPolicies(sec.to_owned(), ptype.to_owned(), { + #[cfg(feature = "incremental")] + { + rules.clone() + } + #[cfg(not(feature = "incremental"))] + { + rules + } + }); #[cfg(feature = "watcher")] { if rules_added && self.has_auto_notify_watcher_enabled() { @@ -136,6 +182,21 @@ impl InternalApi for Enforcer { } } } + if sec != "g" || !self.has_auto_build_role_links_enabled() { + return Ok(rules_added); + } + #[cfg(not(feature = "incremental"))] + { + self.build_role_links()?; + } + #[cfg(feature = "incremental")] + { + self.build_incremental_role_links(EventData::AddPolicies( + sec.to_owned(), + ptype.to_owned(), + rules, + ))?; + } Ok(rules_added) } @@ -156,18 +217,31 @@ impl InternalApi for Enforcer { } let rule_removed = self.get_mut_model().remove_policy(sec, ptype, { - #[cfg(any(feature = "watcher", feature = "logging"))] + #[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))] { rule.clone() } - #[cfg(all(not(feature = "watcher"), not(feature = "logging")))] + #[cfg(all( + not(feature = "watcher"), + not(feature = "logging"), + not(feature = "incremental") + ))] { rule } }); #[cfg(any(feature = "watcher", feature = "logging"))] { - let event_data = EventData::RemovePolicy(sec.to_owned(), ptype.to_owned(), rule); + let event_data = EventData::RemovePolicy(sec.to_owned(), ptype.to_owned(), { + #[cfg(feature = "incremental")] + { + rule.clone() + } + #[cfg(not(feature = "incremental"))] + { + rule + } + }); #[cfg(feature = "watcher")] { if rule_removed && self.has_auto_notify_watcher_enabled() { @@ -181,6 +255,21 @@ impl InternalApi for Enforcer { } } } + if sec != "g" || !self.has_auto_build_role_links_enabled() { + return Ok(rule_removed); + } + #[cfg(not(feature = "incremental"))] + { + self.build_role_links()?; + } + #[cfg(feature = "incremental")] + { + self.build_incremental_role_links(EventData::RemovePolicy( + sec.to_owned(), + ptype.to_owned(), + rule, + ))?; + } Ok(rule_removed) } @@ -201,11 +290,15 @@ impl InternalApi for Enforcer { } let rules_removed = self.get_mut_model().remove_policies(sec, ptype, { - #[cfg(any(feature = "watcher", feature = "logging"))] + #[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))] { rules.clone() } - #[cfg(all(not(feature = "watcher"), not(feature = "logging")))] + #[cfg(all( + not(feature = "watcher"), + not(feature = "logging"), + not(feature = "incremental") + ))] { rules } @@ -213,7 +306,16 @@ impl InternalApi for Enforcer { #[cfg(any(feature = "watcher", feature = "logging"))] { - let event_data = EventData::RemovePolicies(sec.to_owned(), ptype.to_owned(), rules); + let event_data = EventData::RemovePolicies(sec.to_owned(), ptype.to_owned(), { + #[cfg(feature = "incremental")] + { + rules.clone() + } + #[cfg(not(feature = "incremental"))] + { + rules + } + }); #[cfg(feature = "watcher")] { if rules_removed && self.has_auto_notify_watcher_enabled() { @@ -227,6 +329,21 @@ impl InternalApi for Enforcer { } } } + if sec != "g" || !self.has_auto_build_role_links_enabled() { + return Ok(rules_removed); + } + #[cfg(not(feature = "incremental"))] + { + self.build_role_links()?; + } + #[cfg(feature = "incremental")] + { + self.build_incremental_role_links(EventData::RemovePolicies( + sec.to_owned(), + ptype.to_owned(), + rules, + ))?; + } Ok(rules_removed) } @@ -253,8 +370,16 @@ impl InternalApi for Enforcer { #[cfg(any(feature = "watcher", feature = "logging"))] { - let event_data = - EventData::RemoveFilteredPolicy(sec.to_owned(), ptype.to_owned(), rules.clone()); + let event_data = EventData::RemoveFilteredPolicy(sec.to_owned(), ptype.to_owned(), { + #[cfg(feature = "incremental")] + { + rules.clone() + } + #[cfg(not(feature = "incremental"))] + { + rules + } + }); #[cfg(feature = "watcher")] { if rules_removed && self.has_auto_notify_watcher_enabled() { @@ -268,6 +393,21 @@ impl InternalApi for Enforcer { } } } + if sec != "g" || !self.has_auto_build_role_links_enabled() { + return Ok((rules_removed, rules)); + } + #[cfg(not(feature = "incremental"))] + { + self.build_role_links()?; + } + #[cfg(feature = "incremental")] + { + self.build_incremental_role_links(EventData::RemoveFilteredPolicy( + sec.to_owned(), + ptype.to_owned(), + rules.clone(), + ))?; + } Ok((rules_removed, rules)) } diff --git a/src/management_api.rs b/src/management_api.rs index 48f1eae3..406b23f3 100644 --- a/src/management_api.rs +++ b/src/management_api.rs @@ -1,8 +1,5 @@ use crate::{InternalApi, Result}; -#[cfg(feature = "incremental")] -use crate::emitter::EventData; - use async_trait::async_trait; #[async_trait] @@ -206,33 +203,7 @@ where ptype: &str, params: Vec, ) -> Result { - let rule_added = self - .add_policy_internal("g", ptype, { - #[cfg(not(feature = "incremental"))] - { - params - } - - #[cfg(feature = "incremental")] - { - params.clone() - } - }) - .await?; - #[cfg(not(feature = "incremental"))] - { - self.build_role_links()?; - } - - #[cfg(feature = "incremental")] - { - let sec = "g".to_owned(); - let ptype = ptype.to_owned(); - let d = EventData::AddPolicy(sec, ptype, params); - - self.build_incremental_role_links(d)?; - } - + let rule_added = self.add_policy_internal("g", ptype, params).await?; Ok(rule_added) } @@ -241,32 +212,7 @@ where ptype: &str, paramss: Vec>, ) -> Result { - let all_added = self - .add_policies_internal("g", ptype, { - #[cfg(not(feature = "incremental"))] - { - paramss - } - - #[cfg(feature = "incremental")] - { - paramss.clone() - } - }) - .await?; - #[cfg(not(feature = "incremental"))] - { - self.build_role_links()?; - } - - #[cfg(feature = "incremental")] - { - let sec = "g".to_owned(); - let ptype = ptype.to_owned(); - let d = EventData::AddPolicies(sec, ptype, paramss); - - self.build_incremental_role_links(d)?; - } + let all_added = self.add_policies_internal("g", ptype, paramss).await?; Ok(all_added) } @@ -275,33 +221,7 @@ where ptype: &str, params: Vec, ) -> Result { - let rule_removed = self - .remove_policy_internal("g", ptype, { - #[cfg(not(feature = "incremental"))] - { - params - } - - #[cfg(feature = "incremental")] - { - params.clone() - } - }) - .await?; - - #[cfg(not(feature = "incremental"))] - { - self.build_role_links()?; - } - - #[cfg(feature = "incremental")] - { - let sec = "g".to_owned(); - let ptype = ptype.to_owned(); - let d = EventData::RemovePolicy(sec, ptype, params); - - self.build_incremental_role_links(d)?; - } + let rule_removed = self.remove_policy_internal("g", ptype, params).await?; Ok(rule_removed) } @@ -310,31 +230,7 @@ where ptype: &str, paramss: Vec>, ) -> Result { - let all_removed = self - .remove_policies_internal("g", ptype, { - #[cfg(not(feature = "incremental"))] - { - paramss - } - - #[cfg(feature = "incremental")] - { - paramss.clone() - } - }) - .await?; - #[cfg(not(feature = "incremental"))] - { - self.build_role_links()?; - } - #[cfg(feature = "incremental")] - { - let sec = "g".to_owned(); - let ptype = ptype.to_owned(); - let d = EventData::RemovePolicies(sec, ptype, paramss); - - self.build_incremental_role_links(d)?; - } + let all_removed = self.remove_policies_internal("g", ptype, paramss).await?; Ok(all_removed) } @@ -348,20 +244,6 @@ where let (rule_removed, rules) = self .remove_filtered_policy_internal("g", ptype, field_index, field_values) .await?; - #[cfg(not(feature = "incremental"))] - { - self.build_role_links()?; - } - - #[cfg(feature = "incremental")] - { - let sec = "g".to_owned(); - let ptype = ptype.to_owned(); - let d = EventData::RemovePolicies(sec, ptype, rules); - - self.build_incremental_role_links(d)?; - } - Ok(rule_removed) }