From beb5f3a2170c8130a671100114f45396ee31549c Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 24 Oct 2023 10:57:51 +0200 Subject: [PATCH] hide logbook for guests --- src/tera/boatdamage.rs | 12 ++++++--- src/tera/log.rs | 39 ++++++++++++++++++++--------- src/tera/stat.rs | 6 ++--- templates/includes/macros.html.tera | 4 +-- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/tera/boatdamage.rs b/src/tera/boatdamage.rs index 886c57d..ab6eed8 100644 --- a/src/tera/boatdamage.rs +++ b/src/tera/boatdamage.rs @@ -13,7 +13,7 @@ use crate::{ model::{ boat::Boat, boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified}, - user::{CoxUser, TechUser, User}, + user::{CoxUser, NonGuestUser, TechUser, User}, }, tera::log::KioskCookie, }; @@ -42,7 +42,11 @@ async fn index_kiosk( } #[get("/", rank = 2)] -async fn index(db: &State, flash: Option>, user: User) -> Template { +async fn index( + db: &State, + flash: Option>, + user: NonGuestUser, +) -> Template { let boatdamages = BoatDamage::all(db).await; let boats = Boat::all(db).await; @@ -69,14 +73,14 @@ pub struct FormBoatDamageToAdd<'r> { async fn create<'r>( db: &State, data: Form>, - user: User, + user: NonGuestUser, ) -> Flash { return Flash::error(Redirect::to("/log"), "Du musst noch kurz geduldig sein. Sobald wir unser Logbuch umgestellt haben, kannst du es hier im Ruderassistenten verwenden ;)"); let boatdamage_to_add = BoatDamageToAdd { boat_id: data.boat_id, desc: data.desc, lock_boat: data.lock_boat, - user_id_created: user.id as i32, + user_id_created: user.user.id as i32, }; match BoatDamage::create(db, boatdamage_to_add).await { Ok(_) => Flash::success( diff --git a/src/tera/log.rs b/src/tera/log.rs index f6ee126..e4b225a 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -41,8 +41,12 @@ impl<'r> FromRequest<'r> for KioskCookie { } #[get("/", rank = 2)] -async fn index(db: &State, flash: Option>, user: User) -> Template { - let boats = Boat::for_user(db, &user).await; +async fn index( + db: &State, + flash: Option>, + user: NonGuestUser, +) -> Template { + let boats = Boat::for_user(db, &user.user).await; let coxes: Vec = futures::future::join_all( User::cox(db) @@ -153,13 +157,17 @@ async fn kiosk( Template::render("kiosk", context.into_json()) } -async fn create_logbook(db: &SqlitePool, data: Form, user: &User) -> Flash { +async fn create_logbook( + db: &SqlitePool, + data: Form, + user: &NonGuestUser, +) -> Flash { return Flash::error(Redirect::to("/log"), "Du musst noch kurz geduldig sein. Sobald wir unser Logbuch umgestellt haben, kannst du es hier im Ruderassistenten verwenden ;)"); match Logbook::create( db, data.into_inner(), - user + &user.user ) .await { @@ -181,7 +189,11 @@ async fn create_logbook(db: &SqlitePool, data: Form, user: &User) -> F } #[post("/", data = "", rank = 2)] -async fn create(db: &State, data: Form, user: User) -> Flash { +async fn create( + db: &State, + data: Form, + user: NonGuestUser, +) -> Flash { create_logbook(db, data, &user).await } @@ -192,14 +204,14 @@ async fn create_kiosk( _kiosk: KioskCookie, ) -> Flash { let creator = User::find_by_id(db, data.shipmaster as i32).await.unwrap(); - create_logbook(db, data, &creator).await + create_logbook(db, data, &NonGuestUser::try_from(creator).unwrap()).await //TODO: fixme } async fn home_logbook( db: &SqlitePool, data: Form, logbook_id: i32, - user: &User, + user: &NonGuestUser, ) -> Flash { return Flash::error(Redirect::to("/log"), "Du musst noch kurz geduldig sein. Sobald wir unser Logbuch umgestellt haben, kannst du es hier im Ruderassistenten verwenden ;)"); let logbook: Option = Logbook::find_by_id(db, logbook_id).await; @@ -210,7 +222,7 @@ async fn home_logbook( ); }; - match logbook.home(db, user, data.into_inner()).await { + match logbook.home(db, &user.user, data.into_inner()).await { Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt korrekt eingetragen"), Err(LogbookUpdateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")), Err(_) => Flash::error( @@ -232,9 +244,12 @@ async fn home_kiosk( db, data, logbook_id, - &User::find_by_id(db, logbook.shipmaster as i32) - .await - .unwrap(), + &NonGuestUser::try_from( + User::find_by_id(db, logbook.shipmaster as i32) + .await + .unwrap(), //TODO: fixme + ) + .unwrap(), ) .await } @@ -244,7 +259,7 @@ async fn home( db: &State, data: Form, logbook_id: i32, - user: User, + user: NonGuestUser, ) -> Flash { home_logbook(db, data, logbook_id, &user).await } diff --git a/src/tera/stat.rs b/src/tera/stat.rs index c5d0e41..3927d8a 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -4,15 +4,15 @@ use sqlx::SqlitePool; use crate::model::{ stat::{self, Stat}, - user::User, + user::{NonGuestUser, User}, }; use super::log::KioskCookie; #[get("/", rank = 2)] -async fn index(db: &State, user: User) -> Template { +async fn index(db: &State, user: NonGuestUser) -> Template { let stat = Stat::get_rowed_km(db).await; - let personal = stat::get_personal(db, &user).await; + let personal = stat::get_personal(db, &user.user).await; let kiosk = false; Template::render( diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 66463b4..194f04c 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -13,6 +13,7 @@ {% include "includes/question-icon" %} FAQs + {% if not loggedin_user.is_guest %} {% include "includes/book" %} @@ -23,11 +24,9 @@ Ausfahrt eintragen - {% if not loggedin_user.is_guest %} Logbuch - {% endif %} Statistik @@ -41,6 +40,7 @@ + {% endif %} {% if loggedin_user.is_admin %}