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},
|
2022-01-13 11:16:38 +00:00
|
|
|
serde::Deserialize,
|
2021-11-03 09:52:02 +00:00
|
|
|
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;
|
2021-11-08 15:38:10 +00:00
|
|
|
use crate::routes::authentication::Authenticated;
|
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 {
|
2022-01-13 11:16:38 +00:00
|
|
|
let mut context = Context::new();
|
|
|
|
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 {
|
2022-01-13 11:16:38 +00:00
|
|
|
let mut context = Context::new();
|
|
|
|
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
|
|
|
|
2021-10-28 12:33:27 +00:00
|
|
|
#[derive(Debug, Deserialize, FromForm)]
|
|
|
|
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
|
|
|
#[get("/add")]
|
2021-11-08 15:55:58 +00:00
|
|
|
pub fn add_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
|
2022-01-13 11:16:38 +00:00
|
|
|
let mut context = Context::new();
|
|
|
|
context.insert("back", &Some("/settings/admin/configure".to_string()));
|
|
|
|
context.insert("title", &Some("Add 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
|
|
|
|
2021-10-28 08:01:31 +00:00
|
|
|
// template_dir is set in Rocket.toml
|
2022-01-13 11:16:38 +00:00
|
|
|
Template::render("settings/admin/add_admin", &context.into_json())
|
2021-10-28 08:01:31 +00:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
let url = uri!(configure_admin);
|
|
|
|
match result {
|
|
|
|
Ok(_) => Flash::success(Redirect::to(url), "Successfully added new admin"),
|
|
|
|
Err(_) => Flash::error(Redirect::to(url), "Failed to add new admin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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, Deserialize, FromForm)]
|
|
|
|
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!(configure_admin);
|
|
|
|
match result {
|
|
|
|
Ok(_) => Flash::success(Redirect::to(url), "Successfully removed admin id"),
|
|
|
|
Err(_) => Flash::error(Redirect::to(url), "Failed to remove admin id"),
|
|
|
|
}
|
|
|
|
}
|