This commit is contained in:
JMARyA 2024-08-08 14:28:23 +02:00
parent 4193b37b6b
commit 95c5592170
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 54 additions and 1 deletions

View file

@ -49,3 +49,37 @@ pub async fn login_route(login: Json<LoginData>) -> FallibleApiResponse {
"token": ses.token
}))
}
#[derive(Deserialize)]
pub struct PasswdData {
pub old: String,
pub new: String,
}
#[post("/passwd", data = "<passwd>")]
pub async fn passwd_route(passwd: Json<PasswdData>, mut u: User) -> FallibleApiResponse {
u.passwd(&passwd.old, &passwd.new)
.await
.map_err(|_| api_error("Password change failed"))?;
Ok(json!({
"ok": 1
}))
}
#[post("/userCreate", data = "<user>")]
pub async fn user_create_route(user: Json<LoginData>, u: User) -> FallibleApiResponse {
if !u.is_admin() {
return Err(api_error("Forbidden"));
}
let new_user = User::create(
&user.username,
&user.password,
crate::library::user::UserRole::Regular,
)
.await
.unwrap();
Ok(json!({"created": new_user._id}))
}