diff --git a/peach-web/src/main.rs b/peach-web/src/main.rs index baed99d..023775c 100644 --- a/peach-web/src/main.rs +++ b/peach-web/src/main.rs @@ -140,7 +140,8 @@ fn main() { }, (GET) (/auth/login) => { - Response::html(routes::authentication::login::build_template()) + Response::html(routes::authentication::login::build_template(request)) + .reset_flash() }, (POST) (/auth/login) => { @@ -152,7 +153,8 @@ fn main() { }, (GET) (/auth/reset) => { - Response::html(routes::authentication::reset::build_template()) + Response::html(routes::authentication::reset::build_template(request)) + .reset_flash() }, (POST) (/auth/reset) => { @@ -184,7 +186,8 @@ fn main() { }, (GET) (/settings/admin/configure) => { - Response::html(routes::settings::admin::configure::build_template()) + Response::html(routes::settings::admin::configure::build_template(request)) + .reset_flash() }, (POST) (/settings/admin/delete) => { diff --git a/peach-web/src/routes/authentication/change.rs b/peach-web/src/routes/authentication/change.rs index f85e9e9..c33abad 100644 --- a/peach-web/src/routes/authentication/change.rs +++ b/peach-web/src/routes/authentication/change.rs @@ -1,7 +1,7 @@ use log::info; use maud::{html, PreEscaped}; use peach_lib::password_utils; -use rouille::{input, post_input, try_or_400, Request, Response}; +use rouille::{post_input, try_or_400, Request, Response}; use crate::{ error::PeachWebError, @@ -105,6 +105,6 @@ pub fn handle_form(request: &Request) -> Response { ), }; - // set the flash cookie headers and redirect to the configure admin page + // set the flash cookie headers and redirect to the change password page Response::redirect_303("/auth/change").add_flash(flash_name, flash_msg) } diff --git a/peach-web/src/routes/authentication/login.rs b/peach-web/src/routes/authentication/login.rs index 7c0912e..aa643c9 100644 --- a/peach-web/src/routes/authentication/login.rs +++ b/peach-web/src/routes/authentication/login.rs @@ -3,12 +3,18 @@ use maud::{html, PreEscaped}; use peach_lib::password_utils; use rouille::{post_input, try_or_400, Request, Response}; -use crate::templates; +use crate::{ + templates, + utils::flash::{FlashRequest, FlashResponse}, +}; // HELPER AND ROUTES FOR /auth/login (GET and POST) /// Login form template builder. -pub fn build_template() -> PreEscaped { +pub fn build_template(request: &Request) -> PreEscaped { + // check for flash cookies; will be (None, None) if no flash cookies are found + let (flash_name, flash_msg) = request.retrieve_flash(); + let form_template = html! { (PreEscaped("")) div class="card center" { @@ -23,9 +29,11 @@ pub fn build_template() -> PreEscaped { a href="/settings/admin/forgot_password" class="label-small link font-gray" { "Forgot Password?" } } } + } + // render flash message if cookies were found in the request + @if let (Some(name), Some(msg)) = (flash_name, flash_msg) { (PreEscaped("")) - // TODO: render flash message - //{% include "snippets/flash_message" %} + (templates::flash::build_template(name, msg)) } } }; @@ -46,8 +54,6 @@ pub fn handle_form(request: &Request) -> Response { // return a 400 error if the admin_id field is missing let data = try_or_400!(post_input!(request, { password: String })); - // TODO: match on result and define flash message accordingly - // then send the redirect response match password_utils::verify_password(&data.password) { Ok(_) => { info!("Successful login attempt"); @@ -60,18 +66,16 @@ pub fn handle_form(request: &Request) -> Response { Response::redirect_303("/") } - Err(_e) => { + Err(err) => { info!("Unsuccessful login attempt"); - //let err_msg = format!("Invalid password: {}", e); + let err_msg = format!("Invalid password: {}", err); + let (flash_name, flash_msg) = ( + "flash_name=error".to_string(), + format!("flash_msg=Failed to save new password: {}", err_msg), + ); + // if unsuccessful login, render /login page again - - /* - // TODO: add flash message - context.insert("flash_name", &("error".to_string())); - context.insert("flash_msg", &(err_msg)); - */ - - Response::redirect_303("/auth/login") + Response::redirect_303("/auth/login").add_flash(flash_name, flash_msg) } } } diff --git a/peach-web/src/routes/authentication/reset.rs b/peach-web/src/routes/authentication/reset.rs index 37a6ec5..6d19afd 100644 --- a/peach-web/src/routes/authentication/reset.rs +++ b/peach-web/src/routes/authentication/reset.rs @@ -3,12 +3,19 @@ use maud::{html, PreEscaped}; use peach_lib::password_utils; use rouille::{post_input, try_or_400, Request, Response}; -use crate::{error::PeachWebError, templates}; +use crate::{ + error::PeachWebError, + templates, + utils::flash::{FlashRequest, FlashResponse}, +}; // HELPER AND ROUTES FOR /auth/reset (GET and POST) /// Password reset form template builder. -pub fn build_template() -> PreEscaped { +pub fn build_template(request: &Request) -> PreEscaped { + // check for flash cookies; will be (None, None) if no flash cookies are found + let (flash_name, flash_msg) = request.retrieve_flash(); + let form_template = html! { (PreEscaped("")) div class="card center" { @@ -28,9 +35,11 @@ pub fn build_template() -> PreEscaped { a class="button button-secondary center" href="/settings/admin" title="Cancel"{ "Cancel" } } } - (PreEscaped("")) - // TODO: render flash message - //{% include "snippets/flash_message" %} + // render flash message if cookies were found in the request + @if let (Some(name), Some(msg)) = (flash_name, flash_msg) { + (PreEscaped("")) + (templates::flash::build_template(name, msg)) + } } }; @@ -79,23 +88,22 @@ pub fn handle_form(request: &Request) -> Response { })); // save submitted admin id to file - let _result = save_password( + let (flash_name, flash_msg) = match save_password( &data.temporary_password, &data.new_password1, &data.new_password2, - ); - - // TODO: match on result and define flash message accordingly - // then send the redirect response + ) { + Ok(_) => ( + // = + "flash_name=success".to_string(), + "flash_msg=New password has been saved. Return home to login".to_string(), + ), + Err(err) => ( + "flash_name=error".to_string(), + format!("flash_msg=Failed to reset password: {}", err), + ), + }; // redirect to the configure admin page - // TODO: add flash message - Response::redirect_303("/auth/reset") + Response::redirect_303("/auth/reset").add_flash(flash_name, flash_msg) } - -/* - 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)), - } -*/ diff --git a/peach-web/src/routes/settings/admin/add.rs b/peach-web/src/routes/settings/admin/add.rs index cc63873..863d3c7 100644 --- a/peach-web/src/routes/settings/admin/add.rs +++ b/peach-web/src/routes/settings/admin/add.rs @@ -1,6 +1,8 @@ use peach_lib::config_manager; use rouille::{post_input, try_or_400, Request, Response}; +use crate::utils::flash::FlashResponse; + // HELPER AND ROUTES FOR /settings/admin/add /// Parse an `admin_id` from the submitted form, save it to file @@ -14,20 +16,20 @@ pub fn handle_form(request: &Request) -> Response { ssb_id: String, })); - // save submitted admin id to file - let _result = config_manager::add_ssb_admin_id(&data.ssb_id); + // TODO: verify that the given ssb_id is valid - // TODO: match on result and define flash message accordingly - // then send the redirect response + // save submitted admin id to file + let (flash_name, flash_msg) = match config_manager::add_ssb_admin_id(&data.ssb_id) { + Ok(_) => ( + "flash_name=success".to_string(), + "flash_msg=Added SSB administrator".to_string(), + ), + Err(err) => ( + "flash_name=error".to_string(), + format!("flash_msg=Failed to add new administrator: {}", err), + ), + }; // redirect to the configure admin page - // TODO: add flash message - Response::redirect_303("/settings/admin/configure") + Response::redirect_303("/settings/admin/configure").add_flash(flash_name, flash_msg) } - -/* - 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)), - } -*/ diff --git a/peach-web/src/routes/settings/admin/configure.rs b/peach-web/src/routes/settings/admin/configure.rs index 7edcb9e..9b37a1d 100644 --- a/peach-web/src/routes/settings/admin/configure.rs +++ b/peach-web/src/routes/settings/admin/configure.rs @@ -1,14 +1,27 @@ use maud::{html, PreEscaped}; use peach_lib::config_manager; +use rouille::Request; -use crate::templates; +use crate::{templates, utils::flash::FlashRequest}; /// Administrator settings menu template builder. -pub fn build_template() -> PreEscaped { +pub fn build_template(request: &Request) -> PreEscaped { + // check for flash cookies; will be (None, None) if no flash cookies are found + let (mut flash_name, mut flash_msg) = request.retrieve_flash(); + // attempt to load peachcloud config file - let ssb_admins = config_manager::load_peach_config() - .ok() - .map(|config| config.ssb_admin_ids); + let ssb_admins = match config_manager::load_peach_config() { + Ok(config) => Some(config.ssb_admin_ids), + // note: this will overwrite any received flash cookie values + // TODO: find a way to include the `err` in the flash_msg + // currently produces an error because we end up with Some(String) + // instead of Some(str) + Err(_err) => { + flash_name = Some("flash_name=error"); + flash_msg = Some("flash_msg=Failed to read PeachCloud configuration file"); + None + } + }; let menu_template = html! { (PreEscaped("")) @@ -38,10 +51,11 @@ pub fn build_template() -> PreEscaped { } (PreEscaped("")) input class="button button-primary center" type="submit" title="Add SSB administrator" value="Add Admin"; + } + // render flash message if cookies were found in the request + @if let (Some(name), Some(msg)) = (flash_name, flash_msg) { (PreEscaped("")) - @if ssb_admins.is_none() { - (templates::flash::build_template("error", "Failed to read PeachCloud configuration file")) - } + (templates::flash::build_template(name, &msg)) } } }; diff --git a/peach-web/src/routes/settings/admin/delete.rs b/peach-web/src/routes/settings/admin/delete.rs index 5a21c36..06bfc1c 100644 --- a/peach-web/src/routes/settings/admin/delete.rs +++ b/peach-web/src/routes/settings/admin/delete.rs @@ -1,6 +1,8 @@ use peach_lib::config_manager; use rouille::{post_input, try_or_400, Request, Response}; +use crate::utils::flash::FlashResponse; + // HELPERS AND ROUTES FOR /settings/admin/delete /// Parse an `admin_id` from the submitted form, delete it from file @@ -15,22 +17,19 @@ pub fn handle_form(request: &Request) -> Response { })); // remove submitted admin id from file - let _result = config_manager::delete_ssb_admin_id(&data.ssb_id); - - // TODO: match on result and define flash message accordingly - // then send the redirect response - - // redirect to the configure admin page - // TODO: add flash message - Response::redirect_303("/settings/admin/configure") -} - -/* - 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), + // match on the result and set flash name and msg accordingly + let (flash_name, flash_msg) = match config_manager::delete_ssb_admin_id(&data.ssb_id) { + Ok(_) => ( + // = + "flash_name=success".to_string(), + "flash_msg=Removed SSB administrator".to_string(), ), - } -*/ + Err(err) => ( + "flash_name=error".to_string(), + format!("flash_msg=Failed to remove administrator: {}", err), + ), + }; + + // set the flash cookie headers and redirect to the configure admin page + Response::redirect_303("/settings/admin/configure").add_flash(flash_name, flash_msg) +}