Can delete account

This commit is contained in:
Pierre HUBERT 2023-06-06 10:19:54 +02:00
parent 85e5be0328
commit 2f3f61b159
3 changed files with 29 additions and 4 deletions

View File

@ -115,7 +115,7 @@ pub async fn request_delete_account(remote_ip: RemoteIP, token: LoginToken) -> H
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct DeleteTokenBody { pub struct DeleteAccountTokenBody {
token: String, token: String,
} }
@ -125,8 +125,17 @@ struct CheckDeleteTokenResponse {
} }
/// Check delete account token /// Check delete account token
pub async fn check_delete_token(req: Json<DeleteTokenBody>) -> HttpResult { pub async fn check_delete_token(req: Json<DeleteAccountTokenBody>) -> HttpResult {
let token = users_service::get_by_account_delete_token(&req.token).await?; let user = users_service::get_by_account_delete_token(&req.token).await?;
Ok(HttpResponse::Ok().json(CheckDeleteTokenResponse { email: token.email })) Ok(HttpResponse::Ok().json(CheckDeleteTokenResponse { email: user.email }))
}
/// Delete account
pub async fn delete_account(req: Json<DeleteAccountTokenBody>) -> HttpResult {
let user = users_service::get_by_account_delete_token(&req.token).await?;
users_service::delete_account(&user).await?;
Ok(HttpResponse::Accepted().finish())
} }

View File

@ -70,6 +70,10 @@ async fn main() -> std::io::Result<()> {
"/user/check_delete_token", "/user/check_delete_token",
web::post().to(user_controller::check_delete_token), web::post().to(user_controller::check_delete_token),
) )
.route(
"/user/delete_account",
web::post().to(user_controller::delete_account),
)
}) })
.bind(AppConfig::get().listen_address.as_str())? .bind(AppConfig::get().listen_address.as_str())?
.run() .run()

View File

@ -171,6 +171,18 @@ pub async fn delete_not_validated_accounts() -> anyhow::Result<()> {
}) })
} }
/// Delete account
pub async fn delete_account(user: &User) -> anyhow::Result<()> {
log::info!("Delete account #{:?}", user.id());
// TODO : remove families memberships
db_connection::execute(|conn| {
diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id))).execute(conn)?;
Ok(())
})
}
/// Mark account as validated /// Mark account as validated
pub async fn validate_account(user: &mut User) -> anyhow::Result<()> { pub async fn validate_account(user: &mut User) -> anyhow::Result<()> {
if user.time_activate > 0 { if user.time_activate > 0 {