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(teams) only premium users create teams (#2164)

Will Hutchinson0895da90c74d
diff --git a/rust/cloud-storage/roles_and_permissions/src/domain/port.rs b/rust/cloud-storage/roles_and_permissions/src/domain/port.rs
index 25a18684a5..7d129ef9e8 100644
--- a/rust/cloud-storage/roles_and_permissions/src/domain/port.rs
+++ b/rust/cloud-storage/roles_and_permissions/src/domain/port.rs
@@ -5,7 +5,7 @@ use std::collections::HashSet;
use macro_user_id::{email::Email, lowercased::Lowercase, user_id::MacroUserIdStr};
use crate::domain::model::{
- Permission, ProductTier, RoleId, SubscriptionStatus, UserRolesAndPermissionsError,
+ Permission, PermissionId, ProductTier, RoleId, SubscriptionStatus, UserRolesAndPermissionsError,
};
#[cfg(test)]
@@ -43,6 +43,12 @@ pub trait UserRolesAndPermissionsRepository: Clone + Send + Sync + 'static {
/// The UserRolesAndPermissionsService defines a set of actions to perform on the users for their roles and permissions
pub trait UserRolesAndPermissionsService: Clone + Send + Sync + 'static {
+ /// Gets the users permissions
+ fn get_user_permissions(
+ &self,
+ user_id: &MacroUserIdStr<'_>,
+ ) -> impl Future<Output = Result<HashSet<PermissionId>, UserRolesAndPermissionsError>> + Send;
+
/// Given a user id and a subscription status, update the user's roles accordingly
fn update_user_roles_and_permissions_for_subscription(
&self,
diff --git a/rust/cloud-storage/roles_and_permissions/src/domain/service.rs b/rust/cloud-storage/roles_and_permissions/src/domain/service.rs
index 3dddf745ed..75b31dd810 100644
--- a/rust/cloud-storage/roles_and_permissions/src/domain/service.rs
+++ b/rust/cloud-storage/roles_and_permissions/src/domain/service.rs
@@ -96,4 +96,18 @@ where
.remove_roles_from_user(user_id, role_ids.as_ref())
.await
}
+ #[tracing::instrument(skip(self), err)]
+ async fn get_user_permissions(
+ &self,
+ user_id: &MacroUserIdStr<'_>,
+ ) -> Result<std::collections::HashSet<super::model::PermissionId>, UserRolesAndPermissionsError>
+ {
+ Ok(self
+ .user_roles_and_permissions_repository
+ .get_user_permissions(user_id)
+ .await?
+ .into_iter()
+ .map(|p| p.id)
+ .collect())
+ }
}
diff --git a/rust/cloud-storage/teams/src/domain/team_repo.rs b/rust/cloud-storage/teams/src/domain/team_repo.rs
index b28a2797d6..8325dca63e 100644
--- a/rust/cloud-storage/teams/src/domain/team_repo.rs
+++ b/rust/cloud-storage/teams/src/domain/team_repo.rs
@@ -1,5 +1,7 @@
//! Contains the domain logic for teams
+use std::collections::HashSet;
+
use macro_user_id::{email::Email, lowercased::Lowercase, user_id::MacroUserIdStr};
use crate::domain::model::{
@@ -286,4 +288,12 @@ pub trait TeamService: Clone + Send + Sync + 'static {
team_id: &uuid::Uuid,
user_id: &MacroUserIdStr<'_>,
) -> impl Future<Output = Result<Option<TeamRole>, TeamError>> + Send;
+
+ /// Gets the team users permissions
+ fn get_team_user_permissions(
+ &self,
+ user_id: &MacroUserIdStr<'_>,
+ ) -> impl Future<
+ Output = Result<HashSet<roles_and_permissions::domain::model::PermissionId>, TeamError>,
+ > + Send;
}
diff --git a/rust/cloud-storage/teams/src/domain/team_service.rs b/rust/cloud-storage/teams/src/domain/team_service.rs
index 2f322b3d0f..151b927a9e 100644
--- a/rust/cloud-storage/teams/src/domain/team_service.rs
+++ b/rust/cloud-storage/teams/src/domain/team_service.rs
@@ -544,4 +544,15 @@ where
) -> Result<Option<TeamRole>, TeamError> {
self.team_repository.get_team_role(team_id, user_id).await
}
+
+ #[tracing::instrument(skip(self), err)]
+ async fn get_team_user_permissions(
+ &self,
+ user_id: &MacroUserIdStr<'_>,
+ ) -> Result<HashSet<roles_and_permissions::domain::model::PermissionId>, TeamError> {
+ self.user_roles_and_permissions_service
+ .get_user_permissions(user_id)
+ .await
+ .map_err(|e| TeamError::StorageLayerError(e.into()))
+ }
}
diff --git a/rust/cloud-storage/teams/src/inbound/axum_router/create_team.rs b/rust/cloud-storage/teams/src/inbound/axum_router/create_team.rs
index 10fa513c3b..540394323f 100644
--- a/rust/cloud-storage/teams/src/inbound/axum_router/create_team.rs
+++ b/rust/cloud-storage/teams/src/inbound/axum_router/create_team.rs
@@ -1,9 +1,8 @@
use axum::{Json, extract::State};
-use model_user::axum_extractor::MacroUserExtractor;
use crate::domain::{model::Team, team_repo::TeamService};
-use super::TeamRouterState;
+use super::{TeamRouterState, middleware::TeamPremiumUserExtractor};
/// The request body to create a new team
#[derive(serde::Serialize, serde::Deserialize, utoipa::ToSchema)]
@@ -26,12 +25,12 @@ pub struct CreateTeamRequest {
#[tracing::instrument(skip_all, err)]
pub async fn handler<T: TeamService>(
State(state): State<TeamRouterState<T>>,
- user_context: MacroUserExtractor,
+ premium_user: TeamPremiumUserExtractor<T>,
Json(req): Json<CreateTeamRequest>,
) -> Result<Json<Team>, crate::domain::model::CreateTeamError> {
let team = state
.service
- .create_team(&user_context.macro_user_id, &req.name)
+ .create_team(&premium_user.user_context.macro_user_id, &req.name)
.await?;
Ok(Json(team))
diff --git a/rust/cloud-storage/teams/src/inbound/axum_router/middleware.rs b/rust/cloud-storage/teams/src/inbound/axum_router/middleware.rs
index f23b6d582b..32dd3cb1d5 100644
--- a/rust/cloud-storage/teams/src/inbound/axum_router/middleware.rs
+++ b/rust/cloud-storage/teams/src/inbound/axum_router/middleware.rs
@@ -134,3 +134,77 @@ where
})
}
}
+
+/// Extractor that verifies the user has a specific permission, exposing the
+/// user context so the handler doesn't need to extract it again.
+pub struct TeamPremiumUserExtractor<TS: TeamService> {
+ /// The authenticated user context, available for use by the handler.
+ pub user_context: MacroUserExtractor,
+ _ts: PhantomData<TS>,
+}
+
+/// Errors from premium user extraction
+#[derive(Debug, thiserror::Error)]
+pub enum PremiumUserErr {
+ /// User context failed to extract
+ #[error("Internal server error")]
+ UserContextErr,
+ /// Failed to fetch permissions
+ #[error("Failed to fetch permissions")]
+ InternalErr(#[from] crate::domain::model::TeamError),
+ /// User does not have the required permission
+ #[error("User does not have the required permission")]
+ MissingPermission,
+}
+
+impl IntoResponse for PremiumUserErr {
+ fn into_response(self) -> Response {
+ let err = Json(ErrorResponse {
+ message: &self.to_string(),
+ });
+ match self {
+ PremiumUserErr::UserContextErr | PremiumUserErr::InternalErr(_) => {
+ (StatusCode::INTERNAL_SERVER_ERROR, err)
+ }
+ PremiumUserErr::MissingPermission => (StatusCode::FORBIDDEN, err),
+ }
+ .into_response()
+ }
+}
+
+use roles_and_permissions::domain::model::PermissionId;
+
+/// The permission ID required to create a team.
+const CREATE_TEAM_PERMISSION: PermissionId = PermissionId::ReadProfessionalFeatures;
+
+impl<S, TS> FromRequestParts<S> for TeamPremiumUserExtractor<TS>
+where
+ TeamRouterState<TS>: FromRef<S>,
+ S: Send + Sync + Clone + 'static,
+ TS: TeamService,
+{
+ type Rejection = PremiumUserErr;
+
+ async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
+ let router_state = TeamRouterState::<TS>::from_ref(state);
+
+ let user_context: MacroUserExtractor = parts
+ .extract()
+ .await
+ .map_err(|_| PremiumUserErr::UserContextErr)?;
+
+ let permissions: std::collections::HashSet<PermissionId> = router_state
+ .service
+ .get_team_user_permissions(&user_context.macro_user_id)
+ .await?;
+
+ if !permissions.contains(&CREATE_TEAM_PERMISSION) {
+ return Err(PremiumUserErr::MissingPermission);
+ }
+
+ Ok(Self {
+ user_context,
+ _ts: PhantomData,
+ })
+ }
+}