Skip to content

Commit

Permalink
chore: update casbin to version 2.5.0 (#878)
Browse files Browse the repository at this point in the history
Co-authored-by: Nathan.fooo <[email protected]>
  • Loading branch information
khorshuheng and appflowy authored Oct 15, 2024
1 parent 2b02a8d commit 1c51656
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 47 deletions.
30 changes: 6 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/access-control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ actix-http.workspace = true
app-error.workspace = true
anyhow.workspace = true
async-trait.workspace = true
casbin = { version = "2.2.0", features = [
casbin = { version = "2.5.0", features = [
"cached",
"runtime-tokio",
"incremental",
Expand Down
30 changes: 14 additions & 16 deletions libs/access-control/src/casbin/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::metrics::{tick_metric, AccessControlMetrics};

use anyhow::anyhow;
use app_error::AppError;
use casbin::rhai::ImmutableString;
use casbin::function_map::OperatorFunction;
use casbin::rhai::{Dynamic, ImmutableString};
use casbin::{CoreApi, DefaultModel, Enforcer, MgmtApi};
use database_entity::dto::{AFAccessLevel, AFRole};

Expand Down Expand Up @@ -52,10 +53,7 @@ impl AccessControl {
let mut enforcer = casbin::Enforcer::new(model, adapter).await.map_err(|e| {
AppError::Internal(anyhow!("Failed to create access control enforcer: {}", e))
})?;
enforcer.add_function(
"cmpRoleOrLevel",
|r: ImmutableString, p: ImmutableString| cmp_role_or_level(r.as_str(), p.as_str()),
);
enforcer.add_function("cmpRoleOrLevel", OperatorFunction::Arg2(cmp_role_or_level));

let enforcer = Arc::new(AFEnforcer::new(enforcer, NoEnforceGroup).await?);
tick_metric(
Expand Down Expand Up @@ -198,29 +196,29 @@ pub async fn casbin_model() -> Result<DefaultModel, AppError> {
/// * `r_act` - The role or access level from the request, prefixed with "r:" for roles or "l:" for levels.
/// * `p_act` - The role or access level from the policy, prefixed with "r:" for roles or "l:" for levels.
///
pub fn cmp_role_or_level(r_act: &str, p_act: &str) -> bool {
pub fn cmp_role_or_level(r_act: ImmutableString, p_act: ImmutableString) -> Dynamic {
trace!("cmp_role_or_level: r: {} p: {}", r_act, p_act);

if r_act.starts_with("r:") && p_act.starts_with("r:") {
let r = AFRole::from_enforce_act(r_act);
let p = AFRole::from_enforce_act(p_act);
return p >= r;
let r = AFRole::from_enforce_act(r_act.as_str());
let p = AFRole::from_enforce_act(p_act.as_str());
return Dynamic::from_bool(p >= r);
}

if r_act.starts_with("l:") && p_act.starts_with("l:") {
let r = AFAccessLevel::from_enforce_act(r_act);
let p = AFAccessLevel::from_enforce_act(p_act);
return p >= r;
let r = AFAccessLevel::from_enforce_act(r_act.as_str());
let p = AFAccessLevel::from_enforce_act(p_act.as_str());
return Dynamic::from_bool(p >= r);
}

if r_act.starts_with("l:") && p_act.starts_with("r:") {
let r = AFAccessLevel::from_enforce_act(r_act);
let role = AFRole::from_enforce_act(p_act);
let r = AFAccessLevel::from_enforce_act(r_act.as_str());
let role = AFRole::from_enforce_act(p_act.as_str());
let p = AFAccessLevel::from(&role);
return p >= r;
return Dynamic::from_bool(p >= r);
}

false
Dynamic::from_bool(false)
}

/// Represents the entity stored at the index of the access control policy.
Expand Down
7 changes: 2 additions & 5 deletions libs/access-control/src/casbin/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ mod tests {
entity::ObjectType,
};
use async_trait::async_trait;
use casbin::{prelude::*, rhai::ImmutableString};
use casbin::{function_map::OperatorFunction, prelude::*};
use database_entity::dto::{AFAccessLevel, AFRole};

use super::{AFEnforcer, EnforcerGroup};
Expand All @@ -268,10 +268,7 @@ mod tests {
.await
.unwrap();

enforcer.add_function(
"cmpRoleOrLevel",
|r: ImmutableString, p: ImmutableString| cmp_role_or_level(r.as_str(), p.as_str()),
);
enforcer.add_function("cmpRoleOrLevel", OperatorFunction::Arg2(cmp_role_or_level));
AFEnforcer::new(enforcer, enforce_group).await.unwrap()
}
#[tokio::test]
Expand Down
6 changes: 5 additions & 1 deletion libs/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ pgvector = { workspace = true, features = ["sqlx"] }
tracing = { version = "0.1.40" }
uuid = { workspace = true, features = ["serde", "v4"] }
chrono = { version = "0.4", features = ["serde"] }
redis.workspace = true
redis = { workspace = true, features = [
"aio",
"tokio-comp",
"connection-manager",
] }
futures-util = "0.3.30"
bytes = "1.5"
aws-sdk-s3 = { version = "1.36.0", features = [
Expand Down

0 comments on commit 1c51656

Please sign in to comment.