58 lines
1.5 KiB
Rust
58 lines
1.5 KiB
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use maud::html;
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::utils::{password_change::PasswordChangeBuilder, ApplicationError, HtmxTargetHeader};
|
|
use brass_db::{models::User, NoneToken};
|
|
|
|
#[derive(Deserialize)]
|
|
struct ChangePasswordForm {
|
|
currentpassword: String,
|
|
password: String,
|
|
passwordretyped: String,
|
|
}
|
|
|
|
#[actix_web::post("/users/changepassword")]
|
|
async fn post(
|
|
user: web::ReqData<User>,
|
|
header: web::Header<HtmxTargetHeader>,
|
|
form: web::Form<ChangePasswordForm>,
|
|
pool: web::Data<PgPool>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let is_dry = header.is_some_and_equal("password-strength");
|
|
|
|
let mut builder = PasswordChangeBuilder::<NoneToken>::new(
|
|
pool.get_ref(),
|
|
user.id,
|
|
&form.password,
|
|
&form.passwordretyped,
|
|
)
|
|
.with_current_password(&form.currentpassword);
|
|
|
|
let change = builder.build();
|
|
|
|
let response = if is_dry {
|
|
match change.validate_for_input().await {
|
|
Ok(r) => r,
|
|
Err(e) => HttpResponse::UnprocessableEntity().body(e.message),
|
|
}
|
|
} else {
|
|
if let Err(e) = change.validate().await {
|
|
return Ok(HttpResponse::UnprocessableEntity().body(e.message));
|
|
}
|
|
|
|
change.commit().await?;
|
|
HttpResponse::Ok().body(
|
|
html! {
|
|
div class="block" {
|
|
"Passwort erfolgreich geändert."
|
|
}
|
|
}
|
|
.into_string(),
|
|
)
|
|
};
|
|
|
|
Ok(response)
|
|
}
|