106 lines
3.5 KiB
Rust
106 lines
3.5 KiB
Rust
use maud::{html, Markup, PreEscaped};
|
|
use peach_network::network;
|
|
use rouille::{post_input, try_or_400, Request, Response};
|
|
|
|
use crate::{
|
|
templates,
|
|
utils::{
|
|
flash::{FlashRequest, FlashResponse},
|
|
theme,
|
|
},
|
|
WLAN_IFACE,
|
|
};
|
|
|
|
// ROUTE: /settings/network/wifi/modify?<ssid>
|
|
|
|
fn render_ssid_input(selected_ap: Option<String>) -> Markup {
|
|
html! {
|
|
(PreEscaped("<!-- input for network ssid -->"))
|
|
input id="ssid" name="ssid" class="center input" type="text" placeholder="SSID" title="Network name (SSID) for WiFi access point" value=[selected_ap] autofocus;
|
|
}
|
|
}
|
|
|
|
fn render_password_input() -> Markup {
|
|
html! {
|
|
(PreEscaped("<!-- input for network password -->"))
|
|
input id="pass" name="pass" class="center input" type="password" placeholder="Password" title="Password for WiFi access point";
|
|
}
|
|
}
|
|
|
|
fn render_buttons() -> Markup {
|
|
html! {
|
|
(PreEscaped("<!-- BUTTONS -->"))
|
|
div id="buttons" {
|
|
input id="savePassword" class="button button-primary center" title="Save" type="submit" value="Save";
|
|
a class="button button-secondary center" href="/settings/network" title="Cancel" { "Cancel" }
|
|
}
|
|
}
|
|
}
|
|
|
|
/// WiFi access point password modification form template builder.
|
|
pub fn build_template(request: &Request, selected_ap: Option<String>) -> PreEscaped<String> {
|
|
let (flash_name, flash_msg) = request.retrieve_flash();
|
|
|
|
let form_template = html! {
|
|
(PreEscaped("<!-- NETWORK MODIFY AP PASSWORD FORM -->"))
|
|
div class="card center" {
|
|
form id="wifiModify" action="/settings/network/wifi/modify" method="post" {
|
|
(render_ssid_input(selected_ap))
|
|
(render_password_input())
|
|
(render_buttons())
|
|
}
|
|
// 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))
|
|
}
|
|
}
|
|
};
|
|
|
|
let body = templates::nav::build_template(
|
|
form_template,
|
|
"Change WiFi Password",
|
|
Some("/settings/network"),
|
|
);
|
|
|
|
let theme = theme::get_theme();
|
|
|
|
templates::base::build_template(body, theme)
|
|
}
|
|
|
|
/// Parse the SSID and password for an access point and save the new password.
|
|
pub fn handle_form(request: &Request) -> Response {
|
|
let data = try_or_400!(post_input!(request, {
|
|
ssid: String,
|
|
pass: String,
|
|
}));
|
|
|
|
let (name, msg) = match network::id(WLAN_IFACE, &data.ssid) {
|
|
Ok(Some(id)) => match network::modify(&id, &data.ssid, &data.pass) {
|
|
Ok(_) => ("success".to_string(), "WiFi password updated".to_string()),
|
|
Err(err) => (
|
|
"error".to_string(),
|
|
format!("Failed to update WiFi password: {}", err),
|
|
),
|
|
},
|
|
Ok(None) => (
|
|
"error".to_string(),
|
|
format!(
|
|
"Failed to update WiFi password: no saved credentials found for network {}",
|
|
&data.ssid
|
|
),
|
|
),
|
|
Err(err) => (
|
|
"error".to_string(),
|
|
format!(
|
|
"Failed to update WiFi password: no ID found for network {}: {}",
|
|
&data.ssid, err
|
|
),
|
|
),
|
|
};
|
|
|
|
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
|
|
|
|
Response::redirect_303("/settings/network/wifi/modify").add_flash(flash_name, flash_msg)
|
|
}
|