From db3158d4e78979eb344fb5bbb715c3625ae7fb0e Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 7 Apr 2024 23:17:06 +0200 Subject: [PATCH 1/2] only allow people with 'donau linz' role to be in logbook --- src/tera/log.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/tera/log.rs b/src/tera/log.rs index 686867d..1fe6b15 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -1,5 +1,6 @@ use std::net::IpAddr; +use futures::{stream, StreamExt}; use rocket::{ form::Form, get, @@ -49,20 +50,24 @@ async fn index( ) -> Template { let boats = Boat::for_user(db, &user).await; - let coxes: Vec = futures::future::join_all( + let mut coxes: Vec = futures::future::join_all( User::cox(db) .await .into_iter() .map(|user| UserWithWaterStatus::from_user(user, db)), ) .await; - let users: Vec = futures::future::join_all( + coxes.retain(|u| u.roles.contains(&"Donau Linz".into())); + + let mut users: Vec = futures::future::join_all( User::all(db) .await .into_iter() .map(|user| UserWithWaterStatus::from_user(user, db)), ) .await; + users.retain(|u| u.roles.contains(&"Donau Linz".into())); + let logtypes = LogType::all(db).await; let distances = Logbook::distances(db).await; From 3b9103e9aa0b3892d634004b1ba50ebf07cf8348 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 8 Apr 2024 19:04:57 +0200 Subject: [PATCH 2/2] fix membership application error --- src/model/user.rs | 24 ++++++++++++++++++++++++ src/tera/admin/user.rs | 14 ++++++++------ src/tera/log.rs | 1 - 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index ca2cac3..e1dbd15 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -959,6 +959,30 @@ impl Deref for PlannedEventUser { &self.0 } } +#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] +pub struct UserWithRolesAndMembershipPdf { + #[serde(flatten)] + pub user: User, + pub membership_pdf: bool, + pub roles: Vec, +} + +impl UserWithRolesAndMembershipPdf { + pub(crate) async fn from_user(db: &SqlitePool, user: User) -> Self { + let membership_pdf: bool = + sqlx::query_scalar!("SELECT membership_pdf FROM user WHERE id = $1", user.id) + .fetch_optional(db) + .await + .unwrap() + .is_some(); + + Self { + roles: user.roles(db).await, + user, + membership_pdf, + } + } +} #[derive(FromRow, Serialize, Deserialize, Clone, Debug)] pub struct UserWithMembershipPdf { diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index 332c74a..ed6ff61 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -5,7 +5,10 @@ use crate::model::{ log::Log, logbook::Logbook, role::Role, - user::{AdminUser, User, UserWithMembershipPdf, UserWithRoles, VorstandUser}, + user::{ + AdminUser, User, UserWithMembershipPdf, UserWithRoles, UserWithRolesAndMembershipPdf, + VorstandUser, + }, }; use futures::future::join_all; use rocket::{ @@ -45,13 +48,13 @@ async fn index( let user_futures: Vec<_> = User::all(db) .await .into_iter() - .map(|u| async move { UserWithRoles::from_user(u, db).await }) + .map(|u| async move { UserWithRolesAndMembershipPdf::from_user(db, u).await }) .collect(); let user: User = user.into(); let allowed_to_edit = user.has_role(db, "admin").await; - let users: Vec = join_all(user_futures).await; + let users: Vec = join_all(user_futures).await; let roles = Role::all(db).await; let families = Family::all_with_members(db).await; @@ -78,14 +81,13 @@ async fn index_admin( let user_futures: Vec<_> = User::all(db) .await .into_iter() - .map(|u| async move { UserWithRoles::from_user(u, db).await }) + .map(|u| async move { UserWithRolesAndMembershipPdf::from_user(db, u).await }) .collect(); + let users: Vec = join_all(user_futures).await; let user: User = user.user; let allowed_to_edit = user.has_role(db, "admin").await; - let users: Vec = join_all(user_futures).await; - let roles = Role::all(db).await; let families = Family::all_with_members(db).await; diff --git a/src/tera/log.rs b/src/tera/log.rs index 1fe6b15..011aff7 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -1,6 +1,5 @@ use std::net::IpAddr; -use futures::{stream, StreamExt}; use rocket::{ form::Form, get,