225 lines
7.6 KiB
Rust
225 lines
7.6 KiB
Rust
|
|
use log::{info};
|
|
|
|
|
|
use rocket::request::{FlashMessage, Form};
|
|
use rocket::response::{Flash, Redirect};
|
|
use rocket::{get, post, uri};
|
|
use rocket::request::FromForm;
|
|
|
|
use rocket_contrib::templates::Template;
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use crate::error::PeachWebError;
|
|
|
|
use peach_lib::config_manager;
|
|
use peach_lib::config_manager::{load_peach_config};
|
|
use peach_lib::password_utils;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, FromForm)]
|
|
pub struct AddAdminForm {
|
|
pub ssb_id: String,
|
|
}
|
|
#[derive(Debug, Deserialize, FromForm)]
|
|
pub struct DeleteAdminForm {
|
|
pub ssb_id: String,
|
|
}
|
|
|
|
|
|
/// # helpers and routes for /settings/change_password
|
|
/////////////////////////////////
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct ChangePasswordContext {
|
|
pub back: Option<String>,
|
|
pub title: Option<String>,
|
|
pub flash_name: Option<String>,
|
|
pub flash_msg: Option<String>,
|
|
}
|
|
impl ChangePasswordContext {
|
|
pub fn build() -> ChangePasswordContext {
|
|
ChangePasswordContext {
|
|
back: None,
|
|
title: None,
|
|
flash_name: None,
|
|
flash_msg: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, FromForm)]
|
|
pub struct PasswordForm {
|
|
pub old_password: String,
|
|
pub new_password1: String,
|
|
pub new_password2: String,
|
|
}
|
|
|
|
/// this function is for use by a user who is already logged in to change their password
|
|
pub fn save_password_form(password_form: PasswordForm) -> Result<(), PeachWebError> {
|
|
info!(
|
|
"change password!: {} {} {}",
|
|
password_form.old_password, password_form.new_password1, password_form.new_password2
|
|
);
|
|
password_utils::verify_password(&password_form.old_password)?;
|
|
// if the previous line did not throw an error, then the old password is correct
|
|
password_utils::validate_new_passwords(
|
|
&password_form.new_password1,
|
|
&password_form.new_password2,
|
|
)?;
|
|
// if the previous line did not throw an error, then the new password is valid
|
|
password_utils::set_new_password(&password_form.new_password1)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// this change password route is used by a user who is already logged in
|
|
#[get("/settings/change_password")]
|
|
pub fn change_password(flash: Option<FlashMessage>) -> Template {
|
|
let mut context = ChangePasswordContext::build();
|
|
// set back icon link to network route
|
|
context.back = Some("/network".to_string());
|
|
context.title = Some("Change Password".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.name().to_string());
|
|
context.flash_msg = Some(flash.msg().to_string());
|
|
};
|
|
Template::render("password/change_password", &context)
|
|
}
|
|
|
|
/// this change password route is used by a user who is already logged in
|
|
#[post("/settings/change_password", data = "<password_form>")]
|
|
pub fn change_password_post(password_form: Form<PasswordForm>) -> Template {
|
|
let result = save_password_form(password_form.into_inner());
|
|
match result {
|
|
Ok(_) => {
|
|
let mut context = ChangePasswordContext::build();
|
|
// set back icon link to network route
|
|
context.back = Some("/network".to_string());
|
|
context.title = Some("Change Password".to_string());
|
|
context.flash_name = Some("success".to_string());
|
|
context.flash_msg = Some("New password is now saved".to_string());
|
|
// template_dir is set in Rocket.toml
|
|
Template::render("password/change_password", &context)
|
|
}
|
|
Err(err) => {
|
|
let mut context = ChangePasswordContext::build();
|
|
// set back icon link to network route
|
|
context.back = Some("/network".to_string());
|
|
context.title = Some("Configure DNS".to_string());
|
|
context.flash_name = Some("error".to_string());
|
|
context.flash_msg = Some(format!("Failed to save new password: {}", err));
|
|
Template::render("password/change_password", &context)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
}
|
|
|
|
/// # helpers and routes for /settings/configure_admin
|
|
/////////////////////////////////
|
|
|
|
#[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,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// this is a route for viewing and deleting currently configured admin
|
|
#[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
|
|
context.flash_name = Some(flash.name().to_string());
|
|
context.flash_msg = Some(flash.msg().to_string());
|
|
};
|
|
Template::render("admin/configure_admin", &context)
|
|
}
|
|
|
|
/// # helpers and routes for /settings/admin/add
|
|
/////////////////////////////////
|
|
|
|
#[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,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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
|
|
context.flash_name = Some(flash.name().to_string());
|
|
context.flash_msg = Some(flash.msg().to_string());
|
|
};
|
|
// 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"),
|
|
}
|
|
}
|
|
|
|
#[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"),
|
|
}
|
|
}
|