diff --git a/libs/database/src/collab/collab_db_ops.rs b/libs/database/src/collab/collab_db_ops.rs index 8dd55e13..b1880ddf 100644 --- a/libs/database/src/collab/collab_db_ops.rs +++ b/libs/database/src/collab/collab_db_ops.rs @@ -517,10 +517,10 @@ pub async fn select_collab_members( } #[inline] -pub async fn select_collab_member( +pub async fn select_collab_member<'a, E: Executor<'a, Database = Postgres>>( uid: &i64, oid: &str, - pg_pool: &PgPool, + executor: E, ) -> Result { let row = sqlx::query( r#" @@ -532,7 +532,7 @@ pub async fn select_collab_member( ) .bind(uid) .bind(oid) - .fetch_one(pg_pool) + .fetch_one(executor) .await?; let member = collab_member_try_from_row(row)?; diff --git a/libs/database/src/collab/collab_storage.rs b/libs/database/src/collab/collab_storage.rs index d53ce3c0..9ca55c09 100644 --- a/libs/database/src/collab/collab_storage.rs +++ b/libs/database/src/collab/collab_storage.rs @@ -51,23 +51,11 @@ pub trait CollabStorageAccessControl: Send + Sync + 'static { #[async_trait] pub trait CollabStorage: Send + Sync + 'static { fn config(&self) -> &WriteConfig; - /// Checks if a collaboration with the given object ID exists in the storage. - /// - /// # Arguments - /// - /// * `object_id` - A string slice that holds the ID of the collaboration. - /// - /// # Returns - /// - /// * `bool` - `true` if the collaboration exists, `false` otherwise. - async fn is_exist(&self, object_id: &str) -> bool; async fn cache_collab(&self, object_id: &str, collab: Weak); async fn remove_collab_cache(&self, object_id: &str); - async fn is_collab_exist(&self, oid: &str) -> DatabaseResult; - async fn upsert_collab(&self, uid: &i64, params: CreateCollabParams) -> DatabaseResult<()>; /// Insert/update a new collaboration in the storage. @@ -136,10 +124,6 @@ where self.as_ref().config() } - async fn is_exist(&self, object_id: &str) -> bool { - self.as_ref().is_exist(object_id).await - } - async fn cache_collab(&self, object_id: &str, collab: Weak) { self.as_ref().cache_collab(object_id, collab).await } @@ -148,10 +132,6 @@ where self.as_ref().remove_collab_cache(object_id).await } - async fn is_collab_exist(&self, oid: &str) -> DatabaseResult { - self.as_ref().is_collab_exist(oid).await - } - async fn upsert_collab(&self, uid: &i64, params: CreateCollabParams) -> DatabaseResult<()> { self.as_ref().upsert_collab(uid, params).await } diff --git a/libs/database/src/workspace.rs b/libs/database/src/workspace.rs index ce5f7b31..68c6c851 100644 --- a/libs/database/src/workspace.rs +++ b/libs/database/src/workspace.rs @@ -310,8 +310,8 @@ pub async fn select_workspace_member_list( } #[inline] -pub async fn select_workspace_member( - pg_pool: &PgPool, +pub async fn select_workspace_member<'a, E: Executor<'a, Database = Postgres>>( + executor: E, uid: &i64, workspace_id: &Uuid, ) -> Result { @@ -327,7 +327,7 @@ pub async fn select_workspace_member( workspace_id, uid, ) - .fetch_one(pg_pool) + .fetch_one(executor) .await?; Ok(member) } diff --git a/src/biz/casbin/access_control.rs b/src/biz/casbin/access_control.rs index f465b941..c03c8703 100644 --- a/src/biz/casbin/access_control.rs +++ b/src/biz/casbin/access_control.rs @@ -368,10 +368,6 @@ pub struct CasbinWorkspaceAccessControl { #[async_trait] impl WorkspaceAccessControl for CasbinWorkspaceAccessControl { - async fn get_role_from_uuid(&self, uid: &i64, workspace_id: &Uuid) -> Result { - self.get_role_from_uid(uid, workspace_id).await - } - async fn get_role_from_uid(&self, uid: &i64, workspace_id: &Uuid) -> Result { let policies = self .casbin_access_control diff --git a/src/biz/collab/storage.rs b/src/biz/collab/storage.rs index 73a9edf8..efb0c685 100644 --- a/src/biz/collab/storage.rs +++ b/src/biz/collab/storage.rs @@ -1,7 +1,8 @@ use async_trait::async_trait; use collab::core::collab::MutexCollab; use database::collab::{ - CollabStorage, CollabStorageAccessControl, CollabStoragePgImpl, DatabaseResult, WriteConfig, + is_collab_exists, CollabStorage, CollabStorageAccessControl, CollabStoragePgImpl, DatabaseResult, + WriteConfig, }; use database_entity::dto::{ AFAccessLevel, AFSnapshotMeta, AFSnapshotMetas, CollabParams, CreateCollabParams, @@ -17,6 +18,7 @@ use anyhow::Context; use app_error::AppError; use collab::core::collab_plugin::EncodedCollab; use sqlx::{PgPool, Transaction}; +use std::ops::DerefMut; use std::{ collections::HashMap, sync::{Arc, Weak}, @@ -82,10 +84,6 @@ where self.disk_cache.config() } - async fn is_exist(&self, object_id: &str) -> bool { - self.disk_cache.is_exist(object_id).await - } - async fn cache_collab(&self, object_id: &str, collab: Weak) { tracing::trace!("cache opened collab:{}", object_id); self @@ -104,10 +102,6 @@ where .remove(object_id); } - async fn is_collab_exist(&self, oid: &str) -> DatabaseResult { - self.disk_cache.is_collab_exist(oid).await - } - async fn upsert_collab(&self, uid: &i64, params: CreateCollabParams) -> DatabaseResult<()> { let mut transaction = self .disk_cache @@ -143,7 +137,7 @@ where // 1. If the collab already exists, check if the user has enough permissions to update collab // 2. If the collab doesn't exist, check if the user has enough permissions to create collab. // TODO(nathan): remove is_collab_exist call and use access_control to check if the user has enough permissions to create collab. - let has_permission = if self.is_collab_exist(¶ms.object_id).await? { + let has_permission = if is_collab_exists(¶ms.object_id, transaction.deref_mut()).await? { // If the collab already exists, check if the user has enough permissions to update collab let level = self .access_control diff --git a/src/biz/workspace/access_control.rs b/src/biz/workspace/access_control.rs index a7ef4b4d..63520a6a 100644 --- a/src/biz/workspace/access_control.rs +++ b/src/biz/workspace/access_control.rs @@ -6,7 +6,7 @@ use actix_http::Method; use async_trait::async_trait; use database::user::select_uid_from_uuid; -use sqlx::PgPool; +use sqlx::{Executor, PgPool, Postgres}; use std::collections::hash_map::Entry; use std::collections::HashMap; @@ -20,7 +20,6 @@ use uuid::Uuid; #[async_trait] pub trait WorkspaceAccessControl: Send + Sync + 'static { - async fn get_role_from_uuid(&self, uid: &i64, workspace_id: &Uuid) -> Result; async fn get_role_from_uid(&self, uid: &i64, workspace_id: &Uuid) -> Result; async fn cache_role(&self, uid: &i64, workspace_id: &Uuid, role: AFRole) -> Result<(), AppError>; @@ -179,27 +178,6 @@ fn spawn_listen_on_workspace_member_change( }); } -#[async_trait] -impl WorkspaceAccessControl for WorkspaceAccessControlImpl { - async fn get_role_from_uuid(&self, uid: &i64, workspace_id: &Uuid) -> Result { - let role = self.get_user_workspace_role(uid, workspace_id).await?; - Ok(role) - } - - async fn get_role_from_uid(&self, uid: &i64, workspace_id: &Uuid) -> Result { - let role = self.get_user_workspace_role(uid, workspace_id).await?; - Ok(role) - } - - async fn cache_role(&self, uid: &i64, workspace_id: &Uuid, role: AFRole) -> Result<(), AppError> { - Err(AppError::Internal(anyhow!("Not support"))) - } - - async fn remove_member(&self, uid: &i64, workspace_id: &Uuid) -> Result<(), AppError> { - Err(AppError::Internal(anyhow!("Not support"))) - } -} - #[derive(Clone)] pub struct WorkspaceHttpAccessControl(pub Arc); #[async_trait]