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;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
2023-05-30 13:12:58 +00:00
|
|
|
/// Get the information of the user
|
|
|
|
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-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(())
|
|
|
|
})
|
|
|
|
}
|