From 26fee59c5d8000935a9715aeaa3da5437c8ccdaa Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 30 Nov 2024 15:07:12 +0100 Subject: [PATCH] Can determine the rights of the token over the group --- .../src/controllers/server_controller.rs | 18 ++--- remote_backend/src/virtweb_client.rs | 81 +++++++++++++++++++ 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/remote_backend/src/controllers/server_controller.rs b/remote_backend/src/controllers/server_controller.rs index 38bbf81..d84d51f 100644 --- a/remote_backend/src/controllers/server_controller.rs +++ b/remote_backend/src/controllers/server_controller.rs @@ -66,16 +66,16 @@ pub async fn rights() -> HttpResult { let group_vms = virtweb_client::group_vm_info(&g).await?; res.groups.push(GroupInfo { - id: g, + id: g.clone(), vms: group_vms, - can_get_state: false, //TODO - can_start: false, - can_shutdown: false, - can_kill: false, - can_reset: false, - can_suspend: false, - can_resume: false, - can_screenshot: false, + can_get_state: rights.is_route_allowed("GET", &g.route_vm_state(None)), + can_start: rights.is_route_allowed("GET", &g.route_vm_start(None)), + can_shutdown: rights.is_route_allowed("GET", &g.route_vm_shutdown(None)), + can_kill: rights.is_route_allowed("GET", &g.route_vm_kill(None)), + can_reset: rights.is_route_allowed("GET", &g.route_vm_reset(None)), + can_suspend: rights.is_route_allowed("GET", &g.route_vm_suspend(None)), + can_resume: rights.is_route_allowed("GET", &g.route_vm_resume(None)), + can_screenshot: rights.is_route_allowed("GET", &g.route_vm_screenshot(None)), }) } diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index d2e9dd9..9344bfb 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -19,6 +19,87 @@ impl GroupID { pub fn route_vm_info(&self) -> String { format!("/api/group/{}/vm/info", self.0) } + + pub fn route_vm_state(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/state{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_start(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/start{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_shutdown(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/shutdown{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_suspend(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/suspend{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_resume(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/resume{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_kill(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/kill{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_reset(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/reset{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } + pub fn route_vm_screenshot(&self, vm: Option) -> String { + format!( + "/api/group/{}/vm/screenshot{}", + self.0, + match vm { + None => "".to_string(), + Some(id) => format!("?vm_id={}", id.0), + } + ) + } } #[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]