Automatically remove outdated account creation requests

This commit is contained in:
2023-05-31 11:06:58 +02:00
parent 62a52b385e
commit 7f8e41b618
4 changed files with 26 additions and 3 deletions

View File

@ -8,7 +8,7 @@ pub async fn send_mail<D: Display>(to: &str, subject: &str, body: D) -> anyhow::
let conf = AppConfig::get();
let email = Message::builder()
.from(conf.mail_sender.parse()?)
.from(format!("GeneIT <{}>", conf.mail_sender).parse()?)
.to(to.parse()?)
.subject(subject)
.header(ContentType::TEXT_PLAIN)

View File

@ -2,6 +2,7 @@
use crate::app_config::AppConfig;
use crate::connections::db_connection;
use crate::constants::PASSWORD_RESET_TOKEN_DURATION;
use crate::models::{NewUser, User, UserID};
use crate::schema::users;
use crate::services::mail_service;
@ -78,3 +79,19 @@ pub async fn request_reset_password(user: &mut User) -> anyhow::Result<()> {
Ok(())
}
/// 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(())
})
}