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>, payload: web::Json<Register>,
data: web::Data<Data>, data: web::Data<Data>,
) -> ServiceResult<impl Responder> { ) -> ServiceResult<impl Responder> {
if !crate::SETTINGS.server.allow_registration {
Err(ServiceError::ClosedForRegistration)?
}
let username = data.creds.username(&payload.username)?; let username = data.creds.username(&payload.username)?;
let hash = data.creds.password(&payload.password)?; let hash = data.creds.password(&payload.password)?;
data.creds.email(Some(&payload.email))?; data.creds.email(Some(&payload.email))?;

View File

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