Can change password by reset
This commit is contained in:
@ -8,7 +8,9 @@ use crate::schema::users;
|
||||
use crate::services::mail_service;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time_utils::time;
|
||||
use bcrypt::DEFAULT_COST;
|
||||
use diesel::prelude::*;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
/// Get the information of the user, by its id
|
||||
pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
|
||||
@ -17,6 +19,13 @@ pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
|
||||
|
||||
/// Get the information of the user, by its password reset token
|
||||
pub async fn get_by_pwd_reset_token(token: &str) -> anyhow::Result<User> {
|
||||
if token.is_empty() {
|
||||
return Err(anyhow::Error::from(std::io::Error::new(
|
||||
ErrorKind::Other,
|
||||
"Token is empty!",
|
||||
)));
|
||||
}
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(users::table
|
||||
.filter(
|
||||
@ -109,3 +118,52 @@ pub async fn delete_not_validated_accounts() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Mark account as validated
|
||||
pub async fn validate_account(user: &User) -> anyhow::Result<()> {
|
||||
if user.time_activate > 0 {
|
||||
log::debug!(
|
||||
"Did not activate account {} because it is already activated!",
|
||||
user.id
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
mail_service::send_mail(
|
||||
&user.email,
|
||||
"Activation de votre compte GeneIT",
|
||||
"Bonjour,\n\n\
|
||||
Votre compte GeneIT a été activé avec succès !\n\n\
|
||||
Cordialement,\n\n\
|
||||
L'équipe de GeneIT",
|
||||
)
|
||||
.await?;
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(
|
||||
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
|
||||
.set((users::dsl::time_activate.eq(time() as i64),))
|
||||
.execute(conn)?,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Change user paswsord
|
||||
pub async fn change_password(user: &User, new_password: &str) -> anyhow::Result<()> {
|
||||
let hash = bcrypt::hash(new_password, DEFAULT_COST)?;
|
||||
|
||||
db_connection::execute(|conn| {
|
||||
Ok(
|
||||
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
|
||||
.set((
|
||||
users::dsl::password.eq(hash),
|
||||
users::dsl::reset_password_token.eq(None::<String>),
|
||||
))
|
||||
.execute(conn)?,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user