conditional registration blocking

This commit is contained in:
realaravinth 2021-04-11 13:00:34 +05:30
parent 16dd4125c3
commit d1aea6c456
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 9 additions and 21 deletions

View file

@ -48,6 +48,9 @@ pub async fn signup(
payload: web::Json<Register>,
data: web::Data<Data>,
) -> ServiceResult<impl Responder> {
if !crate::SETTINGS.server.allow_registration {
Err(ServiceError::ClosedForRegistration)?
}
let username = data.creds.username(&payload.username)?;
let hash = data.creds.password(&payload.password)?;
data.creds.email(Some(&payload.email))?;

View file

@ -18,7 +18,6 @@
use std::convert::From;
use actix_web::{
client::SendRequestError,
dev::HttpResponseBuilder,
error::ResponseError,
http::{header, StatusCode},
@ -39,6 +38,11 @@ pub enum ServiceError {
#[display(fmt = "internal server error")]
InternalServerError,
#[display(
fmt = "This server is is closed for registration. Contact admin if this is unexpecter"
)]
ClosedForRegistration,
#[display(fmt = "The value you entered for email is not an email")] //405j
NotAnEmail,
#[display(fmt = "The value you entered for URL is not a URL")] //405j
@ -74,17 +78,12 @@ pub enum ServiceError {
#[display(fmt = "Username not available")]
UsernameTaken,
/// when the a token name is already taken
#[display(fmt = "token name not available")]
TokenNameTaken,
/// token not found
#[display(fmt = "Token not found. Is token registered?")]
TokenNotFound,
#[display(fmt = "{}", _0)]
CaptchaError(CaptchaError),
#[display(fmt = "Couldn't reach your server. If Problem presists, contact support")]
ClientServerUnreachable,
}
#[derive(Serialize, Deserialize)]
@ -107,6 +106,7 @@ impl ResponseError for ServiceError {
#[cfg(not(tarpaulin_include))]
fn status_code(&self) -> StatusCode {
match self {
ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN,
ServiceError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::NotAnEmail => StatusCode::BAD_REQUEST,
ServiceError::NotAUrl => StatusCode::BAD_REQUEST,
@ -123,9 +123,7 @@ impl ResponseError for ServiceError {
ServiceError::UsernameTaken => StatusCode::BAD_REQUEST,
ServiceError::TokenNameTaken => StatusCode::BAD_REQUEST,
ServiceError::TokenNotFound => StatusCode::NOT_FOUND,
ServiceError::ClientServerUnreachable => StatusCode::SERVICE_UNAVAILABLE,
ServiceError::CaptchaError(e) => match e {
CaptchaError::MailboxError => StatusCode::INTERNAL_SERVER_ERROR,
_ => StatusCode::BAD_REQUEST,
@ -156,19 +154,6 @@ impl From<ValidationErrors> for ServiceError {
}
}
impl From<SendRequestError> for ServiceError {
fn from(e: SendRequestError) -> ServiceError {
debug!("{:?}", &e);
match e {
SendRequestError::Url(_) => ServiceError::NotAUrl,
SendRequestError::Send(_) => ServiceError::InternalServerError,
SendRequestError::Response(_) => ServiceError::InternalServerError,
SendRequestError::Body(_) => ServiceError::InternalServerError,
_ => ServiceError::ClientServerUnreachable,
}
}
}
impl From<ParseError> for ServiceError {
fn from(_: ParseError) -> ServiceError {
ServiceError::NotAUrl