mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
Add get_user and update_user
This commit is contained in:
parent
e93c1cc438
commit
c7786cb58d
2 changed files with 97 additions and 4 deletions
|
@ -12,6 +12,7 @@ use jsonrpsee::{
|
||||||
RpcModule,
|
RpcModule,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
|
@ -38,6 +39,16 @@ impl Into<ErrorObject<'static>> for ApiError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct GetStringID {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct GetIntID {
|
||||||
|
id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
||||||
let mut module = RpcModule::new(state);
|
let mut module = RpcModule::new(state);
|
||||||
|
|
||||||
|
@ -48,6 +59,10 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
module
|
||||||
|
.register_method("system.get_user", system::get_user)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_method("system.get_users", system::get_users)
|
.register_method("system.get_users", system::get_users)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -56,6 +71,10 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
||||||
.register_method("system.create_user", system::create_user)
|
.register_method("system.create_user", system::create_user)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
module
|
||||||
|
.register_method("system.update_user", system::update_user)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
module
|
module
|
||||||
.register_method("system.delete_user", system::delete_user)
|
.register_method("system.delete_user", system::delete_user)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -3,12 +3,50 @@ use std::collections::HashMap;
|
||||||
use crate::{definitions::system::User, state::RpcState};
|
use crate::{definitions::system::User, state::RpcState};
|
||||||
use jsonrpsee::types::Params;
|
use jsonrpsee::types::Params;
|
||||||
use pwhash::sha512_crypt;
|
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<HashMap<String, User>, ApiError> {
|
#[derive(Serialize, Clone)]
|
||||||
Ok(state.config_manager.get_pending_config().system.users)
|
pub struct GetUser {
|
||||||
|
name: String,
|
||||||
|
comment: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_user(p: Params, state: &RpcState) -> Result<GetUser, ApiError> {
|
||||||
|
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<Vec<GetUser>, ApiError> {
|
||||||
|
let mut res: Vec<GetUser> = 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)]
|
#[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<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)]
|
#[derive(Deserialize)]
|
||||||
struct DeleteUser {
|
struct DeleteUser {
|
||||||
name: String,
|
name: String,
|
||||||
|
|
Loading…
Add table
Reference in a new issue