use rocket::{ form::{Form, FromForm}, get, post, request::FlashMessage, response::{Flash, Redirect}, uri, }; use rocket_dyn_templates::{tera::Context, Template}; use peach_lib::config_manager; use crate::error::PeachWebError; use crate::routes::authentication::Authenticated; use crate::utils; // HELPERS AND ROUTES FOR /settings/admin /// Administrator settings menu. #[get("/")] pub fn admin_menu(flash: Option, _auth: Authenticated) -> Template { // retrieve current ui theme let theme = utils::get_theme(); let mut context = Context::new(); context.insert("theme", &theme); context.insert("back", &Some("/settings".to_string())); context.insert("title", &Some("Administrator Settings".to_string())); // check to see if there is a flash message to display if let Some(flash) = flash { context.insert("flash_name", &Some(flash.kind().to_string())); context.insert("flash_msg", &Some(flash.message().to_string())); }; Template::render("settings/admin/menu", &context.into_json()) } // HELPERS AND ROUTES FOR /settings/admin/configure /// View and delete currently configured admin. #[get("/configure")] pub fn configure_admin(flash: Option, _auth: Authenticated) -> Template { // retrieve current ui theme let theme = utils::get_theme(); let mut context = Context::new(); context.insert("theme", &theme); context.insert("back", &Some("/settings/admin".to_string())); context.insert("title", &Some("Configure Admin".to_string())); // check to see if there is a flash message to display if let Some(flash) = flash { context.insert("flash_name", &Some(flash.kind().to_string())); context.insert("flash_msg", &Some(flash.message().to_string())); }; // 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()) } // HELPERS AND ROUTES FOR /settings/admin/add #[derive(Debug, 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)?; // if the previous line didn't throw an error then it was a success Ok(()) } #[post("/add", data = "")] pub fn add_admin_post(add_admin_form: Form, _auth: Authenticated) -> Flash { let result = save_add_admin_form(add_admin_form.into_inner()); let url = uri!("/settings/admin/configure"); 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)), } } // HELPERS AND ROUTES FOR /settings/admin/delete #[derive(Debug, FromForm)] pub struct DeleteAdminForm { pub ssb_id: String, } #[post("/delete", data = "")] pub fn delete_admin_post( delete_admin_form: Form, _auth: Authenticated, ) -> Flash { let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id); let url = uri!("/settings/admin", configure_admin); 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), ), } }