Factorize RON error response for not authorized content

This commit is contained in:
Greg Burri 2025-05-01 20:56:20 +02:00
parent f23fd2b832
commit f31167dd95
2 changed files with 20 additions and 38 deletions

View file

@ -1,12 +1,14 @@
use axum::{ use axum::{
body::Bytes, body::Bytes,
http::{HeaderValue, StatusCode, header}, http::{HeaderValue, StatusCode, header},
response::{IntoResponse, Response}, response::{ErrorResponse, IntoResponse, Response},
}; };
use common::ron_api; use common::ron_api;
use ron::de::from_bytes; use ron::de::from_bytes;
use serde::{Serialize, de::DeserializeOwned}; use serde::{Serialize, de::DeserializeOwned};
use crate::consts;
pub const RON_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/ron"); pub const RON_CONTENT_TYPE: HeaderValue = HeaderValue::from_static("application/ron");
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]
@ -42,6 +44,13 @@ pub fn ron_error(status: StatusCode, message: &str) -> impl IntoResponse {
) )
} }
pub fn ron_error_not_autorized() -> ErrorResponse {
ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))
}
pub fn ron_response_ok<T>(ron: T) -> impl IntoResponse pub fn ron_response_ok<T>(ron: T) -> impl IntoResponse
where where
T: Serialize, T: Serialize,

View file

@ -1,9 +1,6 @@
use axum::{ use axum::response::Result;
http::StatusCode,
response::{ErrorResponse, Result},
};
use crate::{consts, data::db, data::model, ron_utils::ron_error}; use crate::{data::db, data::model, ron_utils::ron_error_not_autorized};
pub async fn check_user_rights_recipe( pub async fn check_user_rights_recipe(
connection: &db::Connection, connection: &db::Connection,
@ -12,10 +9,7 @@ pub async fn check_user_rights_recipe(
) -> Result<()> { ) -> Result<()> {
match user { match user {
Some(user) if connection.can_edit_recipe(user.id, recipe_id).await? => Ok(()), Some(user) if connection.can_edit_recipe(user.id, recipe_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -26,10 +20,7 @@ pub async fn check_user_rights_recipe_group(
) -> Result<()> { ) -> Result<()> {
match user { match user {
Some(user) if connection.can_edit_recipe_group(user.id, group_id).await? => Ok(()), Some(user) if connection.can_edit_recipe_group(user.id, group_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -46,10 +37,7 @@ pub async fn check_user_rights_recipe_groups(
{ {
Ok(()) Ok(())
} }
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -60,10 +48,7 @@ pub async fn check_user_rights_recipe_step(
) -> Result<()> { ) -> Result<()> {
match user { match user {
Some(user) if connection.can_edit_recipe_step(user.id, step_id).await? => Ok(()), Some(user) if connection.can_edit_recipe_step(user.id, step_id).await? => Ok(()),
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -80,10 +65,7 @@ pub async fn check_user_rights_recipe_steps(
{ {
Ok(()) Ok(())
} }
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -100,10 +82,7 @@ pub async fn check_user_rights_recipe_ingredient(
{ {
Ok(()) Ok(())
} }
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -120,10 +99,7 @@ pub async fn check_user_rights_recipe_ingredients(
{ {
Ok(()) Ok(())
} }
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }
@ -140,9 +116,6 @@ pub async fn check_user_rights_shopping_list_entry(
{ {
Ok(()) Ok(())
} }
_ => Err(ErrorResponse::from(ron_error( _ => Err(ron_error_not_autorized()),
StatusCode::UNAUTHORIZED,
consts::NOT_AUTHORIZED_MESSAGE,
))),
} }
} }