Kaynağa Gözat

Create read/write for user permissions.

Lee Morgan 4 hafta önce
ebeveyn
işleme
47c6fbed30
5 değiştirilmiş dosya ile 54 ekleme ve 2 silme
  1. 2 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 5 0
      src/main.rs
  4. 44 0
      src/users.rs
  5. 2 2
      src/websocket.rs

+ 2 - 0
Cargo.lock

@@ -649,6 +649,7 @@ dependencies = [
  "awc",
  "dotenvy",
  "futures-util",
+ "serde",
  "serde_json",
  "uuid",
 ]
@@ -1190,6 +1191,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
 dependencies = [
  "serde_core",
+ "serde_derive",
 ]
 
 [[package]]

+ 1 - 0
Cargo.toml

@@ -9,6 +9,7 @@ actix-web = "4.14.0"
 awc = "3.8.2"
 dotenvy = "0.15.7"
 futures-util = "0.3.33"
+serde = { version = "1.0.229", features = ["derive"]}
 serde_json = "1.0.151"
 uuid = { version = "1.24.0", features = ["v4", "serde"]}
 

+ 5 - 0
src/main.rs

@@ -3,6 +3,7 @@ use actix_web::{HttpServer, App, middleware};
 mod routes;
 mod controllers;
 mod websocket;
+mod users;
 
 #[actix_web::main]
 async fn main() -> Result<(), std::io::Error> {
@@ -13,6 +14,10 @@ async fn main() -> Result<(), std::io::Error> {
     let ha_ip: String = env_var("HA_IP");
     let ha_token: String = env_var("HA_TOKEN");
 
+    //Read User Data
+    let users = users::User::read();
+    println!("{:#?}", users);
+
     //Connect to HA
     websocket::connect_to_ha(ha_ip, ha_token)
         .await

+ 44 - 0
src/users.rs

@@ -1,5 +1,8 @@
 use uuid::Uuid;
+use serde::{Serialize, Deserialize};
+use std::{fs, io::Write, path::Path};
 
+#[derive(Debug, Serialize, Deserialize)]
 pub struct User {
     pub id: Uuid,
     pub name: String,
@@ -8,12 +11,53 @@ pub struct User {
     pub devices: Vec<Device>
 }
 
+#[derive(Debug, Serialize, Deserialize)]
 pub struct Device {
     id: String,
     device_type: DeviceType,
     name: String
 }
 
+#[derive(Debug, Serialize, Deserialize)]
 pub enum DeviceType{
     Switch
 }
+
+impl User {
+    pub fn read() -> Vec<User> {
+        let path = "/var/lib/ha_permissions/users.json";
+
+        if !Path::new(path).exists() {
+            return Vec::new();
+        }
+
+        let data = fs::read_to_string(path).expect("Failed to read users file");
+
+        if data.trim().is_empty() {
+            return Vec::new();
+        }
+
+        serde_json::from_str(&data).expect("Failed to parse users data")
+    }
+
+    pub fn write(users: Vec<User>) {
+        let path = "/var/lib/ha_permissions/users.json";
+        let data = serde_json::to_string_pretty(&users)
+            .expect("Failed to serialize users");
+
+        if let Some(parent) = Path::new(path).parent() {
+            fs::create_dir_all(parent)
+                .expect("Failed to create directory");
+        }
+
+        let tmp_path = format!("{}.tmp", path);
+        let mut file = fs::File::create(&tmp_path)
+            .expect("Failed to create temp file");
+
+        file.write_all(data.as_bytes())
+            .expect("Failed to write temp file");
+
+        fs::rename(&tmp_path, path)
+            .expect("Failed to replace users file");
+    }
+}

+ 2 - 2
src/websocket.rs

@@ -33,9 +33,9 @@ pub async fn connect_to_ha(ip: String, token: String) -> Result<(), Box<dyn std:
                 connection.send(Message::Text(devices_msg.to_string().into())).await?;
                 message_id += 1;*/
 
-                let entities_msg = json!({"type": "config/entity_registry/list", "id": message_id});
+                /*let entities_msg = json!({"type": "config/entity_registry/list", "id": message_id});
                 connection.send(Message::Text(entities_msg.to_string().into())).await?;
-                message_id += 1;
+                message_id += 1;*/
             }
 
             if parsed["type"] == "result" {