This commit is contained in:
MythEclipse
2025-09-25 17:26:39 +07:00
parent ac44867ec1
commit df11aa5eb8
+44 -111
View File
@@ -15,6 +15,39 @@ use axum::response::IntoResponse;
use axum::{Extension, Json}; use axum::{Extension, Json};
use super::teams_service::{TeamsServiceTrait, TeamsService}; use super::teams_service::{TeamsServiceTrait, TeamsService};
// Helper function for endpoints requiring authentication without specific permissions
async fn authenticated<F, Fut>(
headers: HeaderMap,
state: Extension<AppState>,
f: F,
) -> impl IntoResponse
where
F: FnOnce(crate::Claims, AppState) -> Fut,
Fut: std::future::Future<Output = impl IntoResponse> + Send,
{
match permissions_guard(headers, state, vec![]).await {
Ok((claims, state)) => f(claims, state).await,
Err(response) => response,
}
}
// Helper function for endpoints requiring specific permissions
async fn with_perms<F, Fut>(
headers: HeaderMap,
state: Extension<AppState>,
perms: Vec<String>,
f: F,
) -> impl IntoResponse
where
F: FnOnce(crate::Claims, AppState) -> Fut,
Fut: std::future::Future<Output = impl IntoResponse> + Send,
{
match permissions_guard(headers, state, perms).await {
Ok((claims, state)) => f(claims, state).await,
Err(response) => response,
}
}
#[utoipa::path( #[utoipa::path(
get, get,
security( security(
@@ -105,16 +138,7 @@ pub async fn post_create_team(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Json(payload): Json<TeamsCreateRequestDto>, Json(payload): Json<TeamsCreateRequestDto>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::create_team(&state, claims, payload)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::create_team(&state, claims, payload).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -138,16 +162,7 @@ pub async fn put_update_team(
Path(id): Path<String>, Path(id): Path<String>,
Json(payload): Json<TeamsUpdateRequestDto>, Json(payload): Json<TeamsUpdateRequestDto>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::update_team(&state, claims, id, payload)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::update_team(&state, claims, id, payload).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -169,16 +184,7 @@ pub async fn delete_team(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::delete_team(&state, claims, id)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::delete_team(&state, claims, id).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -202,16 +208,7 @@ pub async fn post_invite_team_members(
Path(team_id): Path<String>, Path(team_id): Path<String>,
Json(payload): Json<TeamInviteRequestDto>, Json(payload): Json<TeamInviteRequestDto>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::invite_team_members(&state, claims, team_id, payload)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::invite_team_members(&state, claims, team_id, payload).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -234,16 +231,7 @@ pub async fn post_accept_invitation(
Path(token): Path<String>, Path(token): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let accept_dto = TeamAcceptInvitationRequestDto { token }; let accept_dto = TeamAcceptInvitationRequestDto { token };
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::accept_invitation(&state, claims, accept_dto)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::accept_invitation(&state, claims, accept_dto).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -288,16 +276,7 @@ pub async fn get_team_members(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::get_team_members(&state, claims, id)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::get_team_members(&state, claims, id).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -319,16 +298,7 @@ pub async fn post_leave_team(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), move |claims, state| TeamsService::leave_team(&state, claims, id)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::leave_team(&state, claims, id).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -346,16 +316,7 @@ pub async fn post_leave_current_team(
headers: HeaderMap, headers: HeaderMap,
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( authenticated(headers, Extension(state), |claims, state| TeamsService::leave_current_team(&state, claims)).await
headers,
Extension(state),
vec![],
)
.await
{
Ok((claims, state)) => TeamsService::leave_current_team(&state, claims).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -383,16 +344,7 @@ pub async fn get_admin_team_list(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
axum::extract::Query(meta): axum::extract::Query<MetaRequestDto>, axum::extract::Query(meta): axum::extract::Query<MetaRequestDto>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( with_perms(headers, Extension(state), vec![PermissionsEnum::ReadListTeams.to_string()], move |_claims, state| TeamsService::get_admin_team_list(&state, meta)).await
headers,
Extension(state),
vec![PermissionsEnum::ReadListTeams.to_string()],
)
.await
{
Ok((_claims, state)) => TeamsService::get_admin_team_list(&state, meta).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -414,16 +366,7 @@ pub async fn get_admin_team_by_id(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( with_perms(headers, Extension(state), vec![PermissionsEnum::ReadDetailTeams.to_string()], move |_claims, state| TeamsService::get_admin_team_by_id(&state, id)).await
headers,
Extension(state),
vec![PermissionsEnum::ReadDetailTeams.to_string()],
)
.await
{
Ok((_claims, state)) => TeamsService::get_admin_team_by_id(&state, id).await,
Err(response) => response,
}
} }
#[utoipa::path( #[utoipa::path(
@@ -445,15 +388,5 @@ pub async fn get_admin_team_members(
Extension(state): Extension<AppState>, Extension(state): Extension<AppState>,
Path(id): Path<String>, Path(id): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match permissions_guard( with_perms(headers, Extension(state), vec![PermissionsEnum::ReadDetailTeams.to_string()], move |_claims, state| TeamsService::get_admin_team_members(&state, id)).await
headers,
Extension(state),
vec![PermissionsEnum::ReadDetailTeams.to_string()],
)
.await
{
Ok((_claims, state)) => TeamsService::get_admin_team_members(&state, id).await,
Err(response) => response,
}
}
} }