Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

add [tasks] - grant permissions to task assignee (#760)

Daniel Kweonebc1e431167a
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 96e77372c8..7ac584a5f2 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -6594,6 +6594,7 @@ dependencies = [
"anyhow",
"comms_service_client",
"email_db_client",
+ "macro_db_client",
"macro_db_migrator",
"macro_middleware",
"macro_uuid",
diff --git a/rust/cloud-storage/properties/Cargo.toml b/rust/cloud-storage/properties/Cargo.toml
index 43a9667341..aaa7656810 100644
--- a/rust/cloud-storage/properties/Cargo.toml
+++ b/rust/cloud-storage/properties/Cargo.toml
@@ -8,6 +8,7 @@ version = "0.1.0"
anyhow = { workspace = true }
comms_service_client = { path = "../comms_service_client" }
email_db_client = { path = "../email_db_client" }
+macro_db_client = { path = "../macro_db_client" }
macro_middleware = { path = "../macro_middleware", default-features = false, features = ["cloud_storage", "user_permissions"] }
macro_uuid = { path = "../macro_uuid" }
models_permissions = { path = "../models_permissions" }
diff --git a/rust/cloud-storage/properties/src/domain/ports.rs b/rust/cloud-storage/properties/src/domain/ports.rs
index 3aaf6e0740..04c459dd46 100644
--- a/rust/cloud-storage/properties/src/domain/ports.rs
+++ b/rust/cloud-storage/properties/src/domain/ports.rs
@@ -104,4 +104,12 @@ pub trait PermissionService: Send + Sync + 'static {
entity_id: &str,
entity_type: EntityType,
) -> impl Future<Output = Result<(), Self::Err>> + Send;
+
+ /// Grant edit permissions to users for a task.
+ /// This is used when task assignees are updated to ensure they can edit the task.
+ fn grant_permissions_to_task(
+ &self,
+ user_ids: &[String],
+ task_id: &str,
+ ) -> impl Future<Output = Result<(), Self::Err>> + Send;
}
diff --git a/rust/cloud-storage/properties/src/domain/service_impl/task_properties.rs b/rust/cloud-storage/properties/src/domain/service_impl/task_properties.rs
index 1ce0c4d505..921f4ec55c 100644
--- a/rust/cloud-storage/properties/src/domain/service_impl/task_properties.rs
+++ b/rust/cloud-storage/properties/src/domain/service_impl/task_properties.rs
@@ -89,15 +89,15 @@ where
Ok(())
}
- /// Handle task assignees property with notifications and permissions.
+ /// Handle task assignees property with permissions.
/// Assignees is a multi-select entity property, so only accepts MultiEntityReference.
- /// If value is None (clearing assignees), there's nothing to do for notifications/permissions.
+ /// If value is None (clearing assignees), there's nothing to do for permissions.
pub async fn handle_task_assignees_property(
&self,
entity_id: &str,
value: Option<SetPropertyValue>,
) -> Result<(), PropertiesErr> {
- // Clearing assignees - nothing to do for notifications/permissions
+ // Clearing assignees - nothing to do for permissions
let Some(SetPropertyValue::MultiEntityReference { references }) = &value else {
if value.is_some() {
// Assignees is multi-select, so only MultiEntityReference is valid
@@ -110,34 +110,46 @@ where
};
let assignee_ids: Vec<String> = references.iter().map(|r| r.entity_id.clone()).collect();
+ if assignee_ids.is_empty() {
+ return Ok(());
+ }
+
let task_id = Uuid::parse_str(entity_id)
.map_err(|_| PropertiesErr::Validation("Invalid task ID".to_string()))?;
- self.handle_task_assignee_notifications(task_id, &assignee_ids)
- .await?;
+
self.handle_task_assignee_permissions(task_id, &assignee_ids)
.await?;
Ok(())
}
- /// Handle notifications when task assignees are updated.
- /// This is a no-op for now.
- pub async fn handle_task_assignee_notifications(
- &self,
- _task_id: Uuid,
- _assignee_ids: &[String],
- ) -> Result<(), PropertiesErr> {
- // No-op for now
- Ok(())
- }
-
/// Handle permissions when task assignees are updated.
- /// This is a no-op for now.
+ /// Grants edit permissions to all assignees so they can edit the task.
pub async fn handle_task_assignee_permissions(
&self,
- _task_id: Uuid,
- _assignee_ids: &[String],
+ task_id: Uuid,
+ assignee_ids: &[String],
) -> Result<(), PropertiesErr> {
- // No-op for now
+ if assignee_ids.is_empty() {
+ return Ok(());
+ }
+
+ let permission_service = self
+ .permission_service
+ .as_ref()
+ .ok_or(PropertiesErr::PermissionDenied)?;
+
+ tracing::debug!(
+ task_id = %task_id,
+ assignee_count = assignee_ids.len(),
+ "granting edit permissions to task assignees"
+ );
+
+ permission_service
+ .grant_permissions_to_task(assignee_ids, &task_id.to_string())
+ .await
+ .map_err(anyhow::Error::from)
+ .map_err(PropertiesErr::Repo)?;
+
Ok(())
}
}
diff --git a/rust/cloud-storage/properties/src/domain/test.rs b/rust/cloud-storage/properties/src/domain/test.rs
index 4dee503f8b..387717c557 100644
--- a/rust/cloud-storage/properties/src/domain/test.rs
+++ b/rust/cloud-storage/properties/src/domain/test.rs
@@ -6,7 +6,9 @@ use crate::domain::{
service::PropertiesService,
};
use anyhow::anyhow;
-use models_properties::{EntityType, service::property_value::PropertyValue};
+use models_properties::{
+ EntityType, service::property_value::PropertyValue, shared::EntityReference,
+};
use system_properties::{StatusOption, SystemPropertyKey};
use uuid::Uuid;
@@ -314,3 +316,149 @@ async fn test_get_system_property_value_error_path() {
assert_eq!(err.to_string(), "db error");
}
+
+// ============================================================================
+// handle_task_assignee_permissions unit tests
+// ============================================================================
+
+#[tokio::test]
+async fn test_handle_task_assignee_permissions_grants_permissions() {
+ let repo = MockPropertiesRepo::new();
+ let mut perm_service = MockPermissionService::new();
+
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+ let assignee_ids = vec!["user1".to_string(), "user2".to_string()];
+
+ perm_service
+ .expect_grant_permissions_to_task()
+ .withf(move |user_ids, task_id_param| {
+ user_ids.len() == 2
+ && user_ids.contains(&"user1".to_string())
+ && user_ids.contains(&"user2".to_string())
+ && task_id_param == &task_id.to_string()
+ })
+ .returning(|_, _| Box::pin(async { Ok(()) }));
+
+ let service = PropertiesServiceImpl::new(repo, Some(perm_service));
+
+ service
+ .handle_task_assignee_permissions(task_id, &assignee_ids)
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn test_handle_task_assignee_permissions_empty_assignees() {
+ let repo = MockPropertiesRepo::new();
+ let perm_service = MockPermissionService::new();
+
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+
+ let service = PropertiesServiceImpl::new(repo, Some(perm_service));
+
+ // Should return Ok without calling permission service
+ service
+ .handle_task_assignee_permissions(task_id, &[])
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn test_handle_task_assignee_permissions_no_service() {
+ let repo = MockPropertiesRepo::new();
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+ let assignee_ids = vec!["user1".to_string()];
+
+ let service = PropertiesServiceImpl::new(repo, None::<MockPermissionService>);
+
+ let err = service
+ .handle_task_assignee_permissions(task_id, &assignee_ids)
+ .await
+ .unwrap_err();
+
+ assert!(matches!(
+ err,
+ crate::domain::error::PropertiesErr::PermissionDenied
+ ));
+}
+
+#[tokio::test]
+async fn test_handle_task_assignee_permissions_error_propagates() {
+ let repo = MockPropertiesRepo::new();
+ let mut perm_service = MockPermissionService::new();
+
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+ let assignee_ids = vec!["user1".to_string()];
+
+ perm_service
+ .expect_grant_permissions_to_task()
+ .returning(|_, _| Box::pin(async { Err(anyhow!("permission error")) }));
+
+ let service = PropertiesServiceImpl::new(repo, Some(perm_service));
+
+ let err = service
+ .handle_task_assignee_permissions(task_id, &assignee_ids)
+ .await
+ .unwrap_err();
+
+ assert_eq!(err.to_string(), "permission error");
+}
+
+// ============================================================================
+// handle_task_assignees_property integration tests
+// ============================================================================
+
+#[tokio::test]
+async fn test_handle_task_assignees_property_calls_permission_handler() {
+ let mut repo = MockPropertiesRepo::new();
+ let mut perm_service = MockPermissionService::new();
+
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+ let entity_id = task_id.to_string();
+ let assignees = vec!["user1".to_string(), "user2".to_string()];
+
+ let value = Some(
+ models_properties::api::requests::SetPropertyValue::MultiEntityReference {
+ references: assignees
+ .iter()
+ .map(|id| models_properties::shared::EntityReference {
+ entity_type: EntityType::User,
+ entity_id: id.clone(),
+ specific_message_id: None,
+ })
+ .collect(),
+ },
+ );
+
+ // Mock: permissions should be granted to all assignees
+ let entity_id_clone = entity_id.clone();
+ perm_service
+ .expect_grant_permissions_to_task()
+ .times(1)
+ .withf(move |user_ids, tid| user_ids.len() == 2 && tid == &entity_id_clone)
+ .returning(|_, _| Box::pin(async { Ok(()) }));
+
+ let service = PropertiesServiceImpl::new(repo, Some(perm_service));
+
+ service
+ .handle_task_assignees_property(&entity_id, value)
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn test_handle_task_assignees_property_clearing_assignees() {
+ let repo = MockPropertiesRepo::new();
+ let perm_service = MockPermissionService::new();
+
+ let task_id = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
+ let entity_id = task_id.to_string();
+
+ let service = PropertiesServiceImpl::new(repo, Some(perm_service));
+
+ // Should return Ok without calling any handlers
+ service
+ .handle_task_assignees_property(&entity_id, None)
+ .await
+ .unwrap();
+}
diff --git a/rust/cloud-storage/properties/src/outbound/permission_service.rs b/rust/cloud-storage/properties/src/outbound/permission_service.rs
index 9e8a031060..f622ac8b65 100644
--- a/rust/cloud-storage/properties/src/outbound/permission_service.rs
+++ b/rust/cloud-storage/properties/src/outbound/permission_service.rs
@@ -82,4 +82,31 @@ impl PermissionService for PermissionServiceImpl {
Some(_) | None => anyhow::bail!("Access denied"),
}
}
+
+ #[tracing::instrument(skip(self), fields(task_id = %task_id, user_count = user_ids.len()), err)]
+ async fn grant_permissions_to_task(
+ &self,
+ user_ids: &[String],
+ task_id: &str,
+ ) -> Result<(), Self::Err> {
+ if user_ids.is_empty() {
+ return Ok(());
+ }
+
+ // Tasks are stored as "document" type in the permission system
+ let item_type = "document";
+
+ // Grant edit permissions to all users
+ macro_db_client::item_access::insert::upsert_user_item_access_bulk(
+ &self.db,
+ user_ids,
+ task_id,
+ item_type,
+ AccessLevel::Edit,
+ None, // No channel association for direct task assignee permissions
+ )
+ .await?;
+
+ Ok(())
+ }
}