Refactor users management (#7)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* Improve general settings management by admin
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
use actix::{Actor, Context, Handler, Message, MessageResult};
|
||||
use std::net::IpAddr;
|
||||
|
||||
use crate::data::user::{FactorID, GrantedClients, TwoFactor, User, UserID};
|
||||
use crate::data::user::{FactorID, GeneralSettings, GrantedClients, TwoFactor, User, UserID};
|
||||
use crate::utils::err::Res;
|
||||
|
||||
/// User storage interface
|
||||
pub trait UsersBackend {
|
||||
pub trait UsersSyncBackend {
|
||||
fn find_by_username_or_email(&self, u: &str) -> Option<User>;
|
||||
fn find_by_user_id(&self, id: &UserID) -> Option<User>;
|
||||
fn get_entire_users_list(&self) -> Vec<User>;
|
||||
fn create_user_account(&mut self, settings: GeneralSettings) -> Res<UserID>;
|
||||
fn set_general_user_settings(&mut self, settings: GeneralSettings) -> Res;
|
||||
fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool;
|
||||
fn verify_user_password(&self, user: &UserID, password: &str) -> bool;
|
||||
fn add_2fa_factor(&mut self, user: &UserID, factor: TwoFactor) -> bool;
|
||||
@ -17,9 +19,6 @@ pub trait UsersBackend {
|
||||
fn clear_2fa_login_history(&mut self, id: &UserID) -> bool;
|
||||
fn delete_account(&mut self, id: &UserID) -> bool;
|
||||
fn set_granted_2fa_clients(&mut self, id: &UserID, clients: GrantedClients) -> bool;
|
||||
|
||||
// FIXME : remove this
|
||||
fn update_or_insert_user(&mut self, user: User) -> Res;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -62,6 +61,10 @@ pub struct GetAllUsersRequest;
|
||||
#[derive(Debug)]
|
||||
pub struct GetAllUsersResult(pub Vec<User>);
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Option<UserID>")]
|
||||
pub struct CreateAccount(pub GeneralSettings);
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "bool")]
|
||||
pub struct ChangePasswordRequest {
|
||||
@ -92,20 +95,20 @@ pub struct SetGrantedClients(pub UserID, pub GrantedClients);
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "bool")]
|
||||
pub struct UpdateUserRequest(pub User);
|
||||
pub struct UpdateUserSettings(pub GeneralSettings);
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "bool")]
|
||||
pub struct DeleteUserRequest(pub UserID);
|
||||
|
||||
pub struct UsersActor {
|
||||
manager: Box<dyn UsersBackend>,
|
||||
manager: Box<dyn UsersSyncBackend>,
|
||||
}
|
||||
|
||||
impl UsersActor {
|
||||
pub fn new<E>(manager: E) -> Self
|
||||
where
|
||||
E: UsersBackend + 'static,
|
||||
E: UsersSyncBackend + 'static,
|
||||
{
|
||||
Self {
|
||||
manager: Box::new(manager),
|
||||
@ -138,6 +141,20 @@ impl Handler<LoginRequest> for UsersActor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<CreateAccount> for UsersActor {
|
||||
type Result = <CreateAccount as actix::Message>::Result;
|
||||
|
||||
fn handle(&mut self, msg: CreateAccount, _ctx: &mut Self::Context) -> Self::Result {
|
||||
match self.manager.create_user_account(msg.0) {
|
||||
Ok(id) => Some(id),
|
||||
Err(e) => {
|
||||
log::error!("Failed to create user account! {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<ChangePasswordRequest> for UsersActor {
|
||||
type Result = <ChangePasswordRequest as actix::Message>::Result;
|
||||
|
||||
@ -220,14 +237,14 @@ impl Handler<GetAllUsersRequest> for UsersActor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<UpdateUserRequest> for UsersActor {
|
||||
type Result = <UpdateUserRequest as actix::Message>::Result;
|
||||
impl Handler<UpdateUserSettings> for UsersActor {
|
||||
type Result = <UpdateUserSettings as actix::Message>::Result;
|
||||
|
||||
fn handle(&mut self, msg: UpdateUserRequest, _ctx: &mut Self::Context) -> Self::Result {
|
||||
match self.manager.update_or_insert_user(msg.0) {
|
||||
fn handle(&mut self, msg: UpdateUserSettings, _ctx: &mut Self::Context) -> Self::Result {
|
||||
match self.manager.set_general_user_settings(msg.0) {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
log::error!("Failed to update user information! {:?}", e);
|
||||
log::error!("Failed to update general user information! {:?}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user