From ed9d93410eabd220cd6397566723244036a45683 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 19 Aug 2024 11:27:10 +0200 Subject: [PATCH] =?UTF-8?q?allow=20vorstand=20to=20add=20scheckb=C3=BCcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/user.rs | 8 ++++ src/tera/admin/user.rs | 54 +++++++++++++++++++++++ templates/admin/user/scheckbuch.html.tera | 28 ++++++++++++ 3 files changed, 90 insertions(+) diff --git a/src/model/user.rs b/src/model/user.rs index 981261c..849ba64 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -648,6 +648,14 @@ ORDER BY last_access DESC .is_ok() } + pub async fn create_with_mail(db: &SqlitePool, name: &str, mail: &str) -> bool { + let name = name.trim(); + sqlx::query!("INSERT INTO USER(name, mail) VALUES (?, ?)", name, mail) + .execute(db) + .await + .is_ok() + } + pub async fn update(&self, db: &SqlitePool, data: UserEditForm<'_>) { let mut family_id = data.family_id; diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index 012df89..ef4b24d 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -14,6 +14,7 @@ use crate::{ tera::Config, }; use futures::future::join_all; +use lettre::Address; use rocket::{ form::Form, fs::TempFile, @@ -349,6 +350,58 @@ async fn create( } } +#[derive(FromForm, Debug)] +struct UserAddScheckbuchForm<'r> { + name: &'r str, + mail: &'r str, +} + +#[post("/user/new/scheckbuch", data = "")] +async fn create_scheckbuch( + db: &State, + data: Form>, + admin: VorstandUser, + config: &State, +) -> Flash { + // 1. Check mail adress + let mail = data.mail.trim(); + if mail.parse::
().is_err() { + return Flash::error( + Redirect::to("/admin/user/scheckbuch"), + format!("Keine gültige Mailadresse"), + ); + } + + // 2. Check name + let name = data.name.trim(); + if User::find_by_name(db, name).await.is_some() { + return Flash::error( + Redirect::to("/admin/user/scheckbuch"), + format!( + "Kann kein Scheckbuch erstellen, der Name wird bereits von einem User verwendet" + ), + ); + } + + // 3. Create user + User::create_with_mail(db, name, mail).await; + let user = User::find_by_name(db, name).await.unwrap(); + + // 4. Add 'scheckbuch' role + let scheckbuch = Role::find_by_name(db, "scheckbuch").await.unwrap(); + user.add_role(db, &scheckbuch).await; + + // 4. Send welcome mail (+ notification) + user.send_welcome_email(db, &config.smtp_pw).await.unwrap(); + + Log::create( + db, + format!("{} created new scheckbuch: {data:?}", admin.name), + ) + .await; + Flash::success(Redirect::to("/admin/user/scheckbuch"), &format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {mail} verschickt.")) +} + pub fn routes() -> Vec { routes![ index, @@ -356,6 +409,7 @@ pub fn routes() -> Vec { resetpw, update, create, + create_scheckbuch, delete, fees, fees_paid, diff --git a/templates/admin/user/scheckbuch.html.tera b/templates/admin/user/scheckbuch.html.tera index 08a6409..522138c 100644 --- a/templates/admin/user/scheckbuch.html.tera +++ b/templates/admin/user/scheckbuch.html.tera @@ -4,6 +4,34 @@ {% block content %}

Scheckbücher

+
+
+

Neues Scheckbuch hinzufügen

+
+
+ + +
+
+ + +
+
+
+
+ +
+