From 1f35be88ad32b0c9446fc4adae7a8268d3d34073 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 24 Oct 2023 10:16:26 +0200 Subject: [PATCH] scheckbuch can't see logbuch --- src/model/user.rs | 34 +++++++++++++++++++++++++++++ src/tera/log.rs | 4 ++-- templates/includes/macros.html.tera | 2 ++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index fe570f2..8a0dc85 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -59,6 +59,7 @@ pub enum LoginError { NotAnAdmin, NotACox, NotATech, + GuestNotAllowed, NoPasswordSet(User), DeserializationError, } @@ -464,6 +465,39 @@ impl<'r> FromRequest<'r> for AdminUser { } } +#[derive(Debug, Serialize, Deserialize)] +pub struct NonGuestUser { + pub(crate) user: User, +} + +impl TryFrom for NonGuestUser { + type Error = LoginError; + + fn try_from(user: User) -> Result { + if user.is_guest { + Err(LoginError::GuestNotAllowed) + } else { + Ok(NonGuestUser { user }) + } + } +} + +#[async_trait] +impl<'r> FromRequest<'r> for NonGuestUser { + type Error = LoginError; + + async fn from_request(req: &'r Request<'_>) -> request::Outcome { + match User::from_request(req).await { + Outcome::Success(user) => match user.try_into() { + Ok(user) => Outcome::Success(user), + Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)), + }, + Outcome::Failure(f) => Outcome::Failure(f), + Outcome::Forward(f) => Outcome::Forward(f), + } + } +} + #[cfg(test)] mod test { use crate::testdb; diff --git a/src/tera/log.rs b/src/tera/log.rs index 54b9ff7..f6ee126 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -23,7 +23,7 @@ use crate::model::{ LogbookUpdateError, }, logtype::LogType, - user::{User, UserWithWaterStatus}, + user::{NonGuestUser, User, UserWithWaterStatus}, }; pub struct KioskCookie(String); @@ -80,7 +80,7 @@ async fn index(db: &State, flash: Option>, user: Us } #[get("/show", rank = 2)] -async fn show(db: &State, user: User) -> Template { +async fn show(db: &State, user: NonGuestUser) -> Template { let logs = Logbook::completed(db).await; Template::render("log.completed", context!(logs, loggedin_user: &user)) diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 0c04f2a..66463b4 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -23,9 +23,11 @@ Ausfahrt eintragen + {% if not loggedin_user.is_guest %} Logbuch + {% endif %} Statistik