nekrochan/src/web/staff/actions/create_account.rs

50 řádky
1.3 KiB
Rust
Spustitelný soubor

use actix_web::{post, web::Data, HttpRequest, HttpResponse};
use pwhash::bcrypt::hash;
use serde::Deserialize;
use crate::{
ctx::Ctx, db::models::Account, error::NekrochanError, qsform::QsForm,
web::tcx::account_from_auth,
};
#[derive(Deserialize)]
pub struct CreateAccountForm {
username: String,
#[serde(rename = "account_password")]
password: String,
}
#[post("/staff/actions/create-account")]
pub async fn create_account(
ctx: Data<Ctx>,
req: HttpRequest,
QsForm(form): QsForm<CreateAccountForm>,
) -> Result<HttpResponse, NekrochanError> {
let account = account_from_auth(&ctx, &req).await?;
if !account.perms().owner() {
return Err(NekrochanError::InsufficientPermissionError);
}
let username = form.username.trim().to_owned();
let password = form.password.trim().to_owned();
if username.is_empty() || username.len() > 32 {
return Err(NekrochanError::UsernameFormatError);
}
if password.len() < 8 {
return Err(NekrochanError::PasswordFormatError);
}
let password = hash(password)?;
let _ = Account::create(&ctx, username, password).await?;
let res = HttpResponse::SeeOther()
.append_header(("Location", "/staff/accounts"))
.finish();
Ok(res)
}