diff options
author | dartcafe <github@dartcafe.de> | 2022-03-23 01:00:44 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2022-03-23 01:00:44 +0300 |
commit | ef7425c7e96a7fd1368bd479f90fe95010a08658 (patch) | |
tree | c04d26bed6a037fceae7ce2328733de52575bfe5 /lib | |
parent | eff300b9dc9838696b2128ff7224e5390e5c715c (diff) |
use trait
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/PublicController.php | 17 | ||||
-rw-r--r-- | lib/Service/SystemService.php | 8 |
2 files changed, 13 insertions, 12 deletions
diff --git a/lib/Controller/PublicController.php b/lib/Controller/PublicController.php index 29b355c4..5fd0d9f0 100644 --- a/lib/Controller/PublicController.php +++ b/lib/Controller/PublicController.php @@ -27,7 +27,6 @@ use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\Template\PublicTemplateResponse; @@ -335,11 +334,9 @@ class PublicController extends Controller { * @PublicPage */ public function validatePublicUsername(string $userName, string $token): DataResponse { - try { - return new DataResponse(['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName], Http::STATUS_OK); - } catch (\Exception $e) { - return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT); - } + return $this->response(function () use ($userName, $token) { + return ['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName]; + }); } /** @@ -348,11 +345,9 @@ class PublicController extends Controller { * @PublicPage */ public function validateEmailAddress(string $emailAddress): DataResponse { - try { - return new DataResponse(['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress], Http::STATUS_OK); - } catch (\Exception $e) { - return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT); - } + return $this->response(function () use ($emailAddress) { + return ['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress]; + }); } /** diff --git a/lib/Service/SystemService.php b/lib/Service/SystemService.php index f075640e..c3f5e763 100644 --- a/lib/Service/SystemService.php +++ b/lib/Service/SystemService.php @@ -26,6 +26,7 @@ namespace OCA\Polls\Service; use OCA\Polls\Exceptions\TooShortException; use OCA\Polls\Exceptions\InvalidUsernameException; use OCA\Polls\Exceptions\InvalidEmailAddress; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Helper\Container; use OCA\Polls\Db\ShareMapper; @@ -135,7 +136,12 @@ class SystemService { * @return true */ public function validatePublicUsername(string $userName, string $token): bool { - $share = $this->shareMapper->findByToken($token); + try { + $share = $this->shareMapper->findByToken($token); + } catch (\Exception $e) { + throw new NotAuthorizedException('Token invalid'); + } + if (!$userName) { throw new TooShortException('Username must not be empty'); |