Can request new user password on login

This commit is contained in:
2022-04-02 08:30:01 +02:00
parent 0f4a5cde57
commit 4b8c9fdfdc
7 changed files with 222 additions and 11 deletions

View File

@@ -2,9 +2,11 @@ use crate::data::entity_manager::EntityManager;
use crate::data::service::ServiceID;
use crate::utils::err::Res;
pub type UserID = String;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct User {
pub uid: String,
pub uid: UserID,
pub first_name: String,
pub last_last: String,
pub username: String,
@@ -67,4 +69,44 @@ impl EntityManager<User> {
}
None
}
pub fn find_by_user_id(&self, id: &UserID) -> Option<User> {
for entry in self.iter() {
if entry.uid.eq(id) {
return Some(entry.clone());
}
}
None
}
/// Update user information
fn update_user<F>(&mut self, id: &UserID, update: F) -> bool where F: FnOnce(User) -> User {
let user = match self.find_by_user_id(id) {
None => return false,
Some(user) => user
};
if let Err(e) = self.replace_entries(|u| u.uid.eq(id), &update(user)) {
log::error!("Failed to update user information! {:?}", e);
return false;
}
true
}
pub fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool {
let new_hash = match hash_password(password) {
Ok(h) => { h }
Err(e) => {
log::error!("Failed to hash user password! {}", e);
return false;
}
};
self.update_user(id, |mut user| {
user.password = new_hash;
user.need_reset_password = temporary;
user
})
}
}