clean code; if logged out: save url where user tried to go to, go there once logged in, Fixes #179
This commit is contained in:
parent
60b9a4dbba
commit
b7499bd6cb
@ -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 <no-reply@rudernlinz.at>"
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<SqlitePool>,
|
||||
admin: AdminUser,
|
||||
config: &State<Config>,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
) -> &'static str {
|
||||
async fn fee(db: &State<SqlitePool>, _admin: AdminUser, config: &State<Config>) -> &'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<TempFile<'a>>,
|
||||
//pub(crate) files: Vec<TempFile<'a>>,
|
||||
}
|
||||
|
||||
#[post("/mail", data = "<data>")]
|
||||
async fn update(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<MailToSend<'_>>,
|
||||
data: Form<MailToSend>,
|
||||
config: &State<Config>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
|
@ -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?<user_ids>")]
|
||||
async fn fees_paid(
|
||||
db: &State<SqlitePool>,
|
||||
admin: AdminUser,
|
||||
_admin: AdminUser,
|
||||
user_ids: Vec<i32>,
|
||||
) -> Flash<Redirect> {
|
||||
let mut res = String::new();
|
||||
|
@ -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/<userid>")]
|
||||
|
@ -125,7 +125,7 @@ async fn new_kiosk(
|
||||
async fn kiosk(
|
||||
db: &State<SqlitePool>,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
kiosk: KioskCookie,
|
||||
_kiosk: KioskCookie,
|
||||
) -> Template {
|
||||
let boats = Boat::all(db).await;
|
||||
let coxes: Vec<UserWithWaterStatus> = futures::future::join_all(
|
||||
|
@ -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<SqlitePool>, login: Form<LoginForm<'_>>) -> 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")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user