Permissions, shutdown route

This commit is contained in:
Jack Mechem 2026-03-30 19:53:55 -07:00
parent 15d0d174a9
commit ac01c03094
4 changed files with 31 additions and 1 deletions

View file

@ -107,7 +107,8 @@
security.polkit.extraConfig = '' security.polkit.extraConfig = ''
polkit.addRule(function(action, subject) { polkit.addRule(function(action, subject) {
if ((action.id == "org.freedesktop.systemd1.manage-units" || if ((action.id == "org.freedesktop.systemd1.manage-units" ||
action.id == "org.freedesktop.login1.reboot") && action.id == "org.freedesktop.login1.reboot" ||
action.id == "org.freedesktop.login1.power-off") &&
subject.user == "server-dash-api") { subject.user == "server-dash-api") {
return polkit.Result.YES; return polkit.Result.YES;
} }

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/24v5zn615rqab56z2i1vhi27mdisv0l1-server-dash-api-0.1.0

View file

@ -28,6 +28,7 @@ async fn main() {
get(routes::services::service_logs), get(routes::services::service_logs),
) )
.route("/system/reboot", post(routes::system::system_reboot)) .route("/system/reboot", post(routes::system::system_reboot))
.route("/system/shutdown", post(routes::system::system_shutdown))
.route_layer(middleware::from_fn(auth::require_auth)); .route_layer(middleware::from_fn(auth::require_auth));
let app = Router::new() let app = Router::new()

View file

@ -4,6 +4,33 @@ use zbus::Connection;
use crate::auth; use crate::auth;
use crate::models; use crate::models;
// POST /system/shutdown
pub async fn system_shutdown(headers: HeaderMap) -> impl IntoResponse {
let conn = match Connection::system().await {
Ok(c) => c,
Err(e) => {
return models::ActionResponse::err(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string())
.into_response();
}
};
let result = conn
.call_method(
Some("org.freedesktop.login1"),
"/org/freedesktop/login1",
Some("org.freedesktop.login1.Manager"),
"PowerOff",
&(false,),
)
.await;
match result {
Ok(_) => models::ActionResponse::ok("Shutting down...".to_string()).into_response(),
Err(e) => models::ActionResponse::err(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string())
.into_response(),
}
}
// POST /system/reboot // POST /system/reboot
pub async fn system_reboot(headers: HeaderMap) -> impl IntoResponse { pub async fn system_reboot(headers: HeaderMap) -> impl IntoResponse {
let conn = match Connection::system().await { let conn = match Connection::system().await {