From ed9d93410eabd220cd6397566723244036a45683 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 19 Aug 2024 11:27:10 +0200 Subject: [PATCH 1/3] =?UTF-8?q?allow=20vorstand=20to=20add=20scheckb=C3=BC?= =?UTF-8?q?cher?= 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

+
+
+ + +
+
+ + +
+
+
+
+ +
+
From eb15421d082deed53ff1af24c58f093ca0182e44 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 19 Aug 2024 11:53:28 +0200 Subject: [PATCH 2/3] allow move from schnuppern to scheckbuch --- src/tera/admin/user.rs | 47 ++++++++++++++++++++++- templates/admin/schnupper/index.html.tera | 3 +- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index 2217cf6..3c5db86 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -7,8 +7,8 @@ use crate::{ logbook::Logbook, role::Role, user::{ - AdminUser, AllowedToEditPaymentStatusUser, User, UserWithDetails, - UserWithMembershipPdf, UserWithRolesAndMembershipPdf, VorstandUser, + AdminUser, AllowedToEditPaymentStatusUser, SchnupperBetreuerUser, User, + UserWithDetails, UserWithMembershipPdf, UserWithRolesAndMembershipPdf, VorstandUser, }, }, tera::Config, @@ -408,6 +408,48 @@ async fn create_scheckbuch( Flash::success(Redirect::to("/admin/user/scheckbuch"), &format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {mail} verschickt.")) } +#[get("/user/move/schnupperant//to/scheckbuch")] +async fn schnupper_to_scheckbuch( + db: &State, + id: i32, + admin: SchnupperBetreuerUser, + config: &State, +) -> Flash { + let Some(user) = User::find_by_id(db, id).await else { + return Flash::error( + Redirect::to("/admin/schnupper"), + format!("user id not found"), + ); + }; + + if !user.has_role(db, "schnupperant").await { + return Flash::error( + Redirect::to("/admin/schnupper"), + format!("kein schnupperant..."), + ); + } + + let schnupperant = Role::find_by_name(db, "schnupperant").await.unwrap(); + let paid = Role::find_by_name(db, "paid").await.unwrap(); + user.remove_role(db, &schnupperant).await; + user.remove_role(db, &paid).await; + + let scheckbuch = Role::find_by_name(db, "scheckbuch").await.unwrap(); + user.add_role(db, &scheckbuch).await; + + user.send_welcome_email(db, &config.smtp_pw).await.unwrap(); + + Log::create( + db, + format!( + "{} created new scheckbuch (from schnupperant): {}", + admin.name, user.name + ), + ) + .await; + Flash::success(Redirect::to("/admin/schnupper"), &format!("Scheckbuch erfolgreich erstellt. Eine E-Mail in der alles erklärt wird, wurde an {} verschickt.", user.mail.unwrap())) +} + pub fn routes() -> Vec { routes![ index, @@ -416,6 +458,7 @@ pub fn routes() -> Vec { update, create, create_scheckbuch, + schnupper_to_scheckbuch, delete, fees, fees_paid, diff --git a/templates/admin/schnupper/index.html.tera b/templates/admin/schnupper/index.html.tera index 1c3ddc1..2fd0437 100644 --- a/templates/admin/schnupper/index.html.tera +++ b/templates/admin/schnupper/index.html.tera @@ -12,7 +12,8 @@ {% for user in schnupperanten %}
  • - {{ user.name }} ({{ user.mail }} | {{ user.notes }}) + {{ user.name }} ({{ user.mail }} {% if user.notes %} | {{ user.notes }}{% endif %}) + Zu Scheckbuch umwandeln
  • {% endfor %} From 116c7523d2a338ce5ed9e46b7d048cad6759b9ca Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 19 Aug 2024 11:57:51 +0200 Subject: [PATCH 3/3] styling --- Rocket.toml | 2 +- templates/admin/schnupper/index.html.tera | 8 +++- templates/admin/user/scheckbuch.html.tera | 52 +++++++++++------------ templates/includes/forms/log.html.tera | 9 ++-- templates/index.html.tera | 8 +--- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Rocket.toml b/Rocket.toml index 4b4856e..cc93d2b 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -2,7 +2,7 @@ secret_key = "/NtVGizglEoyoxBLzsRDWTy4oAG1qDw4J4O+CWJSv+fypD7W9sam8hUY4j90EZsbZk8wEradS5zBoWtWKi3k8w==" rss_key = "rss-key-for-ci" limits = { file = "10 MiB", data-form = "10 MiB"} -smtp_pw = "8kIjlLH79Ky6D3jQ" +smtp_pw = "8kIjlLH79Ky6D3j" usage_log_path = "./usage.txt" openweathermap_key = "c8dab8f91b5b815d76e9879cbaecd8d5" wordpress_key = "pw-to-allow-sending-notifications" diff --git a/templates/admin/schnupper/index.html.tera b/templates/admin/schnupper/index.html.tera index 2fd0437..755985d 100644 --- a/templates/admin/schnupper/index.html.tera +++ b/templates/admin/schnupper/index.html.tera @@ -12,8 +12,12 @@ {% for user in schnupperanten %}
  • - {{ user.name }} ({{ user.mail }} {% if user.notes %} | {{ user.notes }}{% endif %}) - Zu Scheckbuch umwandeln + {{ user.name }} ({{ user.mail }} + {%- if user.notes %} | {{ user.notes }}{% endif -%} + ) + Zu Scheckbuch umwandeln
  • {% endfor %} diff --git a/templates/admin/user/scheckbuch.html.tera b/templates/admin/user/scheckbuch.html.tera index c01625f..249ce1d 100644 --- a/templates/admin/user/scheckbuch.html.tera +++ b/templates/admin/user/scheckbuch.html.tera @@ -4,34 +4,34 @@ {% block content %}

    Scheckbücher

    -
    -
    -

    Neues Scheckbuch hinzufügen

    -
    -
    - - -
    -
    - - -
    + +
    +

    Neues Scheckbuch hinzufügen

    +
    +
    + + +
    +
    + +
    -
    - -
    - +
    +
    + +
    +
    diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index b72dad1..9b1e94b 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -53,10 +53,13 @@ id="destination" name="destination" value="" - data-relation="distance_in_km" - autocomplete="off" /> + data-relation="distance_in_km" + autocomplete="off" /> - {% for distance in distances %}
    diff --git a/templates/index.html.tera b/templates/index.html.tera index a275d0f..a9cd032 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -35,9 +35,7 @@
    {% if notification.link %} - + {% endif %} {% if not notification.read_at %} @@ -65,9 +63,7 @@
    {{ notification.message | safe }}
    {% if notification.link %} - + {% endif %}