Add web site settings

This commit is contained in:
Greg Burri 2025-01-19 21:05:46 +01:00
parent 65489e7692
commit f1ea7841a2
8 changed files with 74 additions and 19 deletions

View file

@ -16,6 +16,7 @@ use tracing::{event, Level};
use crate::consts;
pub mod recipe;
pub mod settings;
pub mod user;
const CURRENT_DB_VERSION: u32 = 1;

View file

@ -0,0 +1,26 @@
use std::str::FromStr;
use super::{Connection, DBError, Result};
impl Connection {
pub async fn get_new_user_registration_enabled(&self) -> Result<bool> {
self.get("new_user_registration_enabled").await
}
async fn get<T>(&self, name: &str) -> Result<T>
where
T: FromStr,
{
let v: String = sqlx::query_scalar("SELECT [value] FROM [Settings] WHERE [name] = $1")
.bind(name)
.fetch_one(&self.pool)
.await?;
T::from_str(&v).map_err(|_| {
DBError::Other(format!(
"Can't convert string value \"{}\" when reading setting {}",
v, name
))
})
}
}

View file

@ -68,7 +68,7 @@ type Result<T> = std::result::Result<T, AppError>;
impl axum::response::IntoResponse for AppError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, "Template error").into_response()
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
}
}

View file

@ -32,20 +32,29 @@ use crate::{
#[debug_handler]
pub async fn sign_up_get(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
Extension(tr): Extension<translation::Tr>,
) -> Result<impl IntoResponse> {
Ok(Html(
SignUpFormTemplate {
user,
tr,
email: String::new(),
message: "",
message_email: "",
message_password: "",
}
.render()?,
))
) -> Result<Response> {
if connection.get_new_user_registration_enabled().await? {
Ok(Html(
SignUpFormTemplate {
user,
tr,
email: String::new(),
message: "",
message_email: "",
message_password: "",
}
.render()?,
)
.into_response())
} else {
Ok(
Html(MessageTemplate::new_with_user(tr.t(Sentence::SignUpClosed), tr, user).render()?)
.into_response(),
)
}
}
#[derive(Deserialize, Debug)]
@ -109,6 +118,13 @@ pub async fn sign_up_post(
.into_response())
}
if !connection.get_new_user_registration_enabled().await? {
return Ok(Html(
MessageTemplate::new_with_user(tr.t(Sentence::SignUpClosed), tr, user).render()?,
)
.into_response());
}
// Validation of email and password.
if form_data.email.parse::<Address>().is_err() {
return error_response(SignUpError::InvalidEmail, &form_data, user, tr);

View file

@ -41,8 +41,10 @@ pub enum Sentence {
SignUpEmailValidationSuccess,
SignUpValidationExpired,
SignUpValidationErrorTryAgain,
SignUpClosed,
ChooseAPassword,
ReEnterPassword,
AccountMustBeValidatedFirst,
InvalidEmail,
PasswordDontMatch,