From b7499bd6cb751d96e479a7d0db5719405b915ea8 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 22 Jan 2024 22:08:05 +0100 Subject: [PATCH] clean code; if logged out: save url where user tried to go to, go there once logged in, Fixes #179 --- src/model/mail.rs | 10 +++------- src/model/user.rs | 14 ++++++-------- src/rest/mod.rs | 2 +- src/tera/admin/mail.rs | 15 +++++---------- src/tera/admin/user.rs | 6 +++--- src/tera/auth.rs | 10 +++++++++- src/tera/log.rs | 2 +- src/tera/mod.rs | 16 +++++++++++++--- 8 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/model/mail.rs b/src/model/mail.rs index 5dcb24d..fe24abd 100644 --- a/src/model/mail.rs +++ b/src/model/mail.rs @@ -1,12 +1,8 @@ use std::error::Error; use lettre::{ - message::{ - header::{self, ContentType}, - MultiPart, SinglePart, - }, - transport::smtp::authentication::Credentials, - Message, SmtpTransport, Transport, + message::header::ContentType, transport::smtp::authentication::Credentials, Message, + SmtpTransport, Transport, }; use sqlx::SqlitePool; @@ -17,7 +13,7 @@ use super::{family::Family, role::Role, user::User}; pub struct Mail {} impl Mail { - pub async fn send(db: &SqlitePool, data: MailToSend<'_>, smtp_pw: String) -> bool { + pub async fn send(db: &SqlitePool, data: MailToSend, smtp_pw: String) -> bool { let mut email = Message::builder() .from( "ASKÖ Ruderverein Donau Linz " diff --git a/src/model/user.rs b/src/model/user.rs index 6ebc55e..51992c1 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -2,7 +2,6 @@ use std::ops::{Deref, DerefMut}; use argon2::{password_hash::SaltString, Argon2, PasswordHasher}; use chrono::{Datelike, Local, NaiveDate}; -use chrono_tz::Etc::UTC; use log::info; use rocket::{ async_trait, @@ -101,12 +100,12 @@ pub enum LoginError { } #[derive(Debug, Serialize)] -pub(crate) struct Fee { - pub(crate) sum_in_cents: i32, - pub(crate) parts: Vec<(String, i32)>, - pub(crate) name: String, - pub(crate) user_ids: String, - pub(crate) paid: bool, +pub struct Fee { + pub sum_in_cents: i32, + pub parts: Vec<(String, i32)>, + pub name: String, + pub user_ids: String, + pub paid: bool, } impl Fee { @@ -156,7 +155,6 @@ impl User { let mut fee = Fee::new(); if let Some(family) = Family::find_by_opt_id(db, self.family_id).await { - let mut names = String::new(); for member in family.members(db).await { fee.add_person(&member); if member.has_role(db, "paid").await { diff --git a/src/rest/mod.rs b/src/rest/mod.rs index ef69ad9..bf08819 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -1,4 +1,4 @@ -use rocket::{form::Form, fs::FileServer, post, routes, Build, FromForm, Rocket, State}; +use rocket::{form::Form, post, routes, Build, FromForm, Rocket, State}; use serde_json::json; use sqlx::SqlitePool; diff --git a/src/tera/admin/mail.rs b/src/tera/admin/mail.rs index 7b7e4e7..450c513 100644 --- a/src/tera/admin/mail.rs +++ b/src/tera/admin/mail.rs @@ -1,5 +1,4 @@ use rocket::form::Form; -use rocket::fs::TempFile; use rocket::response::{Flash, Redirect}; use rocket::{get, request::FlashMessage, routes, Route, State}; use rocket::{post, FromForm}; @@ -34,28 +33,24 @@ async fn index( } #[get("/mail/fee")] -async fn fee( - db: &State, - admin: AdminUser, - config: &State, - flash: Option>, -) -> &'static str { +async fn fee(db: &State, _admin: AdminUser, config: &State) -> &'static str { Mail::fees(db, config.smtp_pw.clone()).await; "SUCC" } #[derive(FromForm, Debug)] -pub struct MailToSend<'a> { +pub struct MailToSend { + //<'a> { pub(crate) role_id: i32, pub(crate) subject: String, pub(crate) body: String, - pub(crate) files: Vec>, + //pub(crate) files: Vec>, } #[post("/mail", data = "")] async fn update( db: &State, - data: Form>, + data: Form, config: &State, _admin: AdminUser, ) -> Flash { diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index c89ff00..91e8ee8 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use crate::model::{ family::Family, role::Role, - user::{AdminUser, Fee, User, UserWithRoles, VorstandUser}, + user::{AdminUser, User, UserWithRoles, VorstandUser}, }; -use futures::future::{self, join_all}; +use futures::future::join_all; use rocket::{ form::Form, get, post, @@ -80,7 +80,7 @@ async fn fees( #[get("/user/fees/paid?")] async fn fees_paid( db: &State, - admin: AdminUser, + _admin: AdminUser, user_ids: Vec, ) -> Flash { let mut res = String::new(); diff --git a/src/tera/auth.rs b/src/tera/auth.rs index 7874b9c..be48ea2 100644 --- a/src/tera/auth.rs +++ b/src/tera/auth.rs @@ -89,7 +89,15 @@ async fn login( ) .await; - Flash::success(Redirect::to("/"), "Login erfolgreich") + // Check for redirect_url cookie and redirect accordingly + match cookies.get_private("redirect_url") { + Some(redirect_cookie) => { + let redirect_url = redirect_cookie.value().to_string(); + cookies.remove_private(redirect_cookie); // Remove the cookie after using it + Flash::success(Redirect::to(redirect_url), "Login erfolgreich") + } + None => Flash::success(Redirect::to("/"), "Login erfolgreich"), + } } #[get("/set-pw/")] diff --git a/src/tera/log.rs b/src/tera/log.rs index 0d480e0..6d6c815 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -125,7 +125,7 @@ async fn new_kiosk( async fn kiosk( db: &State, flash: Option>, - kiosk: KioskCookie, + _kiosk: KioskCookie, ) -> Template { let boats = Boat::all(db).await; let coxes: Vec = futures::future::join_all( diff --git a/src/tera/mod.rs b/src/tera/mod.rs index f6931f7..f4ec85a 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -3,10 +3,14 @@ use rocket::{ fairing::AdHoc, form::Form, fs::FileServer, - get, post, + get, + http::Cookie, + post, request::FlashMessage, response::{Flash, Redirect}, - routes, Build, FromForm, Rocket, State, + routes, + time::{Duration, OffsetDateTime}, + Build, FromForm, Request, Rocket, State, }; use rocket_dyn_templates::Template; use serde::Deserialize; @@ -51,7 +55,13 @@ async fn wikiauth(db: &State, login: Form>) -> String } #[catch(401)] //Unauthorized -fn unauthorized_error() -> Redirect { +fn unauthorized_error(req: &Request) -> Redirect { + // Save the URL the user tried to access, to be able to go there once logged in + let mut redirect_cookie = Cookie::new("redirect_url", format!("{}", req.uri())); + println!("{}", req.uri()); + redirect_cookie.set_expires(OffsetDateTime::now_utc() + Duration::hours(1)); + req.cookies().add_private(redirect_cookie); + Redirect::to("/auth") }