Add communication with user actor
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
use std::path::{Path, PathBuf};
 | 
			
		||||
use std::slice::Iter;
 | 
			
		||||
 | 
			
		||||
use crate::utils::err::Res;
 | 
			
		||||
 | 
			
		||||
@@ -44,4 +45,9 @@ impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned
 | 
			
		||||
        self.list.push(el);
 | 
			
		||||
        self.save()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Iterate over the entries of this entity manager
 | 
			
		||||
    pub fn iter(&self) -> Iter<'_, E> {
 | 
			
		||||
        self.list.iter()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
use crate::data::entity_manager::EntityManager;
 | 
			
		||||
use crate::data::service::ServiceID;
 | 
			
		||||
use crate::utils::err::Res;
 | 
			
		||||
 | 
			
		||||
@@ -38,11 +39,32 @@ impl Default for User {
 | 
			
		||||
            need_reset_password: false,
 | 
			
		||||
            enabled: true,
 | 
			
		||||
            admin: false,
 | 
			
		||||
            authorized_services: None
 | 
			
		||||
            authorized_services: None,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn hash_password<P: AsRef<[u8]>>(pwd: P) -> Res<String> {
 | 
			
		||||
    Ok(bcrypt::hash(pwd, bcrypt::DEFAULT_COST)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn verify_password<P: AsRef<[u8]>>(pwd: P, hash: &str) -> bool {
 | 
			
		||||
    match bcrypt::verify(pwd, hash) {
 | 
			
		||||
        Ok(r) => r,
 | 
			
		||||
        Err(e) => {
 | 
			
		||||
            log::warn!("Failed to verify password! {:?}", e);
 | 
			
		||||
            false
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl EntityManager<User> {
 | 
			
		||||
    pub 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) {
 | 
			
		||||
                return Some(entry.clone());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        None
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user