116 lines
3.1 KiB
Rust
116 lines
3.1 KiB
Rust
use crate::model::user::{AdminUser, User};
|
|
use rocket::{
|
|
form::Form,
|
|
get, post,
|
|
request::FlashMessage,
|
|
response::{Flash, Redirect},
|
|
routes, FromForm, Route, State,
|
|
};
|
|
use rocket_dyn_templates::{tera::Context, Template};
|
|
use sqlx::SqlitePool;
|
|
|
|
#[get("/user")]
|
|
async fn index(
|
|
db: &State<SqlitePool>,
|
|
admin: AdminUser,
|
|
flash: Option<FlashMessage<'_>>,
|
|
) -> Template {
|
|
let users = User::all(db).await;
|
|
|
|
let mut context = Context::new();
|
|
if let Some(msg) = flash {
|
|
context.insert("flash", &msg.into_inner());
|
|
}
|
|
context.insert("users", &users);
|
|
context.insert("loggedin_user", &admin.user);
|
|
|
|
Template::render("admin/user/index", context.into_json())
|
|
}
|
|
|
|
#[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 {
|
|
Some(user) => {
|
|
user.reset_pw(db).await;
|
|
Flash::success(
|
|
Redirect::to("/admin/user"),
|
|
format!("Passwort von {} zurückgesetzt", user.name),
|
|
)
|
|
}
|
|
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
|
|
}
|
|
}
|
|
|
|
#[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"),
|
|
format!("Benutzer {} gelöscht", user.name),
|
|
)
|
|
}
|
|
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
|
|
}
|
|
}
|
|
|
|
#[derive(FromForm)]
|
|
pub struct UserEditForm {
|
|
pub(crate) id: i32,
|
|
pub(crate) is_guest: bool,
|
|
pub(crate) is_cox: bool,
|
|
pub(crate) is_admin: bool,
|
|
pub(crate) is_tech: bool,
|
|
pub(crate) dob: Option<String>,
|
|
pub(crate) weight: Option<String>,
|
|
pub(crate) sex: Option<String>,
|
|
}
|
|
|
|
#[post("/user", data = "<data>")]
|
|
async fn update(
|
|
db: &State<SqlitePool>,
|
|
data: Form<UserEditForm>,
|
|
_admin: AdminUser,
|
|
) -> Flash<Redirect> {
|
|
let user = User::find_by_id(db, data.id).await;
|
|
let Some(user) = user else {
|
|
return Flash::error(
|
|
Redirect::to("/admin/user"),
|
|
format!("User with ID {} does not exist!", data.id),
|
|
);
|
|
};
|
|
|
|
user.update(db, data.into_inner()).await;
|
|
|
|
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
|
}
|
|
|
|
#[derive(FromForm)]
|
|
struct UserAddForm<'r> {
|
|
name: &'r str,
|
|
is_guest: bool,
|
|
}
|
|
|
|
#[post("/user/new", data = "<data>")]
|
|
async fn create(
|
|
db: &State<SqlitePool>,
|
|
data: Form<UserAddForm<'_>>,
|
|
_admin: AdminUser,
|
|
) -> Flash<Redirect> {
|
|
if User::create(db, data.name, data.is_guest).await {
|
|
Flash::success(Redirect::to("/admin/user"), "Successfully created user")
|
|
} else {
|
|
Flash::error(
|
|
Redirect::to("/admin/user"),
|
|
format!("User {} already exists", data.name),
|
|
)
|
|
}
|
|
}
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
routes![index, resetpw, update, create, delete]
|
|
}
|