Add communication with user actor

This commit is contained in:
2022-03-30 12:41:22 +02:00
parent bfe4674f88
commit 6fdac7fbb1
6 changed files with 94 additions and 10 deletions

View File

@@ -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
}
}