use rocket::{ get, post, request::FlashMessage, form::{Form, FromForm}, response::{Flash, Redirect}, uri, }; use rocket_dyn_templates::Template; use rocket::serde::{Deserialize, Serialize}; use peach_lib::config_manager; use peach_lib::config_manager::load_peach_config; use crate::error::PeachWebError; use crate::routes::authentication::Authenticated; // HELPERS AND ROUTES FOR /settings/configure_admin #[derive(Debug, Serialize)] pub struct ConfigureAdminContext { pub ssb_admin_ids: Vec, pub back: Option, pub title: Option, pub flash_name: Option, pub flash_msg: Option, } impl ConfigureAdminContext { pub fn build() -> ConfigureAdminContext { let peach_config = load_peach_config().unwrap(); let ssb_admin_ids = peach_config.ssb_admin_ids; ConfigureAdminContext { ssb_admin_ids, back: None, title: None, flash_name: None, flash_msg: None, } } } /// View and delete currently configured admin. #[get("/settings/configure_admin")] pub fn configure_admin(flash: Option, _auth: Authenticated) -> Template { let mut context = ConfigureAdminContext::build(); // set back icon link to network route context.back = Some("/network".to_string()); context.title = Some("Configure Admin".to_string()); // check to see if there is a flash message to display if let Some(flash) = flash { // add flash message contents to the context object context.flash_name = Some(flash.kind().to_string()); context.flash_msg = Some(flash.message().to_string()); }; Template::render("admin/configure_admin", &context) } // HELPERS AND ROUTES FOR /settings/admin/add #[derive(Debug, Deserialize, FromForm)] pub struct AddAdminForm { pub ssb_id: String, } #[derive(Debug, Serialize)] pub struct AddAdminContext { pub back: Option, pub title: Option, pub flash_name: Option, pub flash_msg: Option, } impl AddAdminContext { pub fn build() -> AddAdminContext { AddAdminContext { back: None, title: None, flash_name: None, flash_msg: None, } } } 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(()) } #[get("/settings/admin/add")] pub fn add_admin(flash: Option, _auth: Authenticated) -> Template { let mut context = AddAdminContext::build(); context.back = Some("/settings/configure_admin".to_string()); context.title = Some("Add Admin".to_string()); // check to see if there is a flash message to display if let Some(flash) = flash { // add flash message contents to the context object context.flash_name = Some(flash.kind().to_string()); context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml Template::render("admin/add_admin", &context) } #[post("/settings/admin/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!(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"), } } // HELPERS AND ROUTES FOR /settings/admin/delete #[derive(Debug, Deserialize, FromForm)] pub struct DeleteAdminForm { pub ssb_id: String, } #[post("/settings/admin/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!(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"), } }