2023-12-23 21:27:52 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::model::{
|
2024-01-18 16:37:54 +01:00
|
|
|
family::Family,
|
2023-12-23 21:27:52 +01:00
|
|
|
role::Role,
|
2024-01-19 00:39:15 +01:00
|
|
|
user::{AdminUser, Fee, User, UserWithRoles, VorstandUser},
|
2023-12-23 21:27:52 +01:00
|
|
|
};
|
2024-01-19 00:39:15 +01:00
|
|
|
use futures::future::{self, join_all};
|
2023-04-04 10:44:14 +02:00
|
|
|
use rocket::{
|
|
|
|
form::Form,
|
|
|
|
get, post,
|
2023-04-28 19:59:07 +02:00
|
|
|
request::FlashMessage,
|
2023-04-04 10:44:14 +02:00
|
|
|
response::{Flash, Redirect},
|
|
|
|
routes, FromForm, Route, State,
|
|
|
|
};
|
2023-04-28 19:59:07 +02:00
|
|
|
use rocket_dyn_templates::{tera::Context, Template};
|
2023-04-04 10:44:14 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
|
|
|
#[get("/user")]
|
2023-04-28 19:59:07 +02:00
|
|
|
async fn index(
|
|
|
|
db: &State<SqlitePool>,
|
|
|
|
admin: AdminUser,
|
|
|
|
flash: Option<FlashMessage<'_>>,
|
|
|
|
) -> Template {
|
2023-12-23 21:27:52 +01:00
|
|
|
let user_futures: Vec<_> = User::all(db)
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.map(|u| async move { UserWithRoles::from_user(u, db).await })
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let users: Vec<UserWithRoles> = join_all(user_futures).await;
|
|
|
|
|
|
|
|
let roles = Role::all(db).await;
|
2024-01-18 16:37:54 +01:00
|
|
|
let families = Family::all_with_members(db).await;
|
2023-04-28 19:59:07 +02:00
|
|
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
if let Some(msg) = flash {
|
|
|
|
context.insert("flash", &msg.into_inner());
|
|
|
|
}
|
|
|
|
context.insert("users", &users);
|
2023-12-23 21:27:52 +01:00
|
|
|
context.insert("roles", &roles);
|
2024-01-18 16:37:54 +01:00
|
|
|
context.insert("families", &families);
|
2023-12-23 21:27:52 +01:00
|
|
|
context.insert(
|
|
|
|
"loggedin_user",
|
|
|
|
&UserWithRoles::from_user(admin.user, db).await,
|
|
|
|
);
|
2023-04-28 19:59:07 +02:00
|
|
|
|
|
|
|
Template::render("admin/user/index", context.into_json())
|
2023-04-04 10:44:14 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 21:36:38 +01:00
|
|
|
#[get("/user/fees")]
|
|
|
|
async fn fees(
|
|
|
|
db: &State<SqlitePool>,
|
2024-01-19 00:39:15 +01:00
|
|
|
admin: VorstandUser,
|
2024-01-18 21:36:38 +01:00
|
|
|
flash: Option<FlashMessage<'_>>,
|
|
|
|
) -> Template {
|
2024-01-19 00:39:15 +01:00
|
|
|
let mut context = Context::new();
|
2024-01-18 21:36:38 +01:00
|
|
|
|
2024-01-19 08:02:09 +01:00
|
|
|
let users = User::all_payer_groups(db).await;
|
2024-01-19 00:39:15 +01:00
|
|
|
let mut fees = Vec::new();
|
|
|
|
for user in users {
|
|
|
|
if let Some(fee) = user.fee(db).await {
|
|
|
|
fees.push(fee);
|
|
|
|
}
|
|
|
|
}
|
2024-01-18 21:36:38 +01:00
|
|
|
|
2024-01-19 00:39:15 +01:00
|
|
|
context.insert("fees", &fees);
|
2024-01-18 21:36:38 +01:00
|
|
|
|
|
|
|
if let Some(msg) = flash {
|
|
|
|
context.insert("flash", &msg.into_inner());
|
|
|
|
}
|
|
|
|
context.insert(
|
|
|
|
"loggedin_user",
|
2024-01-19 00:39:15 +01:00
|
|
|
&UserWithRoles::from_user(admin.into(), db).await,
|
2024-01-18 21:36:38 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
Template::render("admin/user/fees", context.into_json())
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
#[get("/user/<user>/reset-pw")]
|
|
|
|
async fn resetpw(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<Redirect> {
|
|
|
|
let user = User::find_by_id(db, user).await;
|
|
|
|
match user {
|
2023-04-10 14:25:31 +02:00
|
|
|
Some(user) => {
|
2023-04-04 10:44:14 +02:00
|
|
|
user.reset_pw(db).await;
|
|
|
|
Flash::success(
|
|
|
|
Redirect::to("/admin/user"),
|
2023-10-23 22:52:11 +02:00
|
|
|
format!("Passwort von {} zurückgesetzt", user.name),
|
2023-04-04 10:44:14 +02:00
|
|
|
)
|
|
|
|
}
|
2023-04-10 14:25:31 +02:00
|
|
|
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
|
2023-04-04 10:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 19:29:20 +02:00
|
|
|
#[get("/user/<user>/delete")]
|
|
|
|
async fn delete(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<Redirect> {
|
|
|
|
let user = User::find_by_id(db, user).await;
|
|
|
|
match user {
|
|
|
|
Some(user) => {
|
|
|
|
user.delete(db).await;
|
|
|
|
Flash::success(
|
|
|
|
Redirect::to("/admin/user"),
|
2023-10-23 22:52:11 +02:00
|
|
|
format!("Benutzer {} gelöscht", user.name),
|
2023-04-28 19:29:20 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 21:27:52 +01:00
|
|
|
#[derive(FromForm, Debug)]
|
2023-11-02 12:15:10 +01:00
|
|
|
pub struct UserEditForm {
|
|
|
|
pub(crate) id: i32,
|
|
|
|
pub(crate) dob: Option<String>,
|
|
|
|
pub(crate) weight: Option<String>,
|
|
|
|
pub(crate) sex: Option<String>,
|
2023-12-23 21:27:52 +01:00
|
|
|
pub(crate) roles: HashMap<String, String>,
|
2023-12-30 21:21:30 +01:00
|
|
|
pub(crate) member_since_date: Option<String>,
|
|
|
|
pub(crate) birthdate: Option<String>,
|
|
|
|
pub(crate) mail: Option<String>,
|
|
|
|
pub(crate) nickname: Option<String>,
|
|
|
|
pub(crate) notes: Option<String>,
|
|
|
|
pub(crate) phone: Option<String>,
|
|
|
|
pub(crate) address: Option<String>,
|
2024-01-18 16:37:54 +01:00
|
|
|
pub(crate) family_id: Option<i64>,
|
2023-04-04 10:44:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/user", data = "<data>")]
|
2023-04-04 12:19:56 +02:00
|
|
|
async fn update(
|
|
|
|
db: &State<SqlitePool>,
|
|
|
|
data: Form<UserEditForm>,
|
|
|
|
_admin: AdminUser,
|
|
|
|
) -> Flash<Redirect> {
|
2023-04-04 10:44:14 +02:00
|
|
|
let user = User::find_by_id(db, data.id).await;
|
2023-04-10 14:25:31 +02:00
|
|
|
let Some(user) = user else {
|
2023-08-02 14:29:19 +02:00
|
|
|
return Flash::error(
|
|
|
|
Redirect::to("/admin/user"),
|
|
|
|
format!("User with ID {} does not exist!", data.id),
|
|
|
|
);
|
2023-04-04 10:44:14 +02:00
|
|
|
};
|
|
|
|
|
2023-11-02 12:15:10 +01:00
|
|
|
user.update(db, data.into_inner()).await;
|
2023-04-04 10:44:14 +02:00
|
|
|
|
|
|
|
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:56:36 +02:00
|
|
|
#[derive(FromForm)]
|
2023-05-24 12:11:55 +02:00
|
|
|
struct UserAddForm<'r> {
|
|
|
|
name: &'r str,
|
2023-04-05 20:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/user/new", data = "<data>")]
|
|
|
|
async fn create(
|
|
|
|
db: &State<SqlitePool>,
|
2023-05-24 12:11:55 +02:00
|
|
|
data: Form<UserAddForm<'_>>,
|
2023-04-05 20:56:36 +02:00
|
|
|
_admin: AdminUser,
|
|
|
|
) -> Flash<Redirect> {
|
2023-12-23 21:27:52 +01:00
|
|
|
if User::create(db, data.name).await {
|
2023-04-05 20:56:36 +02:00
|
|
|
Flash::success(Redirect::to("/admin/user"), "Successfully created user")
|
|
|
|
} else {
|
|
|
|
Flash::error(
|
|
|
|
Redirect::to("/admin/user"),
|
|
|
|
format!("User {} already exists", data.name),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
pub fn routes() -> Vec<Route> {
|
2024-01-18 21:36:38 +01:00
|
|
|
routes![index, resetpw, update, create, delete, fees]
|
2023-04-04 10:44:14 +02:00
|
|
|
}
|