use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
source::{
+ captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
local_user::{LocalUser, LocalUserInsertForm},
person::{Person, PersonInsertForm},
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::{
claims::Claims,
- error::LemmyError,
+ error::{LemmyError, LemmyErrorExt, LemmyErrorType},
utils::{
slurs::{check_slurs, check_slurs_opt},
validation::is_valid_actor_name,
async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
let data: &Register = self;
- let site_view = SiteView::read_local(context.pool()).await?;
+ let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_site = site_view.local_site;
let require_registration_application =
local_site.registration_mode == RegistrationMode::RequireApplication;
if local_site.registration_mode == RegistrationMode::Closed {
- return Err(LemmyError::from_message("registration_closed"));
+ return Err(LemmyErrorType::RegistrationClosed)?;
}
password_length_check(&data.password)?;
honeypot_check(&data.honeypot)?;
if local_site.require_email_verification && data.email.is_none() {
- return Err(LemmyError::from_message("email_required"));
+ return Err(LemmyErrorType::EmailRequired)?;
}
if local_site.site_setup && require_registration_application && data.answer.is_none() {
- return Err(LemmyError::from_message(
- "registration_application_answer_required",
- ));
+ return Err(LemmyErrorType::RegistrationApplicationAnswerRequired)?;
}
// Make sure passwords match
if data.password != data.password_verify {
- return Err(LemmyError::from_message("passwords_dont_match"));
+ return Err(LemmyErrorType::PasswordsDoNotMatch)?;
+ }
+
+ if local_site.site_setup && local_site.captcha_enabled {
+ if let Some(captcha_uuid) = &data.captcha_uuid {
+ let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
+ let check = CaptchaAnswer::check_captcha(
+ &mut context.pool(),
+ CheckCaptchaAnswer {
+ uuid,
+ answer: data.captcha_answer.clone().unwrap_or_default(),
+ },
+ )
+ .await?;
+ if !check {
+ return Err(LemmyErrorType::CaptchaIncorrect)?;
+ }
+ } else {
+ return Err(LemmyErrorType::CaptchaIncorrect)?;
+ }
}
let slur_regex = local_site_to_slur_regex(&local_site);
)?;
if let Some(email) = &data.email {
- if LocalUser::is_email_taken(context.pool(), email).await? {
- return Err(LemmyError::from_message("email_already_exists"));
+ if LocalUser::is_email_taken(&mut context.pool(), email).await? {
+ return Err(LemmyErrorType::EmailAlreadyExists)?;
}
}
.build();
// insert the person
- let inserted_person = Person::create(context.pool(), &person_form)
+ let inserted_person = Person::create(&mut context.pool(), &person_form)
.await
- .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
+ .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?;
// Automatically set their application as accepted, if they created this with open registration.
// Also fixes a bug which allows users to log in when registrations are changed to closed.
.accepted_application(accepted_application)
.build();
- let inserted_local_user = LocalUser::create(context.pool(), &local_user_form).await?;
+ let inserted_local_user = LocalUser::create(&mut context.pool(), &local_user_form).await?;
if local_site.site_setup && require_registration_application {
// Create the registration application
answer: data.answer.clone().expect("must have an answer"),
};
- RegistrationApplication::create(context.pool(), &form).await?;
+ RegistrationApplication::create(&mut context.pool(), &form).await?;
}
// Email the admins
if local_site.application_email_admins {
- send_new_applicant_email_to_admins(&data.username, context.pool(), context.settings())
+ send_new_applicant_email_to_admins(&data.username, &mut context.pool(), context.settings())
.await?;
}
.clone()
.expect("email was provided");
- send_verification_email(&local_user_view, &email, context.pool(), context.settings())
- .await?;
+ send_verification_email(
+ &local_user_view,
+ &email,
+ &mut context.pool(),
+ context.settings(),
+ )
+ .await?;
login_response.verify_email_sent = true;
}