clean code; if logged out: save url where user tried to go to, go there once logged in, Fixes #179
Some checks failed
CI/CD Pipeline / deploy-staging (push) Blocked by required conditions
CI/CD Pipeline / deploy-main (push) Blocked by required conditions
CI/CD Pipeline / test (push) Has been cancelled

This commit is contained in:
philipp 2024-01-22 22:08:05 +01:00
parent 60b9a4dbba
commit b7499bd6cb
8 changed files with 41 additions and 34 deletions

View File

@ -1,12 +1,8 @@
use std::error::Error; use std::error::Error;
use lettre::{ use lettre::{
message::{ message::header::ContentType, transport::smtp::authentication::Credentials, Message,
header::{self, ContentType}, SmtpTransport, Transport,
MultiPart, SinglePart,
},
transport::smtp::authentication::Credentials,
Message, SmtpTransport, Transport,
}; };
use sqlx::SqlitePool; use sqlx::SqlitePool;
@ -17,7 +13,7 @@ use super::{family::Family, role::Role, user::User};
pub struct Mail {} pub struct Mail {}
impl 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() let mut email = Message::builder()
.from( .from(
"ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>" "ASKÖ Ruderverein Donau Linz <no-reply@rudernlinz.at>"

View File

@ -2,7 +2,6 @@ use std::ops::{Deref, DerefMut};
use argon2::{password_hash::SaltString, Argon2, PasswordHasher}; use argon2::{password_hash::SaltString, Argon2, PasswordHasher};
use chrono::{Datelike, Local, NaiveDate}; use chrono::{Datelike, Local, NaiveDate};
use chrono_tz::Etc::UTC;
use log::info; use log::info;
use rocket::{ use rocket::{
async_trait, async_trait,
@ -101,12 +100,12 @@ pub enum LoginError {
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub(crate) struct Fee { pub struct Fee {
pub(crate) sum_in_cents: i32, pub sum_in_cents: i32,
pub(crate) parts: Vec<(String, i32)>, pub parts: Vec<(String, i32)>,
pub(crate) name: String, pub name: String,
pub(crate) user_ids: String, pub user_ids: String,
pub(crate) paid: bool, pub paid: bool,
} }
impl Fee { impl Fee {
@ -156,7 +155,6 @@ impl User {
let mut fee = Fee::new(); let mut fee = Fee::new();
if let Some(family) = Family::find_by_opt_id(db, self.family_id).await { 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 { for member in family.members(db).await {
fee.add_person(&member); fee.add_person(&member);
if member.has_role(db, "paid").await { if member.has_role(db, "paid").await {

View File

@ -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 serde_json::json;
use sqlx::SqlitePool; use sqlx::SqlitePool;

View File

@ -1,5 +1,4 @@
use rocket::form::Form; use rocket::form::Form;
use rocket::fs::TempFile;
use rocket::response::{Flash, Redirect}; use rocket::response::{Flash, Redirect};
use rocket::{get, request::FlashMessage, routes, Route, State}; use rocket::{get, request::FlashMessage, routes, Route, State};
use rocket::{post, FromForm}; use rocket::{post, FromForm};
@ -34,28 +33,24 @@ async fn index(
} }
#[get("/mail/fee")] #[get("/mail/fee")]
async fn fee( async fn fee(db: &State<SqlitePool>, _admin: AdminUser, config: &State<Config>) -> &'static str {
db: &State<SqlitePool>,
admin: AdminUser,
config: &State<Config>,
flash: Option<FlashMessage<'_>>,
) -> &'static str {
Mail::fees(db, config.smtp_pw.clone()).await; Mail::fees(db, config.smtp_pw.clone()).await;
"SUCC" "SUCC"
} }
#[derive(FromForm, Debug)] #[derive(FromForm, Debug)]
pub struct MailToSend<'a> { pub struct MailToSend {
//<'a> {
pub(crate) role_id: i32, pub(crate) role_id: i32,
pub(crate) subject: String, pub(crate) subject: String,
pub(crate) body: String, pub(crate) body: String,
pub(crate) files: Vec<TempFile<'a>>, //pub(crate) files: Vec<TempFile<'a>>,
} }
#[post("/mail", data = "<data>")] #[post("/mail", data = "<data>")]
async fn update( async fn update(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<MailToSend<'_>>, data: Form<MailToSend>,
config: &State<Config>, config: &State<Config>,
_admin: AdminUser, _admin: AdminUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {

View File

@ -3,9 +3,9 @@ use std::collections::HashMap;
use crate::model::{ use crate::model::{
family::Family, family::Family,
role::Role, 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::{ use rocket::{
form::Form, form::Form,
get, post, get, post,
@ -80,7 +80,7 @@ async fn fees(
#[get("/user/fees/paid?<user_ids>")] #[get("/user/fees/paid?<user_ids>")]
async fn fees_paid( async fn fees_paid(
db: &State<SqlitePool>, db: &State<SqlitePool>,
admin: AdminUser, _admin: AdminUser,
user_ids: Vec<i32>, user_ids: Vec<i32>,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
let mut res = String::new(); let mut res = String::new();

View File

@ -89,7 +89,15 @@ async fn login(
) )
.await; .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/<userid>")] #[get("/set-pw/<userid>")]

View File

@ -125,7 +125,7 @@ async fn new_kiosk(
async fn kiosk( async fn kiosk(
db: &State<SqlitePool>, db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>, flash: Option<FlashMessage<'_>>,
kiosk: KioskCookie, _kiosk: KioskCookie,
) -> Template { ) -> Template {
let boats = Boat::all(db).await; let boats = Boat::all(db).await;
let coxes: Vec<UserWithWaterStatus> = futures::future::join_all( let coxes: Vec<UserWithWaterStatus> = futures::future::join_all(

View File

@ -3,10 +3,14 @@ use rocket::{
fairing::AdHoc, fairing::AdHoc,
form::Form, form::Form,
fs::FileServer, fs::FileServer,
get, post, get,
http::Cookie,
post,
request::FlashMessage, request::FlashMessage,
response::{Flash, Redirect}, response::{Flash, Redirect},
routes, Build, FromForm, Rocket, State, routes,
time::{Duration, OffsetDateTime},
Build, FromForm, Request, Rocket, State,
}; };
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use serde::Deserialize; use serde::Deserialize;
@ -51,7 +55,13 @@ async fn wikiauth(db: &State<SqlitePool>, login: Form<LoginForm<'_>>) -> String
} }
#[catch(401)] //Unauthorized #[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") Redirect::to("/auth")
} }