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.

feat(channels): join channel using viewonly permission (#5076)

Will Hutchinson4109e70fecd1
diff --git a/crates/channels/src/inbound/axum_router.rs b/crates/channels/src/inbound/axum_router.rs
index 1a285ba7f1..86eab6e7c8 100644
--- a/crates/channels/src/inbound/axum_router.rs
+++ b/crates/channels/src/inbound/axum_router.rs
@@ -36,7 +36,7 @@ use entity_access::{
models::{
AccessError, AccessLevel, AdminParticipantRole, EntityAccessAuth, EntityAccessReceipt,
EntityPermission, EntityType, MemberParticipantRole, OwnerParticipantRole,
- RequiredPermission,
+ RequiredPermission, ViewOnly,
},
ports::EntityAccessService,
},
@@ -208,13 +208,6 @@ pub struct MessageContextParams {
after: i64,
}
-/// Path params for channel-level endpoints.
-#[derive(Debug, Deserialize)]
-pub struct ChannelPath {
- /// Channel ID from path.
- channel_id: Uuid,
-}
-
/// Path params for the attachment-references endpoint.
#[derive(Debug, Deserialize)]
pub struct AttachmentReferencesPath {
@@ -911,14 +904,11 @@ pub async fn join_channel_handler<
Auth: MacroAuthorizationService,
>(
State(state): State<ChannelsRouterState<S, Svc, Auth>>,
- Path(path): Path<ChannelPath>,
- user: MacroAuthorizationExtractor<Auth>,
+ access: ChannelAccessLevelExtractor<ViewOnly, Svc, Auth>,
) -> Result<StatusCode, ChannelsHandlerErr> {
- let channel_id = path.channel_id;
- state
- .service
- .join_channel(Sender::new_from_user(user.macro_user_id), channel_id)
- .await?;
+ let channel_id = channel_id_from_receipt(&access.entity_access_receipt)?;
+ let actor = user_actor_from_receipt(&access.entity_access_receipt)?;
+ state.service.join_channel(actor, channel_id).await?;
Ok(StatusCode::OK)
}
diff --git a/crates/channels/src/inbound/axum_router/test.rs b/crates/channels/src/inbound/axum_router/test.rs
index 912505078a..e8d621b3a7 100644
--- a/crates/channels/src/inbound/axum_router/test.rs
+++ b/crates/channels/src/inbound/axum_router/test.rs
@@ -69,6 +69,7 @@ struct PermissionCall {
#[derive(Clone)]
struct TestAccessService {
mode: AccessMode,
+ channel_view_only: bool,
permission_calls: Arc<Mutex<Vec<PermissionCall>>>,
}
@@ -77,6 +78,13 @@ impl TestAccessService {
Self::new(AccessMode::Allow)
}
+ fn channel_view_only() -> Self {
+ Self {
+ channel_view_only: true,
+ ..Self::allow()
+ }
+ }
+
fn deny() -> Self {
Self::new(AccessMode::Deny)
}
@@ -88,6 +96,7 @@ impl TestAccessService {
fn new(mode: AccessMode) -> Self {
Self {
mode,
+ channel_view_only: false,
permission_calls: Arc::default(),
}
}
@@ -186,6 +195,10 @@ impl EntityAccessService for TestAccessService {
organization_id: user_org_id,
});
+ if self.channel_view_only && entity_type == EntityType::Channel {
+ return Ok(EntityPermission::ChannelViewOnly);
+ }
+
match self.mode {
AccessMode::Allow => match entity_type {
EntityType::Channel => Ok(EntityPermission::ChannelRole {
@@ -642,6 +655,7 @@ impl ChannelService for JoinLinkService {
#[derive(Clone, Default)]
struct RecordingMutationService {
+ joins: Arc<Mutex<Vec<(Sender, Uuid)>>>,
posts: Arc<Mutex<Vec<(Sender, Uuid, PostMessageRequest)>>>,
}
@@ -827,9 +841,10 @@ impl ChannelService for RecordingMutationService {
async fn join_channel(
&self,
- _actor: Sender,
- _channel_id: Uuid,
+ actor: Sender,
+ channel_id: Uuid,
) -> Result<(), ChannelMutationErr> {
+ self.joins.lock().unwrap().push((actor, channel_id));
Ok(())
}
@@ -1300,6 +1315,103 @@ async fn authenticated_user_can_join_by_code_without_channel_access() {
assert_eq!(users[0].as_ref(), "macro|[email protected]");
}
+#[tokio::test]
+async fn channel_view_only_user_can_join_channel_by_id() {
+ let channel_id = Uuid::new_v4();
+ let mutation_service = RecordingMutationService::default();
+ let joins = mutation_service.joins.clone();
+ let access_service = TestAccessService::channel_view_only();
+ let router = channels_router(ChannelsRouterState::new(
+ mutation_service,
+ access_service.clone(),
+ authorization_state(),
+ ))
+ .layer(axum::middleware::map_request(attach_bearer));
+
+ let response = router
+ .oneshot(
+ Request::post(format!("/{channel_id}/join"))
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK);
+ assert_eq!(
+ access_service.permission_calls(),
+ [PermissionCall {
+ user_id: Some(TEST_USER_ID.to_string()),
+ entity_id: channel_id.to_string(),
+ entity_type: EntityType::Channel,
+ organization_id: None,
+ }]
+ );
+ let joins = joins.lock().unwrap();
+ assert_eq!(joins.len(), 1);
+ assert_eq!(joins[0].0.as_ref(), TEST_USER_ID);
+ assert_eq!(joins[0].1, channel_id);
+}
+
+#[tokio::test]
+async fn user_without_channel_access_cannot_join_channel_by_id() {
+ let channel_id = Uuid::new_v4();
+ let mutation_service = RecordingMutationService::default();
+ let joins = mutation_service.joins.clone();
+ let router = channels_router(ChannelsRouterState::new(
+ mutation_service,
+ TestAccessService::deny(),
+ authorization_state(),
+ ))
+ .layer(axum::middleware::map_request(attach_bearer));
+
+ let response = router
+ .oneshot(
+ Request::post(format!("/{channel_id}/join"))
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
+ assert!(joins.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn malformed_channel_id_does_not_invoke_join_service() {
+ let mutation_service = RecordingMutationService::default();
+ let joins = mutation_service.joins.clone();
+ let access_service = TestAccessService::channel_view_only();
+ let router = channels_router(ChannelsRouterState::new(
+ mutation_service,
+ access_service.clone(),
+ authorization_state(),
+ ))
+ .layer(axum::middleware::map_request(attach_bearer));
+
+ let response = router
+ .oneshot(
+ Request::post("/not-a-uuid/join")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
+ assert_eq!(
+ access_service.permission_calls(),
+ [PermissionCall {
+ user_id: Some(TEST_USER_ID.to_string()),
+ entity_id: "not-a-uuid".to_string(),
+ entity_type: EntityType::Channel,
+ organization_id: None,
+ }]
+ );
+ assert!(joins.lock().unwrap().is_empty());
+}
+
#[tokio::test]
async fn post_message_route_uses_entity_access_and_mutation_service() {
let mutation_service = RecordingMutationService::default();