2023-04-04 10:44:14 +02:00
|
|
|
use crate::model::user::{AdminUser, User};
|
|
|
|
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-04-04 10:44:14 +02:00
|
|
|
let users = User::all(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);
|
|
|
|
context.insert("loggedin_user", &admin.user);
|
|
|
|
|
|
|
|
Template::render("admin/user/index", 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"),
|
|
|
|
format!("Successfully reset pw of {}", user.name),
|
|
|
|
)
|
|
|
|
}
|
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"),
|
|
|
|
format!("Sucessfully deleted user {}", user.name),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UserEditForm {
|
|
|
|
id: i32,
|
|
|
|
is_guest: bool,
|
|
|
|
is_cox: bool,
|
|
|
|
is_admin: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-04-04 10:44:14 +02:00
|
|
|
return Flash::error(
|
|
|
|
Redirect::to("/admin/user"),
|
|
|
|
format!("User with ID {} does not exist!", data.id),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
user.update(db, data.is_cox, data.is_admin, data.is_guest)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
|
|
|
}
|
|
|
|
|
2023-04-05 20:56:36 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UserAddForm {
|
|
|
|
name: String,
|
|
|
|
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.clone(), data.is_guest).await {
|
|
|
|
//TODO: fix clone() above
|
|
|
|
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> {
|
2023-04-28 19:29:20 +02:00
|
|
|
routes![index, resetpw, update, create, delete]
|
2023-04-04 10:44:14 +02:00
|
|
|
}
|