mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 10:38:20 +00:00
Add Create User rpc method
This commit is contained in:
parent
2346e38ff4
commit
c10f5529d6
2 changed files with 41 additions and 0 deletions
|
@ -45,6 +45,10 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
|||
.register_method("system.get_users", system::get_users)
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_method("system.create_user", system::create_user)
|
||||
.unwrap();
|
||||
|
||||
module
|
||||
.register_method("network.get_static_routes", network::get_static_routes)
|
||||
.unwrap();
|
||||
|
|
|
@ -2,9 +2,46 @@ use std::collections::HashMap;
|
|||
|
||||
use crate::{definitions::system::User, state::RpcState};
|
||||
use jsonrpsee::types::Params;
|
||||
use pwhash::sha512_crypt;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::ApiError;
|
||||
|
||||
pub fn get_users(_: Params, state: &RpcState) -> Result<HashMap<String, User>, ApiError> {
|
||||
Ok(state.config_manager.get_pending_config().system.users)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CreateUser {
|
||||
name: String,
|
||||
password: String,
|
||||
comment: String,
|
||||
}
|
||||
|
||||
pub fn create_user(p: Params, state: &RpcState) -> Result<(), ApiError> {
|
||||
let u: CreateUser = p.parse().unwrap();
|
||||
|
||||
let hash = sha512_crypt::hash(u.password).unwrap();
|
||||
|
||||
let mut cm = state.config_manager.clone();
|
||||
let mut tx = cm.start_transaction();
|
||||
|
||||
if tx
|
||||
.changes
|
||||
.system
|
||||
.users
|
||||
.insert(
|
||||
u.name,
|
||||
User {
|
||||
comment: u.comment,
|
||||
hash: hash,
|
||||
},
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
return tx.commit().map_err(|source| ApiError::InvalidParams);
|
||||
} else {
|
||||
tx.revert();
|
||||
Err(ApiError::InvalidParams)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue