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

36 lines
1.2 KiB
Rust

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
/// (`/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,
}));
// TODO: verify that the given ssb_id is valid
// 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
Response::redirect_303("/settings/admin/configure").add_flash(flash_name, flash_msg)
}