Refactor users management (#7)
All checks were successful
continuous-integration/drone/push Build is passing

* Improve general settings management by admin
This commit is contained in:
2022-11-26 16:06:16 +01:00
parent a2d731bfff
commit fc6ab00e30
6 changed files with 166 additions and 58 deletions

View File

@ -1,7 +1,7 @@
use crate::actors::users_actor::UsersBackend;
use crate::actors::users_actor::UsersSyncBackend;
use crate::data::entity_manager::EntityManager;
use crate::data::user::{FactorID, GrantedClients, TwoFactor, User, UserID};
use crate::utils::err::Res;
use crate::data::user::{FactorID, GeneralSettings, GrantedClients, TwoFactor, User, UserID};
use crate::utils::err::{new_error, Res};
use crate::utils::time::time;
use std::net::IpAddr;
@ -39,7 +39,7 @@ fn verify_password<P: AsRef<[u8]>>(pwd: P, hash: &str) -> bool {
}
}
impl UsersBackend for EntityManager<User> {
impl UsersSyncBackend for EntityManager<User> {
fn find_by_username_or_email(&self, u: &str) -> Option<User> {
for entry in self.iter() {
if entry.username.eq(u) || entry.email.eq(u) {
@ -62,6 +62,28 @@ impl UsersBackend for EntityManager<User> {
self.cloned()
}
fn create_user_account(&mut self, settings: GeneralSettings) -> Res<UserID> {
let mut user = User {
uid: UserID(uuid::Uuid::new_v4().to_string()),
..Default::default()
};
user.update_general_settings(settings);
self.insert(user.clone())?;
Ok(user.uid)
}
fn set_general_user_settings(&mut self, settings: GeneralSettings) -> Res {
let res = self.update_user(&settings.uid.clone(), |mut user| {
user.update_general_settings(settings);
user
});
match res {
true => Ok(()),
false => new_error("Failed to update user general settings!".to_string()),
}
}
fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool {
let new_hash = match hash_password(password) {
Ok(h) => h,
@ -144,8 +166,4 @@ impl UsersBackend for EntityManager<User> {
user
})
}
fn update_or_insert_user(&mut self, user: User) -> Res {
self.update_or_push(user)
}
}