username: type to parse username

This commit is contained in:
Philippe Loctaux 2023-05-04 22:47:11 +02:00
parent 0e77f7be5e
commit bdd5eca9f1
7 changed files with 119 additions and 24 deletions

View file

@ -63,8 +63,20 @@ pub async fn user_settings_personal_form(
}
// Update username
if user.username() != form.username {
if let Err(e) = user.set_username(&mut transaction, form.username).await {
if user.username().0 != form.username {
// Parse username
let username = match Username::from_str(form.username) {
Ok(username) => username,
Err(_) => {
return Ok(Flash::new(
Redirect::to(uri!(user_settings_personal)),
FlashKind::Danger,
INVALID_USERNAME_ERROR,
));
}
};
if let Err(e) = user.set_username(&mut transaction, &username).await {
return Ok(Flash::new(
Redirect::to(uri!(user_settings_personal)),
FlashKind::Danger,

View file

@ -1,6 +1,7 @@
use super::prelude::*;
use apps::App;
use hash::{Secret, SecretString};
use id::INVALID_USERNAME_ERROR;
use rocket::{get, post};
use settings::Settings;
use std::str::FromStr;
@ -38,6 +39,18 @@ async fn create_first_account(
) -> Result<Either<Redirect, Flash<Redirect>>> {
let form = form.into_inner();
// Parse username
let username = match Username::from_str(form.username) {
Ok(username) => username,
Err(_) => {
return Ok(Either::Right(Flash::new(
Redirect::to(uri!(setup)),
FlashKind::Danger,
INVALID_USERNAME_ERROR,
)));
}
};
// Parse url
let url = match Url::parse(form.url) {
Ok(url) => url,
@ -82,14 +95,7 @@ async fn create_first_account(
.await?;
// Insert user in database
User::insert(
&mut transaction,
&user_id,
true,
form.username,
Some(&password),
)
.await?;
User::insert(&mut transaction, &user_id, true, &username, Some(&password)).await?;
// Store UserID in settings
Settings::set_first_admin(&mut transaction, &user_id).await?;