2021-11-03 09:52:02 +00:00
|
|
|
use rocket::{
|
|
|
|
get, post,
|
2021-11-04 13:10:41 +00:00
|
|
|
request::FlashMessage,
|
|
|
|
form::{Form, FromForm},
|
2021-11-03 09:52:02 +00:00
|
|
|
response::{Flash, Redirect},
|
|
|
|
uri,
|
|
|
|
};
|
2021-11-04 11:03:40 +00:00
|
|
|
use rocket_dyn_templates::Template;
|
2021-11-03 14:03:21 +00:00
|
|
|
use rocket::serde::{Deserialize, Serialize};
|
2021-10-28 08:01:31 +00:00
|
|
|
|
|
|
|
use peach_lib::config_manager;
|
2021-11-03 09:52:02 +00:00
|
|
|
use peach_lib::config_manager::load_peach_config;
|
2021-10-28 08:01:31 +00:00
|
|
|
|
2021-10-28 12:33:27 +00:00
|
|
|
use crate::error::PeachWebError;
|
2021-10-28 08:01:31 +00:00
|
|
|
|
2021-11-03 09:52:02 +00:00
|
|
|
// HELPERS AND ROUTES FOR /settings/configure_admin
|
2021-10-28 08:01:31 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct ConfigureAdminContext {
|
|
|
|
pub ssb_admin_ids: Vec<String>,
|
|
|
|
pub back: Option<String>,
|
|
|
|
pub title: Option<String>,
|
|
|
|
pub flash_name: Option<String>,
|
|
|
|
pub flash_msg: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 09:52:02 +00:00
|
|
|
/// View and delete currently configured admin.
|
2021-10-28 08:01:31 +00:00
|
|
|
#[get("/settings/configure_admin")]
|
|
|
|
pub fn configure_admin(flash: Option<FlashMessage>) -> 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
|
2021-11-04 11:03:40 +00:00
|
|
|
context.flash_name = Some(flash.kind().to_string());
|
|
|
|
context.flash_msg = Some(flash.message().to_string());
|
2021-10-28 08:01:31 +00:00
|
|
|
};
|
|
|
|
Template::render("admin/configure_admin", &context)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-10-28 08:01:31 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct AddAdminContext {
|
|
|
|
pub back: Option<String>,
|
|
|
|
pub title: Option<String>,
|
|
|
|
pub flash_name: Option<String>,
|
|
|
|
pub flash_msg: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAdminContext {
|
|
|
|
pub fn build() -> AddAdminContext {
|
|
|
|
AddAdminContext {
|
|
|
|
back: None,
|
|
|
|
title: None,
|
|
|
|
flash_name: None,
|
|
|
|
flash_msg: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 12:33:27 +00:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2021-10-28 08:01:31 +00:00
|
|
|
#[get("/settings/admin/add")]
|
|
|
|
pub fn add_admin(flash: Option<FlashMessage>) -> 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
|
2021-11-04 11:03:40 +00:00
|
|
|
context.flash_name = Some(flash.kind().to_string());
|
|
|
|
context.flash_msg = Some(flash.message().to_string());
|
2021-10-28 08:01:31 +00:00
|
|
|
};
|
|
|
|
// template_dir is set in Rocket.toml
|
|
|
|
Template::render("admin/add_admin", &context)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/settings/admin/add", data = "<add_admin_form>")]
|
|
|
|
pub fn add_admin_post(add_admin_form: Form<AddAdminForm>) -> Flash<Redirect> {
|
|
|
|
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-10-28 08:01:31 +00:00
|
|
|
#[post("/settings/admin/delete", data = "<delete_admin_form>")]
|
|
|
|
pub fn delete_admin_post(delete_admin_form: Form<DeleteAdminForm>) -> Flash<Redirect> {
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|