Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(initiatives): entity_access_db_utils delete_user_entity_access_rows helper (#6420)
Will Hutchinson5fdee1f51b9e
diff --git a/.sqlx/query-38c7a45eb17e6724271aea04cd37c694903deaa7b0ab7aea71142b73ca040d7f.json b/.sqlx/query-38c7a45eb17e6724271aea04cd37c694903deaa7b0ab7aea71142b73ca040d7f.jsonnew file mode 100644index 0000000000..a4cdaf5870--- /dev/null+++ b/.sqlx/query-38c7a45eb17e6724271aea04cd37c694903deaa7b0ab7aea71142b73ca040d7f.json@@ -0,0 +1,29 @@+{+ "db_name": "PostgreSQL",+ "query": "\n DELETE FROM entity_access\n WHERE entity_id = $1\n AND entity_type = $2\n AND source_type = $3\n AND source_id = ANY($4)\n AND access_level != 'owner'\n AND granted_from_project_id IS NULL\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ {+ "Custom": {+ "name": "entity_access_source_type",+ "kind": {+ "Enum": [+ "channel",+ "team",+ "user",+ "bot"+ ]+ }+ }+ },+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "38c7a45eb17e6724271aea04cd37c694903deaa7b0ab7aea71142b73ca040d7f"+}diff --git a/crates/entity_access_db_utils/src/lib.rs b/crates/entity_access_db_utils/src/lib.rsindex e26007b29d..2eeaaaa3d4 100644--- a/crates/entity_access_db_utils/src/lib.rs+++ b/crates/entity_access_db_utils/src/lib.rs@@ -128,6 +128,50 @@ pub async fn remove_non_owner_user_entity_access(Ok(())}+/// Deletes the direct user grants named by `user_ids` on the given entity.+///+/// Only the rows this entity granted those users directly are removed. An owner+/// row survives even when its user is named, and a grant inherited from a project+/// (`granted_from_project_id IS NOT NULL`) survives because the project still+/// grants it. Team, bot, and channel grants are out of scope. Use this to revoke+/// one membership. [`remove_non_owner_user_entity_access`] is the wider+/// public-to-private sweep.+///+/// *NOTE*: The transaction does not get committed automatically.+#[tracing::instrument(skip(transaction), err)]+pub async fn delete_user_entity_access_rows(+ transaction: &mut Transaction<'_, Postgres>,+ entity_id: ¯o_uuid::Uuid,+ entity_type: EntityType,+ user_ids: &[MacroUserIdStr<'_>],+) -> Result<(), sqlx::Error> {+ if user_ids.is_empty() {+ return Ok(());+ }++ let macro_ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();++ sqlx::query!(+ r#"+ DELETE FROM entity_access+ WHERE entity_id = $1+ AND entity_type = $2+ AND source_type = $3+ AND source_id = ANY($4)+ AND access_level != 'owner'+ AND granted_from_project_id IS NULL+ "#,+ entity_id,+ entity_type.as_ref(),+ EntityAccessSourceType::User as _,+ macro_ids.as_slice(),+ )+ .execute(transaction.as_mut())+ .await?;++ Ok(())+}+/// Deletes all entity_access rows for a given (entity_id, entity_type)./// *NOTE*: The transaction does not get committed automatically.#[tracing::instrument(skip(transaction), err)]diff --git a/crates/entity_access_db_utils/src/test.rs b/crates/entity_access_db_utils/src/test.rsindex c837b31dc6..f2e060c235 100644--- a/crates/entity_access_db_utils/src/test.rs+++ b/crates/entity_access_db_utils/src/test.rs@@ -229,6 +229,19 @@ async fn fetch_entity_access_rows(.unwrap()}+fn grants(rows: &[EntityAccessRow]) -> Vec<(&str, &str, AccessLevel, Option<&str>)> {+ rows.iter()+ .map(|row| {+ (+ row.source_type.as_str(),+ row.source_id.as_str(),+ row.access_level,+ row.granted_from_project_id.as_deref(),+ )+ })+ .collect()+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS",fixtures(path = "../fixtures", scripts("upsert_test_data"))@@ -719,6 +732,211 @@ async fn remove_non_owner_user_entity_access_preserves_non_user_inherited_and_otassert!(other_type_rows[0].granted_from_project_id.is_none());}+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../fixtures", scripts("upsert_test_data"))+)]+async fn delete_user_entity_access_rows_removes_targeted_direct_grants_only(pool: Pool<Postgres>) {+ let initiative_id = Uuid::from_u128(0x0b01);+ let other_initiative_id = Uuid::from_u128(0x0b02);+ let root_project_id = ROOT_PROJECT_ID.to_string();+ let owner = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let alice = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let bob = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let carol = MacroUserIdStr::try_from_email("[email protected]").unwrap();++ let mut tx = pool.begin().await.unwrap();+ for (user, access_level, granted_from_project_id) in [+ (&owner, AccessLevel::Owner, None),+ (&alice, AccessLevel::Edit, None),+ (&bob, AccessLevel::View, None),+ (&bob, AccessLevel::Edit, Some(root_project_id.as_str())),+ (&carol, AccessLevel::View, None),+ ] {+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ user.as_ref(),+ EntityAccessSourceType::User,+ access_level,+ granted_from_project_id,+ )+ .await;+ }+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ BOT_PRINCIPAL,+ EntityAccessSourceType::Bot,+ AccessLevel::Edit,+ None,+ )+ .await;+ for (source_id, source_type, access_level) in [+ ("team-1", EntityAccessSourceType::Team, AccessLevel::Edit),+ (+ "channel-1",+ EntityAccessSourceType::Channel,+ AccessLevel::Comment,+ ),+ ] {+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ source_id,+ source_type,+ access_level,+ None,+ )+ .await;+ }+ insert_entity_access_for_test(+ &mut tx,+ &other_initiative_id,+ EntityType::Initiative,+ alice.as_ref(),+ EntityAccessSourceType::User,+ AccessLevel::Edit,+ None,+ )+ .await;+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Document,+ alice.as_ref(),+ EntityAccessSourceType::User,+ AccessLevel::Edit,+ None,+ )+ .await;++ delete_user_entity_access_rows(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ &[owner.clone(), alice.clone(), bob.clone()],+ )+ .await+ .unwrap();+ tx.commit().await.unwrap();++ let rows = fetch_entity_access_rows(&pool, &initiative_id, EntityType::Initiative).await;+ assert_eq!(+ grants(&rows),+ vec![+ ("bot", BOT_PRINCIPAL, AccessLevel::Edit, None),+ ("channel", "channel-1", AccessLevel::Comment, None),+ ("team", "team-1", AccessLevel::Edit, None),+ (+ "user",+ "macro|[email protected]",+ AccessLevel::Edit,+ Some(root_project_id.as_str()),+ ),+ ("user", "macro|[email protected]", AccessLevel::View, None),+ ("user", "macro|[email protected]", AccessLevel::Owner, None),+ ]+ );++ let other_entity_rows =+ fetch_entity_access_rows(&pool, &other_initiative_id, EntityType::Initiative).await;+ assert_eq!(+ grants(&other_entity_rows),+ vec![("user", "macro|[email protected]", AccessLevel::Edit, None)]+ );++ let other_type_rows =+ fetch_entity_access_rows(&pool, &initiative_id, EntityType::Document).await;+ assert_eq!(+ grants(&other_type_rows),+ vec![("user", "macro|[email protected]", AccessLevel::Edit, None)]+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn delete_user_entity_access_rows_without_user_ids_keeps_every_row(pool: Pool<Postgres>) {+ let initiative_id = Uuid::from_u128(0x0b03);+ let owner = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let alice = MacroUserIdStr::try_from_email("[email protected]").unwrap();++ let mut tx = pool.begin().await.unwrap();+ for (user, access_level) in [(&owner, AccessLevel::Owner), (&alice, AccessLevel::Edit)] {+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ user.as_ref(),+ EntityAccessSourceType::User,+ access_level,+ None,+ )+ .await;+ }++ delete_user_entity_access_rows(&mut tx, &initiative_id, EntityType::Initiative, &[])+ .await+ .unwrap();+ tx.commit().await.unwrap();++ let rows = fetch_entity_access_rows(&pool, &initiative_id, EntityType::Initiative).await;+ assert_eq!(+ grants(&rows),+ vec![+ ("user", "macro|[email protected]", AccessLevel::Edit, None),+ ("user", "macro|[email protected]", AccessLevel::Owner, None),+ ]+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn delete_user_entity_access_rows_keeps_team_and_channel_grants_that_share_a_targeted_user_id(+ pool: Pool<Postgres>,+) {+ let initiative_id = Uuid::from_u128(0x0b04);+ let alice = MacroUserIdStr::try_from_email("[email protected]").unwrap();++ let mut tx = pool.begin().await.unwrap();+ for (source_type, access_level) in [+ (EntityAccessSourceType::User, AccessLevel::Edit),+ (EntityAccessSourceType::Team, AccessLevel::Edit),+ (EntityAccessSourceType::Channel, AccessLevel::Comment),+ ] {+ insert_entity_access_for_test(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ alice.as_ref(),+ source_type,+ access_level,+ None,+ )+ .await;+ }++ delete_user_entity_access_rows(+ &mut tx,+ &initiative_id,+ EntityType::Initiative,+ &[alice.clone()],+ )+ .await+ .unwrap();+ tx.commit().await.unwrap();++ let rows = fetch_entity_access_rows(&pool, &initiative_id, EntityType::Initiative).await;+ assert_eq!(+ grants(&rows),+ vec![+ ("channel", alice.as_ref(), AccessLevel::Comment, None),+ ("team", alice.as_ref(), AccessLevel::Edit, None),+ ]+ );+}+const OWNER_TEAM_ID: Uuid = Uuid::from_u128(0x0000000a_0000_0000_0000_00000000000a);const BOT_PRINCIPAL: &str = "bot|0000000b-0000-0000-0000-00000000000b";