From c7786cb58d32ca8a7e3f40f87107b7e462988760 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Mon, 30 Oct 2023 02:02:41 +0100 Subject: [PATCH] Add get_user and update_user --- src/api/mod.rs | 19 +++++++++++ src/api/system.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 1de4c04..2abaf04 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -12,6 +12,7 @@ use jsonrpsee::{ RpcModule, }; +use serde::Deserialize; use thiserror::Error; use tracing::info; @@ -38,6 +39,16 @@ impl Into> for ApiError { } } +#[derive(Deserialize)] +struct GetStringID { + id: String, +} + +#[derive(Deserialize)] +struct GetIntID { + id: i64, +} + pub fn new_rpc_module(state: RpcState) -> RpcModule { let mut module = RpcModule::new(state); @@ -48,6 +59,10 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule { }) .unwrap(); + module + .register_method("system.get_user", system::get_user) + .unwrap(); + module .register_method("system.get_users", system::get_users) .unwrap(); @@ -56,6 +71,10 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule { .register_method("system.create_user", system::create_user) .unwrap(); + module + .register_method("system.update_user", system::update_user) + .unwrap(); + module .register_method("system.delete_user", system::delete_user) .unwrap(); diff --git a/src/api/system.rs b/src/api/system.rs index d0516d7..1366d41 100644 --- a/src/api/system.rs +++ b/src/api/system.rs @@ -3,12 +3,50 @@ use std::collections::HashMap; use crate::{definitions::system::User, state::RpcState}; use jsonrpsee::types::Params; use pwhash::sha512_crypt; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -use super::ApiError; +use super::{ApiError, GetStringID}; -pub fn get_users(_: Params, state: &RpcState) -> Result, ApiError> { - Ok(state.config_manager.get_pending_config().system.users) +#[derive(Serialize, Clone)] +pub struct GetUser { + name: String, + comment: String, +} + +pub fn get_user(p: Params, state: &RpcState) -> Result { + let u: GetStringID = p.parse().unwrap(); + + match state + .config_manager + .get_pending_config() + .system + .users + .get(&u.id) + { + Some(user) => Ok(GetUser { + name: u.id, + comment: user.comment.clone(), + }), + None => Err(ApiError::InvalidParams), + } +} + +pub fn get_users(_: Params, state: &RpcState) -> Result, ApiError> { + let mut res: Vec = Vec::new(); + for u in state + .config_manager + .get_pending_config() + .system + .users + .iter() + { + res.push(GetUser { + name: u.0.to_string(), + comment: u.1.comment.clone(), + }) + } + + Ok(res) } #[derive(Deserialize)] @@ -49,6 +87,42 @@ pub fn create_user(p: Params, state: &RpcState) -> Result<(), ApiError> { } } +#[derive(Deserialize)] +struct UpdateUser { + name: String, + password: String, + comment: Option, +} + +pub fn update_user(p: Params, state: &RpcState) -> Result<(), ApiError> { + let u: UpdateUser = p.parse().unwrap(); + + let mut cm = state.config_manager.clone(); + let mut tx = cm.start_transaction(); + + match tx.changes.system.users.get(&u.name) { + Some(user) => { + tx.changes.system.users.insert( + u.name, + User { + comment: match u.comment { + Some(c) => c, + None => "".to_string(), + }, + // Only Update Password if field is not empty + hash: if u.password == "" { + user.hash.clone() + } else { + sha512_crypt::hash(u.password).unwrap() + }, + }, + ); + Ok(()) + } + None => Err(ApiError::InvalidParams), + } +} + #[derive(Deserialize)] struct DeleteUser { name: String,