From fffdb77ff6a76a36e3ec52e7651b618cbe8e0c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 8 Jul 2017 14:21:19 +0200 Subject: [PATCH] Cleanup status codes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- appinfo/database.xml | 2 - controller/apicontroller.php | 63 +++++++++++++++++++------------ db/registration.php | 3 -- service/mailservice.php | 1 - service/registrationexception.php | 5 ++- service/registrationservice.php | 2 +- 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 572c9aa..ff4d751 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -42,12 +42,10 @@ token text true - true client_secret text - true requested diff --git a/controller/apicontroller.php b/controller/apicontroller.php index 4ddba0c..a00a85b 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -49,8 +49,9 @@ class ApiController extends OCSController { /** @var Defaults */ private $defaults; - const OCS_STATUS_PENDING = 403; - const OCS_STATUS_RESENT = 403; + const REGISTRATION_STATUS_COMPLETE = 0; + const REGISTRATION_STATUS_PENDING = 1; + const REGISTRATION_STATUS_EXISTING = 2; public function __construct($appName, IRequest $request, @@ -103,33 +104,39 @@ class ApiController extends OCSController { try { /** @var Registration $registration */ $registration = $this->registrationService->getRegistrationForSecret($clientSecret); - if(!$registration->getEmailConfirmed()) { - throw new OCSException( - $this->l10n->t('Your registration is pending. Please confirm your email address.'), - self::OCS_STATUS_PENDING - ); - } else { - // create account if email confirmed and not already created - $user = $this->registrationService->getUserAccount($registration); - if($user === null) { - $user = $this->registrationService->createAccount($registration); - } - $this->registrationService->loginUser($user->getUID(), $registration->getUsername(), $registration->getPassword(), true); - $appPassword = $this->registrationService->generateAppPassword($user->getUID()); - $data = [ - 'appPassword' => $appPassword, - 'cloudUrl' => $this->defaults->getBaseUrl() - ]; - $this->registrationService->deleteRegistration($registration); - return new DataResponse($data, Http::STATUS_OK); - } } catch (DoesNotExistException $e) { throw new OCSNotFoundException('No pending registration.'); } + + if (!$registration->getEmailConfirmed()) { + return new DataResponse( + [ + 'registrationStatus' => self::REGISTRATION_STATUS_PENDING, + 'message' => $this->l10n->t('Your registration is pending. Please confirm your email address.') + ], + Http::STATUS_OK + ); + } else { + // create account if email confirmed and not already created + $user = $this->registrationService->getUserAccount($registration); + if ($user === null) { + $user = $this->registrationService->createAccount($registration); + } + $this->registrationService->loginUser($user->getUID(), $registration->getUsername(), $registration->getPassword(), true); + $appPassword = $this->registrationService->generateAppPassword($user->getUID()); + $data = [ + 'appPassword' => $appPassword, + 'cloudUrl' => $this->defaults->getBaseUrl(), + 'registrationStatus' => self::REGISTRATION_STATUS_COMPLETE + ]; + $this->registrationService->deleteRegistration($registration); + return new DataResponse($data, Http::STATUS_OK); + } } /** * @PublicPage + * @AnonRateThrottle(limit=5, period=1) * * @param string $username * @param string $displayname @@ -152,17 +159,23 @@ class ApiController extends OCSController { } else { $this->registrationService->generateNewToken($registration); $this->mailService->sendTokenByMail($registration); - throw new OCSException($this->l10n->t('There is already a pending registration with this email, a new verification email has been sent to the address.'), self::OCS_STATUS_RESENT); + return new DataResponse( + [ + 'registrationStatus' => self::REGISTRATION_STATUS_EXISTING, + 'message' => $this->l10n->t('There is already a pending registration with this email, a new verification email has been sent to the address.') + ], + Http::STATUS_OK + ); } $data['message'] = $this->l10n->t('Your registration is pending. Please confirm your email address.'); - $data['status'] = Registration::STATUS_PENDING; + $data['registrationStatus'] = self::REGISTRATION_STATUS_PENDING; if($secret !== null) { $data['secret'] = $secret; } return new DataResponse($data, Http::STATUS_OK); } catch (RegistrationException $exception) { - throw new OCSException($exception->getMessage()); + throw new OCSException($exception->getMessage(), $exception->getCode()); } } diff --git a/db/registration.php b/db/registration.php index 475a600..9d8473f 100644 --- a/db/registration.php +++ b/db/registration.php @@ -27,9 +27,6 @@ use OCP\AppFramework\Db\Entity; class Registration extends Entity { - const STATUS_FINISHED = 0; - const STATUS_PENDING = 1; - public $id; protected $email; protected $username; diff --git a/service/mailservice.php b/service/mailservice.php index 7122b21..bed408c 100644 --- a/service/mailservice.php +++ b/service/mailservice.php @@ -79,7 +79,6 @@ class MailService { * @throws RegistrationException */ public function sendTokenByMail(Registration $registration) { - return true; $link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', array('token' => $registration->getToken())); $link = $this->urlGenerator->getAbsoluteURL($link); $template_var = [ diff --git a/service/registrationexception.php b/service/registrationexception.php index 1035811..b3070d1 100644 --- a/service/registrationexception.php +++ b/service/registrationexception.php @@ -32,9 +32,10 @@ class RegistrationException extends \Exception { * * @param string $message * @param string $hint + * @param int $code */ - public function __construct($message, $hint = "") { - parent::__construct($message); + public function __construct($message, $hint = "", $code = 400) { + parent::__construct($message, $code); $this->hint = $hint; } diff --git a/service/registrationservice.php b/service/registrationservice.php index 4131f7e..9da62bb 100644 --- a/service/registrationservice.php +++ b/service/registrationservice.php @@ -241,7 +241,7 @@ class RegistrationService { try { return $this->registrationMapper->findByToken($token); } catch (DoesNotExistException $exception) { - throw new RegistrationException($this->l10n->t('Invalid verification URL. No registration request with this verification URL is found.')); + throw new RegistrationException($this->l10n->t('Invalid verification URL. No registration request with this verification URL is found.', 404)); } }