feat: Enhance validation and permissions handling across controllers

- Added `ValidatedJson` extractor for automatic JSON validation in `events_controller.rs`, `testimonials_controller.rs`, `mentors_controller.rs`, `gacha_items_controller.rs`, and `hackathon_controller.rs`.
- Replaced manual permission checks with `require_permissions!` and `require_auth!` macros in relevant controllers to streamline permission handling.
- Introduced `sanitization` utilities in `sanitization.rs` for improved input sanitization.
- Added `permission_macros.rs` to encapsulate permission checking logic and reduce boilerplate.
- Updated dependencies in `Cargo.toml` to include `serde_json` and `validator`.
- Implemented error handling improvements in `notification_service.rs` for better response management.
This commit is contained in:
MythEclipse
2025-10-28 14:04:41 +07:00
parent d4a6c4c9ea
commit b9a51ce6cc
17 changed files with 551 additions and 225 deletions
@@ -4,12 +4,13 @@ use crate::v1::gacha_items::GachaItemDto;
use crate::v1::gacha_items::gacha_items_dto::{GachaItemRequestDto, GachaItemUpdateRequestDto};
use crate::v1::gacha_items::gacha_items_service::GachaItemService;
use axum::{
Extension, Json,
Extension,
extract::{Path, Query},
http::HeaderMap,
response::IntoResponse,
};
use imphnen_iam::{PermissionsEnum, permissions_guard};
use imphnen_iam::{PermissionsEnum, require_permissions};
use imphnen_libs::ValidatedJson;
#[utoipa::path(
get,
@@ -36,16 +37,9 @@ pub async fn get_gacha_item_list(
Extension(state): Extension<AppState>,
Query(meta): Query<MetaRequestDto>,
) -> impl IntoResponse {
match permissions_guard(
headers,
Extension(state),
vec![PermissionsEnum::ReadListGachaItems],
)
.await
{
Ok((_user, state)) => GachaItemService::get_gacha_item_list(&state, meta).await,
Err(response) => response,
}
require_permissions!(headers, state, [PermissionsEnum::ReadListGachaItems], {
GachaItemService::get_gacha_item_list(&state, meta).await
})
}
#[utoipa::path(
@@ -65,16 +59,9 @@ pub async fn get_gacha_item_by_id(
Extension(state): Extension<AppState>,
Path(id): Path<String>,
) -> impl IntoResponse {
match permissions_guard(
headers,
Extension(state),
vec![PermissionsEnum::ReadDetailGachaItems],
)
.await
{
Ok((_user, state)) => GachaItemService::get_gacha_item_by_id(&state, id).await,
Err(response) => response,
}
require_permissions!(headers, state, [PermissionsEnum::ReadDetailGachaItems], {
GachaItemService::get_gacha_item_by_id(&state, id).await
})
}
#[utoipa::path(
@@ -92,18 +79,11 @@ pub async fn get_gacha_item_by_id(
pub async fn post_create_gacha_item(
headers: HeaderMap,
Extension(state): Extension<AppState>,
Json(payload): Json<GachaItemRequestDto>,
ValidatedJson(payload): ValidatedJson<GachaItemRequestDto>,
) -> impl IntoResponse {
match permissions_guard(
headers,
Extension(state),
vec![PermissionsEnum::CreateGachaItems],
)
.await
{
Ok((_user, state)) => GachaItemService::create_gacha_item(&state, payload).await,
Err(response) => response,
}
require_permissions!(headers, state, [PermissionsEnum::CreateGachaItems], {
GachaItemService::create_gacha_item(&state, payload).await
})
}
#[utoipa::path(
@@ -122,18 +102,11 @@ pub async fn put_update_gacha_item(
headers: HeaderMap,
Extension(state): Extension<AppState>,
Path(id): Path<String>,
Json(payload): Json<GachaItemUpdateRequestDto>,
ValidatedJson(payload): ValidatedJson<GachaItemUpdateRequestDto>,
) -> impl IntoResponse {
match permissions_guard(
headers,
Extension(state),
vec![PermissionsEnum::UpdateGachaItems],
)
.await
{
Ok((_user, state)) => GachaItemService::update_gacha_item(&state, payload, id).await,
Err(response) => response,
}
require_permissions!(headers, state, [PermissionsEnum::UpdateGachaItems], {
GachaItemService::update_gacha_item(&state, payload, id).await
})
}
#[utoipa::path(
@@ -152,14 +125,7 @@ pub async fn delete_gacha_item(
Extension(state): Extension<AppState>,
Path(id): Path<String>,
) -> impl IntoResponse {
match permissions_guard(
headers,
Extension(state),
vec![PermissionsEnum::DeleteGachaItems],
)
.await
{
Ok((_user, state)) => GachaItemService::delete_gacha_item(&state, id).await,
Err(response) => response,
}
require_permissions!(headers, state, [PermissionsEnum::DeleteGachaItems], {
GachaItemService::delete_gacha_item(&state, id).await
})
}
@@ -1,6 +1,6 @@
use crate::AppState;
use imphnen_entities::{MetaRequestDto, ResponseListSuccessDto, ResponseSuccessDto};
use imphnen_utils::{common_response, make_thing, success_list_response, success_response, validate_request};
use imphnen_utils::{common_response, make_thing, success_list_response, success_response};
use crate::v1::gacha_items::GachaItemDto;
use crate::v1::gacha_items::gacha_items_dto::{GachaItemRequestDto, GachaItemUpdateRequestDto};
use crate::v1::gacha_items::gacha_items_repository::GachaItemRepository;
@@ -44,9 +44,7 @@ impl GachaItemService {
state: &AppState,
payload: GachaItemRequestDto,
) -> Response {
if let Err((status, message)) = validate_request(&payload) {
return common_response(status, &message);
}
// Validation is now automatic via ValidatedJson extractor
let repo = GachaItemRepository::new(state);
let schema = GachaItemSchema {
id: make_thing(&ResourceEnum::GachaItems.to_string(), &payload.name), // Fixed: Use payload.name or some other identifier
@@ -65,9 +63,7 @@ impl GachaItemService {
payload: GachaItemUpdateRequestDto,
id: String,
) -> Response {
if let Err((status, message)) = validate_request(&payload) {
return common_response(status, &message);
}
// Validation is now automatic via ValidatedJson extractor
let repo = GachaItemRepository::new(state);
// Get current gacha item data first