2023-08-24 22:06:37 +02:00
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
|
|
pub mod data;
|
2023-09-08 11:53:08 +02:00
|
|
|
pub mod company;
|
2023-09-22 21:26:13 +02:00
|
|
|
pub mod user;
|
|
|
|
|
pub mod auth_middleware;
|
2023-11-11 16:27:40 +01:00
|
|
|
pub mod opening_hours;
|
2023-09-22 21:26:13 +02:00
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! perm_check {
|
|
|
|
|
($check:ident) => {
|
|
|
|
|
use crate::backend::user::$check;
|
|
|
|
|
use actix_web::http::StatusCode;
|
|
|
|
|
use leptos_actix::ResponseOptions;
|
|
|
|
|
|
|
|
|
|
if !$check().await {
|
|
|
|
|
let response = expect_context::<ResponseOptions>();
|
|
|
|
|
response.set_status(StatusCode::FORBIDDEN);
|
|
|
|
|
|
|
|
|
|
return Ok(ApiResponse::Error("Forbidden".to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-24 22:06:37 +02:00
|
|
|
|
2023-09-27 11:36:52 +02:00
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! user_check {
|
|
|
|
|
($check:expr) => {
|
|
|
|
|
use crate::perm_check;
|
|
|
|
|
use crate::backend::user::logged_in_user;
|
|
|
|
|
|
|
|
|
|
perm_check!(is_logged_in);
|
2023-10-09 17:33:20 +02:00
|
|
|
let user = logged_in_user().await.unwrap_or(User::default());
|
2023-09-27 11:36:52 +02:00
|
|
|
|
2023-10-09 17:33:20 +02:00
|
|
|
if !user.admin && user.login != $check {
|
2023-09-27 11:36:52 +02:00
|
|
|
let response = expect_context::<ResponseOptions>();
|
|
|
|
|
response.set_status(StatusCode::FORBIDDEN);
|
|
|
|
|
|
|
|
|
|
return Ok(ApiResponse::Error("You can change your own profile only".to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 22:06:37 +02:00
|
|
|
cfg_if!{
|
|
|
|
|
if #[cfg(feature = "ssr")] {
|
|
|
|
|
use sqlx::PgPool;
|
2023-10-09 17:33:20 +02:00
|
|
|
use actix_web::web::Data;
|
|
|
|
|
use leptos_actix::extract;
|
|
|
|
|
use leptos::ServerFnError;
|
2023-08-24 22:06:37 +02:00
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct AppData {
|
|
|
|
|
db_pool: PgPool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppData {
|
|
|
|
|
pub fn new(db_pool: PgPool) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
db_pool
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn db_pool(&self) -> &PgPool {
|
|
|
|
|
&self.db_pool
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-09 17:33:20 +02:00
|
|
|
|
|
|
|
|
pub async fn get_pool() -> Result<PgPool, ServerFnError> {
|
|
|
|
|
extract(|data: Data<AppData>| async move { data.db_pool().clone() }).await
|
|
|
|
|
}
|
2023-08-24 22:06:37 +02:00
|
|
|
}
|
|
|
|
|
}
|