peach-workspace/peach-web/src/routes/settings/admin.rs

123 lines
3.9 KiB
Rust
Raw Normal View History

2021-11-03 09:52:02 +00:00
use rocket::{
2021-11-15 15:32:00 +00:00
form::{Form, FromForm},
2021-11-03 09:52:02 +00:00
get, post,
2021-11-04 13:10:41 +00:00
request::FlashMessage,
2021-11-03 09:52:02 +00:00
response::{Flash, Redirect},
uri,
};
2022-01-13 11:16:38 +00:00
use rocket_dyn_templates::{tera::Context, Template};
2021-10-28 08:01:31 +00:00
use peach_lib::config_manager;
2021-10-28 12:33:27 +00:00
use crate::error::PeachWebError;
use crate::routes::authentication::Authenticated;
use crate::utils;
2021-10-28 08:01:31 +00:00
2021-11-15 18:56:22 +00:00
// HELPERS AND ROUTES FOR /settings/admin
/// Administrator settings menu.
#[get("/")]
pub fn admin_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve current ui theme
let theme = utils::get_theme();
2022-01-13 11:16:38 +00:00
let mut context = Context::new();
context.insert("theme", &theme);
2022-01-13 11:16:38 +00:00
context.insert("back", &Some("/settings".to_string()));
context.insert("title", &Some("Administrator Settings".to_string()));
2021-11-15 18:56:22 +00:00
// check to see if there is a flash message to display
if let Some(flash) = flash {
2022-01-13 11:16:38 +00:00
context.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
2021-11-15 18:56:22 +00:00
};
2021-10-28 08:01:31 +00:00
2022-01-13 11:16:38 +00:00
Template::render("settings/admin/menu", &context.into_json())
2021-10-28 08:01:31 +00:00
}
2022-01-13 11:16:38 +00:00
// HELPERS AND ROUTES FOR /settings/admin/configure
2021-10-28 08:01:31 +00:00
2021-11-15 18:56:22 +00:00
/// View and delete currently configured admin.
#[get("/configure")]
2021-11-08 15:55:58 +00:00
pub fn configure_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve current ui theme
let theme = utils::get_theme();
2022-01-13 11:16:38 +00:00
let mut context = Context::new();
context.insert("theme", &theme);
2022-01-13 11:16:38 +00:00
context.insert("back", &Some("/settings/admin".to_string()));
context.insert("title", &Some("Configure Admin".to_string()));
2021-10-28 08:01:31 +00:00
// check to see if there is a flash message to display
if let Some(flash) = flash {
2022-01-13 11:16:38 +00:00
context.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
2021-10-28 08:01:31 +00:00
};
2022-01-13 11:16:38 +00:00
// load the peach configuration vector
match config_manager::load_peach_config() {
Ok(config) => {
// retrieve the vector of ssb admin ids
let ssb_admin_ids = config.ssb_admin_ids;
context.insert("ssb_admin_ids", &ssb_admin_ids);
}
// if load fails, overwrite the flash_name and flash_msg
Err(e) => {
context.insert("flash_name", &Some("error".to_string()));
context.insert(
"flash_msg",
&Some(format!("Failed to load Peach config: {}", e)),
);
}
}
Template::render("settings/admin/configure_admin", &context.into_json())
2021-10-28 08:01:31 +00:00
}
2021-11-03 09:52:02 +00:00
// HELPERS AND ROUTES FOR /settings/admin/add
2021-10-28 08:01:31 +00:00
#[derive(Debug, FromForm)]
2021-10-28 12:33:27 +00:00
pub struct AddAdminForm {
pub ssb_id: String,
}
pub fn save_add_admin_form(admin_form: AddAdminForm) -> Result<(), PeachWebError> {
let _result = config_manager::add_ssb_admin_id(&admin_form.ssb_id)?;
2021-11-15 15:32:00 +00:00
// if the previous line didn't throw an error then it was a success
2021-10-28 12:33:27 +00:00
Ok(())
}
2021-11-15 15:32:00 +00:00
#[post("/add", data = "<add_admin_form>")]
2021-11-08 15:55:58 +00:00
pub fn add_admin_post(add_admin_form: Form<AddAdminForm>, _auth: Authenticated) -> Flash<Redirect> {
2021-10-28 08:01:31 +00:00
let result = save_add_admin_form(add_admin_form.into_inner());
2022-01-14 13:31:40 +00:00
let url = uri!("/settings/admin/configure");
2021-10-28 08:01:31 +00:00
match result {
Ok(_) => Flash::success(Redirect::to(url), "Added SSB administrator"),
Err(e) => Flash::error(Redirect::to(url), format!("Failed to add new admin: {}", e)),
2021-10-28 08:01:31 +00:00
}
}
2021-11-03 09:52:02 +00:00
// HELPERS AND ROUTES FOR /settings/admin/delete
2021-10-28 12:33:27 +00:00
#[derive(Debug, FromForm)]
2021-10-28 12:33:27 +00:00
pub struct DeleteAdminForm {
pub ssb_id: String,
}
2021-11-15 15:32:00 +00:00
#[post("/delete", data = "<delete_admin_form>")]
pub fn delete_admin_post(
delete_admin_form: Form<DeleteAdminForm>,
_auth: Authenticated,
) -> Flash<Redirect> {
2021-10-28 08:01:31 +00:00
let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id);
let url = uri!("/settings/admin", configure_admin);
2021-10-28 08:01:31 +00:00
match result {
Ok(_) => Flash::success(Redirect::to(url), "Removed SSB administrator"),
Err(e) => Flash::error(
Redirect::to(url),
format!("Failed to remove admin id: {}", e),
),
2021-10-28 08:01:31 +00:00
}
}