From 63837e707901a420d554e179c512839af2be47dc Mon Sep 17 00:00:00 2001 From: Pellaeon Lin Date: Sun, 24 Jun 2018 15:08:44 +0800 Subject: [PATCH] Update# refactor RegistrationService::validateEmail and registercontroller --- controller/apicontroller.php | 2 +- controller/registercontroller.php | 19 ++++++++++++++----- service/registrationservice.php | 8 ++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/controller/apicontroller.php b/controller/apicontroller.php index 07eb43b..c11732d 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -148,7 +148,7 @@ class ApiController extends OCSController { try { $secret = null; $registration = $this->registrationService->validateEmail($email); - if($registration === null) { + if($registration === true) { $this->registrationService->validateDisplayname($displayname); $this->registrationService->validateUsername($username); $registration = $this->registrationService->createRegistration($email, $username, $password, $displayname); diff --git a/controller/registercontroller.php b/controller/registercontroller.php index 9c914f2..c2fdeec 100644 --- a/controller/registercontroller.php +++ b/controller/registercontroller.php @@ -67,21 +67,30 @@ class RegisterController extends Controller { } /** + * User POST email, if email is valid and not duplicate, we send token by mail * @PublicPage * * @param string $email * @return TemplateResponse */ - public function validateEmail($email) { - if (!$this->registrationService->checkAllowedDomains($email)) { + public function validateEmail($email) {//TODO rename to receiveUserEmail + if (!$this->registrationService->checkAllowedDomains($email)) {//TODO Duplicate code with Service return new TemplateResponse('registration', 'domains', [ 'domains' => $this->registrationService->getAllowedDomains() ], 'guest'); } try { - $this->registrationService->validateEmail($email); - $registration = $this->registrationService->createRegistration($email); - $this->mailService->sendTokenByMail($registration); + $reg = $this->registrationService->validateEmail($email); + if ( $reg === true ) { + $registration = $this->registrationService->createRegistration($email); + $this->mailService->sendTokenByMail($registration); + } else { + $this->registrationService->generateNewToken($reg); + $this->mailService->sendTokenByMail($reg); + return new TemplateResponse('registration', 'message', array('msg' => + $this->l10n->t('There is already a pending registration with this email, a new verification email has been sent to the address.') + ), 'guest'); + } } catch (RegistrationException $e) { return $this->renderError($e->getMessage(), $e->getHint()); } diff --git a/service/registrationservice.php b/service/registrationservice.php index ea8f7cd..7715dd2 100644 --- a/service/registrationservice.php +++ b/service/registrationservice.php @@ -147,7 +147,7 @@ class RegistrationService { /** * @param string $email - * @return Registration + * @return Registration|true if there is a pending reg with this email, return the pending reg, if there are no problems with the email, return true. * @throws RegistrationException */ public function validateEmail($email) { @@ -156,8 +156,8 @@ class RegistrationService { // check for pending registrations try { - return $this->registrationMapper->find($email); - } catch (\Exception $e) {} + return $this->registrationMapper->find($email);//if not found DB will throw a exception + } catch (DoesNotExistException $e) {} if ( $this->userManager->getByEmail($email) ) { throw new RegistrationException( @@ -174,7 +174,7 @@ class RegistrationService { ) ); } - return null; + return true; } /**