add flash messages to admin settings and auth routes

This commit is contained in:
glyph 2022-03-20 15:36:24 +02:00
parent cd7c2bc230
commit 976fac973d
7 changed files with 109 additions and 79 deletions

View File

@ -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) => {

View File

@ -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)
}

View File

@ -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<String> {
pub fn build_template(request: &Request) -> PreEscaped<String> {
// 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("<!-- LOGIN FORM -->"))
div class="card center" {
@ -23,9 +29,11 @@ pub fn build_template() -> PreEscaped<String> {
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("<!-- FLASH MESSAGE -->"))
// 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)
}
}
}

View File

@ -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<String> {
pub fn build_template(request: &Request) -> PreEscaped<String> {
// 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("<!-- RESET PASSWORD PAGE -->"))
div class="card center" {
@ -28,9 +35,11 @@ pub fn build_template() -> PreEscaped<String> {
a class="button button-secondary center" href="/settings/admin" title="Cancel"{ "Cancel" }
}
}
(PreEscaped("<!-- FLASH MESSAGE -->"))
// 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("<!-- FLASH MESSAGE -->"))
(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(_) => (
// <cookie-name>=<cookie-value>
"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)),
}
*/

View File

@ -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)),
}
*/

View File

@ -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<String> {
pub fn build_template(request: &Request) -> PreEscaped<String> {
// 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("<!-- CONFIGURE ADMIN PAGE -->"))
@ -38,10 +51,11 @@ pub fn build_template() -> PreEscaped<String> {
}
(PreEscaped("<!-- BUTTONS -->"))
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("<!-- FLASH MESSAGE -->"))
@if ssb_admins.is_none() {
(templates::flash::build_template("error", "Failed to read PeachCloud configuration file"))
}
(templates::flash::build_template(name, &msg))
}
}
};

View File

@ -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(_) => (
// <cookie-name>=<cookie-value>
"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)
}