Cleanup status codes

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2017-07-08 14:21:19 +02:00
parent 74b5428193
commit fffdb77ff6
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
6 changed files with 42 additions and 34 deletions

View File

@ -42,12 +42,10 @@
<name>token</name> <name>token</name>
<type>text</type> <type>text</type>
<notnull>true</notnull> <notnull>true</notnull>
<unique>true</unique>
</field> </field>
<field> <field>
<name>client_secret</name> <name>client_secret</name>
<type>text</type> <type>text</type>
<unique>true</unique>
</field> </field>
<field> <field>
<name>requested</name> <name>requested</name>

View File

@ -49,8 +49,9 @@ class ApiController extends OCSController {
/** @var Defaults */ /** @var Defaults */
private $defaults; private $defaults;
const OCS_STATUS_PENDING = 403; const REGISTRATION_STATUS_COMPLETE = 0;
const OCS_STATUS_RESENT = 403; const REGISTRATION_STATUS_PENDING = 1;
const REGISTRATION_STATUS_EXISTING = 2;
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
@ -103,33 +104,39 @@ class ApiController extends OCSController {
try { try {
/** @var Registration $registration */ /** @var Registration $registration */
$registration = $this->registrationService->getRegistrationForSecret($clientSecret); $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) { } catch (DoesNotExistException $e) {
throw new OCSNotFoundException('No pending registration.'); 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 * @PublicPage
* @AnonRateThrottle(limit=5, period=1)
* *
* @param string $username * @param string $username
* @param string $displayname * @param string $displayname
@ -152,17 +159,23 @@ class ApiController extends OCSController {
} else { } else {
$this->registrationService->generateNewToken($registration); $this->registrationService->generateNewToken($registration);
$this->mailService->sendTokenByMail($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['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) { if($secret !== null) {
$data['secret'] = $secret; $data['secret'] = $secret;
} }
return new DataResponse($data, Http::STATUS_OK); return new DataResponse($data, Http::STATUS_OK);
} catch (RegistrationException $exception) { } catch (RegistrationException $exception) {
throw new OCSException($exception->getMessage()); throw new OCSException($exception->getMessage(), $exception->getCode());
} }
} }

View File

@ -27,9 +27,6 @@ use OCP\AppFramework\Db\Entity;
class Registration extends Entity { class Registration extends Entity {
const STATUS_FINISHED = 0;
const STATUS_PENDING = 1;
public $id; public $id;
protected $email; protected $email;
protected $username; protected $username;

View File

@ -79,7 +79,6 @@ class MailService {
* @throws RegistrationException * @throws RegistrationException
*/ */
public function sendTokenByMail(Registration $registration) { public function sendTokenByMail(Registration $registration) {
return true;
$link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', array('token' => $registration->getToken())); $link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', array('token' => $registration->getToken()));
$link = $this->urlGenerator->getAbsoluteURL($link); $link = $this->urlGenerator->getAbsoluteURL($link);
$template_var = [ $template_var = [

View File

@ -32,9 +32,10 @@ class RegistrationException extends \Exception {
* *
* @param string $message * @param string $message
* @param string $hint * @param string $hint
* @param int $code
*/ */
public function __construct($message, $hint = "") { public function __construct($message, $hint = "", $code = 400) {
parent::__construct($message); parent::__construct($message, $code);
$this->hint = $hint; $this->hint = $hint;
} }

View File

@ -241,7 +241,7 @@ class RegistrationService {
try { try {
return $this->registrationMapper->findByToken($token); return $this->registrationMapper->findByToken($token);
} catch (DoesNotExistException $exception) { } 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));
} }
} }