Update# refactor RegistrationService::validateEmail and

registercontroller
This commit is contained in:
Pellaeon Lin 2018-06-24 15:08:44 +08:00
parent 6dfe732882
commit 63837e7079
3 changed files with 19 additions and 10 deletions

View File

@ -148,7 +148,7 @@ class ApiController extends OCSController {
try { try {
$secret = null; $secret = null;
$registration = $this->registrationService->validateEmail($email); $registration = $this->registrationService->validateEmail($email);
if($registration === null) { if($registration === true) {
$this->registrationService->validateDisplayname($displayname); $this->registrationService->validateDisplayname($displayname);
$this->registrationService->validateUsername($username); $this->registrationService->validateUsername($username);
$registration = $this->registrationService->createRegistration($email, $username, $password, $displayname); $registration = $this->registrationService->createRegistration($email, $username, $password, $displayname);

View File

@ -67,21 +67,30 @@ class RegisterController extends Controller {
} }
/** /**
* User POST email, if email is valid and not duplicate, we send token by mail
* @PublicPage * @PublicPage
* *
* @param string $email * @param string $email
* @return TemplateResponse * @return TemplateResponse
*/ */
public function validateEmail($email) { public function validateEmail($email) {//TODO rename to receiveUserEmail
if (!$this->registrationService->checkAllowedDomains($email)) { if (!$this->registrationService->checkAllowedDomains($email)) {//TODO Duplicate code with Service
return new TemplateResponse('registration', 'domains', [ return new TemplateResponse('registration', 'domains', [
'domains' => $this->registrationService->getAllowedDomains() 'domains' => $this->registrationService->getAllowedDomains()
], 'guest'); ], 'guest');
} }
try { try {
$this->registrationService->validateEmail($email); $reg = $this->registrationService->validateEmail($email);
if ( $reg === true ) {
$registration = $this->registrationService->createRegistration($email); $registration = $this->registrationService->createRegistration($email);
$this->mailService->sendTokenByMail($registration); $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) { } catch (RegistrationException $e) {
return $this->renderError($e->getMessage(), $e->getHint()); return $this->renderError($e->getMessage(), $e->getHint());
} }

View File

@ -147,7 +147,7 @@ class RegistrationService {
/** /**
* @param string $email * @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 * @throws RegistrationException
*/ */
public function validateEmail($email) { public function validateEmail($email) {
@ -156,8 +156,8 @@ class RegistrationService {
// check for pending registrations // check for pending registrations
try { try {
return $this->registrationMapper->find($email); return $this->registrationMapper->find($email);//if not found DB will throw a exception
} catch (\Exception $e) {} } catch (DoesNotExistException $e) {}
if ( $this->userManager->getByEmail($email) ) { if ( $this->userManager->getByEmail($email) ) {
throw new RegistrationException( throw new RegistrationException(
@ -174,7 +174,7 @@ class RegistrationService {
) )
); );
} }
return null; return true;
} }
/** /**