From f07f8be52fd09814d4577535093765c6b9702fa7 Mon Sep 17 00:00:00 2001 From: mhfowler Date: Thu, 28 Oct 2021 14:33:27 +0200 Subject: [PATCH] Reformat --- peach-web/src/routes/authentication.rs | 112 +++++++++++++------ peach-web/src/routes/device.rs | 20 +--- peach-web/src/routes/helpers.rs | 8 -- peach-web/src/routes/index.rs | 6 - peach-web/src/routes/ping.rs | 14 +-- peach-web/src/routes/scuttlebutt.rs | 11 +- peach-web/src/routes/settings/admin.rs | 133 ++++------------------- peach-web/src/routes/settings/dns.rs | 14 +-- peach-web/src/routes/settings/network.rs | 16 ++- 9 files changed, 116 insertions(+), 218 deletions(-) diff --git a/peach-web/src/routes/authentication.rs b/peach-web/src/routes/authentication.rs index c795d88..7b69cf5 100644 --- a/peach-web/src/routes/authentication.rs +++ b/peach-web/src/routes/authentication.rs @@ -1,45 +1,17 @@ use serde::{Serialize, Deserialize}; - use rocket_contrib::json::{Json}; use log::{debug, info}; - - use rocket::request::{FlashMessage, Form}; use rocket::response::{Flash, Redirect}; use rocket::{get, post}; use rocket::request::FromForm; - use rocket_contrib::templates::Template; +use peach_lib::password_utils; + use crate::utils::{build_json_response, JsonResponse}; use crate::error::PeachWebError; -use peach_lib::password_utils; - - -#[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(()) -} /// # helpers and routes for /login ///////////////////////////////// @@ -77,6 +49,9 @@ pub fn login(flash: Option) -> Template { Template::render("login", &context) } +/// # helpers and routes for /logout +///////////////////////////////// + #[post("/logout")] pub fn logout() -> Flash { // logout authenticated user @@ -93,7 +68,7 @@ pub fn logout() -> Flash { Flash::success(Redirect::to("/"), "Logged out") } -/// # helpers and routes for password reset +/// # helpers and routes for /reset_password ////////////////////////////////////////// #[derive(Debug, Deserialize, FromForm)] @@ -226,6 +201,10 @@ pub fn reset_password_form_endpoint( } +/// # helpers and routes for /send_password_reset +//////////////////////////////////////////////// + + #[derive(Debug, Serialize)] pub struct SendPasswordResetContext { pub back: Option, @@ -245,7 +224,6 @@ impl SendPasswordResetContext { } } - /// this route is used by a user who is not logged in to send a new password reset link #[get("/send_password_reset")] pub fn send_password_reset_page(flash: Option) -> Template { @@ -289,6 +267,76 @@ pub fn send_password_reset_post() -> Template { } } +/// # helpers and routes for /settings/change_password +////////////////////////////////////////// + +#[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) -> 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 = "")] +pub fn change_password_post(password_form: Form) -> 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) + } + } +} + #[post("/api/v1/settings/change_password", data = "")] pub fn save_password_form_endpoint(password_form: Json) -> Json { let result = save_password_form(password_form.into_inner()); diff --git a/peach-web/src/routes/device.rs b/peach-web/src/routes/device.rs index 7379701..ad72ee8 100644 --- a/peach-web/src/routes/device.rs +++ b/peach-web/src/routes/device.rs @@ -1,36 +1,22 @@ - - use serde::Serialize; - use rocket_contrib::json::{Json}; use log::{debug, info, warn}; - - use rocket::request::{FlashMessage}; use rocket::response::{Flash, Redirect}; use rocket::{get, post}; - - +use std::io; +use std::process::{Command, Output}; use rocket_contrib::templates::Template; -use crate::utils::{build_json_response, JsonResponse}; - use peach_lib::config_manager::load_peach_config; use peach_lib::dyndns_client; - use peach_lib::network_client; - use peach_lib::oled_client; use peach_lib::sbot_client; use peach_lib::stats_client; use peach_lib::stats_client::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}; - - - -use std::io; -use std::process::{Command, Output}; - +use crate::utils::{build_json_response, JsonResponse}; /// # helpers and routes for /device ///////////////////////////////// diff --git a/peach-web/src/routes/helpers.rs b/peach-web/src/routes/helpers.rs index ca9b99a..75ca8e7 100644 --- a/peach-web/src/routes/helpers.rs +++ b/peach-web/src/routes/helpers.rs @@ -1,24 +1,16 @@ - use std::path::{Path, PathBuf}; use log::{debug}; - - - use rocket::response::{NamedFile}; use rocket::{catch, get}; - - use rocket_contrib::templates::Template; use serde::Serialize; - #[get("/", rank = 2)] pub fn files(file: PathBuf) -> Option { NamedFile::open(Path::new("static/").join(file)).ok() } - /// # helpers and routes for /404 ///////////////////////////////// #[derive(Debug, Serialize)] diff --git a/peach-web/src/routes/index.rs b/peach-web/src/routes/index.rs index 9f30df0..ea29588 100644 --- a/peach-web/src/routes/index.rs +++ b/peach-web/src/routes/index.rs @@ -1,10 +1,4 @@ - - - - - use rocket::request::{FlashMessage}; - use rocket::{get}; use rocket_contrib::templates::Template; use serde::Serialize; diff --git a/peach-web/src/routes/ping.rs b/peach-web/src/routes/ping.rs index 739e120..8163b60 100644 --- a/peach-web/src/routes/ping.rs +++ b/peach-web/src/routes/ping.rs @@ -1,26 +1,14 @@ //! Helper routes for pinging services to check that they are active - use rocket_contrib::json::{Json}; use log::{debug, warn}; - - - - use rocket::{get}; - - - - -use crate::utils::{build_json_response, JsonResponse}; - use peach_lib::dyndns_client::{is_dns_updater_online}; use peach_lib::network_client; - use peach_lib::oled_client; - use peach_lib::stats_client; +use crate::utils::{build_json_response, JsonResponse}; // status route: useful for checking connectivity from web client #[get("/api/v1/ping")] diff --git a/peach-web/src/routes/scuttlebutt.rs b/peach-web/src/routes/scuttlebutt.rs index b826df1..9dc5e9e 100644 --- a/peach-web/src/routes/scuttlebutt.rs +++ b/peach-web/src/routes/scuttlebutt.rs @@ -1,18 +1,11 @@ //! Routes for scuttlebutt related functionality. - - - - - use rocket::request::{FlashMessage}; - use rocket::{get}; - - use rocket_contrib::templates::Template; use serde::Serialize; - +/// # helpers and routes for /messages +///////////////////////////////// #[derive(Debug, Serialize)] pub struct MessageContext { diff --git a/peach-web/src/routes/settings/admin.rs b/peach-web/src/routes/settings/admin.rs index 4868330..d7495c3 100644 --- a/peach-web/src/routes/settings/admin.rs +++ b/peach-web/src/routes/settings/admin.rs @@ -1,128 +1,16 @@ - 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, - pub title: Option, - pub flash_name: Option, - pub flash_msg: Option, -} -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) -> 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 = "")] -pub fn change_password_post(password_form: Form) -> 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(()) -} +use crate::error::PeachWebError; /// # helpers and routes for /settings/configure_admin ///////////////////////////////// @@ -169,6 +57,11 @@ pub fn configure_admin(flash: Option) -> Template { /// # 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, @@ -188,6 +81,12 @@ impl AddAdminContext { } } +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) -> Template { let mut context = AddAdminContext::build(); @@ -213,6 +112,14 @@ pub fn add_admin_post(add_admin_form: Form) -> Flash { } } +/// # 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) -> Flash { let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id); diff --git a/peach-web/src/routes/settings/dns.rs b/peach-web/src/routes/settings/dns.rs index 98ce9ca..9e63428 100644 --- a/peach-web/src/routes/settings/dns.rs +++ b/peach-web/src/routes/settings/dns.rs @@ -1,19 +1,11 @@ - use rocket_contrib::json::{Json}; use log::{info}; - - use rocket::request::{FlashMessage, Form}; - use rocket::{get, post}; use rocket::request::FromForm; - use rocket_contrib::templates::Template; use serde::{Serialize, Deserialize}; -use crate::error::PeachWebError; -use crate::utils::{build_json_response, JsonResponse}; - use peach_lib::config_manager; use peach_lib::config_manager::{load_peach_config}; use peach_lib::dyndns_client; @@ -23,6 +15,9 @@ use peach_lib::error::PeachError; use peach_lib::jsonrpc_client_core::{Error, ErrorKind}; use peach_lib::jsonrpc_core::types::error::ErrorCode; +use crate::error::PeachWebError; +use crate::utils::{build_json_response, JsonResponse}; + #[derive(Debug, Deserialize, FromForm)] pub struct DnsForm { @@ -81,9 +76,6 @@ pub fn save_dns_configuration(dns_form: DnsForm) -> Result<(), PeachWebError> { } } -/// # helpers and routes for /network/dns -///////////////////////////////// - #[derive(Debug, Serialize)] pub struct ConfigureDNSContext { pub external_domain: String, diff --git a/peach-web/src/routes/settings/network.rs b/peach-web/src/routes/settings/network.rs index 6f3fd7b..824c4be 100644 --- a/peach-web/src/routes/settings/network.rs +++ b/peach-web/src/routes/settings/network.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; - use rocket_contrib::json; use rocket_contrib::json::{Json}; use log::{debug, warn}; @@ -13,18 +12,15 @@ use rocket::UriDisplayQuery; use rocket_contrib::templates::Template; use serde::{Serialize, Deserialize}; +use peach_lib::network_client; +use peach_lib::network_client::{AccessPoint, Networks, Scan}; +use peach_lib::stats_client::{Traffic}; + use crate::utils::{build_json_response, JsonResponse}; use crate::utils::monitor; use crate::utils::monitor::{Threshold, Data, Alert}; -use peach_lib::network_client; -use peach_lib::network_client::{AccessPoint, Networks, Scan}; - - - -use peach_lib::stats_client::{Traffic}; - /// # structs used by network routes //////////////////////////////////// @@ -585,6 +581,9 @@ pub fn deploy_client() -> Flash { } } +/// # helpers and routes for /network/wifi/add +///////////////////////////////// + #[get("/network/wifi/add")] pub fn network_add_wifi(flash: Option) -> Template { let mut context = NetworkContext::build(); @@ -601,7 +600,6 @@ pub fn network_add_wifi(flash: Option) -> Template { Template::render("network_add", &context) } - // used in /network/wifi/add? #[derive(Debug, Serialize)] pub struct NetworkAddContext {