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.

fix(dss): internal auth get document (#918)

Sean Aye54d2d323528a
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 7d680505c2..2f7be26f64 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -2704,6 +2704,7 @@ dependencies = [
"aws-sdk-secretsmanager",
"aws-sdk-sqs",
"axum",
+ "axum-extra",
"base64 0.22.1",
"bytes",
"bzip2",
diff --git a/rust/cloud-storage/comms/src/domain/models/channel_name.rs b/rust/cloud-storage/comms/src/domain/models/channel_name.rs
index ad79d1bdcb..ff633b49d6 100644
--- a/rust/cloud-storage/comms/src/domain/models/channel_name.rs
+++ b/rust/cloud-storage/comms/src/domain/models/channel_name.rs
@@ -27,7 +27,9 @@ pub fn resolve_channel_name(
}
}),
ChannelType::Private => resolve_private_channel_name(channel_name, participants, name_lookup),
- ChannelType::DirectMessage => resolve_direct_message_channel_name(channel_name, participants, &channel_id.0, user_id, name_lookup),
+ ChannelType::DirectMessage => match resolve_direct_message_channel_name(channel_name, participants, &channel_id.0, user_id, name_lookup) {
+ Ok(c) | Err(c) => c,
+ }
}
}
@@ -47,29 +49,33 @@ pub fn resolve_private_channel_name(
.join(", ")
}
+#[tracing::instrument(err)]
pub fn resolve_direct_message_channel_name(
channel_name: Option<&str>,
participants: &[ChannelParticipant],
channel_id: &Uuid,
user_id: MacroUserIdStr<'_>,
name_lookup: &NameLookup,
-) -> ChannelName {
+) -> Result<ChannelName, ChannelName> {
// Direct Message Channels should not have a name
if channel_name.is_some() {
tracing::warn!(channel_id=?channel_id, "direct message channel should not have a name");
}
if !participants.iter().any(|p| p.user_id == user_id) {
- return resolve_private_channel_name(channel_name, participants, name_lookup);
+ return Ok(resolve_private_channel_name(
+ channel_name,
+ participants,
+ name_lookup,
+ ));
}
let other_participant = participants.iter().find(|p| p.user_id != user_id);
if let Some(other) = other_participant {
- id_to_display_name(other.user_id.copied(), name_lookup)
+ Ok(id_to_display_name(other.user_id.copied(), name_lookup))
} else {
- tracing::error!(channel_id=?channel_id, "no other participant found, in a direct message channel");
- "Unknown".to_string()
+ Err("Unknown".to_string())
}
}
diff --git a/rust/cloud-storage/comms/src/domain/models/channel_name/tests.rs b/rust/cloud-storage/comms/src/domain/models/channel_name/tests.rs
index 517b4ae266..3f54e55449 100644
--- a/rust/cloud-storage/comms/src/domain/models/channel_name/tests.rs
+++ b/rust/cloud-storage/comms/src/domain/models/channel_name/tests.rs
@@ -86,8 +86,9 @@ fn test_resolve_direct_message_channel_name() {
&direct_message_channel.id.0,
MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
&Default::default()
- ),
- "user2"
+ )
+ .as_deref(),
+ Ok("user2")
);
}
@@ -113,8 +114,9 @@ fn test_resolve_direct_message_channel_name_for_other() {
&direct_message_channel.id.0,
MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
&Default::default()
- ),
- "user1, user2"
+ )
+ .as_deref(),
+ Ok("user1, user2")
);
}
diff --git a/rust/cloud-storage/document_storage_service/Cargo.toml b/rust/cloud-storage/document_storage_service/Cargo.toml
index b2ef2bf2fe..7c2120973d 100644
--- a/rust/cloud-storage/document_storage_service/Cargo.toml
+++ b/rust/cloud-storage/document_storage_service/Cargo.toml
@@ -32,6 +32,7 @@ aws-sdk-s3 = { workspace = true }
aws-sdk-secretsmanager = { workspace = true }
aws-sdk-sqs = { workspace = true }
axum = { workspace = true, features = ["macros", "multipart", "tower-log"] }
+axum-extra = { workspace = true }
base64 = { workspace = true }
bytes = { workspace = true }
bzip2 = { workspace = true, default-features = false, features = [
diff --git a/rust/cloud-storage/document_storage_service/src/api/documents/get_document.rs b/rust/cloud-storage/document_storage_service/src/api/documents/get_document.rs
index cf61d40e47..d31f7ca101 100644
--- a/rust/cloud-storage/document_storage_service/src/api/documents/get_document.rs
+++ b/rust/cloud-storage/document_storage_service/src/api/documents/get_document.rs
@@ -13,7 +13,7 @@ use model::{
DocumentBasic,
response::{GetDocumentResponse, GetDocumentResponseData},
},
- response::{GenericErrorResponse, GenericResponse},
+ response::GenericErrorResponse,
user::UserContext,
};
use models_permissions::share_permission::access_level::ViewAccessLevel;
@@ -43,17 +43,24 @@ pub struct Params {
(status = 500, body=GenericErrorResponse),
)
)]
-#[tracing::instrument(skip(db, user_context, access_level), fields(user_id=?user_context.user_id))]
+#[tracing::instrument(skip(db, user_context, access), fields(user_id=?user_context.user_id))]
pub async fn handler(
State(db): State<PgPool>,
- DocumentAccessExtractor { access_level, .. }: DocumentAccessExtractor<ViewAccessLevel>,
- maybe_internal: Option<Extension<InternalUser>>,
+ access: axum_extra::either::Either<
+ DocumentAccessExtractor<ViewAccessLevel>,
+ Option<Extension<InternalUser>>,
+ >,
user_context: Extension<UserContext>,
Path(Params { document_id }): Path<Params>,
-) -> impl IntoResponse {
- let access_level = match maybe_internal {
- Some(Extension(InternalUser { access_level })) => access_level,
- None => access_level,
+) -> Result<Json<GetDocumentResponse>, StatusCode> {
+ let access_level = match access {
+ axum_extra::either::Either::E1(DocumentAccessExtractor { access_level, .. }) => {
+ access_level
+ }
+ axum_extra::either::Either::E2(Some(Extension(InternalUser { access_level }))) => {
+ access_level
+ }
+ axum_extra::either::Either::E2(None) => return Err(StatusCode::UNAUTHORIZED),
};
let document_metadata = match get_document(&db, &document_id).await {
@@ -66,10 +73,7 @@ pub async fn handler(
{
status_code = StatusCode::NOT_FOUND;
}
- return GenericResponse::builder()
- .message("unable to get document")
- .is_error(true)
- .send(status_code);
+ return Err(status_code);
}
};
@@ -78,22 +82,18 @@ pub async fn handler(
Ok(view_location) => view_location,
Err(e) => {
tracing::error!(error=?e, "error getting view location");
- return GenericResponse::builder()
- .message("error getting view location")
- .is_error(true)
- .send(StatusCode::INTERNAL_SERVER_ERROR);
+ return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
- let response_data = GetDocumentResponseData {
- document_metadata,
- user_access_level: access_level,
- view_location: view_location.map(|v| v.location),
- };
-
- GenericResponse::builder()
- .data(&response_data)
- .send(StatusCode::OK)
+ Ok(Json(GetDocumentResponse {
+ error: false,
+ data: GetDocumentResponseData {
+ document_metadata,
+ user_access_level: access_level,
+ view_location: view_location.map(|v| v.location),
+ },
+ }))
}
/// Gets the basic document info for a document id.