From bfaccd761379d5543f79ce2eda3018e4da1f02f6 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Fri, 12 Jun 2020 19:50:06 +0200 Subject: REST POC - initializing with comments --- lib/AppInfo/Application.php | 1 - lib/Controller/CommentApiController.php | 126 +++++++++++++++++++++ lib/Controller/CommentController.php | 181 +++-------------------------- lib/Controller/PollController.php | 20 ++-- lib/Db/ShareMapper.php | 2 +- lib/Exception/NotAuthorizedException.php | 34 ++++++ lib/Service/CommentService.php | 188 +++++++++++++++++++++++++++++++ 7 files changed, 375 insertions(+), 177 deletions(-) create mode 100644 lib/Controller/CommentApiController.php create mode 100644 lib/Exception/NotAuthorizedException.php create mode 100644 lib/Service/CommentService.php (limited to 'lib') diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 309c8f40..7a88db86 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -31,7 +31,6 @@ namespace OCA\Polls\AppInfo; // use OCA\Polls\Controller\OptionController; // use OCA\Polls\Controller\VoteController; // use OCA\Polls\Controller\ShareController; -// use OCA\Polls\Db\CommentMapper; // use OCA\Polls\Db\OptionMapper; // use OCA\Polls\Db\PollMapper; // use OCA\Polls\Db\NotificationMapper; diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php new file mode 100644 index 00000000..31d4d8bc --- /dev/null +++ b/lib/Controller/CommentApiController.php @@ -0,0 +1,126 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Controller; + +use Exception; + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\ApiController; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\CommentService; + + + +class CommentApiController extends ApiController { + + /** + * CommentApiController constructor. + * @param string $appName + * @param IRequest $request + * @param CommentService $commentService + */ + + public function __construct( + string $appName, + IRequest $request, + CommentService $commentService + ) { + parent::__construct($appName, + $request, + 'POST, GET, DELETE', + 'Authorization, Content-Type, Accept', + 1728000); + $this->commentService = $commentService; + } + + /** + * get + * Read all comments of a poll based on the poll id and return list as array + * @NoAdminRequired + * @CORS + * @PublicPage + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function get($pollId, $token = '') { + return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK); + } + + /** + * Read all comments of a poll based on a share token and return list as array + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @PublicPage + * @param string $token + * @return DataResponse + */ + public function getByToken($token) { + return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); + } + + /** + * Write a new comment to the db and returns the new comment as array + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @PublicPage + * @param int $pollId + * @param string $message + * @param string $token + * @return DataResponse + */ + public function add($message, $pollId, $token) { + try { + return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK); + } catch (Exception $e) { + return new OCSForbiddenException($e); + } + } + + /** + * Delete Comment + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @PublicPage + * @param int $commentId + * @param string $token + * @return DataResponse + */ + public function delete($commentId, $token) { + try { + return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK); + } catch (Exception $e) { + return new DataResponse($e, Http::STATUS_UNAUTHORIZED); + } + + } + +} diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 082bbbb3..17d0de94 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -24,8 +24,6 @@ namespace OCA\Polls\Controller; use Exception; -use OCP\AppFramework\Db\DoesNotExistException; - use OCP\IRequest; use OCP\ILogger; @@ -33,61 +31,28 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\IGroupManager; - -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\Comment; -use OCA\Polls\Db\CommentMapper; -use OCA\Polls\Service\AnonymizeService; -use OCA\Polls\Model\Acl; +use OCA\Polls\Service\CommentService; class CommentController extends Controller { - private $userId; - private $mapper; - private $logger; - - private $groupManager; - private $pollMapper; - private $anonymizer; - private $acl; - /** * CommentController constructor. * @param string $appName - * @param $UserId - * @param CommentMapper $mapper - * @param IGroupManager $groupManager - * @param PollMapper $pollMapper - * @param AnonymizeService $anonymizer - * @param Acl $acl + * @param IRequest $request + * @param CommentService $commentService */ public function __construct( string $appName, - $userId, IRequest $request, - ILogger $logger, - CommentMapper $mapper, - IGroupManager $groupManager, - PollMapper $pollMapper, - AnonymizeService $anonymizer, - Acl $acl + CommentService $commentService ) { parent::__construct($appName, $request); - $this->userId = $userId; - $this->mapper = $mapper; - $this->logger = $logger; - $this->groupManager = $groupManager; - $this->pollMapper = $pollMapper; - $this->anonymizer = $anonymizer; - $this->acl = $acl; + $this->commentService = $commentService; } - /** * get * Read all comments of a poll based on the poll id and return list as array @@ -97,27 +62,10 @@ class CommentController extends Controller { * @return DataResponse */ public function get($pollId) { - - try { - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - if (!$this->acl->getAllowSeeUsernames()) { - $this->anonymizer->set($pollId, $this->acl->getUserId()); - return new DataResponse((array) $this->anonymizer->getComments(), Http::STATUS_OK); - } else { - return new DataResponse((array) $this->mapper->findByPoll($pollId), Http::STATUS_OK); - } - - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - + return new DataResponse($this->commentService->get($pollId), Http::STATUS_OK); } /** - * getByToken * Read all comments of a poll based on a share token and return list as array * @NoAdminRequired * @NoCSRFRequired @@ -126,136 +74,41 @@ class CommentController extends Controller { * @return DataResponse */ public function getByToken($token) { - - try { - $this->acl->setToken($token); - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - - return $this->get($this->acl->getPollId()); - + return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); } /** * Write a new comment to the db and returns the new comment as array * @NoAdminRequired - * @NoCSRFRequired + * @PublicPage * @param int $pollId - * @param string $userId * @param string $message - * @return DataResponse - */ - public function write($pollId, $userId, $message) { - if (!\OC::$server->getUserSession()->isLoggedIn() && !$this->acl->getFoundByToken()) { - $this->logger->alert('not allowed ' . json_encode(\OC::$server->getUserSession()->isLoggedIn())); - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - if ($this->acl->getAllowComment()) { - $comment = new Comment(); - $comment->setPollId($pollId); - $comment->setUserId($userId); - $comment->setComment($message); - $comment->setDt(date('Y-m-d H:i:s')); - - - try { - $comment = $this->mapper->insert($comment); - } catch (\Exception $e) { - $this->logger->alert('conflict ' . json_encode($e)); - return new DataResponse($e, Http::STATUS_CONFLICT); - } - } else { - $this->logger->alert('unauthorized '); - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - return new DataResponse($comment, Http::STATUS_OK); - - } - - /** - * writeByToken - * @NoAdminRequired - * @PublicPage - * @NoCSRFRequired - * @param Array $option - * @param string $setTo * @param string $token * @return DataResponse */ - public function writeByToken($token, $message) { - + public function add($message, $pollId, $token) { try { - $this->acl->setToken($token); - return $this->write($this->acl->getPollId(), $this->acl->getUserId(), $message); - - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK); + } catch (Exception $e) { + return new DataResponse($e, Http::STATUS_UNAUTHORIZED); } - - } - /** - * delete * Delete Comment - * @NoCSRFRequired - * @NoAdminRequired - * @param int $pollId - * @param string $message - * @return DataResponse - */ - public function delete($comment) { - if (!\OC::$server->getUserSession()->isLoggedIn() && !$this->acl->getFoundByToken()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($comment['pollId']); - } - - try { - if ($comment['userId'] === $this->acl->getUserId()) { - $comment = $this->mapper->find($comment['id']); - $comment = $this->mapper->delete($comment); - } - } catch (\Exception $e) { - return new DataResponse($e, Http::STATUS_CONFLICT); - } - - return new DataResponse(['comment' => $comment], Http::STATUS_OK); - - } - - /** - * writeByToken * @NoAdminRequired * @PublicPage - * @NoCSRFRequired - * @param Array $option - * @param string $setTo + * @param int $commentId * @param string $token * @return DataResponse */ - public function deleteByToken($token, $comment) { - + public function delete($commentId, $token) { try { - $this->acl->setToken($token); - return $this->delete($comment); - - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK); + } catch (Exception $e) { + return new DataResponse($e, Http::STATUS_UNAUTHORIZED); } - - } } diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index b3494c4c..4542713a 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -38,8 +38,6 @@ use OCP\IUserManager; use OCP\Security\ISecureRandom; - use OCA\Polls\Db\Comment; - use OCA\Polls\Db\CommentMapper; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Option; @@ -48,15 +46,15 @@ use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; + use OCA\Polls\Service\AnonymizeService; + use OCA\Polls\Service\CommentService; use OCA\Polls\Service\LogService; use OCA\Polls\Service\MailService; - use OCA\Polls\Service\AnonymizeService; use OCA\Polls\Model\Acl; class PollController extends Controller { private $userId; - private $commentMapper; private $pollMapper; private $optionMapper; private $shareMapper; @@ -66,13 +64,14 @@ private $groupManager; private $userManager; private $poll; + private $anonymizer; private $logService; + private $commentService; private $mailService; - private $anonymizer; private $acl; /** - * CommentController constructor. + * PollController constructor. * @param string $appName * @param $userId * @param IRequest $request @@ -85,6 +84,7 @@ * @param LogService $logService * @param MailService $mailService * @param AnonymizeService $anonymizer + * @param CommentService $commentService * @param Acl $acl */ @@ -94,7 +94,6 @@ IRequest $request, ILogger $logger, IL10N $trans, - CommentMapper $commentMapper, OptionMapper $optionMapper, PollMapper $pollMapper, ShareMapper $shareMapper, @@ -104,13 +103,13 @@ IUserManager $userManager, LogService $logService, MailService $mailService, + CommentService $commentService, AnonymizeService $anonymizer, Acl $acl ) { parent::__construct($appName, $request); $this->userId = $userId; $this->trans = $trans; - $this->commentMapper = $commentMapper; $this->pollMapper = $pollMapper; $this->optionMapper = $optionMapper; $this->shareMapper = $shareMapper; @@ -121,6 +120,7 @@ $this->poll = $poll; $this->logService = $logService; $this->mailService = $mailService; + $this->commentService = $commentService; $this->anonymizer = $anonymizer; $this->acl = $acl; } @@ -189,7 +189,6 @@ } if ($this->acl->getAllowSeeUsernames()) { - $comments = $this->commentMapper->findByPoll($pollId); if ($this->acl->getAllowSeeResults()) { $votes = $this->voteMapper->findByPoll($pollId); @@ -198,14 +197,13 @@ } } else { $this->anonymizer->set($pollId, $this->acl->getUserId()); - $comments = $this->anonymizer->getComments(); $votes = $this->anonymizer->getVotes(); } return new DataResponse([ 'acl' => $this->acl, - 'comments' => $comments, + 'comments' => $this->commentService->get($pollId), 'options' => $options, 'poll' => $this->poll, 'shares' => $shares, diff --git a/lib/Db/ShareMapper.php b/lib/Db/ShareMapper.php index 7de17583..3a4408f3 100644 --- a/lib/Db/ShareMapper.php +++ b/lib/Db/ShareMapper.php @@ -31,7 +31,7 @@ use OCP\AppFramework\Db\QBMapper; class ShareMapper extends QBMapper { /** - * CommentMapper constructor. + * ShareMapper constructor. * @param IDBConnection $db */ public function __construct(IDBConnection $db) { diff --git a/lib/Exception/NotAuthorizedException.php b/lib/Exception/NotAuthorizedException.php new file mode 100644 index 00000000..fb4a89a4 --- /dev/null +++ b/lib/Exception/NotAuthorizedException.php @@ -0,0 +1,34 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls; + +class NotAuthorizedException extends \Exception { + /** + * NotAuthorizedException Constructor + * @param string $e exception message + */ + public function __construct($e = 'Unauthorized') { + parent::__construct($e); + } +} diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php new file mode 100644 index 00000000..a417ea12 --- /dev/null +++ b/lib/Service/CommentService.php @@ -0,0 +1,188 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Service; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCP\IGroupManager; + +use OCA\Polls\Db\Poll; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\Comment; +use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Service\AnonymizeService; +use OCA\Polls\Model\Acl; + + + +class CommentService { + + private $userId; + private $commentMapper; + private $logger; + + private $groupManager; + private $pollMapper; + private $anonymizer; + private $acl; + private $comment; + + /** + * CommentController constructor. + * @param string $appName + * @param $UserId + * @param CommentMapper $commentMapper + * @param IGroupManager $groupManager + * @param PollMapper $pollMapper + * @param AnonymizeService $anonymizer + * @param Acl $acl + */ + + public function __construct( + string $appName, + $userId, + IRequest $request, + ILogger $logger, + CommentMapper $commentMapper, + IGroupManager $groupManager, + PollMapper $pollMapper, + AnonymizeService $anonymizer, + Acl $acl + ) { + $this->userId = $userId; + $this->commentMapper = $commentMapper; + $this->logger = $logger; + $this->groupManager = $groupManager; + $this->pollMapper = $pollMapper; + $this->anonymizer = $anonymizer; + $this->acl = $acl; + } + + + /** + * get + * Read all comments of a poll based on the poll id and return list as array + * @NoAdminRequired + * @param integer $pollId + * @param string $token + * @return Array + */ + public function get($pollId = 0, $token = '') { + $this->logger->alert('call commentService->get(' . $pollId . ', '. $token . ')'); + + try { + if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { + $this->acl->setToken($token); + } else { + $this->acl->setPollId($pollId); + } + + if (!$this->acl->getAllowSeeUsernames()) { + $this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId()); + return $this->anonymizer->getComments(); + } else { + return $this->commentMapper->findByPoll($this->acl->getPollId()); + } + + } catch (Exception $e) { + $this->logger->alert('Error reading comments for pollId ' . $pollId . ': '. $e); + throw new DoesNotExistException($e); + } + + } + + /** + * Write a new comment to the db and returns the new comment as array + * @NoAdminRequired + * @param string $message + * @param int $pollId + * @param string $token + * @return Comment + */ + public function add($message, $pollId = 0, $token = '') { + $this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")'); + try { + if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { + $this->acl->setToken($token); + } else { + $this->acl->setPollId($pollId); + } + + if ($this->acl->getAllowComment()) { + $this->comment = new Comment(); + $this->comment->setPollId($this->acl->getPollId()); + $this->comment->setUserId($this->acl->getUserId()); + $this->comment->setComment($message); + $this->comment->setDt(date('Y-m-d H:i:s')); + $this->comment = $this->commentMapper->insert($this->comment); + return $this->comment; + } else { + throw new NotAuthorizedException; + } + + } catch (Exception $e) { + $this->logger->alert('Error wrinting comment for pollId ' . $pollId . ': '. $e); + throw new Exception($e); + } + } + + /** + * delete + * Delete Comment + * @NoAdminRequired + * @param int $commentId + * @param string $token + * @return Comment + */ + public function delete($commentId, $token = '') { + $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); + try { + $this->comment = $this->commentMapper->find($commentId); + + if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { + $this->acl->setToken($token); + } else { + $this->acl->setPollId($this->comment->getPollId()); + } + + if ($this->comment->getUserId() === $this->acl->getUserId()) { + $this->commentMapper->delete($this->comment); + return $this->comment; + } else { + throw new NotAuthorizedException; + } + } catch (\Exception $e) { + throw new NotAuthorizedException; + } + } + +} -- cgit v1.2.3 From b3391397a48241a99c6f092fe8be26466db1c474 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 13 Jun 2020 23:33:42 +0200 Subject: added some error handling --- lib/Controller/CommentApiController.php | 22 ++++++--- lib/Controller/CommentController.php | 2 + lib/Exception/NotAuthorizedException.php | 34 -------------- lib/Exceptions/NotAuthorizedException.php | 40 ++++++++++++++++ lib/Model/Acl.php | 16 +++++++ lib/Service/CommentService.php | 76 +++++++++++++------------------ 6 files changed, 104 insertions(+), 86 deletions(-) delete mode 100644 lib/Exception/NotAuthorizedException.php create mode 100644 lib/Exceptions/NotAuthorizedException.php (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 31d4d8bc..83cbb16e 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -31,6 +31,7 @@ use OCP\AppFramework\ApiController; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Service\CommentService; @@ -69,7 +70,11 @@ class CommentApiController extends ApiController { * @return DataResponse */ public function get($pollId, $token = '') { - return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK); + try { + return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e, Http::STATUS_FORBIDDEN); + } } /** @@ -82,7 +87,11 @@ class CommentApiController extends ApiController { * @return DataResponse */ public function getByToken($token) { - return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); + try { + return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e, Http::STATUS_FORBIDDEN); + } } /** @@ -99,8 +108,8 @@ class CommentApiController extends ApiController { public function add($message, $pollId, $token) { try { return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK); - } catch (Exception $e) { - return new OCSForbiddenException($e); + } catch (NotAuthorizedException $e) { + return new DataResponse($e, Http::STATUS_FORBIDDEN); } } @@ -117,10 +126,9 @@ class CommentApiController extends ApiController { public function delete($commentId, $token) { try { return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK); - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_UNAUTHORIZED); + } catch (NotAuthorizedException $e) { + return new DataResponse($e, Http::STATUS_FORBIDDEN); } - } } diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 17d0de94..8e5f8680 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -37,6 +37,8 @@ use OCA\Polls\Service\CommentService; class CommentController extends Controller { + private $commentService; + /** * CommentController constructor. * @param string $appName diff --git a/lib/Exception/NotAuthorizedException.php b/lib/Exception/NotAuthorizedException.php deleted file mode 100644 index fb4a89a4..00000000 --- a/lib/Exception/NotAuthorizedException.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * @author René Gieling - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -namespace OCA\Polls; - -class NotAuthorizedException extends \Exception { - /** - * NotAuthorizedException Constructor - * @param string $e exception message - */ - public function __construct($e = 'Unauthorized') { - parent::__construct($e); - } -} diff --git a/lib/Exceptions/NotAuthorizedException.php b/lib/Exceptions/NotAuthorizedException.php new file mode 100644 index 00000000..9486790f --- /dev/null +++ b/lib/Exceptions/NotAuthorizedException.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class NotAuthorizedException extends \Exception { + /** + * NotAuthorizedException Constructor + * @param string $e exception message + */ + public function __construct($e = 'Unauthorized') { + parent::__construct($e); + } + public function getStatus() { + return Http::STATUS_FORBIDDEN; + } + +} diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 2c42980a..4072f81f 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -136,6 +136,22 @@ class Acl implements JsonSerializable { } } + + /** + * @NoAdminRequired + * @return boolean + */ + public function checkAuthorize($pollId = 0, $token = '') { + + if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { + $this->setToken($token); + } elseif ($pollId) { + $this->setPollId($pollId); + } + + return ($this->userId && $this->poll->getId()); + } + /** * @NoAdminRequired * @return string diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index a417ea12..c92bb80d 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -23,41 +23,36 @@ namespace OCA\Polls\Service; -use Exception; +use \Exception; use OCP\AppFramework\Db\DoesNotExistException; -use OCA\Polls\Exceptions\NotAuthorizedException; -use OCP\IRequest; +use OCP\IGroupManager; use OCP\ILogger; -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataResponse; -use OCP\IGroupManager; +use OCA\Polls\Exceptions\NotAuthorizedException; -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; -use OCA\Polls\Service\AnonymizeService; +use OCA\Polls\Db\Poll; +use OCA\Polls\Db\PollMapper; use OCA\Polls\Model\Acl; +use OCA\Polls\Service\AnonymizeService; class CommentService { private $userId; + private $comment; private $commentMapper; private $logger; - private $groupManager; private $pollMapper; private $anonymizer; private $acl; - private $comment; /** - * CommentController constructor. + * CommentService constructor. * @param string $appName * @param $UserId * @param CommentMapper $commentMapper @@ -70,7 +65,6 @@ class CommentService { public function __construct( string $appName, $userId, - IRequest $request, ILogger $logger, CommentMapper $commentMapper, IGroupManager $groupManager, @@ -87,7 +81,6 @@ class CommentService { $this->acl = $acl; } - /** * get * Read all comments of a poll based on the poll id and return list as array @@ -97,15 +90,13 @@ class CommentService { * @return Array */ public function get($pollId = 0, $token = '') { - $this->logger->alert('call commentService->get(' . $pollId . ', '. $token . ')'); + $this->logger->debug('call commentService->get(' . $pollId . ', '. $token . ')'); - try { - if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { - $this->acl->setToken($token); - } else { - $this->acl->setPollId($pollId); - } + if (!$this->acl->checkAuthorize($pollId, $token)) { + throw new NotAuthorizedException; + } + try { if (!$this->acl->getAllowSeeUsernames()) { $this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId()); return $this->anonymizer->getComments(); @@ -113,7 +104,7 @@ class CommentService { return $this->commentMapper->findByPoll($this->acl->getPollId()); } - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->alert('Error reading comments for pollId ' . $pollId . ': '. $e); throw new DoesNotExistException($e); } @@ -130,13 +121,12 @@ class CommentService { */ public function add($message, $pollId = 0, $token = '') { $this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")'); - try { - if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { - $this->acl->setToken($token); - } else { - $this->acl->setPollId($pollId); - } + if (!$this->acl->checkAuthorize($pollId, $token)) { + throw new NotAuthorizedException; + } + + try { if ($this->acl->getAllowComment()) { $this->comment = new Comment(); $this->comment->setPollId($this->acl->getPollId()); @@ -149,9 +139,9 @@ class CommentService { throw new NotAuthorizedException; } - } catch (Exception $e) { - $this->logger->alert('Error wrinting comment for pollId ' . $pollId . ': '. $e); - throw new Exception($e); + } catch (\Exception $e) { + $this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e); + throw new NotAuthorizedException($e); } } @@ -165,24 +155,20 @@ class CommentService { */ public function delete($commentId, $token = '') { $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); + try { $this->comment = $this->commentMapper->find($commentId); + } catch (DoesNotExistException $e) { + return new DoesNotExistException($e); + } - if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { - $this->acl->setToken($token); - } else { - $this->acl->setPollId($this->comment->getPollId()); - } - - if ($this->comment->getUserId() === $this->acl->getUserId()) { - $this->commentMapper->delete($this->comment); - return $this->comment; - } else { - throw new NotAuthorizedException; - } - } catch (\Exception $e) { + if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) { throw new NotAuthorizedException; } + + $this->commentMapper->delete($this->comment); + return $this->comment; + } } -- cgit v1.2.3 From c6d2e376387755e10d6c4bf4107a14e65a3a5886 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 14 Jun 2020 12:15:00 +0200 Subject: comment changed error handling --- lib/Controller/CommentApiController.php | 14 ++++++++++---- lib/Controller/CommentController.php | 6 ++++-- lib/Model/Acl.php | 4 +++- lib/Service/CommentService.php | 29 +++++++++++------------------ 4 files changed, 28 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 83cbb16e..97d2ecf7 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -24,13 +24,13 @@ namespace OCA\Polls\Controller; use Exception; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\ApiController; -use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; + use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Service\CommentService; @@ -39,6 +39,7 @@ use OCA\Polls\Service\CommentService; class CommentApiController extends ApiController { + private $optionService; /** * CommentApiController constructor. * @param string $appName @@ -74,6 +75,8 @@ class CommentApiController extends ApiController { return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse($e, Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse($pollId, Http::STATUS_NOT_FOUND); } } @@ -125,9 +128,12 @@ class CommentApiController extends ApiController { */ public function delete($commentId, $token) { try { - return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK); + $this->commentService->delete($commentId, $token); + return new DataResponse($commentId, Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e, Http::STATUS_FORBIDDEN); + return new DataResponse($commentId, Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse($commentId, Http::STATUS_NOT_FOUND); } } diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 8e5f8680..84cfd01a 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -107,8 +107,10 @@ class CommentController extends Controller { public function delete($commentId, $token) { try { return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK); - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_UNAUTHORIZED); + } catch (NotAuthorizedException $e) { + return new DataResponse($e, Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse($e, Http::STATUS_OK); } } diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 4072f81f..12fbc741 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -143,7 +143,7 @@ class Acl implements JsonSerializable { */ public function checkAuthorize($pollId = 0, $token = '') { - if ($token && !\OC::$server->getUserSession()->isLoggedIn()) { + if ($token) { $this->setToken($token); } elseif ($pollId) { $this->setPollId($pollId); @@ -388,12 +388,14 @@ class Acl implements JsonSerializable { * @return string */ public function setToken(string $token): Acl { + $this->logger->debug('Share PollId' . $token); try { $this->token = $token; $share = $this->shareMapper->findByToken($token); $this->foundByToken = true; $this->setPollId($share->getPollId()); + $this->logger->debug('Share PollId' . $share->getPollId()); if (($share->getType() === 'group' || $share->getType() === 'user') && !\OC::$server->getUserSession()->isLoggedIn()) { // User must be logged in for shareType user and group diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index c92bb80d..5cc29045 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -93,22 +93,20 @@ class CommentService { $this->logger->debug('call commentService->get(' . $pollId . ', '. $token . ')'); if (!$this->acl->checkAuthorize($pollId, $token)) { + $this->logger->debug('Acl UserId ' . $this->acl->getUserId()); + $this->logger->debug('Acl PollId ' . $this->acl->getPollId()); + $this->logger->debug('Unauthorized access'); throw new NotAuthorizedException; } - try { - if (!$this->acl->getAllowSeeUsernames()) { - $this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId()); - return $this->anonymizer->getComments(); - } else { - return $this->commentMapper->findByPoll($this->acl->getPollId()); - } - - } catch (\Exception $e) { - $this->logger->alert('Error reading comments for pollId ' . $pollId . ': '. $e); - throw new DoesNotExistException($e); + if (!$this->acl->getAllowSeeUsernames()) { + $this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId()); + return $this->anonymizer->getComments(); + } else { + return $this->commentMapper->findByPoll($this->acl->getPollId()); } + } /** @@ -156,17 +154,12 @@ class CommentService { public function delete($commentId, $token = '') { $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); - try { - $this->comment = $this->commentMapper->find($commentId); - } catch (DoesNotExistException $e) { - return new DoesNotExistException($e); - } - + $this->comment = $this->commentMapper->find($commentId); if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) { throw new NotAuthorizedException; } - $this->commentMapper->delete($this->comment); + return $this->comment; } -- cgit v1.2.3 From 76cba400fc9d8139d1b62fb6e1ce3746368a5574 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 14 Jun 2020 22:42:17 +0200 Subject: Added options to API & changed routes & updated comments --- lib/Controller/CommentApiController.php | 26 ++-- lib/Controller/CommentController.php | 4 +- lib/Controller/OptionApiController.php | 161 +++++++++++++++++++++++ lib/Controller/OptionController.php | 169 ++---------------------- lib/Controller/PollController.php | 9 +- lib/Service/CommentService.php | 8 +- lib/Service/OptionService.php | 219 ++++++++++++++++++++++++++++++++ 7 files changed, 421 insertions(+), 175 deletions(-) create mode 100644 lib/Controller/OptionApiController.php create mode 100644 lib/Service/OptionService.php (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 97d2ecf7..fbee1078 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -27,6 +27,7 @@ use Exception; use OCP\AppFramework\Db\DoesNotExistException; use OCP\IRequest; +use \OCP\IURLGenerator; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -40,6 +41,7 @@ use OCA\Polls\Service\CommentService; class CommentApiController extends ApiController { private $optionService; + private $urlGenerator; /** * CommentApiController constructor. * @param string $appName @@ -50,6 +52,7 @@ class CommentApiController extends ApiController { public function __construct( string $appName, IRequest $request, + IURLGenerator $urlGenerator, CommentService $commentService ) { parent::__construct($appName, @@ -58,6 +61,7 @@ class CommentApiController extends ApiController { 'Authorization, Content-Type, Accept', 1728000); $this->commentService = $commentService; + $this->urlGenerator = $urlGenerator; } /** @@ -70,13 +74,13 @@ class CommentApiController extends ApiController { * @param integer $pollId * @return DataResponse */ - public function get($pollId, $token = '') { + public function list($pollId, $token = '') { try { - return new DataResponse($this->commentService->get($pollId, $token), Http::STATUS_OK); + return new DataResponse($this->commentService->list($pollId, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e, Http::STATUS_FORBIDDEN); + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse($pollId, Http::STATUS_NOT_FOUND); + return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); } } @@ -93,7 +97,9 @@ class CommentApiController extends ApiController { try { return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e, Http::STATUS_FORBIDDEN); + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND); } } @@ -110,9 +116,11 @@ class CommentApiController extends ApiController { */ public function add($message, $pollId, $token) { try { - return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK); + return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { - return new DataResponse($e, Http::STATUS_FORBIDDEN); + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); } } @@ -131,9 +139,9 @@ class CommentApiController extends ApiController { $this->commentService->delete($commentId, $token); return new DataResponse($commentId, Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($commentId, Http::STATUS_FORBIDDEN); + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse($commentId, Http::STATUS_NOT_FOUND); + return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND); } } diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 84cfd01a..70403abd 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -63,8 +63,8 @@ class CommentController extends Controller { * @param integer $pollId * @return DataResponse */ - public function get($pollId) { - return new DataResponse($this->commentService->get($pollId), Http::STATUS_OK); + public function list($pollId) { + return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK); } /** diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php new file mode 100644 index 00000000..d6890c31 --- /dev/null +++ b/lib/Controller/OptionApiController.php @@ -0,0 +1,161 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Controller; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; + +use OCP\IRequest; +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Exceptions\NotAuthorizedException; + +use OCA\Polls\Service\OptionService; + +class OptionApiController extends ApiController { + + private $optionService; + + /** + * OptionApiController constructor. + * @param string $appName + * @param IRequest $request + * @param OptionService $optionService + */ + + public function __construct( + string $appName, + IRequest $request, + OptionService $optionService + ) { + parent::__construct($appName, + $request, + 'POST, PUT, GET, DELETE', + 'Authorization, Content-Type, Accept', + 1728000); + $this->optionService = $optionService; + } + + /** + * Get all options of given poll + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param integer $pollId + * @return array Array of Option objects + */ + public function list($pollId) { + try { + return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + } + } + + + /** + * getByToken + * Read all options of a poll based on a share token and return list as array + * @NoAdminRequired + * @PublicPage + * @NoCSRFRequired + * @param string $token + * @return DataResponse + */ + public function getByToken($token) { + try { + return new DataResponse($this->optionService->get(0, $token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND); + } + } + + /** + * Add a new Option to poll + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param Option $option + * @return DataResponse + */ + public function add($pollId, $pollOptionText = '', $timestamp = 0) { + $option = [ + 'pollId' => $pollId, + 'pollOptionText' => $pollOptionText, + 'timestamp' => $timestamp + ]; + + try { + return new DataResponse($this->optionService->add($option), Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + } catch (UniqueConstraintViolationException $e) { + return new DataResponse('Option exists', Http::STATUS_CONFLICT); + } + } + + /** + * Remove a single option + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param Option $option + * @return DataResponse + */ + public function delete($optionId) { + try { + $this->optionService->delete($optionId); + return new DataResponse($optionId, Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND); + } + } + + /** + * Update poll option + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param Option $option + * @return DataResponse + */ + public function update($option) { + try { + return new DataResponse($this->optionService->update($option), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } + } +} diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 0c20b768..7afa4a08 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -24,98 +24,34 @@ namespace OCA\Polls\Controller; use Exception; -use OCP\AppFramework\Db\DoesNotExistException; use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\IGroupManager; -use OCP\Security\ISecureRandom; +use OCA\Polls\Exceptions\NotAuthorizedException; -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\Option; -use OCA\Polls\Db\OptionMapper; -use OCA\Polls\Service\LogService; -use OCA\Polls\Model\Acl; +use OCA\Polls\Service\OptionService; class OptionController extends Controller { - private $userId; - private $optionMapper; - private $options; - private $option; - private $groupManager; - private $pollMapper; - private $logger; - private $logService; - private $acl; + private $optionService; /** * OptionController constructor. * @param string $appName - * @param $UserId * @param IRequest $request - * @param ILogger $logger - * @param OptionMapper $optionMapper - * @param IGroupManager $groupManager - * @param PollMapper $pollMapper - * @param LogService $logService - * @param Acl $acl + * @param OptionService $optionService */ public function __construct( string $appName, - $UserId, IRequest $request, - OptionMapper $optionMapper, - Option $option, - IGroupManager $groupManager, - PollMapper $pollMapper, - ILogger $logger, - LogService $logService, - Acl $acl + OptionService $optionService ) { parent::__construct($appName, $request); - $this->userId = $UserId; - $this->optionMapper = $optionMapper; - $this->option = $option; - $this->groupManager = $groupManager; - $this->pollMapper = $pollMapper; - $this->logger = $logger; - $this->logService = $logService; - $this->acl = $acl; - } - - /** - * Set properties from option array - * @NoAdminRequired - * @param integer $pollId - * @return array Array of Option objects - */ - private function set($option) { - - $this->option->setPollId($option['pollId']); - $this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); - $this->option->setTimestamp($option['timestamp']); - - if ($option['timestamp']) { - $this->option->setOrder($option['timestamp']); - } else { - $this->option->setOrder($option['order']); - } - - if ($option['confirmed']) { - // do not update confirmation date, if option is already confirmed - if (!$this->option->getConfirmed()) { - $this->option->setConfirmed(time()); - } - } else { - $this->option->setConfirmed(0); - } + $this->optionService = $optionService; } /** @@ -125,20 +61,8 @@ class OptionController extends Controller { * @param integer $pollId * @return array Array of Option objects */ - public function get($pollId) { - - try { - - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - $this->options = $this->optionMapper->findByPoll($pollId); - - return new DataResponse($this->options, Http::STATUS_OK); - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } + public function list($pollId) { + return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); } @@ -152,16 +76,7 @@ class OptionController extends Controller { * @return DataResponse */ public function getByToken($token) { - - try { - $this->acl->setToken($token); - // return $this->get($this->acl->getPollId()); - $this->options = $this->optionMapper->findByPoll($this->acl->getPollId()); - return new DataResponse($this->options, Http::STATUS_OK); - - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } + return new DataResponse($this->optionService->list(0, $token), Http::STATUS_OK); } /** @@ -172,20 +87,7 @@ class OptionController extends Controller { * @return DataResponse */ public function add($option) { - - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - try { - $this->option = new Option(); - $this->set($option); - $this->optionMapper->insert($this->option); - $this->logService->setLog($option['pollId'], 'addOption'); - return new DataResponse($this->option, Http::STATUS_OK); - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } + return new DataResponse($this->optionService->add($option), Http::STATUS_OK); } /** @@ -196,20 +98,7 @@ class OptionController extends Controller { * @return DataResponse */ public function update($option) { - - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - try { - $this->option = $this->optionMapper->find($option['id']); - $this->set($option); - $this->optionMapper->update($this->option); - $this->logService->setLog($option['pollId'], 'updateOption'); - return new DataResponse($this->option, Http::STATUS_OK); - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } + return new DataResponse($this->optionService->update($option), Http::STATUS_OK); } /** @@ -220,24 +109,7 @@ class OptionController extends Controller { * @return DataResponse */ public function remove($option) { - try { - - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - $this->optionMapper->remove($option['id']); - $this->logService->setLog($option['pollId'], 'deleteOption'); - - return new DataResponse(array( - 'action' => 'deleted', - 'optionId' => $option['id'] - ), Http::STATUS_OK); - - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - + return new DataResponse($this->optionService->delete($option['id']), Http::STATUS_OK); } /** @@ -248,21 +120,6 @@ class OptionController extends Controller { * @return DataResponse */ public function reorder($pollId, $options) { - $i = 0; - - if (!$this->acl->setPollId($pollId)->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - foreach ($options as $option) { - $this->option = $this->optionMapper->find($option['id']); - if ($pollId === intval($this->option->getPollId())) { - $this->option->setOrder(++$i); - $this->optionMapper->update($this->option); - } - } - - return $this->get($pollId); - + return new DataResponse($this->optionService->reorder($pollId, $options), Http::STATUS_OK); } } diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 4542713a..9a7aa0d9 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -48,6 +48,7 @@ use OCA\Polls\Db\VoteMapper; use OCA\Polls\Service\AnonymizeService; use OCA\Polls\Service\CommentService; + use OCA\Polls\Service\OptionService; use OCA\Polls\Service\LogService; use OCA\Polls\Service\MailService; use OCA\Polls\Model\Acl; @@ -67,6 +68,7 @@ private $anonymizer; private $logService; private $commentService; + private $optionService; private $mailService; private $acl; @@ -85,6 +87,7 @@ * @param MailService $mailService * @param AnonymizeService $anonymizer * @param CommentService $commentService + * @param OptionService $optionService * @param Acl $acl */ @@ -104,6 +107,7 @@ LogService $logService, MailService $mailService, CommentService $commentService, + OptionService $optionService, AnonymizeService $anonymizer, Acl $acl ) { @@ -121,6 +125,7 @@ $this->logService = $logService; $this->mailService = $mailService; $this->commentService = $commentService; + $this->optionService = $optionService; $this->anonymizer = $anonymizer; $this->acl = $acl; } @@ -203,8 +208,8 @@ return new DataResponse([ 'acl' => $this->acl, - 'comments' => $this->commentService->get($pollId), - 'options' => $options, + 'comments' => $this->commentService->list($pollId), + 'options' => $this->optionService->list($pollId), 'poll' => $this->poll, 'shares' => $shares, 'votes' => $votes diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index 5cc29045..d939ec18 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -24,7 +24,6 @@ namespace OCA\Polls\Service; use \Exception; -use OCP\AppFramework\Db\DoesNotExistException; use OCP\IGroupManager; use OCP\ILogger; @@ -89,13 +88,10 @@ class CommentService { * @param string $token * @return Array */ - public function get($pollId = 0, $token = '') { + public function list($pollId = 0, $token = '') { $this->logger->debug('call commentService->get(' . $pollId . ', '. $token . ')'); if (!$this->acl->checkAuthorize($pollId, $token)) { - $this->logger->debug('Acl UserId ' . $this->acl->getUserId()); - $this->logger->debug('Acl PollId ' . $this->acl->getPollId()); - $this->logger->debug('Unauthorized access'); throw new NotAuthorizedException; } @@ -152,8 +148,8 @@ class CommentService { * @return Comment */ public function delete($commentId, $token = '') { - $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); + $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); $this->comment = $this->commentMapper->find($commentId); if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) { throw new NotAuthorizedException; diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php new file mode 100644 index 00000000..bc265e69 --- /dev/null +++ b/lib/Service/OptionService.php @@ -0,0 +1,219 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Service; + +use \Exception; + +use OCP\IGroupManager; +use OCP\ILogger; + +use OCA\Polls\Exceptions\NotAuthorizedException; + +use OCP\Security\ISecureRandom; + +use OCA\Polls\Db\Poll; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\Option; +use OCA\Polls\Db\OptionMapper; +use OCA\Polls\Service\LogService; +use OCA\Polls\Model\Acl; + +class OptionService { + + private $userId; + private $optionMapper; + private $options; + private $option; + private $groupManager; + private $pollMapper; + private $logger; + private $logService; + private $acl; + + /** + * OptionController constructor. + * @param string $appName + * @param $userId + * @param ILogger $logger + * @param OptionMapper $optionMapper + * @param IGroupManager $groupManager + * @param PollMapper $pollMapper + * @param LogService $logService + * @param Acl $acl + */ + + public function __construct( + string $appName, + $userId, + OptionMapper $optionMapper, + Option $option, + IGroupManager $groupManager, + PollMapper $pollMapper, + ILogger $logger, + LogService $logService, + Acl $acl + ) { + $this->userId = $userId; + $this->optionMapper = $optionMapper; + $this->option = $option; + $this->groupManager = $groupManager; + $this->pollMapper = $pollMapper; + $this->logger = $logger; + $this->logService = $logService; + $this->acl = $acl; + } + + /** + * Set properties from option array + * @NoAdminRequired + * @param Array $option + */ + private function set($option) { + + $this->option->setPollId($option['pollId']); + $this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); + $this->option->setTimestamp($option['timestamp']); + + if ($option['timestamp']) { + $this->option->setOrder($option['timestamp']); + } else { + $this->option->setOrder($option['order']); + } + + if ($option['confirmed']) { + // do not update confirmation date, if option is already confirmed + if (!$this->option->getConfirmed()) { + $this->option->setConfirmed(time()); + } + } else { + $this->option->setConfirmed(0); + } + } + + /** + * Get all options of given poll + * @NoAdminRequired + * @param integer $pollId + * @param string $token + * @return array Array of Option objects + */ + public function list($pollId = 0, $token = '') { + $this->logger->debug('call optionService->list(' . $pollId . ', '. $token . ')'); + + if (!$this->acl->checkAuthorize($pollId, $token)) { + throw new NotAuthorizedException; + } + + return $this->optionMapper->findByPoll($pollId); + } + + + /** + * Add a new Option to poll + * @NoAdminRequired + * @param Array $option + * @return Option + */ + public function add($option) { + $this->logger->debug('call optionService->add(' . json_encode($option) . ')'); + + if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + throw new NotAuthorizedException; + } + $this->option = new Option(); + $this->set($option); + $this->optionMapper->insert($this->option); + $this->logService->setLog($option['pollId'], 'addOption'); + return $this->option; + } + + /** + * Remove a single option + * @NoAdminRequired + * @param Option $option + * @return array Array of Option objects + */ + public function delete($optionId) { + $this->logger->debug('call optionService->delete(' . json_encode($optionId) . ')'); + + $this->option = $this->optionMapper->find($optionId); + if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $this->optionMapper->delete($this->option); + + return $this->option; + } + + /** + * Update poll option + * @NoAdminRequired + * @param array $option + * @return Option + */ + public function update($option) { + $this->logger->debug('call optionService->update(' . json_encode($option) . ')'); + + if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + throw new NotAuthorizedException; + } + + try { + $this->option = $this->optionMapper->find($option['id']); + $this->set($option); + $this->optionMapper->update($this->option); + $this->logService->setLog($option['pollId'], 'updateOption'); + return $this->option; + } catch (Exception $e) { + return new DoesNotExistException($e); + } + } + + /** + * Set order by order of the given array + * @NoAdminRequired + * @param array $options + * @return array Array of Option objects + */ + public function reorder($pollId, $options) { + $this->logger->debug('call optionService->reorder(' . $pollId . ', ' . json_encode($options) . ')'); + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $i = 0; + foreach ($options as $option) { + $this->option = $this->optionMapper->find($option['id']); + if ($pollId === intval($this->option->getPollId())) { + $this->option->setOrder(++$i); + $this->optionMapper->update($this->option); + } + } + + return $this->get($pollId); + + } +} -- cgit v1.2.3 From 4632557dde1e437da3768e2baf1166d3056a3315 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 15 Jun 2020 16:33:15 +0200 Subject: fixes and tidy --- lib/Controller/OptionApiController.php | 7 +++---- lib/Service/OptionService.php | 2 -- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php index d6890c31..ded3933e 100644 --- a/lib/Controller/OptionApiController.php +++ b/lib/Controller/OptionApiController.php @@ -24,15 +24,15 @@ namespace OCA\Polls\Controller; use Exception; -use OCP\AppFramework\Db\DoesNotExistException; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Service\OptionService; @@ -134,8 +134,7 @@ class OptionApiController extends ApiController { */ public function delete($optionId) { try { - $this->optionService->delete($optionId); - return new DataResponse($optionId, Http::STATUS_OK); + return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index bc265e69..0d0e7be4 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -30,8 +30,6 @@ use OCP\ILogger; use OCA\Polls\Exceptions\NotAuthorizedException; -use OCP\Security\ISecureRandom; - use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Option; -- cgit v1.2.3 From fe8bc1b4aa307e0d956994c0fdfef51a93f549b2 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 15 Jun 2020 17:02:12 +0200 Subject: added shares to API --- lib/Controller/ShareApiController.php | 167 +++++++++++++++++++++++ lib/Controller/ShareController.php | 157 +++++----------------- lib/Exceptions/InvalidUsername.php | 40 ++++++ lib/Service/ShareService.php | 246 ++++++++++++++++++++++++++++++++++ 4 files changed, 490 insertions(+), 120 deletions(-) create mode 100644 lib/Controller/ShareApiController.php create mode 100644 lib/Exceptions/InvalidUsername.php create mode 100644 lib/Service/ShareService.php (limited to 'lib') diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php new file mode 100644 index 00000000..bc04e376 --- /dev/null +++ b/lib/Controller/ShareApiController.php @@ -0,0 +1,167 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Controller; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; +use OCA\Polls\Exceptions\InvalidUsername; + + +use OCP\IRequest; +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\ShareService; + +class ShareApiController extends ApiController { + + private $shareService; + + /** + * ShareController constructor. + * @param string $appName + * @param string $userId + * @param IRequest $request + * @param ILogger $logger + * @param ShareService $shareService + */ + public function __construct( + string $appName, + IRequest $request, + ShareService $shareService + ) { + parent::__construct($appName, + $request, + 'POST, PUT, GET, DELETE', + 'Authorization, Content-Type, Accept', + 1728000); + $this->shareService = $shareService; + } + + /** + * getByToken + * Get pollId by token + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @PublicPage + * @param string $token + * @return DataResponse + */ + public function get($token) { + try { + return new DataResponse($this->shareService->get($token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); + } + } + + /** + * get + * Read all shares of a poll based on the poll id and return list as array + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function list($pollId) { + try { + return new DataResponse($this->shareService->list($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + } + } + + /** + * Write a new share to the db and returns the new share as array + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param int $pollId + * @param string $message + * @return DataResponse + */ + public function add($pollId, $type, $userId = '', $userEmail = '') { + try { + return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (\Exception $e) { + return new DataResponse($e, Http::STATUS_CONFLICT); + } + + } + + /** + * createPersonalShare + * Write a new share to the db and returns the new share as array + * @NoAdminRequired + * @CORS + * @PublicPage + * @NoCSRFRequired + * @param int $pollId + * @param string $message + * @return DataResponse + */ + public function createPersonalShare($token, $userName) { + + try { + return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (InvalidUsername $e) { + return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT); + } catch (DoesNotExistException $e) { + // return forbidden in all not catched error cases + return new DataResponse($e, Http::STATUS_FORBIDDEN); + } + } + + /** + * remove + * remove share + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param Share $share + * @return DataResponse + */ + + public function delete($token) { + try { + return new DataResponse($this->shareService->remove($token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (Exception $e) { + return new DataResponse($e, Http::STATUS_NOT_FOUND); + } + } +} diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index 15eee195..c2e769ad 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -25,6 +25,8 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; +use OCA\Polls\Exceptions\InvalidUsername; use OCP\IRequest; @@ -33,60 +35,36 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\Security\ISecureRandom; -use OCA\Polls\Db\Poll; use OCA\Polls\Model\Acl; -use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\Share; -use OCA\Polls\Db\ShareMapper; -use OCA\Polls\Service\MailService; -// TODO: Change to Service -use OCA\Polls\Controller\SystemController; +use OCA\Polls\Service\ShareService; class ShareController extends Controller { private $logger; - private $acl; - private $mapper; + private $shareService; private $userId; - private $pollMapper; - private $systemController; - private $mailService; - /** * ShareController constructor. * @param string $appName * @param string $userId * @param IRequest $request * @param ILogger $logger - * @param ShareMapper $mapper - * @param PollMapper $pollMapper - * @param SystemController $systemController - * @param MailService $mailService - * @param Acl $acl + * @param ShareService $shareService */ public function __construct( string $appName, $userId, IRequest $request, ILogger $logger, - ShareMapper $mapper, - PollMapper $pollMapper, - SystemController $systemController, - MailService $mailService, - Acl $acl + ShareService $shareService ) { parent::__construct($appName, $request); $this->logger = $logger; $this->userId = $userId; - $this->mapper = $mapper; - $this->pollMapper = $pollMapper; - $this->systemController = $systemController; - $this->mailService = $mailService; - $this->acl = $acl; + $this->shareService = $shareService; } /** @@ -100,11 +78,11 @@ class ShareController extends Controller { */ public function get($token) { try { - $share = $this->mapper->findByToken($token); - return new DataResponse($share, Http::STATUS_OK); - + return new DataResponse($this->shareService->get($token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse(null, Http::STATUS_NOT_FOUND); + return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); } } @@ -117,21 +95,13 @@ class ShareController extends Controller { * @return DataResponse */ public function getShares($pollId) { - if ($this->acl->setPollId($pollId)->getAllowEdit()) { - try { - $shares = $this->mapper->findByPoll($pollId); - return new DataResponse((array) $shares, Http::STATUS_OK); - - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - - } else { - $this->logger->alert('no access'); - - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + try { + return new DataResponse($this->shareService->findByPoll($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); } - } /** @@ -143,32 +113,16 @@ class ShareController extends Controller { * @return DataResponse */ public function write($pollId, $share) { - $this->acl->setPollId($pollId); - if (!$this->acl->getAllowEdit()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - $newShare = new Share(); - $newShare->setType($share['type']); - $newShare->setPollId($share['pollId']); - $newShare->setUserId($share['userId']); - $newShare->setUserEmail(isset($share['userEmail']) ? $share['userEmail'] : ''); - $newShare->setToken(\OC::$server->getSecureRandom()->generate( - 16, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - )); - try { - $newShare = $this->mapper->insert($newShare); - $sendResult = $this->mailService->sendInvitationMail($newShare->getToken()); - - return new DataResponse([ - 'share' => $newShare, - 'sendResult' => $sendResult - ], Http::STATUS_OK); - + $return = $this->shareService->write( + $pollId, + $share['type'], + $share['userId'], + isset($share['userEmail']) ? $share['userEmail'] : '' + ); + return new DataResponse($return, Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (\Exception $e) { return new DataResponse($e, Http::STATUS_CONFLICT); } @@ -188,43 +142,11 @@ class ShareController extends Controller { public function createPersonalShare($token, $userName) { try { - $publicShare = $this->mapper->findByToken($token); - - // Return of validatePublicUsername is a DataResponse - $checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token); - - // if status is not 200, return DataResponse from validatePublicUsername - if ($checkUsername->getStatus() !== 200) { - return $checkUsername; - } - - if ($publicShare->getType() === 'public') { - - $userShare = new Share(); - $userShare->setToken(\OC::$server->getSecureRandom()->generate( - 16, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - )); - $userShare->setType('external'); - $userShare->setPollId($publicShare->getPollId()); - $userShare->setUserId($userName); - $userShare->setUserEmail(''); - $userShare = $this->mapper->insert($userShare); - return new DataResponse($userShare, Http::STATUS_OK); - - } elseif ($publicShare->getType() === 'email') { - - $publicShare->setType('external'); - $publicShare->setUserId($userName); - $this->mapper->update($publicShare); - return new DataResponse($publicShare, Http::STATUS_OK); - - } else { - return new DataResponse(['message'=> 'Wrong share type: ' . $userShare->getType()], Http::STATUS_FORBIDDEN); - } - + return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (InvalidUsername $e) { + return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT); } catch (DoesNotExistException $e) { // return forbidden in all not catched error cases return new DataResponse($e, Http::STATUS_FORBIDDEN); @@ -242,17 +164,12 @@ class ShareController extends Controller { public function remove($share) { try { - if ($this->acl->setPollId($share['pollId'])->getAllowEdit()) { - $this->mapper->remove($share['id']); - - return new DataResponse(array( - 'action' => 'deleted', - 'shareId' => $share['id'] - ), Http::STATUS_OK); - } else { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - + return new DataResponse(array( + 'action' => 'deleted', + 'shareId' => $this->shareService->remove($share['token'])->getId() + ), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } diff --git a/lib/Exceptions/InvalidUsername.php b/lib/Exceptions/InvalidUsername.php new file mode 100644 index 00000000..b75c8ea0 --- /dev/null +++ b/lib/Exceptions/InvalidUsername.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class InvalidUsername extends \Exception { + /** + * InvalidUsername Constructor + * @param string $e exception message + */ + public function __construct($e = 'Invalid username') { + parent::__construct($e); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php new file mode 100644 index 00000000..c362e97a --- /dev/null +++ b/lib/Service/ShareService.php @@ -0,0 +1,246 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Service; + +use Exception; + +use OCP\ILogger; +use OCP\Security\ISecureRandom; + +use OCA\Polls\Exceptions\NotAuthorizedException; +use OCA\Polls\Exceptions\InvalidUsername; + +use OCA\Polls\Db\Poll; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\Share; +use OCA\Polls\Db\ShareMapper; +use OCA\Polls\Service\MailService; +use OCA\Polls\Model\Acl; +// TODO: Change to Service +use OCA\Polls\Controller\SystemController; + +class ShareService { + + private $logger; + private $acl; + private $shareMapper; + private $share; + private $userId; + + private $pollMapper; + private $systemController; + private $mailService; + + /** + * ShareController constructor. + * @param string $appName + * @param string $userId + * @param IRequest $request + * @param ILogger $logger + * @param ShareMapper $shareMapper + * @param Share $share + * @param PollMapper $pollMapper + * @param SystemController $systemController + * @param MailService $mailService + * @param Acl $acl + */ + public function __construct( + string $appName, + $userId, + ILogger $logger, + ShareMapper $shareMapper, + Share $share, + PollMapper $pollMapper, + SystemController $systemController, + MailService $mailService, + Acl $acl + ) { + $this->logger = $logger; + $this->userId = $userId; + $this->shareMapper = $shareMapper; + $this->share = $share; + $this->pollMapper = $pollMapper; + $this->systemController = $systemController; + $this->mailService = $mailService; + $this->acl = $acl; + } + + /** + * get + * Read all shares of a poll based on the poll id and return list as array + * @NoAdminRequired + * @param integer $pollId + * @return DataResponse + */ + public function list($pollId) { + if ($this->acl->setPollId($pollId)->getAllowEdit()) { + return $this->shareMapper->findByPoll($pollId); + } else { + throw new NotAuthorizedException; + } + } + + /** + * getByToken + * Get pollId by token + * @NoAdminRequired + * @param string $token + * @return Array + */ + public function get($token) { + $this->share = $this->shareMapper->findByToken($token); + return $this->share; + } + + /** + * Write a new share to the db and returns the new share as array + * @NoAdminRequired + * @depricated + * @param int $pollId + * @param string $share + * @return Array + */ + // TODO: Replace with $this->add and separate sending invitations + public function write($pollId, $type, $userId, $userEmail = '') { + $this->acl->setPollId($pollId); + if (!$this->acl->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $this->share = new Share(); + $this->share->setType($type); + $this->share->setPollId($pollId); + $this->share->setUserId($userId); + $this->share->setUserEmail($userEmail); + $this->share->setToken(\OC::$server->getSecureRandom()->generate( + 16, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + )); + + $this->share = $this->shareMapper->insert($this->share); + $sendResult = $this->mailService->sendInvitationMail($this->share->getToken()); + + return [ + 'share' => $this->share, + 'sendResult' => $sendResult + ]; + } + + /** + * Write a new share to the db and returns the new share as array + * @NoAdminRequired + * @param int $pollId + * @param string $share + * @return Array + */ + public function add($pollId, $type, $userId, $userEmail = '') { + $this->acl->setPollId($pollId); + if (!$this->acl->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $this->share = new Share(); + $this->share->setType($type); + $this->share->setPollId($pollId); + $this->share->setUserId($userId); + $this->share->setUserEmail($userEmail); + $this->share->setToken(\OC::$server->getSecureRandom()->generate( + 16, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + )); + + return $this->shareMapper->insert($this->share); + + } + + /** + * createPersonalShare + * Write a new share to the db and returns the new share as array + * @NoAdminRequired + * @param string $token + * @param string $userName + * @return Share + */ + public function createPersonalShare($token, $userName) { + + $publicShare = $this->shareMapper->findByToken($token); + + // Return of validatePublicUsername is a DataResponse + $checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token); + + // if status is not 200, return DataResponse from validatePublicUsername + if ($checkUsername->getStatus() !== 200) { + throw new InvalidUsername; + } + + if ($publicShare->getType() === 'public') { + + $this->share = new Share(); + $this->share->setToken(\OC::$server->getSecureRandom()->generate( + 16, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + )); + $this->share->setType('external'); + $this->share->setPollId($publicShare->getPollId()); + $this->share->setUserId($userName); + $this->share->setUserEmail(''); + $this->share = $this->shareMapper->insert($this->share); + return $this->share; + + } elseif ($publicShare->getType() === 'email') { + + $publicShare->setType('external'); + $publicShare->setUserId($userName); + $this->shareMapper->update($publicShare); + return new DataResponse($publicShare, Http::STATUS_OK); + + } else { + throw new NotAuthorizedException; + } + } + + /** + * remove + * remove share + * @NoAdminRequired + * @param string $token + * @return Share + */ + + public function remove($token) { + $this->share = $this->shareMapper->findByToken($token); + if ($this->acl->setPollId($this->share->getPollId())->getAllowEdit()) { + $this->shareMapper->delete($this->share); + return $this->share; + } else { + throw new NotAuthorizedException; + } + } +} -- cgit v1.2.3 From 076f8b3785f5728d7e61e5b9ac1233bda7e9fe3d Mon Sep 17 00:00:00 2001 From: dartcafe Date: Tue, 16 Jun 2020 13:12:02 +0200 Subject: Added subscription to API --- lib/Controller/SubscriptionApiController.php | 116 +++++++++++++++++++++++ lib/Controller/SubscriptionController.php | 28 ++---- lib/Service/ShareService.php | 5 - lib/Service/SubscriptionService.php | 134 +++++++++++++++++++++++++++ 4 files changed, 258 insertions(+), 25 deletions(-) create mode 100644 lib/Controller/SubscriptionApiController.php create mode 100644 lib/Service/SubscriptionService.php (limited to 'lib') diff --git a/lib/Controller/SubscriptionApiController.php b/lib/Controller/SubscriptionApiController.php new file mode 100644 index 00000000..b89f009c --- /dev/null +++ b/lib/Controller/SubscriptionApiController.php @@ -0,0 +1,116 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Controller; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; + +use OCP\IRequest; +use OCP\ILogger; + +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\SubscriptionService; + +class SubscriptionApiController extends ApiController { + + private $userId; + private $subscriptionService; + private $logger; + + /** + * SubscriptionController constructor. + * @param string $appName + * @param $UserId + * @param SubscriptionService $subscriptionService + * @param IRequest $request + * @param ILogger $logger + */ + + public function __construct( + string $appName, + $userId, + SubscriptionService $subscriptionService, + IRequest $request, + ILogger $logger + + ) { + parent::__construct($appName, + $request, + 'PUT, GET, DELETE', + 'Authorization, Content-Type, Accept', + 1728000); + $this->userId = $userId; + $this->subscriptionService = $subscriptionService; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * CORS + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function get($pollId) { + try { + return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND); + } + } + + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param integer $pollId + */ + public function subscribe($pollId) { + try { + return $this->subscriptionService->set($pollId, true); + return new DataResponse('Subscribed', Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } + } + /** + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param integer $pollId + */ + public function unsubscribe($pollId) { + try { + $this->subscriptionService->set($pollId, false); + return new DataResponse('Unsubscribed', Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } + } +} diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index 8a5bf445..d201fa6e 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -25,7 +25,6 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IRequest; use OCP\ILogger; @@ -73,19 +72,13 @@ class SubscriptionController extends Controller { * @return DataResponse */ public function get($pollId) { - - if (!\OC::$server->getUserSession()->isLoggedIn()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - try { - $this->mapper->findByUserAndPoll($pollId, $this->userId); - } catch (MultipleObjectsReturnedException $e) { - // should not happen, but who knows + return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse(null, Http::STATUS_NOT_FOUND); + return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND); } - return new DataResponse(null, Http::STATUS_OK); } /** @@ -94,15 +87,10 @@ class SubscriptionController extends Controller { * @param integer $pollId */ public function set($pollId, $subscribed) { - if ($subscribed) { - $subscription = new Subscription(); - $subscription->setPollId($pollId); - $subscription->setUserId($this->userId); - $this->mapper->insert($subscription); - return true; - } else { - $this->mapper->unsubscribe($pollId, $this->userId); - return false; + try { + return new DataResponse($this->subscriptionService->set($pollId, $subscribed), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } } } diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index c362e97a..0df25218 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -31,8 +31,6 @@ use OCP\Security\ISecureRandom; use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Exceptions\InvalidUsername; -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Service\MailService; @@ -60,7 +58,6 @@ class ShareService { * @param ILogger $logger * @param ShareMapper $shareMapper * @param Share $share - * @param PollMapper $pollMapper * @param SystemController $systemController * @param MailService $mailService * @param Acl $acl @@ -71,7 +68,6 @@ class ShareService { ILogger $logger, ShareMapper $shareMapper, Share $share, - PollMapper $pollMapper, SystemController $systemController, MailService $mailService, Acl $acl @@ -80,7 +76,6 @@ class ShareService { $this->userId = $userId; $this->shareMapper = $shareMapper; $this->share = $share; - $this->pollMapper = $pollMapper; $this->systemController = $systemController; $this->mailService = $mailService; $this->acl = $acl; diff --git a/lib/Service/SubscriptionService.php b/lib/Service/SubscriptionService.php new file mode 100644 index 00000000..a8801520 --- /dev/null +++ b/lib/Service/SubscriptionService.php @@ -0,0 +1,134 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Service; + +use Exception; +use OCA\Polls\Exceptions\NotAuthorizedException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\ILogger; + +use OCA\Polls\Db\Subscription; +use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Model\Acl; + +class SubscriptionService { + + private $userId; + private $acl; + private $subscriptionMapper; + private $logger; + + /** + * SubscriptionController constructor. + * @param string $appName + * @param $UserId + * @param SubscriptionMapper $subscriptionMapper + * @param IRequest $request + * @param ILogger $logger + * @param Acl $acl + */ + + public function __construct( + string $appName, + $userId, + SubscriptionMapper $subscriptionMapper, + ILogger $logger, + Acl $acl + ) { + $this->userId = $userId; + $this->subscriptionMapper = $subscriptionMapper; + $this->acl = $acl; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * @param integer $pollId + * @return DataResponse + */ + public function get($pollId) { + if (!$this->acl->setPollId($pollId)->getAllowView()) { + throw new NotAuthorizedException; + } + try { + return $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId()); + } catch (MultipleObjectsReturnedException $e) { + // subscription should be unique. If duplicates are found resubscribe + // duplicates are removed in $this->set() + return $this->set($pollId, true); + } + + } + + /** + * @NoAdminRequired + * @param integer $pollId + */ + public function set($pollId, $subscribed) { + if (!$this->acl->setPollId($pollId)->getAllowView()) { + throw new NotAuthorizedException; + } + try { + $subscription = $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId()); + if (!$subscribed) { + $this->subscriptionMapper->delete($subscription); + return 'Unsubscribed'; + } else { + // subscription already exists, just return the existing subscription + return $subscription; + } + } catch (DoesNotExistException $e){ + if ($subscribed) { + $subscription = new Subscription(); + $subscription->setPollId($pollId); + $subscription->setUserId($this->acl->getUserId()); + + $this->subscriptionMapper->insert($subscription); + return $subscription; + } else { + // subscription is not found, just approve the unsubscription + return 'Unsubscribed'; + } + } catch (MultipleObjectsReturnedException $e) { + // Duplicates should not exist but if found, fix it + // unsubscribe from all and resubscribe, if requested + $this->logger->debug('Multiple subscription (dulpicates) found'); + $this->subscriptionMapper->unsubscribe($pollId, $this->acl->getUserId()); + $this->logger->debug('Unsubscribed all for user ' . $this->acl->getUserId() . 'in poll' . $pollId); + if ($subscribed) { + $subscription = new Subscription(); + $subscription->setPollId($pollId); + $subscription->setUserId($this->acl->getUserId()); + $this->subscriptionMapper->insert($subscription); + $this->logger->debug('Added new subscription'); + return $subscription; + } else { + return 'Unsubscribed'; + } + + } + + } +} -- cgit v1.2.3 From b1c1a707d1b5523868d992b96a4ccddd1086ef2b Mon Sep 17 00:00:00 2001 From: dartcafe Date: Wed, 17 Jun 2020 08:34:35 +0200 Subject: Added vote to API --- lib/Controller/PollController.php | 77 +++------------- lib/Controller/VoteApiController.php | 126 ++++++++++++++++++++++++++ lib/Controller/VoteController.php | 142 ++++++----------------------- lib/Db/OptionMapper.php | 22 +++++ lib/Service/VoteService.php | 169 +++++++++++++++++++++++++++++++++++ 5 files changed, 359 insertions(+), 177 deletions(-) create mode 100644 lib/Controller/VoteApiController.php create mode 100644 lib/Service/VoteService.php (limited to 'lib') diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 9a7aa0d9..6bf98137 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -28,29 +28,22 @@ use OCP\IRequest; use OCP\ILogger; - use OCP\IL10N; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; - use OCP\IGroupManager; use OCP\IUser; - use OCP\IUserManager; use OCP\Security\ISecureRandom; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Option; use OCA\Polls\Db\OptionMapper; - use OCA\Polls\Db\Share; - use OCA\Polls\Db\ShareMapper; - use OCA\Polls\Db\Vote; - use OCA\Polls\Db\VoteMapper; - use OCA\Polls\Service\AnonymizeService; use OCA\Polls\Service\CommentService; use OCA\Polls\Service\OptionService; + use OCA\Polls\Service\ShareService; + use OCA\Polls\Service\VoteService; use OCA\Polls\Service\LogService; - use OCA\Polls\Service\MailService; use OCA\Polls\Model\Acl; class PollController extends Controller { @@ -58,18 +51,13 @@ private $userId; private $pollMapper; private $optionMapper; - private $shareMapper; - private $voteMapper; - private $trans; private $logger; - private $groupManager; - private $userManager; private $poll; - private $anonymizer; private $logService; private $commentService; private $optionService; - private $mailService; + private $shareService; + private $voteService; private $acl; /** @@ -78,16 +66,13 @@ * @param $userId * @param IRequest $request * @param ILogger $logger - * @param IL10N $trans * @param OptionMapper $optionMapper * @param PollMapper $pollMapper - * @param IGroupManager $groupManager - * @param IUserManager $userManager * @param LogService $logService - * @param MailService $mailService - * @param AnonymizeService $anonymizer * @param CommentService $commentService * @param OptionService $optionService + * @param ShareService $shareService + * @param VoteService $voteService * @param Acl $acl */ @@ -96,37 +81,27 @@ $userId, IRequest $request, ILogger $logger, - IL10N $trans, OptionMapper $optionMapper, PollMapper $pollMapper, - ShareMapper $shareMapper, - VoteMapper $voteMapper, Poll $poll, - IGroupManager $groupManager, - IUserManager $userManager, LogService $logService, - MailService $mailService, CommentService $commentService, OptionService $optionService, - AnonymizeService $anonymizer, + ShareService $shareService, + VoteService $voteService, Acl $acl ) { parent::__construct($appName, $request); $this->userId = $userId; - $this->trans = $trans; $this->pollMapper = $pollMapper; $this->optionMapper = $optionMapper; - $this->shareMapper = $shareMapper; - $this->voteMapper = $voteMapper; $this->logger = $logger; - $this->groupManager = $groupManager; - $this->userManager = $userManager; $this->poll = $poll; $this->logService = $logService; - $this->mailService = $mailService; $this->commentService = $commentService; $this->optionService = $optionService; - $this->anonymizer = $anonymizer; + $this->shareService = $shareService; + $this->voteService = $voteService; $this->acl = $acl; } @@ -185,34 +160,13 @@ return new DataResponse(null, Http::STATUS_UNAUTHORIZED); } - $options = $this->optionMapper->findByPoll($pollId); - - if ($this->acl->getAllowEdit()) { - $shares = $this->shareMapper->findByPoll($pollId); - } else { - $shares = []; - } - - if ($this->acl->getAllowSeeUsernames()) { - - if ($this->acl->getAllowSeeResults()) { - $votes = $this->voteMapper->findByPoll($pollId); - } else { - $votes = $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId()); - } - } else { - $this->anonymizer->set($pollId, $this->acl->getUserId()); - $votes = $this->anonymizer->getVotes(); - - } - return new DataResponse([ 'acl' => $this->acl, + 'poll' => $this->poll, 'comments' => $this->commentService->list($pollId), 'options' => $this->optionService->list($pollId), - 'poll' => $this->poll, - 'shares' => $shares, - 'votes' => $votes + 'shares' => $this->shareService->list($pollId), + 'votes' => $this->voteService->list($pollId) ], Http::STATUS_OK); } catch (DoesNotExistException $e) { @@ -355,11 +309,6 @@ $this->logService->setLog($this->poll->getId(), 'addPoll'); } return $this->get($this->poll->getId()); - // $this->acl->setPollId($this->poll->getId()); - // return new DataResponse([ - // 'poll' => $this->poll, - // 'acl' => $this->acl - // ], Http::STATUS_OK); } } diff --git a/lib/Controller/VoteApiController.php b/lib/Controller/VoteApiController.php new file mode 100644 index 00000000..71c42966 --- /dev/null +++ b/lib/Controller/VoteApiController.php @@ -0,0 +1,126 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Controller; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\VoteService; + +class VoteApiController extends ApiController { + + private $logger; + private $voteService; + + /** + * VoteController constructor. + * @param string $appName + * @param IRequest $request + * @param ILogger $logger + * @param VoteService $voteService + */ + public function __construct( + string $appName, + IRequest $request, + ILogger $logger, + VoteService $voteService + ) { + parent::__construct($appName, + $request, + 'PUT, GET, DELETE', + 'Authorization, Content-Type, Accept', + 1728000); + $this->voteService = $voteService; + $this->logger = $logger; + } + + /** + * Get all votes of given poll + * Read all votes of a poll based on the poll id and return list as array + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @param integer $pollId + * @return DataResponse + */ + public function list($pollId) { + try { + return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('No votes', Http::STATUS_NOT_FOUND); + } + } + + /** + * set + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @param integer $pollId + * @param Array $option + * @param string $userId + * @param string $setTo + * @return DataResponse + */ + public function set($pollId, $pollOptionText, $setTo) { + try { + return new DataResponse($this->voteService->set($pollId, $pollOptionText, $setTo), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); + } + + } + + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @param integer $voteId + * @param string $userId + * @param integer $pollId + * @return DataResponse + */ + public function delete($pollId, $userId) { + try { + return new DataResponse($this->voteService->delete($userId, $pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + } catch (DoesNotExistException $e) { + return new DataResponse('', Http::STATUS_NOT_FOUND); + } + } + +} diff --git a/lib/Controller/VoteController.php b/lib/Controller/VoteController.php index ad8936bd..7205c14f 100644 --- a/lib/Controller/VoteController.php +++ b/lib/Controller/VoteController.php @@ -23,77 +23,36 @@ namespace OCA\Polls\Controller; -use Exception; +// use Exception; use OCP\AppFramework\Db\DoesNotExistException; - use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCP\IGroupManager; +use OCA\Polls\Service\VoteService; -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\Vote; -use OCA\Polls\Db\VoteMapper; -use OCA\Polls\Db\Share; -use OCA\Polls\Db\ShareMapper; -use OCA\Polls\Service\AnonymizeService; -use OCA\Polls\Service\LogService; -use OCA\Polls\Model\Acl; class VoteController extends Controller { - private $userId; - private $logger; - private $mapper; - private $groupManager; - private $pollMapper; - private $shareMapper; - private $anonymizer; - private $logService; - private $acl; + private $voteService; /** * VoteController constructor. * @param string $appName - * @param $userId * @param IRequest $request * @param ILogger $logger - * @param VoteMapper $mapper - * @param IGroupManager $groupManager - * @param PollMapper $pollMapper - * @param ShareMapper $shareMapper - * @param AnonymizeService $anonymizer - * @param LogService $logService - * @param Acl $acl + * @param VoteService $voteService + */ public function __construct( string $appName, - $UserId, IRequest $request, - ILogger $logger, - VoteMapper $mapper, - IGroupManager $groupManager, - PollMapper $pollMapper, - ShareMapper $shareMapper, - AnonymizeService $anonymizer, - LogService $logService, - Acl $acl + VoteService $voteService ) { parent::__construct($appName, $request); - $this->userId = $UserId; - $this->mapper = $mapper; - $this->logger = $logger; - $this->groupManager = $groupManager; - $this->pollMapper = $pollMapper; - $this->shareMapper = $shareMapper; - $this->anonymizer = $anonymizer; - $this->logService = $logService; - $this->acl = $acl; + $this->voteService = $voteService; } /** @@ -105,26 +64,13 @@ class VoteController extends Controller { * @return DataResponse */ public function get($pollId) { - try { - - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - if (!$this->acl->getAllowSeeResults()) { - return new DataResponse((array) $this->mapper->findByPollAndUser($pollId, $this->acl->getUserId()), Http::STATUS_OK); - } elseif (!$this->acl->getAllowSeeUsernames()) { - $this->anonymizer->set($pollId, $this->acl->getUserId()); - return new DataResponse((array) $this->anonymizer->getVotes(), Http::STATUS_OK); - } else { - return new DataResponse((array) $this->mapper->findByPoll($pollId), Http::STATUS_OK); - } - + return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse('No votes', Http::STATUS_NOT_FOUND); } - } /** @@ -137,28 +83,13 @@ class VoteController extends Controller { * @param string $setTo * @return DataResponse */ - public function set($pollId, $option, $userId, $setTo) { - + public function set($pollId, $option, $setTo) { try { - $vote = $this->mapper->findSingleVote($pollId, $option['pollOptionText'], $userId); - $vote->setVoteAnswer($setTo); - $this->mapper->update($vote); - + return new DataResponse($this->voteService->set($pollId, $option['pollOptionText'], $setTo), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - // Vote does not exist, insert as new Vote - $vote = new Vote(); - - $vote->setPollId($pollId); - $vote->setUserId($userId); - $vote->setVoteOptionText($option['pollOptionText']); - $vote->setVoteOptionId($option['id']); - $vote->setVoteAnswer($setTo); - - $this->mapper->insert($vote); - - } finally { - $this->logService->setLog($vote->getPollId(), 'setVote', $vote->getUserId()); - return new DataResponse($vote, Http::STATUS_OK); + return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); } } @@ -172,27 +103,13 @@ class VoteController extends Controller { * @param integer $pollId * @return DataResponse */ - public function delete($voteId = 0, $userId = '', $pollId = 0) { - $this->logger->alert('Deleting vote no. ' . $voteId); - + public function delete($userId, $pollId) { try { - if ($voteId) { - $vote = $this->mapper->delete($voteId); - $this->logger->alert('Deleting vote no. ' . $voteId); - return new DataResponse(null, Http::STATUS_OK); - } elseif ($pollId && $userId) { - $votes = $this->mapper->deleteByPollAndUser($pollId, $userId); - $this->logger->alert('Deleting votes from ' . $userId . ' in poll ' . $pollId); - return new DataResponse(null, Http::STATUS_OK); - } elseif ($pollId) { - $votes = $this->mapper->deleteByPoll($pollId); - $this->logger->alert('Deleting all votes in poll ' . $pollId); - return new DataResponse(null, Http::STATUS_OK); - } else { - return DataResponse(null, Http::STATUS_NOT_FOUND); - } + return new DataResponse($this->voteService->delete($pollId, $userId), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return DataResponse(null, Http::STATUS_NOT_FOUND); + return new DataResponse('', Http::STATUS_NOT_FOUND); } } @@ -212,13 +129,13 @@ class VoteController extends Controller { */ public function setByToken($option, $setTo, $token) { try { - $this->acl->setToken($token); + return new DataResponse($this->voteService->set(null, $option['pollOptionText'], $setTo, $token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); } - return $this->set($this->acl->getPollId(), $option, $this->acl->getUserId(), $setTo); - } /** @@ -231,15 +148,14 @@ class VoteController extends Controller { * @return DataResponse */ public function getByToken($token) { - try { - $this->acl->setToken($token); + return new DataResponse($this->voteService->list(null, $token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse('No votes', Http::STATUS_NOT_FOUND); } - return $this->get($this->acl->getPollId()); - } } diff --git a/lib/Db/OptionMapper.php b/lib/Db/OptionMapper.php index 07f024d9..3044c90c 100644 --- a/lib/Db/OptionMapper.php +++ b/lib/Db/OptionMapper.php @@ -75,6 +75,28 @@ class OptionMapper extends QBMapper { return $this->findEntities($qb); } + /** + * @param int $pollId + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @return array + */ + + public function findByPollAndText($pollId, $pollOptionText) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) + ) + ->andWhere( + $qb->expr()->eq('poll_option_text', $qb->createNamedParameter($pollOptionText, IQueryBuilder::PARAM_STR)) + ) + ->orderBy('order', 'ASC'); + + return $this->findEntity($qb); + } + /** * @param int $optionId */ diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php new file mode 100644 index 00000000..7f096b83 --- /dev/null +++ b/lib/Service/VoteService.php @@ -0,0 +1,169 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Service; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; + +use OCP\IGroupManager; +use OCP\ILogger; + + +use OCA\Polls\Db\Vote; +use OCA\Polls\Db\VoteMapper; +use OCA\Polls\Db\OptionMapper; +use OCA\Polls\Service\AnonymizeService; +use OCA\Polls\Service\LogService; +use OCA\Polls\Model\Acl; + +class VoteService { + + private $userId; + private $logger; + private $vote; + private $voteMapper; + private $optionMapper; + private $groupManager; + private $anonymizer; + private $logService; + private $acl; + + /** + * VoteController constructor. + * @param string $appName + * @param $userId + * @param ILogger $logger + * @param Vote $vote + * @param VoteMapper $voteMapper + * @param OptionMapper $optionMapper + * @param IGroupManager $groupManager + * @param AnonymizeService $anonymizer + * @param LogService $logService + * @param Acl $acl + */ + public function __construct( + string $appName, + $userId, + ILogger $logger, + VoteMapper $voteMapper, + OptionMapper $optionMapper, + Vote $vote, + IGroupManager $groupManager, + AnonymizeService $anonymizer, + LogService $logService, + Acl $acl + ) { + $this->userId = $userId; + $this->vote = $vote; + $this->voteMapper = $voteMapper; + $this->optionMapper = $optionMapper; + $this->logger = $logger; + $this->groupManager = $groupManager; + $this->anonymizer = $anonymizer; + $this->logService = $logService; + $this->acl = $acl; + } + + /** + * Get all votes of given poll + * Read all votes of a poll based on the poll id and return list as array + * @NoAdminRequired + * @param integer $pollId + * @param string $token + * @return DataResponse + */ + public function list($pollId = 0, $token = '') { + if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) { + throw new NotAuthorizedException; + } + + if (!$this->acl->getAllowSeeResults()) { + return $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId()); + } elseif (!$this->acl->getAllowSeeUsernames()) { + $this->anonymizer->set($pollId, $this->acl->getUserId()); + return $this->anonymizer->getVotes(); + } else { + return $this->voteMapper->findByPoll($pollId); + } + } + + /** + * set + * @NoAdminRequired + * @param integer $pollId + * @param Array $option + * @param string $setTo + * @param string $token + * @return DataResponse + */ + public function set($pollId = 0, $pollOptionText, $setTo, $token = '') { + + if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowVote()) { + throw new NotAuthorizedException; + } + + $option = $this->optionMapper->findByPollAndText($pollId, $pollOptionText); + + try { + $this->vote = $this->voteMapper->findSingleVote($pollId, $option->getPollOptionText(), $this->acl->getUserId()); + $this->vote->setVoteAnswer($setTo); + $this->voteMapper->update($this->vote); + + } catch (DoesNotExistException $e) { + // Vote does not exist, insert as new Vote + $this->vote = new Vote(); + + $this->vote->setPollId($pollId); + $this->vote->setUserId($this->acl->getUserId()); + $this->vote->setVoteOptionText($option->getPollOptionText()); + $this->vote->setVoteOptionId($option->getId()); + $this->vote->setVoteAnswer($setTo); + $this->voteMapper->insert($this->vote); + + } finally { + $this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId()); + return $this->vote; + } + } + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $voteId + * @param string $userId + * @param integer $pollId + * @return DataResponse + */ + public function delete($pollId, $userId) { + + if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $votes = $this->voteMapper->deleteByPollAndUser($pollId, $userId); + $this->logger->alert('Deleted votes from ' . $userId . ' in poll ' . $pollId); + } + +} -- cgit v1.2.3 From d3b8c7c7028b6c112c230df6c010263dfb1a1130 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 20 Jun 2020 11:38:34 +0200 Subject: Completed API with poll --- lib/Controller/PollApiController.php | 217 +++++++++++++ lib/Controller/PollController.php | 331 ++++++-------------- lib/Controller/SubscriptionController.php | 12 +- lib/Exceptions/EmptyTitleException.php | 40 +++ lib/Exceptions/InvalidAccessException.php | 40 +++ lib/Exceptions/InvalidPollTypeException.php | 40 +++ lib/Exceptions/InvalidShowResultsException.php | 40 +++ lib/Service/OptionService.php | 17 ++ lib/Service/PollService.php | 408 +++++++++++++++++++++++++ 9 files changed, 906 insertions(+), 239 deletions(-) create mode 100644 lib/Controller/PollApiController.php create mode 100644 lib/Exceptions/EmptyTitleException.php create mode 100644 lib/Exceptions/InvalidAccessException.php create mode 100644 lib/Exceptions/InvalidPollTypeException.php create mode 100644 lib/Exceptions/InvalidShowResultsException.php create mode 100644 lib/Service/PollService.php (limited to 'lib') diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php new file mode 100644 index 00000000..777df2ba --- /dev/null +++ b/lib/Controller/PollApiController.php @@ -0,0 +1,217 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + namespace OCA\Polls\Controller; + + use Exception; + use OCP\AppFramework\Db\DoesNotExistException; + use OCA\Polls\Exceptions\EmptyTitleException; + use OCA\Polls\Exceptions\InvalidAccessException; + use OCA\Polls\Exceptions\InvalidShowResultsException; + use OCA\Polls\Exceptions\InvalidPollTypeException; + use OCA\Polls\Exceptions\NotAuthorizedException; + + use OCP\IRequest; + use OCP\ILogger; + use OCP\AppFramework\ApiController; + use OCP\AppFramework\Http; + use OCP\AppFramework\Http\DataResponse; + + use OCA\Polls\Service\PollService; + + class PollApiController extends ApiController { + + private $logger; + private $pollService; + + /** + * PollController constructor. + * @param string $appName + * @param $userId + * @param IRequest $request + * @param ILogger $logger + * @param PollService $pollService + */ + + public function __construct( + string $appName, + IRequest $request, + ILogger $logger, + PollService $pollService + ) { + parent::__construct($appName, $request); + $this->logger = $logger; + $this->pollService = $pollService; + } + + + /** + * list + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @return DataResponse + */ + + public function list() { + try { + return new DataResponse($this->pollService->list(), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + + /** + * get + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return array + */ + public function get($pollId) { + try { + return new DataResponse($this->pollService->get($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function delete($pollId) { + try { + return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * deletePermanently + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function deletePermanently($pollId) { + try { + return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function add($type, $title) { + try { + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function update($pollId, $poll) { + try { + return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * clone + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function clone($pollId) { + try { + return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } + + /** + * enum + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function enum() { + return [ + 'poll' => $this->pollService->getValidEnum() + ]; + } + + +} diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 6bf98137..89446ee0 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -23,86 +23,44 @@ namespace OCA\Polls\Controller; - use Exception; - use OCP\AppFramework\Db\DoesNotExistException; - - use OCP\IRequest; - use OCP\ILogger; - use OCP\AppFramework\Controller; - use OCP\AppFramework\Http; - use OCP\AppFramework\Http\DataResponse; - - use OCP\IUser; - use OCP\Security\ISecureRandom; - - use OCA\Polls\Db\Poll; - use OCA\Polls\Db\PollMapper; - use OCA\Polls\Db\Option; - use OCA\Polls\Db\OptionMapper; - use OCA\Polls\Service\CommentService; - use OCA\Polls\Service\OptionService; - use OCA\Polls\Service\ShareService; - use OCA\Polls\Service\VoteService; - use OCA\Polls\Service\LogService; - use OCA\Polls\Model\Acl; +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\EmptyTitleException; +use OCA\Polls\Exceptions\InvalidAccessException; +use OCA\Polls\Exceptions\InvalidShowResultsException; +use OCA\Polls\Exceptions\InvalidPollTypeException; +use OCA\Polls\Exceptions\NotAuthorizedException; + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCA\Polls\Service\PollService; class PollController extends Controller { - private $userId; - private $pollMapper; - private $optionMapper; - private $logger; - private $poll; - private $logService; - private $commentService; - private $optionService; - private $shareService; - private $voteService; - private $acl; + private $logger; + private $pollService; /** * PollController constructor. * @param string $appName - * @param $userId * @param IRequest $request * @param ILogger $logger - * @param OptionMapper $optionMapper - * @param PollMapper $pollMapper - * @param LogService $logService - * @param CommentService $commentService - * @param OptionService $optionService - * @param ShareService $shareService - * @param VoteService $voteService - * @param Acl $acl + * @param PollService $pollService */ public function __construct( - string $appName, - $userId, + string $appName, IRequest $request, ILogger $logger, - OptionMapper $optionMapper, - PollMapper $pollMapper, - Poll $poll, - LogService $logService, - CommentService $commentService, - OptionService $optionService, - ShareService $shareService, - VoteService $voteService, - Acl $acl + PollService $pollService ) { parent::__construct($appName, $request); - $this->userId = $userId; - $this->pollMapper = $pollMapper; - $this->optionMapper = $optionMapper; + $this->pollService = $pollService; $this->logger = $logger; - $this->poll = $poll; - $this->logService = $logService; - $this->commentService = $commentService; - $this->optionService = $optionService; - $this->shareService = $shareService; - $this->voteService = $voteService; - $this->acl = $acl; } @@ -114,29 +72,13 @@ */ public function list() { - if (\OC::$server->getUserSession()->isLoggedIn()) { - $pollList = []; - - try { - - $polls = $this->pollMapper->findAll(); - // TODO: Not the elegant way. Improvement neccessary - foreach ($polls as $poll) { - $combinedPoll = (object) array_merge( - (array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId())))); - if ($combinedPoll->allowView) { - $pollList[] = $combinedPoll; - } - } - - return new DataResponse($pollList, Http::STATUS_OK); - } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); - } - } else { - return new DataResponse([], Http::STATUS_OK); + try { + return new DataResponse($this->pollService->list(), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - } @@ -147,117 +89,95 @@ * @param integer $pollId * @return array */ - public function get($pollId) { - - try { - if (!$this->acl->getFoundByToken()) { - $this->acl->setPollId($pollId); - } - - $this->poll = $this->pollMapper->find($pollId); - - if (!$this->acl->getAllowView()) { - return new DataResponse(null, Http::STATUS_UNAUTHORIZED); - } - - return new DataResponse([ - 'acl' => $this->acl, - 'poll' => $this->poll, - 'comments' => $this->commentService->list($pollId), - 'options' => $this->optionService->list($pollId), - 'shares' => $this->shareService->list($pollId), - 'votes' => $this->voteService->list($pollId) - ], Http::STATUS_OK); - + public function get($pollId, $token) { + try { + return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK); } catch (DoesNotExistException $e) { - $this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']); - return new DataResponse(null, Http::STATUS_NOT_FOUND); - } + return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } } /** - * getByToken - * Read all options of a poll based on a share token and return list as array + * delete * @NoAdminRequired - * @PublicPage * @NoCSRFRequired - * @param string $token + * @param Array $poll * @return DataResponse */ - public function getByToken($token) { + + public function delete($pollId) { try { - return $this->get($this->acl->setToken($token)->getPollId()); + return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - } /** - * delete + * deletePermanently * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function delete($pollId) { - + public function deletePermanently($pollId) { try { - // Find existing poll - $this->poll = $this->pollMapper->find($pollId); - $this->acl->setPollId($this->poll->getId()); - - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } + return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } + } - if ($this->poll->getDeleted()) { - $this->poll->setDeleted(0); - } else { - $this->poll->setDeleted(time()); - } - $this->pollMapper->update($this->poll); - $this->logService->setLog($this->poll->getId(), 'deletePoll'); - return new DataResponse(['deleted' => $pollId], Http::STATUS_OK); + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + public function add($type, $title) { + try { + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * deletePermanently + * write * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function deletePermanently($pollId) { - + public function update($pollId, $poll) { + $this->logger->alert(json_encode($poll)); try { - // Find existing poll - $this->poll = $this->pollMapper->find($pollId); - $this->acl->setPollId($this->poll->getId()); - - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } - - if (!$this->poll->getDeleted()) { - $this->logger->alert('user ' . $this->userId . ' trying to permanently delete active poll'); - return new DataResponse(['message' => 'Permanent deletion of active poll.'], Http::STATUS_CONFLICT); - } - - $this->pollMapper->delete($this->poll); - return new DataResponse([], Http::STATUS_OK); - - } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -265,50 +185,24 @@ * write * @NoAdminRequired * @NoCSRFRequired + * @depicated * @param Array $poll * @return DataResponse */ public function write($poll) { - try { - // Find existing poll - $this->poll = $this->pollMapper->find($poll['id']); - $this->acl->setPollId($this->poll->getId()); - if (!$this->acl->getAllowEdit()) { - $this->logger->alert('Unauthorized write attempt from user ' . $this->userId); - return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); - } - - } catch (Exception $e) { - $this->poll = new Poll(); - - $this->poll->setType($poll['type']); - $this->poll->setOwner($this->userId); - $this->poll->setCreated(time()); - } finally { - $this->poll->setTitle($poll['title']); - $this->poll->setDescription($poll['description']); - $this->poll->setAccess($poll['access']); - $this->poll->setExpire($poll['expire']); - $this->poll->setAnonymous(intval($poll['anonymous'])); - $this->poll->setFullAnonymous(0); - $this->poll->setAllowMaybe(intval($poll['allowMaybe'])); - $this->poll->setVoteLimit(intval($poll['voteLimit'])); - $this->poll->setSettings(''); - $this->poll->setOptions(''); - $this->poll->setShowResults($poll['showResults']); - $this->poll->setDeleted($poll['deleted']); - $this->poll->setAdminAccess($poll['adminAccess']); - - if ($this->poll->getId() > 0) { - $this->pollMapper->update($this->poll); - $this->logService->setLog($this->poll->getId(), 'updatePoll'); - } else { - $this->pollMapper->insert($this->poll); - $this->logService->setLog($this->poll->getId(), 'addPoll'); - } - return $this->get($this->poll->getId()); + return new DataResponse($this->pollService->write($poll), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -320,40 +214,13 @@ * @return DataResponse */ public function clone($pollId) { - $this->poll = $this->pollMapper->find($pollId); - - $clonePoll = new Poll(); - $clonePoll->setOwner($this->userId); - $clonePoll->setCreated(time()); - $clonePoll->setTitle('Clone of ' . $this->poll->getTitle()); - $clonePoll->setDeleted(0); - - $clonePoll->setType($this->poll->getType()); - $clonePoll->setDescription($this->poll->getDescription()); - $clonePoll->setAccess($this->poll->getAccess()); - $clonePoll->setExpire($this->poll->getExpire()); - $clonePoll->setAnonymous(intval($this->poll->getAnonymous())); - $clonePoll->setFullAnonymous(0); - $clonePoll->setAllowMaybe(intval($this->poll->getAllowMaybe())); - $clonePoll->setVoteLimit(intval($this->poll->getVoteLimit())); - $clonePoll->setSettings(''); - $clonePoll->setOptions(''); - $clonePoll->setShowResults($this->poll->getShowResults()); - $clonePoll->setAdminAccess($this->poll->getAdminAccess()); - - $this->pollMapper->insert($clonePoll); - $this->logService->setLog($clonePoll->getId(), 'addPoll'); - - foreach ($this->optionMapper->findByPoll($pollId) as $option) { - $newOption = new Option(); - $newOption->setPollId($clonePoll->getId()); - $newOption->setPollOptionText($option->getPollOptionText()); - $newOption->setTimestamp($option->getTimestamp()); - - $this->optionMapper->insert($newOption); + try { + return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - return new DataResponse(['pollId' => $clonePoll->getId()], Http::STATUS_OK); - } } diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index d201fa6e..f3cc655a 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -28,25 +28,23 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\IRequest; use OCP\ILogger; - use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCA\Polls\Db\Subscription; -use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Service\SubscriptionService; class SubscriptionController extends Controller { private $userId; - private $mapper; + private $subscriptionService; private $logger; /** * SubscriptionController constructor. * @param string $appName * @param $UserId - * @param SubscriptionMapper $mapper + * @param SubscriptionService $subscriptionService * @param IRequest $request * @param ILogger $logger */ @@ -54,14 +52,14 @@ class SubscriptionController extends Controller { public function __construct( string $appName, $userId, - SubscriptionMapper $mapper, + SubscriptionService $subscriptionService, IRequest $request, ILogger $logger ) { parent::__construct($appName, $request); $this->userId = $userId; - $this->mapper = $mapper; + $this->subscriptionService = $subscriptionService; $this->logger = $logger; } diff --git a/lib/Exceptions/EmptyTitleException.php b/lib/Exceptions/EmptyTitleException.php new file mode 100644 index 00000000..acfc4c57 --- /dev/null +++ b/lib/Exceptions/EmptyTitleException.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class EmptyTitleException extends \Exception { + /** + * EmptyTitleException Constructor + * @param string $e exception message + */ + public function __construct($message = 'Poll title must not be empty') { + parent::__construct($message); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Exceptions/InvalidAccessException.php b/lib/Exceptions/InvalidAccessException.php new file mode 100644 index 00000000..c5bfd69a --- /dev/null +++ b/lib/Exceptions/InvalidAccessException.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class InvalidAccessException extends \Exception { + /** + * InvalidAccessException Constructor + * @param string $e exception message + */ + public function __construct($message = 'Invalid access value') { + parent::__construct($message); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Exceptions/InvalidPollTypeException.php b/lib/Exceptions/InvalidPollTypeException.php new file mode 100644 index 00000000..bef690f3 --- /dev/null +++ b/lib/Exceptions/InvalidPollTypeException.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class InvalidPollTypeException extends \Exception { + /** + * InvalidPollTypeException Constructor + * @param string $e exception message + */ + public function __construct($message = 'Invalid pollType value') { + parent::__construct($message); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Exceptions/InvalidShowResultsException.php b/lib/Exceptions/InvalidShowResultsException.php new file mode 100644 index 00000000..67b18a49 --- /dev/null +++ b/lib/Exceptions/InvalidShowResultsException.php @@ -0,0 +1,40 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Exceptions; + +use OCP\AppFramework\Http; + +class InvalidShowResultsException extends \Exception { + /** + * InvalidShowResultsException Constructor + * @param string $e exception message + */ + public function __construct($message = 'Invalid showResults value') { + parent::__construct($message); + } + public function getStatus() { + return Http::STATUS_CONFLICT; + } + +} diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 0d0e7be4..e7f485aa 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -214,4 +214,21 @@ class OptionService { return $this->get($pollId); } + + /** + * Set order by order of the given array + * @NoAdminRequired + * @param array $options + * @return array Array of Option objects + */ + public function clone($fromPollId, $toPollId) { + + foreach ($this->optionMapper->findByPoll($fromPollId) as $option) { + $option->setPollId($toPollId); + $this->optionMapper->insert($option); + } + + return $this->optionMapper->findByPoll($toPollId); + + } } diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php new file mode 100644 index 00000000..ecf35b18 --- /dev/null +++ b/lib/Service/PollService.php @@ -0,0 +1,408 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + namespace OCA\Polls\Service; + + use Exception; + use OCP\AppFramework\Db\DoesNotExistException; + use OCA\Polls\Exceptions\EmptyTitleException; + use OCA\Polls\Exceptions\InvalidAccessException; + use OCA\Polls\Exceptions\InvalidShowResultsException; + use OCA\Polls\Exceptions\InvalidPollTypeException; + use OCA\Polls\Exceptions\NotAuthorizedException; + + use OCP\ILogger; + + use OCA\Polls\Db\Poll; + use OCA\Polls\Db\PollMapper; + use OCA\Polls\Service\CommentService; + use OCA\Polls\Service\OptionService; + use OCA\Polls\Service\ShareService; + use OCA\Polls\Service\VoteService; + use OCA\Polls\Service\LogService; + use OCA\Polls\Model\Acl; + + class PollService { + + private $logger; + private $userid; + private $pollMapper; + private $poll; + private $logService; + private $commentService; + private $optionService; + private $shareService; + private $voteService; + private $acl; + + /** + * PollController constructor. + * @param string $appName + * @param $userId + * @param PollMapper $pollMapper + * @param LogService $logService + * @param CommentService $commentService + * @param OptionService $optionService + * @param ShareService $shareService + * @param VoteService $voteService + * @param Acl $acl + */ + + public function __construct( + string $appName, + $userId, + ILogger $logger, + PollMapper $pollMapper, + Poll $poll, + LogService $logService, + CommentService $commentService, + OptionService $optionService, + ShareService $shareService, + VoteService $voteService, + Acl $acl + ) { + $this->userId = $userId; + $this->logger = $logger; + $this->pollMapper = $pollMapper; + $this->poll = $poll; + $this->logService = $logService; + $this->commentService = $commentService; + $this->optionService = $optionService; + $this->shareService = $shareService; + $this->voteService = $voteService; + $this->acl = $acl; + } + + + /** + * list + * @NoAdminRequired + * @return DataResponse + */ + + public function list() { + if (!\OC::$server->getUserSession()->isLoggedIn()) { + throw new NotAuthorizedException; + } + + $polls = $this->pollMapper->findAll(); + // TODO: Not the elegant way. Improvement neccessary + foreach ($polls as $poll) { + $combinedPoll = (object) array_merge( + (array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId())))); + if ($combinedPoll->allowView) { + $pollList[] = $combinedPoll; + } + } + + return $pollList; + } + + /** + * get + * @NoAdminRequired + * @param integer $pollId + * @return array + */ + public function get($pollId = 0, $token = '') { + $this->poll = $this->pollMapper->find($pollId); + if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) { + throw new NotAuthorizedException; + } + + return [ + 'acl' => $this->acl, + 'poll' => $this->poll, + 'comments' => $this->commentService->list($pollId, $token), + 'options' => $this->optionService->list($pollId, $token), + 'shares' => $this->shareService->list($pollId, $token), + 'votes' => $this->voteService->list($pollId, $token) + ]; + + } + + /** + * delete + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return Poll + */ + + public function delete($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { + throw new NotAuthorizedException; + } + if ($this->poll->getDeleted()) { + $this->poll->setDeleted(0); + } else { + $this->poll->setDeleted(time()); + } + + $this->poll = $this->pollMapper->update($this->poll); + $this->logService->setLog($this->poll->getId(), 'deletePoll'); + return $this->poll; + } + + /** + * deletePermanently + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return Poll + */ + + public function deletePermanently($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) { + throw new NotAuthorizedException; + } + return $this->pollMapper->delete($this->poll); + // return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function add($type, $title) { + if (!\OC::$server->getUserSession()->isLoggedIn()) { + throw new NotAuthorizedException; + } + $this->logger->alert(json_encode($type)); + $this->logger->alert(json_encode($title)); + + // Validate valuess + if (!in_array($type, $this->getValidPollType())) { + throw new InvalidPollTypeException('Invalid poll type'); + } + + if (!$title) { + throw new EmptyTitleException('Title must not be empty'); + } + + $this->poll = new Poll(); + $this->poll->setType($type); + $this->poll->setCreated(time()); + $this->poll->setOwner($this->userId); + $this->poll->setTitle($title); + $this->poll->setDescription(''); + $this->poll->setAccess('hidden'); + $this->poll->setExpire(0); + $this->poll->setAnonymous(0); + $this->poll->setFullAnonymous(0); + $this->poll->setAllowMaybe(0); + $this->poll->setVoteLimit(0); + $this->poll->setSettings(''); + $this->poll->setOptions(''); + $this->poll->setShowResults('always'); + $this->poll->setDeleted(0); + $this->poll->setAdminAccess(0); + $this->poll = $this->pollMapper->insert($this->poll); + + $this->logService->setLog($this->poll->getId(), 'addPoll'); + $this->logger->alert(json_encode($this->poll)); + + return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @depricated + * @param Array $poll + * @return DataResponse + */ + + public function write($poll, $pollId = null) { + + if (!$pollId) { + $pollId = $poll['id']; + } + + // Validate valuess + if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { + throw new InvalidShowResultsException('Invalid value for prop showResults'); + } + + if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) { + throw new InvalidAccessException('Invalid value for prop access'); + } + + if (isset($poll['title']) && !$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + try { + // find pollId + $this->poll = $this->pollMapper->find($pollId); + $this->logService->setLog($this->poll->getId(), 'updatePoll'); + + + } catch (DoesNotExistException $e) { + // if not found create a new poll + + // Validate valuess + if (!in_array($poll['type'], $this->getValidPollType())) { + throw new InvalidPollTypeException('Invalid poll type'); + } + + if (!$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + + $this->poll = new Poll(); + $this->poll->setType($poll['type']); + $this->poll->setOwner($this->userId); + $this->poll->setTitle($poll['title']); + $this->poll->setCreated(time()); + $this->poll = $this->pollMapper->insert($this->poll); + + $this->logService->setLog($this->poll->getId(), 'addPoll'); + } + + if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + $this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle()); + $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); + $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); + $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); + $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); + $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); + $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); + $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); + $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); + $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + + $this->poll->setFullAnonymous(0); + $this->poll->setVoteLimit(0); + $this->poll->setSettings(''); + $this->poll->setOptions(''); + + $this->pollMapper->update($this->poll); + + return $this->poll; + } + + /** + * write + * @NoAdminRequired + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function update($pollId, $poll) { + + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { + throw new NotAuthorizedException; + } + + // Validate valuess + if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { + throw new InvalidShowResultsException('Invalid value for prop showResults'); + } + + if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) { + throw new InvalidAccessException('Invalid value for prop access '. $poll['access']); + } + + if (isset($poll['title']) && !$poll['title']) { + throw new EmptyTitleException('Title must not be empty'); + } + + $this->poll->setTitle($poll['title'] ? $poll['title'] : $this->poll->getTitle()); + $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); + $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); + $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); + $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); + $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); + $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); + $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); + $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); + $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + + $this->pollMapper->update($this->poll); + $this->logService->setLog($this->poll->getId(), 'updatePoll'); + + return $this->poll; + } + + /** + * clone + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function clone($pollId) { + $this->poll = $this->pollMapper->find($pollId); + + $this->poll->setCreated(time()); + $this->poll->setOwner($this->userId); + $this->poll->setTitle('Clone of ' . $this->poll->getTitle()); + $this->poll->setDeleted(0); + $this->poll->setId(0); + + $this->poll = $this->pollMapper->insert($this->poll); + $this->logService->setLog($clonePoll->getId(), 'addPoll'); + + $this->optionService->clone($pollId, $this->poll->getId()); + + return $this->poll; + + } + + public function getValidEnum() { + return [ + 'pollType' => $this->getValidPollType(), + 'access' => $this->getValidAccess(), + 'showResults' => $this->getValidShowResults() + ]; + } + + private function getValidPollType() { + return ['datePoll', 'textPoll']; + } + + private function getValidAccess() { + return ['hidden', 'public']; + } + + private function getValidShowResults() { + return ['always', 'expired', 'never']; + } +} -- cgit v1.2.3 From feb0fc3e16bde4d0fb0871e50a6c60eca83483d2 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 20 Jun 2020 13:15:48 +0200 Subject: Updated API and code polish, tidy --- lib/Controller/CommentApiController.php | 46 ++++------------ lib/Controller/CommentController.php | 4 +- lib/Controller/OptionApiController.php | 56 +++++++------------- lib/Controller/OptionController.php | 3 +- lib/Controller/PollApiController.php | 56 ++++++++++---------- lib/Controller/ShareApiController.php | 79 ++++++++++------------------ lib/Controller/ShareController.php | 8 +-- lib/Controller/SubscriptionApiController.php | 8 +-- lib/Controller/VoteApiController.php | 30 ++--------- lib/Service/CommentService.php | 2 +- 10 files changed, 102 insertions(+), 190 deletions(-) (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index fbee1078..9d93b83b 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -69,37 +69,17 @@ class CommentApiController extends ApiController { * Read all comments of a poll based on the poll id and return list as array * @NoAdminRequired * @CORS - * @PublicPage * @NoCSRFRequired * @param integer $pollId * @return DataResponse */ - public function list($pollId, $token = '') { + public function list($pollId) { try { - return new DataResponse($this->commentService->list($pollId, $token), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); - } - } - - /** - * Read all comments of a poll based on a share token and return list as array - * @NoAdminRequired - * @CORS - * @NoCSRFRequired - * @PublicPage - * @param string $token - * @return DataResponse - */ - public function getByToken($token) { - try { - return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -108,19 +88,17 @@ class CommentApiController extends ApiController { * @NoAdminRequired * @CORS * @NoCSRFRequired - * @PublicPage * @param int $pollId * @param string $message - * @param string $token * @return DataResponse */ - public function add($message, $pollId, $token) { + public function add($pollId, $message) { try { - return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_CREATED); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse($this->commentService->add($message, $pollId), Http::STATUS_CREATED); } catch (DoesNotExistException $e) { return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -129,19 +107,17 @@ class CommentApiController extends ApiController { * @NoAdminRequired * @CORS * @NoCSRFRequired - * @PublicPage * @param int $commentId - * @param string $token * @return DataResponse */ - public function delete($commentId, $token) { + public function delete($commentId) { try { - $this->commentService->delete($commentId, $token); + $this->commentService->delete($commentId); return new DataResponse($commentId, Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 70403abd..e940bfb7 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -88,9 +88,9 @@ class CommentController extends Controller { * @param string $token * @return DataResponse */ - public function add($message, $pollId, $token) { + public function add($pollId, $message, $token) { try { - return new DataResponse($this->commentService->add($message, $pollId, $token), Http::STATUS_OK); + return new DataResponse($this->commentService->add($pollId, $message, $token), Http::STATUS_OK); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_UNAUTHORIZED); } diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php index ded3933e..f23fd350 100644 --- a/lib/Controller/OptionApiController.php +++ b/lib/Controller/OptionApiController.php @@ -66,44 +66,27 @@ class OptionApiController extends ApiController { * @CORS * @NoCSRFRequired * @param integer $pollId - * @return array Array of Option objects + * @return DataResponse */ public function list($pollId) { try { return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); - } - } - - - /** - * getByToken - * Read all options of a poll based on a share token and return list as array - * @NoAdminRequired - * @PublicPage - * @NoCSRFRequired - * @param string $token - * @return DataResponse - */ - public function getByToken($token) { - try { - return new DataResponse($this->optionService->get(0, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('Poll with token ' . $token . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse($e->getMessage(), $e->getStatus()); } } + /** * Add a new Option to poll * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param Option $option + * @param integer $pollId + * @param string $pollOptionText + * @param integer $timestamp * @return DataResponse */ public function add($pollId, $pollOptionText = '', $timestamp = 0) { @@ -115,46 +98,47 @@ class OptionApiController extends ApiController { try { return new DataResponse($this->optionService->add($option), Http::STATUS_CREATED); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); } catch (UniqueConstraintViolationException $e) { return new DataResponse('Option exists', Http::STATUS_CONFLICT); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } + /** - * Remove a single option + * Update poll option * @NoAdminRequired * @CORS * @NoCSRFRequired * @param Option $option * @return DataResponse */ - public function delete($optionId) { + public function update($option) { try { - return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); + return new DataResponse($this->optionService->update($option), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND); + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * Update poll option + * Remove a single option * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param Option $option + * @param integer $optionId * @return DataResponse */ - public function update($option) { + public function delete($optionId) { try { - return new DataResponse($this->optionService->update($option), Http::STATUS_OK); + return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse($e->getMessage(), $e->getStatus()); } } } diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 7afa4a08..2b488c17 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -59,7 +59,7 @@ class OptionController extends Controller { * @NoAdminRequired * @NoCSRFRequired * @param integer $pollId - * @return array Array of Option objects + * @return DataResponse */ public function list($pollId) { return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); @@ -116,6 +116,7 @@ class OptionController extends Controller { * Set order by order of the given array * @NoAdminRequired * @NoCSRFRequired + * @param integer $pollId * @param Array $options * @return DataResponse */ diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index 777df2ba..98d8ef8f 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -102,84 +102,84 @@ } /** - * delete + * write * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function delete($pollId) { + public function add($type, $title) { try { - return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); - } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * deletePermanently + * write * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function deletePermanently($pollId) { + public function update($pollId, $poll) { try { - return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } - } /** - * write + * delete * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function add($type, $title) { + public function delete($pollId) { try { - return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); + return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (InvalidPollTypeException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * write + * deletePermanently * @NoAdminRequired * @NoCSRFRequired * @param Array $poll * @return DataResponse */ - public function update($pollId, $poll) { + public function deletePermanently($pollId) { try { - return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (InvalidAccessException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (InvalidShowResultsException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); } + } /** @@ -191,7 +191,7 @@ */ public function clone($pollId) { try { - return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + return new DataResponse($this->pollService->clone($pollId), Http::STATUS_CREATED); } catch (DoesNotExistException $e) { return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -208,9 +208,7 @@ */ public function enum() { - return [ - 'poll' => $this->pollService->getValidEnum() - ]; + return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK); } diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index bc04e376..968ff115 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -62,27 +62,7 @@ class ShareApiController extends ApiController { } /** - * getByToken - * Get pollId by token - * @NoAdminRequired - * @NoCSRFRequired - * @CORS - * @PublicPage - * @param string $token - * @return DataResponse - */ - public function get($token) { - try { - return new DataResponse($this->shareService->get($token), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); - } - } - - /** - * get + * list * Read all shares of a poll based on the poll id and return list as array * @NoAdminRequired * @CORS @@ -93,65 +73,60 @@ class ShareApiController extends ApiController { public function list($pollId) { try { return new DataResponse($this->shareService->list($pollId), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** - * Write a new share to the db and returns the new share as array - * @NoAdminRequired - * @CORS - * @NoCSRFRequired - * @param int $pollId - * @param string $message - * @return DataResponse - */ - public function add($pollId, $type, $userId = '', $userEmail = '') { + * get share by token + * Get pollId by token + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * @param string $token + * @return DataResponse + */ + public function get($token) { try { - return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED); + return new DataResponse($this->shareService->get($token), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (\Exception $e) { - return new DataResponse($e, Http::STATUS_CONFLICT); + return new DataResponse($e->getMessage(), $e->getStatus()); } - } /** - * createPersonalShare * Write a new share to the db and returns the new share as array * @NoAdminRequired * @CORS - * @PublicPage * @NoCSRFRequired * @param int $pollId - * @param string $message + * @param string $type + * @param string $userId + * @param string $userEmail * @return DataResponse */ - public function createPersonalShare($token, $userName) { - + public function add($pollId, $type, $userId = '', $userEmail = '') { try { - return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED); + return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED); + } catch (\Exception $e) { + return new DataResponse($e, Http::STATUS_CONFLICT); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (InvalidUsername $e) { - return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT); - } catch (DoesNotExistException $e) { - // return forbidden in all not catched error cases - return new DataResponse($e, Http::STATUS_FORBIDDEN); + return new DataResponse($e->getMessage(), $e->getStatus()); } + } /** - * remove - * remove share + * delete share * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param Share $share + * @param string $token * @return DataResponse */ diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index c2e769ad..da80acb5 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -69,7 +69,7 @@ class ShareController extends Controller { /** * getByToken - * Get pollId by token + * Get poll * @NoAdminRequired * @NoCSRFRequired * @PublicPage @@ -109,7 +109,7 @@ class ShareController extends Controller { * @NoAdminRequired * @NoCSRFRequired * @param int $pollId - * @param string $message + * @param Array $share * @return DataResponse */ public function write($pollId, $share) { @@ -135,8 +135,8 @@ class ShareController extends Controller { * @NoAdminRequired * @PublicPage * @NoCSRFRequired - * @param int $pollId - * @param string $message + * @param string $token + * @param string $userName * @return DataResponse */ public function createPersonalShare($token, $userName) { diff --git a/lib/Controller/SubscriptionApiController.php b/lib/Controller/SubscriptionApiController.php index b89f009c..e8c1acfa 100644 --- a/lib/Controller/SubscriptionApiController.php +++ b/lib/Controller/SubscriptionApiController.php @@ -78,10 +78,10 @@ class SubscriptionApiController extends ApiController { public function get($pollId) { try { return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -96,7 +96,7 @@ class SubscriptionApiController extends ApiController { return $this->subscriptionService->set($pollId, true); return new DataResponse('Subscribed', Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse($e->getMessage(), $e->getStatus()); } } /** @@ -110,7 +110,7 @@ class SubscriptionApiController extends ApiController { $this->subscriptionService->set($pollId, false); return new DataResponse('Unsubscribed', Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse($e->getMessage(), $e->getStatus()); } } } diff --git a/lib/Controller/VoteApiController.php b/lib/Controller/VoteApiController.php index 71c42966..8f5b77ac 100644 --- a/lib/Controller/VoteApiController.php +++ b/lib/Controller/VoteApiController.php @@ -73,10 +73,10 @@ class VoteApiController extends ApiController { public function list($pollId) { try { return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('No votes', Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse($e->getMessage(), $e->getStatus()); } } @@ -94,33 +94,11 @@ class VoteApiController extends ApiController { public function set($pollId, $pollOptionText, $setTo) { try { return new DataResponse($this->voteService->set($pollId, $pollOptionText, $setTo), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); - } - - } - - - /** - * delete - * @NoAdminRequired - * @NoCSRFRequired - * @CORS - * @param integer $voteId - * @param string $userId - * @param integer $pollId - * @return DataResponse - */ - public function delete($pollId, $userId) { - try { - return new DataResponse($this->voteService->delete($userId, $pollId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('', Http::STATUS_NOT_FOUND); + return new DataResponse($e->getMessage(), $e->getStatus()); } - } + } } diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index d939ec18..6a2b6d1d 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -113,7 +113,7 @@ class CommentService { * @param string $token * @return Comment */ - public function add($message, $pollId = 0, $token = '') { + public function add($pollId = 0, $message, $token = '') { $this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")'); if (!$this->acl->checkAuthorize($pollId, $token)) { -- cgit v1.2.3 From 0d632d59b202f55da389db28b870b8c64416c00f Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 21 Jun 2020 18:55:23 +0200 Subject: fixes and some code polish --- lib/Controller/PollController.php | 2 +- lib/Controller/VoteController.php | 6 +- lib/Model/Acl.php | 15 +++ lib/Service/CommentService.php | 38 ++----- lib/Service/OptionService.php | 49 +++------ lib/Service/PollService.php | 216 ++++++++++++++++++++++---------------- lib/Service/ShareService.php | 44 +++----- lib/Service/VoteService.php | 47 +++------ 8 files changed, 198 insertions(+), 219 deletions(-) (limited to 'lib') diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 89446ee0..c30c8c32 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -86,6 +86,7 @@ use OCA\Polls\Service\PollService; * get * @NoAdminRequired * @NoCSRFRequired + * @PublicPage * @param integer $pollId * @return array */ @@ -165,7 +166,6 @@ use OCA\Polls\Service\PollService; */ public function update($pollId, $poll) { - $this->logger->alert(json_encode($poll)); try { return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); } catch (DoesNotExistException $e) { diff --git a/lib/Controller/VoteController.php b/lib/Controller/VoteController.php index 7205c14f..64dc8501 100644 --- a/lib/Controller/VoteController.php +++ b/lib/Controller/VoteController.php @@ -26,6 +26,7 @@ namespace OCA\Polls\Controller; // use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCP\ILogger; use OCP\IRequest; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -37,6 +38,7 @@ use OCA\Polls\Service\VoteService; class VoteController extends Controller { private $voteService; + private $logger; /** * VoteController constructor. @@ -48,10 +50,12 @@ class VoteController extends Controller { */ public function __construct( string $appName, + ILogger $logger, IRequest $request, VoteService $voteService ) { parent::__construct($appName, $request); + $this->logger = $logger; $this->voteService = $voteService; } @@ -129,7 +133,7 @@ class VoteController extends Controller { */ public function setByToken($option, $setTo, $token) { try { - return new DataResponse($this->voteService->set(null, $option['pollOptionText'], $setTo, $token), Http::STATUS_OK); + return new DataResponse($this->voteService->set(0, $option['pollOptionText'], $setTo, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); } catch (DoesNotExistException $e) { diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 12fbc741..ebc998f2 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -137,6 +137,21 @@ class Acl implements JsonSerializable { } + /** + * @NoAdminRequired + * @return boolean + */ + public function setPollIdOrToken($pollId = 0, $token = '') { + + if ($token) { + $this->setToken($token); + } elseif ($pollId) { + $this->setPollId($pollId); + } + + return $this; + } + /** * @NoAdminRequired * @return boolean diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index 6a2b6d1d..8ae7ef1e 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -24,16 +24,11 @@ namespace OCA\Polls\Service; use \Exception; - -use OCP\IGroupManager; use OCP\ILogger; use OCA\Polls\Exceptions\NotAuthorizedException; - use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; use OCA\Polls\Model\Acl; use OCA\Polls\Service\AnonymizeService; @@ -41,41 +36,30 @@ use OCA\Polls\Service\AnonymizeService; class CommentService { - private $userId; private $comment; private $commentMapper; private $logger; - private $groupManager; - private $pollMapper; private $anonymizer; private $acl; /** * CommentService constructor. - * @param string $appName - * @param $UserId + * @param ILogger $logger * @param CommentMapper $commentMapper - * @param IGroupManager $groupManager - * @param PollMapper $pollMapper + * @param Comment $comment * @param AnonymizeService $anonymizer * @param Acl $acl */ public function __construct( - string $appName, - $userId, ILogger $logger, CommentMapper $commentMapper, - IGroupManager $groupManager, - PollMapper $pollMapper, + Comment $comment, AnonymizeService $anonymizer, Acl $acl ) { - $this->userId = $userId; $this->commentMapper = $commentMapper; $this->logger = $logger; - $this->groupManager = $groupManager; - $this->pollMapper = $pollMapper; $this->anonymizer = $anonymizer; $this->acl = $acl; } @@ -89,9 +73,8 @@ class CommentService { * @return Array */ public function list($pollId = 0, $token = '') { - $this->logger->debug('call commentService->get(' . $pollId . ', '. $token . ')'); - if (!$this->acl->checkAuthorize($pollId, $token)) { + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { throw new NotAuthorizedException; } @@ -101,8 +84,6 @@ class CommentService { } else { return $this->commentMapper->findByPoll($this->acl->getPollId()); } - - } /** @@ -114,9 +95,8 @@ class CommentService { * @return Comment */ public function add($pollId = 0, $message, $token = '') { - $this->logger->debug('call commentService->write("' . $message . '", ' .$pollId . ', "' .$token . '")'); - if (!$this->acl->checkAuthorize($pollId, $token)) { + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowComment()) { throw new NotAuthorizedException; } @@ -137,6 +117,7 @@ class CommentService { $this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e); throw new NotAuthorizedException($e); } + } /** @@ -148,14 +129,13 @@ class CommentService { * @return Comment */ public function delete($commentId, $token = '') { - - $this->logger->debug('call commentService->delete(' . $commentId . ', "' .$token . '")'); $this->comment = $this->commentMapper->find($commentId); - if (!$this->acl->checkAuthorize($this->comment->getPollId(), $token) || $this->comment->getUserId() !== $this->acl->getUserId()) { + + if ($this->acl->setPollIdOrToken($this->comment->getPollId(), $token)->getUserId() !== $this->acl->getUserId()) { throw new NotAuthorizedException; } - $this->commentMapper->delete($this->comment); + $this->commentMapper->delete($this->comment); return $this->comment; } diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index e7f485aa..139c8267 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -23,15 +23,9 @@ namespace OCA\Polls\Service; -use \Exception; - -use OCP\IGroupManager; -use OCP\ILogger; +use Exception; use OCA\Polls\Exceptions\NotAuthorizedException; - -use OCA\Polls\Db\Poll; -use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Option; use OCA\Polls\Db\OptionMapper; use OCA\Polls\Service\LogService; @@ -39,45 +33,28 @@ use OCA\Polls\Model\Acl; class OptionService { - private $userId; private $optionMapper; private $options; private $option; - private $groupManager; - private $pollMapper; - private $logger; private $logService; private $acl; /** * OptionController constructor. - * @param string $appName - * @param $userId - * @param ILogger $logger * @param OptionMapper $optionMapper - * @param IGroupManager $groupManager - * @param PollMapper $pollMapper + * @param Option $option * @param LogService $logService * @param Acl $acl */ public function __construct( - string $appName, - $userId, OptionMapper $optionMapper, Option $option, - IGroupManager $groupManager, - PollMapper $pollMapper, - ILogger $logger, LogService $logService, Acl $acl ) { - $this->userId = $userId; $this->optionMapper = $optionMapper; $this->option = $option; - $this->groupManager = $groupManager; - $this->pollMapper = $pollMapper; - $this->logger = $logger; $this->logService = $logService; $this->acl = $acl; } @@ -117,13 +94,13 @@ class OptionService { * @return array Array of Option objects */ public function list($pollId = 0, $token = '') { - $this->logger->debug('call optionService->list(' . $pollId . ', '. $token . ')'); - if (!$this->acl->checkAuthorize($pollId, $token)) { + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { throw new NotAuthorizedException; } return $this->optionMapper->findByPoll($pollId); + } @@ -134,15 +111,16 @@ class OptionService { * @return Option */ public function add($option) { - $this->logger->debug('call optionService->add(' . json_encode($option) . ')'); if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { throw new NotAuthorizedException; } + $this->option = new Option(); $this->set($option); $this->optionMapper->insert($this->option); $this->logService->setLog($option['pollId'], 'addOption'); + return $this->option; } @@ -153,9 +131,8 @@ class OptionService { * @return array Array of Option objects */ public function delete($optionId) { - $this->logger->debug('call optionService->delete(' . json_encode($optionId) . ')'); - $this->option = $this->optionMapper->find($optionId); + if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) { throw new NotAuthorizedException; } @@ -163,6 +140,7 @@ class OptionService { $this->optionMapper->delete($this->option); return $this->option; + } /** @@ -172,8 +150,6 @@ class OptionService { * @return Option */ public function update($option) { - $this->logger->debug('call optionService->update(' . json_encode($option) . ')'); - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { throw new NotAuthorizedException; } @@ -183,10 +159,12 @@ class OptionService { $this->set($option); $this->optionMapper->update($this->option); $this->logService->setLog($option['pollId'], 'updateOption'); + return $this->option; } catch (Exception $e) { return new DoesNotExistException($e); } + } /** @@ -196,7 +174,6 @@ class OptionService { * @return array Array of Option objects */ public function reorder($pollId, $options) { - $this->logger->debug('call optionService->reorder(' . $pollId . ', ' . json_encode($options) . ')'); if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; @@ -211,7 +188,7 @@ class OptionService { } } - return $this->get($pollId); + return $this->optionMapper->findByPoll($pollId); } @@ -223,6 +200,10 @@ class OptionService { */ public function clone($fromPollId, $toPollId) { + if (!$this->acl->setPollId($fromPollId)->getAllowView()) { + throw new NotAuthorizedException; + } + foreach ($this->optionMapper->findByPoll($fromPollId) as $option) { $option->setPollId($toPollId); $this->optionMapper->insert($option); diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index ecf35b18..e747450d 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -125,20 +125,46 @@ * @return array */ public function get($pollId = 0, $token = '') { - $this->poll = $this->pollMapper->find($pollId); - if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) { + + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { throw new NotAuthorizedException; } + $this->poll = $this->pollMapper->find($this->acl->getPollId()); + + try { + $comments = $this->commentService->list($this->poll->getId(), $token); + } catch (Exception $e) { + $comments = []; + } + + try { + $options = $this->optionService->list($this->poll->getId(), $token); + } catch (Exception $e) { + $options = []; + + } + + try { + $votes = $this->voteService->list($this->poll->getId(), $token); + } catch (Exception $e) { + $votes = []; + } + + try { + $shares = $this->shareService->list($this->poll->getId()); + } catch (Exception $e) { + $shares = []; + } + return [ 'acl' => $this->acl, 'poll' => $this->poll, - 'comments' => $this->commentService->list($pollId, $token), - 'options' => $this->optionService->list($pollId, $token), - 'shares' => $this->shareService->list($pollId, $token), - 'votes' => $this->voteService->list($pollId, $token) + 'comments' => $comments, + 'options' => $options, + 'shares' => $shares, + 'votes' => $votes ]; - } /** @@ -155,6 +181,7 @@ if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } + if ($this->poll->getDeleted()) { $this->poll->setDeleted(0); } else { @@ -163,6 +190,7 @@ $this->poll = $this->pollMapper->update($this->poll); $this->logService->setLog($this->poll->getId(), 'deletePoll'); + return $this->poll; } @@ -180,8 +208,8 @@ if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) { throw new NotAuthorizedException; } + return $this->pollMapper->delete($this->poll); - // return $this->poll; } /** @@ -196,8 +224,6 @@ if (!\OC::$server->getUserSession()->isLoggedIn()) { throw new NotAuthorizedException; } - $this->logger->alert(json_encode($type)); - $this->logger->alert(json_encode($title)); // Validate valuess if (!in_array($type, $this->getValidPollType())) { @@ -228,95 +254,94 @@ $this->poll = $this->pollMapper->insert($this->poll); $this->logService->setLog($this->poll->getId(), 'addPoll'); - $this->logger->alert(json_encode($this->poll)); return $this->poll; } - /** - * write - * @NoAdminRequired - * @NoCSRFRequired - * @depricated - * @param Array $poll - * @return DataResponse - */ - - public function write($poll, $pollId = null) { - - if (!$pollId) { - $pollId = $poll['id']; - } - - // Validate valuess - if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { - throw new InvalidShowResultsException('Invalid value for prop showResults'); - } - - if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) { - throw new InvalidAccessException('Invalid value for prop access'); - } - - if (isset($poll['title']) && !$poll['title']) { - throw new EmptyTitleException('Title must not be empty'); - } - - try { - // find pollId - $this->poll = $this->pollMapper->find($pollId); - $this->logService->setLog($this->poll->getId(), 'updatePoll'); - - - } catch (DoesNotExistException $e) { - // if not found create a new poll - - // Validate valuess - if (!in_array($poll['type'], $this->getValidPollType())) { - throw new InvalidPollTypeException('Invalid poll type'); - } - - if (!$poll['title']) { - throw new EmptyTitleException('Title must not be empty'); - } - - - $this->poll = new Poll(); - $this->poll->setType($poll['type']); - $this->poll->setOwner($this->userId); - $this->poll->setTitle($poll['title']); - $this->poll->setCreated(time()); - $this->poll = $this->pollMapper->insert($this->poll); - - $this->logService->setLog($this->poll->getId(), 'addPoll'); - } - - if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { - throw new NotAuthorizedException; - } - - $this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle()); - $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); - $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); - $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); - $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); - $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); - $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); - $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); - $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); - $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); - - $this->poll->setFullAnonymous(0); - $this->poll->setVoteLimit(0); - $this->poll->setSettings(''); - $this->poll->setOptions(''); - - $this->pollMapper->update($this->poll); - - return $this->poll; - } + // /** + // * write + // * @NoAdminRequired + // * @NoCSRFRequired + // * @depricated + // * @param Array $poll + // * @return DataResponse + // */ + // + // public function write($poll, $pollId = null) { + // + // if (!$pollId) { + // $pollId = $poll['id']; + // } + // + // // Validate valuess + // if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { + // throw new InvalidShowResultsException('Invalid value for prop showResults'); + // } + // + // if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) { + // throw new InvalidAccessException('Invalid value for prop access'); + // } + // + // if (isset($poll['title']) && !$poll['title']) { + // throw new EmptyTitleException('Title must not be empty'); + // } + // + // try { + // // find pollId + // $this->poll = $this->pollMapper->find($pollId); + // $this->logService->setLog($this->poll->getId(), 'updatePoll'); + // + // + // } catch (DoesNotExistException $e) { + // // if not found create a new poll + // + // // Validate valuess + // if (!in_array($poll['type'], $this->getValidPollType())) { + // throw new InvalidPollTypeException('Invalid poll type'); + // } + // + // if (!$poll['title']) { + // throw new EmptyTitleException('Title must not be empty'); + // } + // + // + // $this->poll = new Poll(); + // $this->poll->setType($poll['type']); + // $this->poll->setOwner($this->userId); + // $this->poll->setTitle($poll['title']); + // $this->poll->setCreated(time()); + // $this->poll = $this->pollMapper->insert($this->poll); + // + // $this->logService->setLog($this->poll->getId(), 'addPoll'); + // } + // + // if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { + // throw new NotAuthorizedException; + // } + // + // $this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle()); + // $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); + // $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); + // $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); + // $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); + // $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); + // $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); + // $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); + // $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); + // $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + // + // $this->poll->setFullAnonymous(0); + // $this->poll->setVoteLimit(0); + // $this->poll->setSettings(''); + // $this->poll->setOptions(''); + // + // $this->pollMapper->update($this->poll); + // + // return $this->poll; + // } /** - * write + * update * @NoAdminRequired * @NoCSRFRequired * @param Array $poll @@ -369,6 +394,11 @@ * @return DataResponse */ public function clone($pollId) { + + if (!$this->acl->setPollId($this->poll->getId())->getAllowView()) { + throw new NotAuthorizedException; + } + $this->poll = $this->pollMapper->find($pollId); $this->poll->setCreated(time()); diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 0df25218..04a40269 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -25,7 +25,6 @@ namespace OCA\Polls\Service; use Exception; -use OCP\ILogger; use OCP\Security\ISecureRandom; use OCA\Polls\Exceptions\NotAuthorizedException; @@ -35,27 +34,18 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Service\MailService; use OCA\Polls\Model\Acl; -// TODO: Change to Service use OCA\Polls\Controller\SystemController; class ShareService { - private $logger; - private $acl; private $shareMapper; private $share; - private $userId; - - private $pollMapper; private $systemController; private $mailService; + private $acl; /** * ShareController constructor. - * @param string $appName - * @param string $userId - * @param IRequest $request - * @param ILogger $logger * @param ShareMapper $shareMapper * @param Share $share * @param SystemController $systemController @@ -63,17 +53,12 @@ class ShareService { * @param Acl $acl */ public function __construct( - string $appName, - $userId, - ILogger $logger, ShareMapper $shareMapper, Share $share, SystemController $systemController, MailService $mailService, Acl $acl ) { - $this->logger = $logger; - $this->userId = $userId; $this->shareMapper = $shareMapper; $this->share = $share; $this->systemController = $systemController; @@ -89,11 +74,12 @@ class ShareService { * @return DataResponse */ public function list($pollId) { - if ($this->acl->setPollId($pollId)->getAllowEdit()) { - return $this->shareMapper->findByPoll($pollId); - } else { + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } + + return $this->shareMapper->findByPoll($pollId); + } /** @@ -118,8 +104,8 @@ class ShareService { */ // TODO: Replace with $this->add and separate sending invitations public function write($pollId, $type, $userId, $userEmail = '') { - $this->acl->setPollId($pollId); - if (!$this->acl->getAllowEdit()) { + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } @@ -152,8 +138,8 @@ class ShareService { * @return Array */ public function add($pollId, $type, $userId, $userEmail = '') { - $this->acl->setPollId($pollId); - if (!$this->acl->getAllowEdit()) { + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } @@ -182,7 +168,6 @@ class ShareService { * @return Share */ public function createPersonalShare($token, $userName) { - $publicShare = $this->shareMapper->findByToken($token); // Return of validatePublicUsername is a DataResponse @@ -195,6 +180,7 @@ class ShareService { if ($publicShare->getType() === 'public') { + $this->share = new Share(); $this->share->setToken(\OC::$server->getSecureRandom()->generate( 16, @@ -231,11 +217,13 @@ class ShareService { public function remove($token) { $this->share = $this->shareMapper->findByToken($token); - if ($this->acl->setPollId($this->share->getPollId())->getAllowEdit()) { - $this->shareMapper->delete($this->share); - return $this->share; - } else { + if (!$this->acl->setPollId($this->share->getPollId())->getAllowEdit()) { throw new NotAuthorizedException; } + + $this->shareMapper->delete($this->share); + + return $this->share; + } } diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php index 7f096b83..739640f9 100644 --- a/lib/Service/VoteService.php +++ b/lib/Service/VoteService.php @@ -26,10 +26,6 @@ namespace OCA\Polls\Service; use Exception; use OCP\AppFramework\Db\DoesNotExistException; -use OCP\IGroupManager; -use OCP\ILogger; - - use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; use OCA\Polls\Db\OptionMapper; @@ -39,47 +35,33 @@ use OCA\Polls\Model\Acl; class VoteService { - private $userId; - private $logger; - private $vote; private $voteMapper; + private $vote; private $optionMapper; - private $groupManager; private $anonymizer; private $logService; private $acl; /** * VoteController constructor. - * @param string $appName - * @param $userId - * @param ILogger $logger - * @param Vote $vote * @param VoteMapper $voteMapper + * @param Vote $vote * @param OptionMapper $optionMapper - * @param IGroupManager $groupManager * @param AnonymizeService $anonymizer * @param LogService $logService * @param Acl $acl */ public function __construct( - string $appName, - $userId, - ILogger $logger, VoteMapper $voteMapper, - OptionMapper $optionMapper, Vote $vote, - IGroupManager $groupManager, + OptionMapper $optionMapper, AnonymizeService $anonymizer, LogService $logService, Acl $acl ) { - $this->userId = $userId; - $this->vote = $vote; $this->voteMapper = $voteMapper; + $this->vote = $vote; $this->optionMapper = $optionMapper; - $this->logger = $logger; - $this->groupManager = $groupManager; $this->anonymizer = $anonymizer; $this->logService = $logService; $this->acl = $acl; @@ -94,17 +76,17 @@ class VoteService { * @return DataResponse */ public function list($pollId = 0, $token = '') { - if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowView()) { + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { throw new NotAuthorizedException; } if (!$this->acl->getAllowSeeResults()) { - return $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId()); + return $this->voteMapper->findByPollAndUser($this->acl->getpollId(), $this->acl->getUserId()); } elseif (!$this->acl->getAllowSeeUsernames()) { - $this->anonymizer->set($pollId, $this->acl->getUserId()); + $this->anonymizer->set($this->acl->getpollId(), $this->acl->getUserId()); return $this->anonymizer->getVotes(); } else { - return $this->voteMapper->findByPoll($pollId); + return $this->voteMapper->findByPoll($this->acl->getpollId()); } } @@ -119,14 +101,14 @@ class VoteService { */ public function set($pollId = 0, $pollOptionText, $setTo, $token = '') { - if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowVote()) { + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowVote()) { throw new NotAuthorizedException; } - - $option = $this->optionMapper->findByPollAndText($pollId, $pollOptionText); + + $option = $this->optionMapper->findByPollAndText($this->acl->getpollId(), $pollOptionText); try { - $this->vote = $this->voteMapper->findSingleVote($pollId, $option->getPollOptionText(), $this->acl->getUserId()); + $this->vote = $this->voteMapper->findSingleVote($this->acl->getpollId(), $option->getPollOptionText(), $this->acl->getUserId()); $this->vote->setVoteAnswer($setTo); $this->voteMapper->update($this->vote); @@ -134,7 +116,7 @@ class VoteService { // Vote does not exist, insert as new Vote $this->vote = new Vote(); - $this->vote->setPollId($pollId); + $this->vote->setPollId($this->acl->getpollId()); $this->vote->setUserId($this->acl->getUserId()); $this->vote->setVoteOptionText($option->getPollOptionText()); $this->vote->setVoteOptionId($option->getId()); @@ -158,12 +140,11 @@ class VoteService { */ public function delete($pollId, $userId) { - if (!$this->acl->checkAuthorize($pollId, $token) && !$this->acl->getAllowEdit()) { + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } $votes = $this->voteMapper->deleteByPollAndUser($pollId, $userId); - $this->logger->alert('Deleted votes from ' . $userId . ' in poll ' . $pollId); } } -- cgit v1.2.3 From 863d50ecfa2963664cb16892d31af65ad3289b5e Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 22 Jun 2020 18:55:42 +0200 Subject: code maintenance --- lib/Controller/CommentApiController.php | 19 +++-- lib/Controller/CommentController.php | 27 ++++---- lib/Controller/OptionApiController.php | 18 ++--- lib/Controller/OptionController.php | 2 +- lib/Controller/PollApiController.php | 34 ++++----- lib/Controller/PollController.php | 59 +++++----------- lib/Controller/ShareApiController.php | 15 ++-- lib/Controller/ShareController.php | 47 ++----------- lib/Controller/SubscriptionApiController.php | 18 ++--- lib/Controller/SubscriptionController.php | 7 +- lib/Controller/VoteApiController.php | 9 +-- lib/Controller/VoteController.php | 21 +++--- lib/Service/CommentService.php | 1 + lib/Service/OptionService.php | 4 +- lib/Service/PollService.php | 100 ++------------------------- lib/Service/ShareService.php | 17 ++--- 16 files changed, 126 insertions(+), 272 deletions(-) (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 9d93b83b..9d6fc1a8 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -40,8 +40,7 @@ use OCA\Polls\Service\CommentService; class CommentApiController extends ApiController { - private $optionService; - private $urlGenerator; + private $commentService; /** * CommentApiController constructor. * @param string $appName @@ -52,7 +51,6 @@ class CommentApiController extends ApiController { public function __construct( string $appName, IRequest $request, - IURLGenerator $urlGenerator, CommentService $commentService ) { parent::__construct($appName, @@ -61,7 +59,6 @@ class CommentApiController extends ApiController { 'Authorization, Content-Type, Accept', 1728000); $this->commentService = $commentService; - $this->urlGenerator = $urlGenerator; } /** @@ -77,9 +74,9 @@ class CommentApiController extends ApiController { try { return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -94,11 +91,11 @@ class CommentApiController extends ApiController { */ public function add($pollId, $message) { try { - return new DataResponse($this->commentService->add($message, $pollId), Http::STATUS_CREATED); + return new DataResponse($this->commentService->add($pollId, $message), Http::STATUS_CREATED); } catch (DoesNotExistException $e) { - return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -115,9 +112,9 @@ class CommentApiController extends ApiController { $this->commentService->delete($commentId); return new DataResponse($commentId, Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Comment does not exist', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Comment does not exist'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index e940bfb7..6f240f05 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -24,6 +24,7 @@ namespace OCA\Polls\Controller; use Exception; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\IRequest; use OCP\ILogger; @@ -31,6 +32,8 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCA\Polls\Exceptions\NotAuthorizedException; + use OCA\Polls\Service\CommentService; @@ -67,18 +70,18 @@ class CommentController extends Controller { return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK); } - /** - * Read all comments of a poll based on a share token and return list as array - * @NoAdminRequired - * @NoCSRFRequired - * @PublicPage - * @param string $token - * @return DataResponse - */ - public function getByToken($token) { - return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); - } - + // /** + // * Read all comments of a poll based on a share token and return list as array + // * @NoAdminRequired + // * @NoCSRFRequired + // * @PublicPage + // * @param string $token + // * @return DataResponse + // */ + // public function getByToken($token) { + // return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK); + // } + // /** * Write a new comment to the db and returns the new comment as array * @NoAdminRequired diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php index f23fd350..5809510d 100644 --- a/lib/Controller/OptionApiController.php +++ b/lib/Controller/OptionApiController.php @@ -72,9 +72,9 @@ class OptionApiController extends ApiController { try { return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -99,11 +99,11 @@ class OptionApiController extends ApiController { try { return new DataResponse($this->optionService->add($option), Http::STATUS_CREATED); } catch (DoesNotExistException $e) { - return new DataResponse('Poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (UniqueConstraintViolationException $e) { - return new DataResponse('Option exists', Http::STATUS_CONFLICT); + return new DataResponse(['error' => 'Option exists'], Http::STATUS_CONFLICT); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -113,14 +113,14 @@ class OptionApiController extends ApiController { * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param Option $option + * @param array $option * @return DataResponse */ public function update($option) { try { return new DataResponse($this->optionService->update($option), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -136,9 +136,9 @@ class OptionApiController extends ApiController { try { return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Option does not exist', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } } diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 2b488c17..ae58c135 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -83,7 +83,7 @@ class OptionController extends Controller { * Add a new Option to poll * @NoAdminRequired * @NoCSRFRequired - * @param Option $option + * @param array $option * @return DataResponse */ public function add($option) { diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index 98d8ef8f..3dcca2dd 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -79,7 +79,7 @@ } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -95,9 +95,9 @@ try { return new DataResponse($this->pollService->get($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -113,11 +113,11 @@ try { return new DataResponse($this->pollService->add($type, $title), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidPollTypeException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -133,15 +133,15 @@ try { return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidAccessException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidShowResultsException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -157,9 +157,9 @@ try { return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -175,9 +175,9 @@ try { return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -193,9 +193,9 @@ try { return new DataResponse($this->pollService->clone($pollId), Http::STATUS_CREATED); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index c30c8c32..aa4a5899 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -77,7 +77,7 @@ use OCA\Polls\Service\PollService; } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -94,9 +94,9 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -112,9 +112,9 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -130,9 +130,9 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -149,11 +149,11 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidPollTypeException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -169,40 +169,15 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidAccessException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidShowResultsException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } - } - - /** - * write - * @NoAdminRequired - * @NoCSRFRequired - * @depicated - * @param Array $poll - * @return DataResponse - */ - - public function write($poll) { - try { - return new DataResponse($this->pollService->write($poll), Http::STATUS_OK); - } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); - } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (InvalidAccessException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (InvalidShowResultsException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); - } catch (EmptyTitleException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -217,9 +192,9 @@ use OCA\Polls\Service\PollService; try { return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Poll not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index 968ff115..4f147fad 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -45,7 +45,6 @@ class ShareApiController extends ApiController { * @param string $appName * @param string $userId * @param IRequest $request - * @param ILogger $logger * @param ShareService $shareService */ public function __construct( @@ -74,9 +73,9 @@ class ShareApiController extends ApiController { try { return new DataResponse($this->shareService->list($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'No shares for poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -93,9 +92,9 @@ class ShareApiController extends ApiController { try { return new DataResponse($this->shareService->get($token), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Token ' . $token . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -114,9 +113,9 @@ class ShareApiController extends ApiController { try { return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED); } catch (\Exception $e) { - return new DataResponse($e, Http::STATUS_CONFLICT); + return new DataResponse(['error' => $e], Http::STATUS_CONFLICT); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -134,7 +133,7 @@ class ShareApiController extends ApiController { try { return new DataResponse($this->shareService->remove($token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index da80acb5..16d9bca3 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -67,43 +67,6 @@ class ShareController extends Controller { $this->shareService = $shareService; } - /** - * getByToken - * Get poll - * @NoAdminRequired - * @NoCSRFRequired - * @PublicPage - * @param string $token - * @return DataResponse - */ - public function get($token) { - try { - return new DataResponse($this->shareService->get($token), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('Token ' . $token . ' not found', Http::STATUS_NOT_FOUND); - } - } - - /** - * get - * Read all shares of a poll based on the poll id and return list as array - * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId - * @return DataResponse - */ - public function getShares($pollId) { - try { - return new DataResponse($this->shareService->findByPoll($pollId), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); - } catch (DoesNotExistException $e) { - return new DataResponse('No shares for poll with id ' . $pollId . ' not found', Http::STATUS_NOT_FOUND); - } - } - /** * Write a new share to the db and returns the new share as array * @NoAdminRequired @@ -112,7 +75,7 @@ class ShareController extends Controller { * @param Array $share * @return DataResponse */ - public function write($pollId, $share) { + public function add($pollId, $share) { try { $return = $this->shareService->write( $pollId, @@ -122,7 +85,7 @@ class ShareController extends Controller { ); return new DataResponse($return, Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (\Exception $e) { return new DataResponse($e, Http::STATUS_CONFLICT); } @@ -144,7 +107,7 @@ class ShareController extends Controller { try { return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidUsername $e) { return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT); } catch (DoesNotExistException $e) { @@ -162,14 +125,14 @@ class ShareController extends Controller { * @return DataResponse */ - public function remove($share) { + public function delete($share) { try { return new DataResponse(array( 'action' => 'deleted', 'shareId' => $this->shareService->remove($share['token'])->getId() ), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (Exception $e) { return new DataResponse($e, Http::STATUS_NOT_FOUND); } diff --git a/lib/Controller/SubscriptionApiController.php b/lib/Controller/SubscriptionApiController.php index e8c1acfa..3a23d403 100644 --- a/lib/Controller/SubscriptionApiController.php +++ b/lib/Controller/SubscriptionApiController.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; use OCP\ILogger; @@ -77,11 +78,12 @@ class SubscriptionApiController extends ApiController { */ public function get($pollId) { try { - return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK); + $this->subscriptionService->get($pollId); + return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND); + return new DataResponse(['status' => 'Not subscribed to poll ' . $pollId], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -93,10 +95,10 @@ class SubscriptionApiController extends ApiController { */ public function subscribe($pollId) { try { - return $this->subscriptionService->set($pollId, true); - return new DataResponse('Subscribed', Http::STATUS_OK); + $this->subscriptionService->set($pollId, true); + return new DataResponse(['status' => 'Subscribed to poll ' . $pollId], Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } /** @@ -108,9 +110,9 @@ class SubscriptionApiController extends ApiController { public function unsubscribe($pollId) { try { $this->subscriptionService->set($pollId, false); - return new DataResponse('Unsubscribed', Http::STATUS_OK); + return new DataResponse(['status' => 'Unsubscribed from poll ' . $pollId], Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } } diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index f3cc655a..194b97e6 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; use OCP\ILogger; @@ -73,9 +74,9 @@ class SubscriptionController extends Controller { try { return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Not subscribed'], Http::STATUS_NOT_FOUND); } } @@ -88,7 +89,7 @@ class SubscriptionController extends Controller { try { return new DataResponse($this->subscriptionService->set($pollId, $subscribed), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } } diff --git a/lib/Controller/VoteApiController.php b/lib/Controller/VoteApiController.php index 8f5b77ac..03caea0f 100644 --- a/lib/Controller/VoteApiController.php +++ b/lib/Controller/VoteApiController.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; use OCP\ILogger; @@ -74,9 +75,9 @@ class VoteApiController extends ApiController { try { return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('No votes', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'No votes'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } @@ -95,9 +96,9 @@ class VoteApiController extends ApiController { try { return new DataResponse($this->voteService->set($pollId, $pollOptionText, $setTo), Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { - return new DataResponse($e->getMessage(), $e->getStatus()); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } diff --git a/lib/Controller/VoteController.php b/lib/Controller/VoteController.php index 64dc8501..f666c004 100644 --- a/lib/Controller/VoteController.php +++ b/lib/Controller/VoteController.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Controller; // use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\ILogger; use OCP\IRequest; @@ -71,9 +72,9 @@ class VoteController extends Controller { try { return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('No votes', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'No votes'], Http::STATUS_NOT_FOUND); } } @@ -91,9 +92,9 @@ class VoteController extends Controller { try { return new DataResponse($this->voteService->set($pollId, $option['pollOptionText'], $setTo), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); } } @@ -111,9 +112,9 @@ class VoteController extends Controller { try { return new DataResponse($this->voteService->delete($pollId, $userId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => ''], Http::STATUS_NOT_FOUND); } } @@ -135,9 +136,9 @@ class VoteController extends Controller { try { return new DataResponse($this->voteService->set(0, $option['pollOptionText'], $setTo, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('Option not found', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); } } @@ -155,9 +156,9 @@ class VoteController extends Controller { try { return new DataResponse($this->voteService->list(null, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { - return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse('No votes', Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'No votes'], Http::STATUS_NOT_FOUND); } } diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index 8ae7ef1e..a1cd7131 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -59,6 +59,7 @@ class CommentService { Acl $acl ) { $this->commentMapper = $commentMapper; + $this->comment = $comment; $this->logger = $logger; $this->anonymizer = $anonymizer; $this->acl = $acl; diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 139c8267..93052c3d 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -34,7 +34,6 @@ use OCA\Polls\Model\Acl; class OptionService { private $optionMapper; - private $options; private $option; private $logService; private $acl; @@ -195,7 +194,8 @@ class OptionService { /** * Set order by order of the given array * @NoAdminRequired - * @param array $options + * @param integer $fromPollId + * @param integer $toPollId * @return array Array of Option objects */ public function clone($fromPollId, $toPollId) { diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index e747450d..d57f1124 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -45,7 +45,6 @@ class PollService { private $logger; - private $userid; private $pollMapper; private $poll; private $logService; @@ -57,8 +56,6 @@ /** * PollController constructor. - * @param string $appName - * @param $userId * @param PollMapper $pollMapper * @param LogService $logService * @param CommentService $commentService @@ -69,8 +66,6 @@ */ public function __construct( - string $appName, - $userId, ILogger $logger, PollMapper $pollMapper, Poll $poll, @@ -81,7 +76,6 @@ VoteService $voteService, Acl $acl ) { - $this->userId = $userId; $this->logger = $logger; $this->pollMapper = $pollMapper; $this->poll = $poll; @@ -97,7 +91,7 @@ /** * list * @NoAdminRequired - * @return DataResponse + * @return array */ public function list() { @@ -105,6 +99,8 @@ throw new NotAuthorizedException; } + $pollList = []; + $polls = $this->pollMapper->findAll(); // TODO: Not the elegant way. Improvement neccessary foreach ($polls as $poll) { @@ -217,7 +213,7 @@ * @NoAdminRequired * @NoCSRFRequired * @param Array $poll - * @return DataResponse + * @return Poll */ public function add($type, $title) { @@ -258,94 +254,12 @@ return $this->poll; } - // /** - // * write - // * @NoAdminRequired - // * @NoCSRFRequired - // * @depricated - // * @param Array $poll - // * @return DataResponse - // */ - // - // public function write($poll, $pollId = null) { - // - // if (!$pollId) { - // $pollId = $poll['id']; - // } - // - // // Validate valuess - // if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { - // throw new InvalidShowResultsException('Invalid value for prop showResults'); - // } - // - // if (isset($poll['access']) && !in_array($poll['access'], $this->getValidShowResults())) { - // throw new InvalidAccessException('Invalid value for prop access'); - // } - // - // if (isset($poll['title']) && !$poll['title']) { - // throw new EmptyTitleException('Title must not be empty'); - // } - // - // try { - // // find pollId - // $this->poll = $this->pollMapper->find($pollId); - // $this->logService->setLog($this->poll->getId(), 'updatePoll'); - // - // - // } catch (DoesNotExistException $e) { - // // if not found create a new poll - // - // // Validate valuess - // if (!in_array($poll['type'], $this->getValidPollType())) { - // throw new InvalidPollTypeException('Invalid poll type'); - // } - // - // if (!$poll['title']) { - // throw new EmptyTitleException('Title must not be empty'); - // } - // - // - // $this->poll = new Poll(); - // $this->poll->setType($poll['type']); - // $this->poll->setOwner($this->userId); - // $this->poll->setTitle($poll['title']); - // $this->poll->setCreated(time()); - // $this->poll = $this->pollMapper->insert($this->poll); - // - // $this->logService->setLog($this->poll->getId(), 'addPoll'); - // } - // - // if (!$this->acl->setPollId($this->poll->getId())->getAllowEdit()) { - // throw new NotAuthorizedException; - // } - // - // $this->poll->setTitle(isset($poll['title']) ? $poll['title'] : $this->poll->getTitle()); - // $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); - // $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); - // $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); - // $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); - // $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); - // $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); - // $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); - // $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); - // $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); - // - // $this->poll->setFullAnonymous(0); - // $this->poll->setVoteLimit(0); - // $this->poll->setSettings(''); - // $this->poll->setOptions(''); - // - // $this->pollMapper->update($this->poll); - // - // return $this->poll; - // } - /** * update * @NoAdminRequired * @NoCSRFRequired * @param Array $poll - * @return DataResponse + * @return Poll */ public function update($pollId, $poll) { @@ -391,7 +305,7 @@ * @NoAdminRequired * @NoCSRFRequired * @param integer $pollId - * @return DataResponse + * @return Poll */ public function clone($pollId) { @@ -408,7 +322,7 @@ $this->poll->setId(0); $this->poll = $this->pollMapper->insert($this->poll); - $this->logService->setLog($clonePoll->getId(), 'addPoll'); + $this->logService->setLog($this->poll->getId(), 'addPoll'); $this->optionService->clone($pollId, $this->poll->getId()); diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 04a40269..a7cf144b 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -71,7 +71,7 @@ class ShareService { * Read all shares of a poll based on the poll id and return list as array * @NoAdminRequired * @param integer $pollId - * @return DataResponse + * @return array */ public function list($pollId) { if (!$this->acl->setPollId($pollId)->getAllowEdit()) { @@ -87,11 +87,10 @@ class ShareService { * Get pollId by token * @NoAdminRequired * @param string $token - * @return Array + * @return Share */ public function get($token) { - $this->share = $this->shareMapper->findByToken($token); - return $this->share; + return $this->shareMapper->findByToken($token); } /** @@ -100,7 +99,7 @@ class ShareService { * @depricated * @param int $pollId * @param string $share - * @return Array + * @return array */ // TODO: Replace with $this->add and separate sending invitations public function write($pollId, $type, $userId, $userEmail = '') { @@ -135,7 +134,7 @@ class ShareService { * @NoAdminRequired * @param int $pollId * @param string $share - * @return Array + * @return array */ public function add($pollId, $type, $userId, $userEmail = '') { @@ -192,15 +191,13 @@ class ShareService { $this->share->setPollId($publicShare->getPollId()); $this->share->setUserId($userName); $this->share->setUserEmail(''); - $this->share = $this->shareMapper->insert($this->share); - return $this->share; + return $this->shareMapper->insert($this->share); } elseif ($publicShare->getType() === 'email') { $publicShare->setType('external'); $publicShare->setUserId($userName); - $this->shareMapper->update($publicShare); - return new DataResponse($publicShare, Http::STATUS_OK); + return $this->shareMapper->update($publicShare); } else { throw new NotAuthorizedException; -- cgit v1.2.3 From 17c03b27e1e0efe74817b3ce622f773a8291bcfd Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 22 Jun 2020 20:43:42 +0200 Subject: code maintenance --- lib/Controller/CommentApiController.php | 3 +-- lib/Controller/OptionController.php | 2 +- lib/Controller/ShareController.php | 2 +- lib/Controller/SubscriptionController.php | 2 +- lib/Service/OptionService.php | 3 ++- lib/Service/PollService.php | 4 ++-- lib/Service/SubscriptionService.php | 13 ++++++++----- 7 files changed, 16 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 9d6fc1a8..ad09654d 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -109,8 +109,7 @@ class CommentApiController extends ApiController { */ public function delete($commentId) { try { - $this->commentService->delete($commentId); - return new DataResponse($commentId, Http::STATUS_OK); + return new DataResponse($this->commentService->delete($commentId), Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Comment does not exist'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index ae58c135..0ec24092 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -94,7 +94,7 @@ class OptionController extends Controller { * Update poll option * @NoAdminRequired * @NoCSRFRequired - * @param Option $option + * @param array $option * @return DataResponse */ public function update($option) { diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index 16d9bca3..aa80cc3d 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -109,7 +109,7 @@ class ShareController extends Controller { } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidUsername $e) { - return new DataResponse($userName . ' is not valid', Http::STATUS_CONFLICT); + return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT); } catch (DoesNotExistException $e) { // return forbidden in all not catched error cases return new DataResponse($e, Http::STATUS_FORBIDDEN); diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index 194b97e6..e90f4a66 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -76,7 +76,7 @@ class SubscriptionController extends Controller { } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse(['error' => 'Not subscribed'], Http::STATUS_NOT_FOUND); + return new DataResponse(['status' => 'Not subscribed'], Http::STATUS_NOT_FOUND); } } diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 93052c3d..0afe4b8c 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -24,8 +24,9 @@ namespace OCA\Polls\Service; use Exception; - +use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; + use OCA\Polls\Db\Option; use OCA\Polls\Db\OptionMapper; use OCA\Polls\Service\LogService; diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index d57f1124..31a6f36d 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -233,7 +233,7 @@ $this->poll = new Poll(); $this->poll->setType($type); $this->poll->setCreated(time()); - $this->poll->setOwner($this->userId); + $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); $this->poll->setTitle($title); $this->poll->setDescription(''); $this->poll->setAccess('hidden'); @@ -316,7 +316,7 @@ $this->poll = $this->pollMapper->find($pollId); $this->poll->setCreated(time()); - $this->poll->setOwner($this->userId); + $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); $this->poll->setTitle('Clone of ' . $this->poll->getTitle()); $this->poll->setDeleted(0); $this->poll->setId(0); diff --git a/lib/Service/SubscriptionService.php b/lib/Service/SubscriptionService.php index a8801520..bca84345 100644 --- a/lib/Service/SubscriptionService.php +++ b/lib/Service/SubscriptionService.php @@ -94,23 +94,26 @@ class SubscriptionService { $subscription = $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId()); if (!$subscribed) { $this->subscriptionMapper->delete($subscription); - return 'Unsubscribed'; + return ['status' => 'Unsubscribed from poll ' . $pollId]; } else { // subscription already exists, just return the existing subscription - return $subscription; + return ['status' => 'Subscribed to poll ' . $pollId]; } + } catch (DoesNotExistException $e){ + if ($subscribed) { $subscription = new Subscription(); $subscription->setPollId($pollId); $subscription->setUserId($this->acl->getUserId()); $this->subscriptionMapper->insert($subscription); - return $subscription; + return ['status' => 'Subscribed to poll ' . $pollId]; } else { // subscription is not found, just approve the unsubscription - return 'Unsubscribed'; + return ['status' => 'Unsubscribed from poll ' . $pollId]; } + } catch (MultipleObjectsReturnedException $e) { // Duplicates should not exist but if found, fix it // unsubscribe from all and resubscribe, if requested @@ -125,7 +128,7 @@ class SubscriptionService { $this->logger->debug('Added new subscription'); return $subscription; } else { - return 'Unsubscribed'; + return ['status' => 'Unsubscribed from poll ' . $pollId]; } } -- cgit v1.2.3 From fd1779a07d81d21f5ba186de522a862693bd3677 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 22 Jun 2020 20:47:34 +0200 Subject: code maintenace --- lib/Service/SubscriptionService.php | 10 ++-------- lib/Service/VoteService.php | 9 +++++---- 2 files changed, 7 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/Service/SubscriptionService.php b/lib/Service/SubscriptionService.php index bca84345..f378e476 100644 --- a/lib/Service/SubscriptionService.php +++ b/lib/Service/SubscriptionService.php @@ -35,29 +35,22 @@ use OCA\Polls\Model\Acl; class SubscriptionService { - private $userId; private $acl; private $subscriptionMapper; private $logger; /** * SubscriptionController constructor. - * @param string $appName - * @param $UserId * @param SubscriptionMapper $subscriptionMapper - * @param IRequest $request * @param ILogger $logger * @param Acl $acl */ public function __construct( - string $appName, - $userId, SubscriptionMapper $subscriptionMapper, ILogger $logger, Acl $acl ) { - $this->userId = $userId; $this->subscriptionMapper = $subscriptionMapper; $this->acl = $acl; $this->logger = $logger; @@ -66,7 +59,7 @@ class SubscriptionService { /** * @NoAdminRequired * @param integer $pollId - * @return DataResponse + * @return array */ public function get($pollId) { if (!$this->acl->setPollId($pollId)->getAllowView()) { @@ -85,6 +78,7 @@ class SubscriptionService { /** * @NoAdminRequired * @param integer $pollId + * @return array */ public function set($pollId, $subscribed) { if (!$this->acl->setPollId($pollId)->getAllowView()) { diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php index 739640f9..4df01c70 100644 --- a/lib/Service/VoteService.php +++ b/lib/Service/VoteService.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Service; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; @@ -73,7 +74,7 @@ class VoteService { * @NoAdminRequired * @param integer $pollId * @param string $token - * @return DataResponse + * @return Vote */ public function list($pollId = 0, $token = '') { if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { @@ -97,14 +98,14 @@ class VoteService { * @param Array $option * @param string $setTo * @param string $token - * @return DataResponse + * @return Vote */ public function set($pollId = 0, $pollOptionText, $setTo, $token = '') { if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowVote()) { throw new NotAuthorizedException; } - + $option = $this->optionMapper->findByPollAndText($this->acl->getpollId(), $pollOptionText); try { @@ -136,7 +137,7 @@ class VoteService { * @param integer $voteId * @param string $userId * @param integer $pollId - * @return DataResponse + * @return Vote */ public function delete($pollId, $userId) { -- cgit v1.2.3 From 40b2720d0d1c74ed3a2a12497185d47fb0b7ab29 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 27 Jun 2020 19:22:17 +0200 Subject: some polish --- lib/AppInfo/Application.php | 13 ----- lib/Controller/CommentApiController.php | 8 +-- lib/Controller/OptionApiController.php | 8 +-- lib/Controller/PollApiController.php | 14 ++--- lib/Controller/PollController.php | 95 ++++++++++++++++++++++++++++----- lib/Controller/ShareApiController.php | 8 +-- lib/Controller/VoteApiController.php | 4 +- lib/Db/Poll.php | 14 +++++ lib/Service/PollService.php | 84 +++++++---------------------- 9 files changed, 137 insertions(+), 111 deletions(-) (limited to 'lib') diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 7a88db86..53922944 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,19 +23,6 @@ namespace OCA\Polls\AppInfo; -// use OCA\Polls\Controller\PageController; -// use OCA\Polls\Controller\ApiController; -// use OCA\Polls\Controller\CommentController; -// use OCA\Polls\Controller\PollController; -// use OCA\Polls\Controller\NotificationController; -// use OCA\Polls\Controller\OptionController; -// use OCA\Polls\Controller\VoteController; -// use OCA\Polls\Controller\ShareController; -// use OCA\Polls\Db\OptionMapper; -// use OCA\Polls\Db\PollMapper; -// use OCA\Polls\Db\NotificationMapper; -// use OCA\Polls\Db\VoteMapper; -// use OCA\Polls\Db\ShareMapper; use OCP\AppFramework\App; use OCP\IContainer; diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index ad09654d..6cc56bba 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -72,7 +72,7 @@ class CommentApiController extends ApiController { */ public function list($pollId) { try { - return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK); + return new DataResponse(['comments' => $this->commentService->list($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -91,7 +91,7 @@ class CommentApiController extends ApiController { */ public function add($pollId, $message) { try { - return new DataResponse($this->commentService->add($pollId, $message), Http::STATUS_CREATED); + return new DataResponse(['comment' => $this->commentService->add($pollId, $message)], Http::STATUS_CREATED); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -109,9 +109,9 @@ class CommentApiController extends ApiController { */ public function delete($commentId) { try { - return new DataResponse($this->commentService->delete($commentId), Http::STATUS_OK); + return new DataResponse(['comment' => $this->commentService->delete($commentId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse(['error' => 'Comment does not exist'], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Comment id ' . $commentId . ' does not exist'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } diff --git a/lib/Controller/OptionApiController.php b/lib/Controller/OptionApiController.php index 5809510d..e3396a3e 100644 --- a/lib/Controller/OptionApiController.php +++ b/lib/Controller/OptionApiController.php @@ -70,7 +70,7 @@ class OptionApiController extends ApiController { */ public function list($pollId) { try { - return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); + return new DataResponse(['options' => $this->optionService->list($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -97,7 +97,7 @@ class OptionApiController extends ApiController { ]; try { - return new DataResponse($this->optionService->add($option), Http::STATUS_CREATED); + return new DataResponse(['option' => $this->optionService->add($option)], Http::STATUS_CREATED); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (UniqueConstraintViolationException $e) { @@ -118,7 +118,7 @@ class OptionApiController extends ApiController { */ public function update($option) { try { - return new DataResponse($this->optionService->update($option), Http::STATUS_OK); + return new DataResponse(['option' => $this->optionService->update($option)], Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } @@ -134,7 +134,7 @@ class OptionApiController extends ApiController { */ public function delete($optionId) { try { - return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); + return new DataResponse(['option' => $this->optionService->delete($optionId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index 3dcca2dd..3c8acc9d 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -75,7 +75,7 @@ public function list() { try { - return new DataResponse($this->pollService->list(), Http::STATUS_OK); + return new DataResponse(['polls' => $this->pollService->list()], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -93,7 +93,7 @@ */ public function get($pollId) { try { - return new DataResponse($this->pollService->get($pollId), Http::STATUS_OK); + return new DataResponse(['poll' => $this->pollService->get($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -111,7 +111,7 @@ public function add($type, $title) { try { - return new DataResponse($this->pollService->add($type, $title), Http::STATUS_CREATED); + return new DataResponse(['poll' => $this->pollService->add($type, $title)], Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidPollTypeException $e) { @@ -131,7 +131,7 @@ public function update($pollId, $poll) { try { - return new DataResponse($this->pollService->update($pollId, $poll), Http::STATUS_OK); + return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -155,7 +155,7 @@ public function delete($pollId) { try { - return new DataResponse($this->pollService->delete($pollId), Http::STATUS_OK); + return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -173,7 +173,7 @@ public function deletePermanently($pollId) { try { - return new DataResponse($this->pollService->deletePermanently($pollId), Http::STATUS_OK); + return new DataResponse(['poll' => $this->pollService->deletePermanently($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -191,7 +191,7 @@ */ public function clone($pollId) { try { - return new DataResponse($this->pollService->clone($pollId), Http::STATUS_CREATED); + return new DataResponse(['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index aa4a5899..071dde17 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -38,11 +38,21 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCA\Polls\Service\PollService; +use OCA\Polls\Service\CommentService; +use OCA\Polls\Service\OptionService; +use OCA\Polls\Service\ShareService; +use OCA\Polls\Service\VoteService; +use OCA\Polls\Model\Acl; - class PollController extends Controller { +class PollController extends Controller { - private $logger; - private $pollService; + private $logger; + private $pollService; + private $commentService; + private $optionService; + private $shareService; + private $voteService; + private $acl; /** * PollController constructor. @@ -50,18 +60,33 @@ use OCA\Polls\Service\PollService; * @param IRequest $request * @param ILogger $logger * @param PollService $pollService - */ + * @param CommentService $commentService + * @param OptionService $optionService + * @param ShareService $shareService + * @param VoteService $voteService + * @param Acl $acl + */ public function __construct( string $appName, IRequest $request, ILogger $logger, - PollService $pollService - ) { + PollService $pollService, + CommentService $commentService, + OptionService $optionService, + ShareService $shareService, + VoteService $voteService, + Acl $acl + ) { parent::__construct($appName, $request); - $this->pollService = $pollService; - $this->logger = $logger; - } + $this->logger = $logger; + $this->pollService = $pollService; + $this->commentService = $commentService; + $this->optionService = $optionService; + $this->shareService = $shareService; + $this->voteService = $voteService; + $this->acl = $acl; + } /** @@ -92,12 +117,54 @@ use OCA\Polls\Service\PollService; */ public function get($pollId, $token) { try { - return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK); + if ($token) { + $poll = $this->pollService->get(0, $token); + $acl = $this->acl->setToken($token); + } else { + $poll = $this->pollService->get($pollId); + $acl = $this->acl->setPollId($pollId); + } + + // $this->poll = $this->pollService->get($pollId, $token); + // return new DataResponse($this->pollService->get($pollId, $token), Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } + + try { + $comments = $this->commentService->list($pollId, $token); + } catch (Exception $e) { + $comments = []; + } + + try { + $options = $this->optionService->list($pollId, $token); + } catch (Exception $e) { + $options = []; + } + + try { + $votes = $this->voteService->list($pollId, $token); + } catch (Exception $e) { + $votes = []; + } + + try { + $shares = $this->shareService->list($pollId); + } catch (Exception $e) { + $shares = []; + } + + return new DataResponse([ + 'acl' => $acl, + 'poll' => $poll, + 'comments' => $comments, + 'options' => $options, + 'shares' => $shares, + 'votes' => $votes + ], Http::STATUS_OK); } /** @@ -138,10 +205,11 @@ use OCA\Polls\Service\PollService; /** - * write + * add * @NoAdminRequired * @NoCSRFRequired - * @param Array $poll + * @param string $type + * @param string $title * @return DataResponse */ @@ -161,7 +229,8 @@ use OCA\Polls\Service\PollService; * write * @NoAdminRequired * @NoCSRFRequired - * @param Array $poll + * @param integer $pollId + * @param array $poll * @return DataResponse */ diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index 4f147fad..cceb7aa6 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -71,7 +71,7 @@ class ShareApiController extends ApiController { */ public function list($pollId) { try { - return new DataResponse($this->shareService->list($pollId), Http::STATUS_OK); + return new DataResponse(['shares' => $this->shareService->list($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'No shares for poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -90,7 +90,7 @@ class ShareApiController extends ApiController { */ public function get($token) { try { - return new DataResponse($this->shareService->get($token), Http::STATUS_OK); + return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Token ' . $token . ' not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -111,7 +111,7 @@ class ShareApiController extends ApiController { */ public function add($pollId, $type, $userId = '', $userEmail = '') { try { - return new DataResponse($this->shareService->add($pollId, $type, $userId, $userEmail), Http::STATUS_CREATED); + return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED); } catch (\Exception $e) { return new DataResponse(['error' => $e], Http::STATUS_CONFLICT); } catch (NotAuthorizedException $e) { @@ -131,7 +131,7 @@ class ShareApiController extends ApiController { public function delete($token) { try { - return new DataResponse($this->shareService->remove($token), Http::STATUS_OK); + return new DataResponse(['share' => $this->shareService->remove($token)], Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (Exception $e) { diff --git a/lib/Controller/VoteApiController.php b/lib/Controller/VoteApiController.php index 03caea0f..21d7762d 100644 --- a/lib/Controller/VoteApiController.php +++ b/lib/Controller/VoteApiController.php @@ -73,7 +73,7 @@ class VoteApiController extends ApiController { */ public function list($pollId) { try { - return new DataResponse($this->voteService->list($pollId), Http::STATUS_OK); + return new DataResponse(['votes' => $this->voteService->list($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'No votes'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { @@ -94,7 +94,7 @@ class VoteApiController extends ApiController { */ public function set($pollId, $pollOptionText, $setTo) { try { - return new DataResponse($this->voteService->set($pollId, $pollOptionText, $setTo), Http::STATUS_OK); + return new DataResponse(['vote' => $this->voteService->set($pollId, $pollOptionText, $setTo)], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 55b12fa2..8fa2d056 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -135,6 +135,20 @@ class Poll extends Entity implements JsonSerializable { ]; } + public function deserializeArray($array) { + $this->setTitle(isset($array['title']) ? $array['title'] : $this->getTitle()); + $this->setDescription(isset($array['description']) ? $array['description'] : $this->getDescription()); + $this->setAccess(isset($array['access']) ? $array['access'] : $this->getAccess()); + $this->setExpire(isset($array['expire']) ? $array['expire'] : $this->getExpire()); + $this->setAnonymous(isset($array['anonymous']) ? $array['anonymous'] : $this->getAnonymous()); + $this->setAllowMaybe(isset($array['allowMaybe']) ? $array['allowMaybe'] : $this->getAllowMaybe()); + $this->setVoteLimit(isset($array['voteLimit']) ? $array['voteLimit'] : $this->getVoteLimit()); + $this->setShowResults(isset($array['showResults']) ? $array['showResults'] : $this->getShowResults()); + $this->setDeleted(isset($array['deleted']) ? $array['deleted'] : $this->getDeleted()); + $this->setAdminAccess(isset($array['adminAccess']) ? $array['adminAccess'] : $this->getAdminAccess()); + return $this; + } + private function getDisplayName() { if (\OC::$server->getUserManager()->get($this->owner) instanceof IUser) { diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index 31a6f36d..d23c8595 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -33,12 +33,8 @@ use OCP\ILogger; - use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; - use OCA\Polls\Service\CommentService; - use OCA\Polls\Service\OptionService; - use OCA\Polls\Service\ShareService; - use OCA\Polls\Service\VoteService; + use OCA\Polls\Db\Poll; use OCA\Polls\Service\LogService; use OCA\Polls\Model\Acl; @@ -48,20 +44,14 @@ private $pollMapper; private $poll; private $logService; - private $commentService; - private $optionService; - private $shareService; - private $voteService; private $acl; /** * PollController constructor. + * @param ILogger $logger * @param PollMapper $pollMapper + * @param Poll $poll * @param LogService $logService - * @param CommentService $commentService - * @param OptionService $optionService - * @param ShareService $shareService - * @param VoteService $voteService * @param Acl $acl */ @@ -70,20 +60,12 @@ PollMapper $pollMapper, Poll $poll, LogService $logService, - CommentService $commentService, - OptionService $optionService, - ShareService $shareService, - VoteService $voteService, Acl $acl ) { $this->logger = $logger; $this->pollMapper = $pollMapper; $this->poll = $poll; $this->logService = $logService; - $this->commentService = $commentService; - $this->optionService = $optionService; - $this->shareService = $shareService; - $this->voteService = $voteService; $this->acl = $acl; } @@ -120,47 +102,30 @@ * @param integer $pollId * @return array */ - public function get($pollId = 0, $token = '') { + public function get($pollId) { - if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { + if (!$this->acl->setPollId($pollId)->getAllowView()) { throw new NotAuthorizedException; } - $this->poll = $this->pollMapper->find($this->acl->getPollId()); + return $this->pollMapper->find($pollId); - try { - $comments = $this->commentService->list($this->poll->getId(), $token); - } catch (Exception $e) { - $comments = []; - } - - try { - $options = $this->optionService->list($this->poll->getId(), $token); - } catch (Exception $e) { - $options = []; + } - } + /** + * get + * @NoAdminRequired + * @param integer $pollId + * @return array + */ + public function getByToken($token) { - try { - $votes = $this->voteService->list($this->poll->getId(), $token); - } catch (Exception $e) { - $votes = []; + if (!$this->acl->setToken($token)->getAllowView()) { + throw new NotAuthorizedException; } - try { - $shares = $this->shareService->list($this->poll->getId()); - } catch (Exception $e) { - $shares = []; - } + return $this->pollMapper->find($this->acl->getPollId()); - return [ - 'acl' => $this->acl, - 'poll' => $this->poll, - 'comments' => $comments, - 'options' => $options, - 'shares' => $shares, - 'votes' => $votes - ]; } /** @@ -212,7 +177,8 @@ * write * @NoAdminRequired * @NoCSRFRequired - * @param Array $poll + * @param string $type + * @param string $title * @return Poll */ @@ -282,17 +248,7 @@ if (isset($poll['title']) && !$poll['title']) { throw new EmptyTitleException('Title must not be empty'); } - - $this->poll->setTitle($poll['title'] ? $poll['title'] : $this->poll->getTitle()); - $this->poll->setDescription(isset($poll['description']) ? $poll['description'] : $this->poll->getDescription()); - $this->poll->setAccess(isset($poll['access']) ? $poll['access'] : $this->poll->getAccess()); - $this->poll->setExpire(isset($poll['expire']) ? $poll['expire'] : $this->poll->getExpire()); - $this->poll->setAnonymous(isset($poll['anonymous']) ? $poll['anonymous'] : $this->poll->getAnonymous()); - $this->poll->setAllowMaybe(isset($poll['allowMaybe']) ? $poll['allowMaybe'] : $this->poll->getAllowMaybe()); - $this->poll->setVoteLimit(isset($poll['voteLimit']) ? $poll['voteLimit'] : $this->poll->getVoteLimit()); - $this->poll->setShowResults(isset($poll['showResults']) ? $poll['showResults'] : $this->poll->getShowResults()); - $this->poll->setDeleted(isset($poll['deleted']) ? $poll['deleted'] : $this->poll->getDeleted()); - $this->poll->setAdminAccess(isset($poll['adminAccess']) ? $poll['adminAccess'] : $this->poll->getAdminAccess()); + $this->poll->deserializeArray($poll); $this->pollMapper->update($this->poll); $this->logService->setLog($this->poll->getId(), 'updatePoll'); -- cgit v1.2.3 From 1230e822b36017a8c693a780f379705e899d1140 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Thu, 2 Jul 2020 10:50:09 +0200 Subject: fix public poll --- lib/Controller/PollController.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 071dde17..27f9c4be 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -118,7 +118,7 @@ class PollController extends Controller { public function get($pollId, $token) { try { if ($token) { - $poll = $this->pollService->get(0, $token); + $poll = $this->pollService->getByToken($token); $acl = $this->acl->setToken($token); } else { $poll = $this->pollService->get($pollId); @@ -134,25 +134,25 @@ class PollController extends Controller { } try { - $comments = $this->commentService->list($pollId, $token); + $comments = $this->commentService->list($this->acl->getPollId(), $token); } catch (Exception $e) { $comments = []; } try { - $options = $this->optionService->list($pollId, $token); + $options = $this->optionService->list($this->acl->getPollId(), $token); } catch (Exception $e) { $options = []; } try { - $votes = $this->voteService->list($pollId, $token); + $votes = $this->voteService->list($this->acl->getPollId(), $token); } catch (Exception $e) { $votes = []; } try { - $shares = $this->shareService->list($pollId); + $shares = $this->shareService->list($this->acl->getPollId()); } catch (Exception $e) { $shares = []; } -- cgit v1.2.3