peach-workspace/peach-web/src/routes/settings/admin/delete.rs

36 lines
1.3 KiB
Rust

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
/// (`/var/lib/peachcloud/config.yml`) and redirect to the administrator
/// configuration URL.
pub fn handle_form(request: &Request) -> Response {
// query the request body for form data
// return a 400 error if the admin_id field is missing
let data = try_or_400!(post_input!(request, {
// the public key of a desired administrator
ssb_id: String,
}));
// remove submitted admin id from file
// 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)
}