2023-05-24 14:19:46 +00:00
|
|
|
//! # Users service
|
|
|
|
|
2023-05-30 13:12:58 +00:00
|
|
|
use crate::app_config::AppConfig;
|
2023-05-26 15:55:19 +00:00
|
|
|
use crate::connections::db_connection;
|
2023-05-31 09:06:58 +00:00
|
|
|
use crate::constants::PASSWORD_RESET_TOKEN_DURATION;
|
2023-05-30 13:12:58 +00:00
|
|
|
use crate::models::{NewUser, User, UserID};
|
2023-05-24 14:19:46 +00:00
|
|
|
use crate::schema::users;
|
2023-05-30 13:12:58 +00:00
|
|
|
use crate::services::mail_service;
|
|
|
|
use crate::utils::string_utils::rand_str;
|
2023-05-24 14:19:46 +00:00
|
|
|
use crate::utils::time_utils::time;
|
2023-05-31 11:33:26 +00:00
|
|
|
use bcrypt::DEFAULT_COST;
|
2023-05-24 14:19:46 +00:00
|
|
|
use diesel::prelude::*;
|
2023-05-31 11:33:26 +00:00
|
|
|
use std::io::ErrorKind;
|
2023-05-24 14:19:46 +00:00
|
|
|
|
2023-05-31 11:56:18 +00:00
|
|
|
/// Get the information of a user, by its id
|
2023-05-30 13:12:58 +00:00
|
|
|
pub async fn get_by_id(id: UserID) -> anyhow::Result<User> {
|
|
|
|
db_connection::execute(|conn| Ok(users::table.filter(users::dsl::id.eq(id.0)).first(conn)?))
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:56:18 +00:00
|
|
|
/// Get the information of a user, by its mail
|
|
|
|
pub async fn get_by_mail(mail: &str) -> anyhow::Result<User> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
Ok(users::table
|
|
|
|
.filter(users::dsl::email.eq(mail))
|
|
|
|
.first(conn)?)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the information of a user, by its password reset token
|
2023-05-31 09:36:14 +00:00
|
|
|
pub async fn get_by_pwd_reset_token(token: &str) -> anyhow::Result<User> {
|
2023-05-31 11:33:26 +00:00
|
|
|
if token.is_empty() {
|
|
|
|
return Err(anyhow::Error::from(std::io::Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"Token is empty!",
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2023-05-31 09:36:14 +00:00
|
|
|
db_connection::execute(|conn| {
|
|
|
|
Ok(users::table
|
|
|
|
.filter(
|
|
|
|
users::dsl::reset_password_token.eq(token.to_string()).and(
|
|
|
|
users::dsl::time_gen_reset_token
|
|
|
|
.ge(time() as i64 - PASSWORD_RESET_TOKEN_DURATION.as_secs() as i64),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.first(conn)?)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:19:46 +00:00
|
|
|
/// Create a new account
|
|
|
|
pub async fn create_account(name: &str, email: &str) -> anyhow::Result<User> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
let res = diesel::insert_into(users::table)
|
|
|
|
.values(&NewUser {
|
|
|
|
name: name.trim(),
|
|
|
|
email: email.trim(),
|
|
|
|
time_create: time() as i64,
|
|
|
|
})
|
|
|
|
.get_result(conn)?;
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
2023-05-25 07:42:43 +00:00
|
|
|
|
|
|
|
/// Check if an email address is already associated with an account
|
|
|
|
pub async fn exists_email(email: &str) -> anyhow::Result<bool> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
let count: i64 = users::table
|
|
|
|
.filter(users::email.eq(email))
|
|
|
|
.count()
|
|
|
|
.get_result(conn)?;
|
|
|
|
|
|
|
|
Ok(count != 0)
|
|
|
|
})
|
|
|
|
}
|
2023-05-30 13:12:58 +00:00
|
|
|
|
|
|
|
/// Request password reset
|
|
|
|
pub async fn request_reset_password(user: &mut User) -> anyhow::Result<()> {
|
|
|
|
// If required, regenerate reset token
|
|
|
|
if user.reset_password_token.is_none() || user.time_gen_reset_token as u64 + 3600 * 2 < time() {
|
|
|
|
user.reset_password_token = Some(rand_str(149));
|
|
|
|
user.time_gen_reset_token = time() as i64;
|
|
|
|
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
Ok(
|
|
|
|
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
|
|
|
|
.set((
|
|
|
|
users::dsl::time_gen_reset_token.eq(user.time_gen_reset_token),
|
|
|
|
users::dsl::reset_password_token.eq(user.reset_password_token.clone()),
|
|
|
|
))
|
|
|
|
.execute(conn)?,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send mail
|
|
|
|
mail_service::send_mail(
|
|
|
|
&user.email,
|
|
|
|
"Réinitialisation de votre mot de passe",
|
|
|
|
format!(
|
|
|
|
"Bonjour, \n\n\
|
|
|
|
Vous pouvez réinitialiser le mot de passe de votre compte à l'adresse suivante : {} \n\n\
|
|
|
|
Ce lien est valide durant 24 heures.\n\n\
|
|
|
|
Cordialement,\n\n\
|
|
|
|
L'équipe de GeneIT",
|
|
|
|
AppConfig::get()
|
|
|
|
.get_password_reset_url(user.reset_password_token.as_deref().unwrap_or(""))
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-31 09:06:58 +00:00
|
|
|
|
|
|
|
/// Delete not validated accounts whose reset token has expired
|
|
|
|
pub async fn delete_not_validated_accounts() -> anyhow::Result<()> {
|
|
|
|
db_connection::execute(|conn| {
|
|
|
|
diesel::delete(
|
|
|
|
users::dsl::users.filter(
|
|
|
|
users::dsl::time_activate.eq(0).and(
|
|
|
|
users::dsl::time_gen_reset_token
|
|
|
|
.lt(time() as i64 - PASSWORD_RESET_TOKEN_DURATION.as_secs() as i64),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.execute(conn)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
2023-05-31 11:33:26 +00:00
|
|
|
|
|
|
|
/// 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(())
|
|
|
|
}
|