From 894ddea3f8cca3d5183b73e16a90b2d2ad3fe2fa Mon Sep 17 00:00:00 2001 From: Worapol Worakunsap Date: Thu, 16 Nov 2023 11:05:46 +0700 Subject: [PATCH] fix: fixed nightly and beta ubuntu Error: --> src/cached_enforcer.rs:77:1 and Error: --> src/effector.rs:33:9 --- src/cached_enforcer.rs | 54 ++++++++++++++++++++++++++++++++++++++++++ src/effector.rs | 13 +--------- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/cached_enforcer.rs b/src/cached_enforcer.rs index 3ab89b90..94442fd7 100644 --- a/src/cached_enforcer.rs +++ b/src/cached_enforcer.rs @@ -6,6 +6,7 @@ use crate::{ core_api::CoreApi, effector::Effector, emitter::{clear_cache, Event, EventData, EventEmitter}, + enforcer::EnforceContext, enforcer::Enforcer, model::Model, rbac::RoleManager, @@ -71,6 +72,21 @@ impl CachedEnforcer { (authorized, false, indices) }) } + pub(crate) fn private_enforce_with_context( + &self, + ctx: EnforceContext, + rvals: &[Dynamic], + cache_key: u64, + ) -> Result<(bool, bool, Option>)> { + Ok(if let Some(authorized) = self.cache.get(&cache_key) { + (authorized, true, None) + } else { + let (authorized, indices) = + self.enforcer.private_enforce_with_context(ctx, &rvals)?; + self.cache.set(cache_key, authorized); + (authorized, false, indices) + }) + } } #[async_trait] @@ -226,6 +242,44 @@ impl CoreApi for CachedEnforcer { Ok(authorized) } + fn enforce_with_context( + &self, + ctx: EnforceContext, + rvals: ARGS, + ) -> Result { + let cache_key = rvals.cache_key(); + let rvals = rvals.try_into_vec()?; + #[allow(unused_variables)] + let (authorized, cached, indices) = + self.private_enforce_with_context(ctx, &rvals, cache_key)?; + + #[cfg(feature = "logging")] + { + self.enforcer.get_logger().print_enforce_log( + rvals.iter().map(|x| x.to_string()).collect(), + authorized, + cached, + ); + + #[cfg(feature = "explain")] + if let Some(indices) = indices { + let all_rules = get_or_err!(self, "p", ModelError::P, "policy") + .get_policy(); + + let rules: Vec = indices + .into_iter() + .filter_map(|y| { + all_rules.iter().nth(y).map(|x| x.join(", ")) + }) + .collect(); + + self.enforcer.get_logger().print_explain_log(rules); + } + } + + Ok(authorized) + } + #[inline] fn enforce_mut(&mut self, rvals: ARGS) -> Result { self.enforce(rvals) diff --git a/src/effector.rs b/src/effector.rs index c505d411..1bb148b1 100644 --- a/src/effector.rs +++ b/src/effector.rs @@ -18,6 +18,7 @@ pub trait EffectorStream: Send + Sync { fn push_effect(&mut self, eft: EffectKind) -> bool; } +#[derive(Clone)] pub struct DefaultEffectStream { done: bool, res: bool, @@ -28,18 +29,6 @@ pub struct DefaultEffectStream { expl: Vec, } -impl Clone for DefaultEffectStream { - fn clone(&self) -> Self { - Self { - done: self.done.clone(), - res: self.res.clone(), - expr: self.expr.clone(), - idx: self.idx.clone(), - cap: self.cap.clone(), - } - } -} - #[derive(Default)] pub struct DefaultEffector;