From 6e3792a58903103ecde77d748bc92c2d9dea7aa6 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 11 Jul 2020 16:54:09 +0200 Subject: Update backend --- lib/Controller/AclController.php | 80 -------- lib/Controller/CommentApiController.php | 16 +- lib/Controller/CommentController.php | 28 +-- lib/Controller/OptionApiController.php | 74 ++++--- lib/Controller/OptionController.php | 85 ++++---- lib/Controller/PageController.php | 1 + lib/Controller/PollApiController.php | 52 ++--- lib/Controller/PollController.php | 114 +++++------ lib/Controller/ShareApiController.php | 44 ++--- lib/Controller/ShareController.php | 69 +++---- lib/Controller/SubscriptionApiController.php | 27 ++- lib/Controller/SubscriptionController.php | 26 ++- lib/Controller/SystemController.php | 37 ++-- lib/Controller/VoteApiController.php | 27 +-- lib/Controller/VoteController.php | 40 ++-- lib/Cron/NotificationCron.php | 5 +- lib/Db/Comment.php | 4 +- lib/Db/Log.php | 8 +- lib/Db/Option.php | 10 +- lib/Db/Poll.php | 30 +-- lib/Db/Share.php | 4 +- lib/Db/Subscription.php | 4 +- lib/Db/Vote.php | 4 +- lib/Exceptions/BadRequestException.php | 40 ++++ lib/Model/Acl.php | 15 +- lib/Service/AnonymizeService.php | 11 +- lib/Service/CommentService.php | 38 ++-- lib/Service/LogService.php | 48 ++--- lib/Service/MailService.php | 33 +++- lib/Service/OptionService.php | 279 ++++++++++++++++++++------- lib/Service/PollService.php | 192 ++++++++++-------- lib/Service/ShareService.php | 58 +++--- lib/Service/SubscriptionService.php | 18 +- lib/Service/VoteService.php | 50 +++-- 34 files changed, 867 insertions(+), 704 deletions(-) delete mode 100644 lib/Controller/AclController.php create mode 100644 lib/Exceptions/BadRequestException.php (limited to 'lib') diff --git a/lib/Controller/AclController.php b/lib/Controller/AclController.php deleted file mode 100644 index 2bb1561f..00000000 --- a/lib/Controller/AclController.php +++ /dev/null @@ -1,80 +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\Controller; - -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataResponse; - -use OCP\IRequest; - -use OCA\Polls\Model\Acl; - - -class AclController extends Controller { - - private $acl; - - /** - * PageController constructor. - * @param string $appName - * @param IRequest $request - * @param Acl $acl - */ - public function __construct( - $appName, - IRequest $request, - Acl $acl - ) { - parent::__construct($appName, $request); - $this->acl = $acl; - } - - /** - * Read acl with poll id for current user - * @NoAdminRequired - * @param integer $pollId - * @return array - */ - public function get($id) { - $acl = $this->acl->setPollId($id); - // $acl = $this->acl->setUserId('dartcafe'); - return new DataResponse($acl, Http::STATUS_OK); - } - - /** - * Read acl with share token - * @NoAdminRequired - * @PublicPage - * @NoCSRFRequired - * @param integer $pollId - * @return array - */ - public function getByToken($token) { - $acl = $this->acl->setToken($token); - return new DataResponse($acl, Http::STATUS_OK); - - } - -} diff --git a/lib/Controller/CommentApiController.php b/lib/Controller/CommentApiController.php index 6cc56bba..16d3b66a 100644 --- a/lib/Controller/CommentApiController.php +++ b/lib/Controller/CommentApiController.php @@ -25,24 +25,25 @@ namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; -use \OCP\IURLGenerator; +use OCP\IURLGenerator; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; -use OCA\Polls\Exceptions\NotAuthorizedException; - use OCA\Polls\Service\CommentService; class CommentApiController extends ApiController { + /** @var CommentService */ private $commentService; + /** - * CommentApiController constructor. + * CommentApiController constructor * @param string $appName * @param IRequest $request * @param CommentService $commentService @@ -62,12 +63,11 @@ class CommentApiController extends ApiController { } /** - * get * Read all comments of a poll based on the poll id and return list as array * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function list($pollId) { @@ -81,7 +81,7 @@ class CommentApiController extends ApiController { } /** - * Write a new comment to the db and returns the new comment as array + * Add comment * @NoAdminRequired * @CORS * @NoCSRFRequired @@ -100,7 +100,7 @@ class CommentApiController extends ApiController { } /** - * Delete Comment + * Delete comment * @NoAdminRequired * @CORS * @NoCSRFRequired diff --git a/lib/Controller/CommentController.php b/lib/Controller/CommentController.php index 6f240f05..d68d875e 100644 --- a/lib/Controller/CommentController.php +++ b/lib/Controller/CommentController.php @@ -25,25 +25,24 @@ namespace OCA\Polls\Controller; 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 OCA\Polls\Exceptions\NotAuthorizedException; - use OCA\Polls\Service\CommentService; class CommentController extends Controller { + /** @var CommentService */ private $commentService; /** - * CommentController constructor. + * CommentController constructor * @param string $appName * @param IRequest $request * @param CommentService $commentService @@ -58,28 +57,15 @@ class CommentController extends Controller { $this->commentService = $commentService; } - /** - * get - * Read all comments of a poll based on the poll id and return list as array - * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId - * @return DataResponse - */ - public function list($pollId) { - 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 + // * Read all comments of a poll based on the poll id and return list as array // * @NoAdminRequired - // * @NoCSRFRequired - // * @PublicPage + // * @param int $pollId // * @param string $token // * @return DataResponse // */ - // public function getByToken($token) { - // return new DataResponse($this->commentService->get(0, $token), 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 index e3396a3e..f0884435 100644 --- a/lib/Controller/OptionApiController.php +++ b/lib/Controller/OptionApiController.php @@ -23,21 +23,21 @@ namespace OCA\Polls\Controller; -use Exception; -use Doctrine\DBAL\Exception\UniqueConstraintViolationException; +use \Exception; +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\Service\OptionService; class OptionApiController extends ApiController { + /** @var OptionService */ private $optionService; /** @@ -65,14 +65,14 @@ class OptionApiController extends ApiController { * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function list($pollId) { try { 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); + return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } @@ -80,26 +80,18 @@ class OptionApiController extends ApiController { /** - * Add a new Option to poll + * Add a new option * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @param string $pollOptionText - * @param integer $timestamp + * @param int $timestamp * @return DataResponse */ - public function add($pollId, $pollOptionText = '', $timestamp = 0) { - $option = [ - 'pollId' => $pollId, - 'pollOptionText' => $pollOptionText, - 'timestamp' => $timestamp - ]; - + public function add($pollId, $timestamp = 0, $pollOptionText = '') { try { - 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); + return new DataResponse(['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText)], Http::STATUS_CREATED); } catch (UniqueConstraintViolationException $e) { return new DataResponse(['error' => 'Option exists'], Http::STATUS_CONFLICT); } catch (NotAuthorizedException $e) { @@ -109,27 +101,27 @@ class OptionApiController extends ApiController { /** - * Update poll option + * Update option * @NoAdminRequired * @CORS * @NoCSRFRequired * @param array $option * @return DataResponse */ - public function update($option) { + public function update($optionId, $timestamp = 0 , $pollOptionText = '') { try { - return new DataResponse(['option' => $this->optionService->update($option)], Http::STATUS_OK); + return new DataResponse(['option' => $this->optionService->update($optionId, $timestamp, $pollOptionText, $order)], Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } /** - * Remove a single option + * Delete option * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $optionId + * @param int $optionId * @return DataResponse */ public function delete($optionId) { @@ -141,4 +133,38 @@ class OptionApiController extends ApiController { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } + + /** + * Switch option confirmation + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param int $optionId + * @return DataResponse + */ + public function confirm($optionId) { + try { + return new DataResponse(['option' => $this->optionService->confirm($optionId)], Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse(['error' => 'Option does not exist'], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } + + /** + * Set order position for option + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param array $option + * @return DataResponse + */ + public function setOrder($optionId, $order) { + try { + return new DataResponse(['option' => $this->optionService->setOrder($optionId, $order)], Http::STATUS_OK); + } catch (NotAuthorizedException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } } diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 0ec24092..1fa382f6 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -26,16 +26,15 @@ namespace OCA\Polls\Controller; use Exception; use OCP\IRequest; + use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; - -use OCA\Polls\Exceptions\NotAuthorizedException; - use OCA\Polls\Service\OptionService; class OptionController extends Controller { + /** @var OptionService */ private $optionService; /** @@ -54,69 +53,73 @@ class OptionController extends Controller { $this->optionService = $optionService; } - /** - * Get all options of given poll - * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId - * @return DataResponse - */ - public function list($pollId) { - return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); - } - + // /** + // * Get all options of given poll + // * @NoAdminRequired + // * @param int $pollId + // * @return DataResponse + // */ + // public function list($pollId) { + // return new DataResponse($this->optionService->list($pollId), Http::STATUS_OK); + // } + // + // + // /** + // * Get all options specified by token + // * Read all options of a poll based on a share token and return list as array + // * @NoAdminRequired + // * @PublicPage + // * @param string $token + // * @return DataResponse + // */ + // public function listByToken($token) { + // return new DataResponse($this->optionService->list(0, $token), Http::STATUS_OK); + // } /** - * 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) { - return new DataResponse($this->optionService->list(0, $token), Http::STATUS_OK); + * Add a new option + * @NoAdminRequired + * @param array $option + * @return DataResponse + */ + public function add($pollId, $timestamp = 0, $pollOptionText = '') { + return new DataResponse($this->optionService->add($pollId, $timestamp, $pollOptionText), Http::STATUS_OK); } /** - * Add a new Option to poll + * Update option * @NoAdminRequired - * @NoCSRFRequired * @param array $option * @return DataResponse */ - public function add($option) { - return new DataResponse($this->optionService->add($option), Http::STATUS_OK); + public function update($optionId, $timestamp, $pollOptionText) { + return new DataResponse($this->optionService->update($optionId, $timestamp, $pollOptionText), Http::STATUS_OK); } /** - * Update poll option + * Delete option * @NoAdminRequired - * @NoCSRFRequired - * @param array $option + * @param Option $option * @return DataResponse */ - public function update($option) { - return new DataResponse($this->optionService->update($option), Http::STATUS_OK); + public function delete($optionId) { + return new DataResponse($this->optionService->delete($optionId), Http::STATUS_OK); } /** - * Remove a single option + * Switch option confirmation * @NoAdminRequired - * @NoCSRFRequired - * @param Option $option + * @param int $optionId * @return DataResponse */ - public function remove($option) { - return new DataResponse($this->optionService->delete($option['id']), Http::STATUS_OK); + public function confirm($optionId) { + return new DataResponse($this->optionService->confirm($optionId), Http::STATUS_OK); } /** - * Set order by order of the given array + * Reorder options * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @param Array $options * @return DataResponse */ diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 71e0a7cd..587a4184 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -37,6 +37,7 @@ use OCP\IURLGenerator; class PageController extends Controller { + /** @var IURLGenerator */ private $urlGenerator; /** diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index 3c8acc9d..20eb2ea0 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -32,7 +32,6 @@ use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; - use OCP\ILogger; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -41,35 +40,32 @@ class PollApiController extends ApiController { - private $logger; + + /** @var PollService */ private $pollService; /** - * PollController constructor. + * PollApiController 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 + * Get list of polls * @NoAdminRequired - * @NoCSRFRequired * @CORS + * @NoCSRFRequired * @return DataResponse */ @@ -85,11 +81,12 @@ /** - * get + * get poll configuration * @NoAdminRequired + * @CORS * @NoCSRFRequired - * @param integer $pollId - * @return array + * @param int $pollId + * @return DataResponse */ public function get($pollId) { try { @@ -102,9 +99,10 @@ } /** - * write + * Add poll * @NoAdminRequired * @NoCSRFRequired + * @CORS * @param Array $poll * @return DataResponse */ @@ -122,10 +120,12 @@ } /** - * write + * Update poll configuration * @NoAdminRequired + * @CORS * @NoCSRFRequired - * @param Array $poll + * @param int $pollId + * @param array $poll * @return DataResponse */ @@ -146,14 +146,15 @@ } /** - * delete + * Switch deleted status (move to deleted polls) * @NoAdminRequired + * @CORS * @NoCSRFRequired - * @param Array $poll + * @param int $pollId * @return DataResponse */ - public function delete($pollId) { + public function trash($pollId) { try { return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { @@ -164,14 +165,15 @@ } /** - * deletePermanently + * Delete poll * @NoAdminRequired + * @CORS * @NoCSRFRequired - * @param Array $poll + * @param int $pollId * @return DataResponse */ - public function deletePermanently($pollId) { + public function delete($pollId) { try { return new DataResponse(['poll' => $this->pollService->deletePermanently($pollId)], Http::STATUS_OK); } catch (DoesNotExistException $e) { @@ -183,10 +185,11 @@ } /** - * clone + * Clone poll * @NoAdminRequired + * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function clone($pollId) { @@ -200,8 +203,9 @@ } /** - * enum + * Get valid values for configuration options * @NoAdminRequired + * @CORS * @NoCSRFRequired * @param Array $poll * @return DataResponse diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 27f9c4be..834fb391 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -21,7 +21,7 @@ * */ - namespace OCA\Polls\Controller; +namespace OCA\Polls\Controller; use Exception; use OCP\AppFramework\Db\DoesNotExistException; @@ -32,7 +32,6 @@ 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; @@ -46,19 +45,28 @@ use OCA\Polls\Model\Acl; class PollController extends Controller { - private $logger; + /** @var PollService */ private $pollService; + + /** @var CommentService */ private $commentService; + + /** @var OptionService */ private $optionService; + + /** @var ShareService */ private $shareService; + + /** @var VoteService */ private $voteService; + + /** @var Acl */ private $acl; /** * PollController constructor. * @param string $appName * @param IRequest $request - * @param ILogger $logger * @param PollService $pollService * @param CommentService $commentService * @param OptionService $optionService @@ -70,7 +78,6 @@ class PollController extends Controller { public function __construct( string $appName, IRequest $request, - ILogger $logger, PollService $pollService, CommentService $commentService, OptionService $optionService, @@ -79,7 +86,6 @@ class PollController extends Controller { Acl $acl ) { parent::__construct($appName, $request); - $this->logger = $logger; $this->pollService = $pollService; $this->commentService = $commentService; $this->optionService = $optionService; @@ -90,9 +96,8 @@ class PollController extends Controller { /** - * list + * Get list of polls * @NoAdminRequired - * @NoCSRFRequired * @return DataResponse */ @@ -108,12 +113,12 @@ class PollController extends Controller { /** - * get + * get complete poll * @NoAdminRequired - * @NoCSRFRequired * @PublicPage - * @param integer $pollId - * @return array + * @param int $pollId + * @param string $token + * @return DataResponse */ public function get($pollId, $token) { try { @@ -125,8 +130,6 @@ class PollController extends Controller { $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) { @@ -134,25 +137,25 @@ class PollController extends Controller { } try { - $comments = $this->commentService->list($this->acl->getPollId(), $token); + $comments = $this->commentService->list($poll->getId(), $token); } catch (Exception $e) { $comments = []; } try { - $options = $this->optionService->list($this->acl->getPollId(), $token); + $options = $this->optionService->list($poll->getId(), $token); } catch (Exception $e) { $options = []; } try { - $votes = $this->voteService->list($this->acl->getPollId(), $token); + $votes = $this->voteService->list($poll->getId(), $token); } catch (Exception $e) { $votes = []; } try { - $shares = $this->shareService->list($this->acl->getPollId()); + $shares = $this->shareService->list($poll->getId()); } catch (Exception $e) { $shares = []; } @@ -168,98 +171,95 @@ class PollController extends Controller { } /** - * delete + * Add poll * @NoAdminRequired - * @NoCSRFRequired - * @param Array $poll + * @param string $type + * @param string $title * @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(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); + return new DataResponse($this->pollService->add($type, $title), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (InvalidPollTypeException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } /** - * deletePermanently + * Update poll configuration * @NoAdminRequired - * @NoCSRFRequired - * @param Array $poll + * @param int $pollId + * @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(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (InvalidAccessException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (InvalidShowResultsException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (EmptyTitleException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } - /** - * add + * Switch deleted status (move to deleted polls) * @NoAdminRequired - * @NoCSRFRequired - * @param string $type - * @param string $title + * @param int $pollId * @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(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); - } catch (InvalidPollTypeException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); - } catch (EmptyTitleException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } /** - * write + * Delete poll * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId - * @param array $poll + * @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(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); - } catch (InvalidAccessException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); - } catch (InvalidShowResultsException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); - } catch (EmptyTitleException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } /** - * clone + * Clone poll * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function clone($pollId) { try { - return new DataResponse($this->pollService->clone($pollId), Http::STATUS_OK); + $poll = $this->pollService->clone($pollId); + $this->optionService->clone($pollId, $poll->getId()); + + return new DataResponse($poll, Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index 3d7912da..261262b8 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -28,7 +28,6 @@ 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; @@ -39,13 +38,15 @@ use OCA\Polls\Service\MailService; class ShareApiController extends ApiController { + /** @var ShareService */ private $shareService; + + /** @var MailService */ private $mailService; /** - * ShareController constructor. + * ShareApiController constructor * @param string $appName - * @param string $userId * @param IRequest $request * @param MailService $mailService * @param ShareService $shareService @@ -66,12 +67,11 @@ class ShareApiController extends ApiController { } /** - * list * Read all shares of a poll based on the poll id and return list as array * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function list($pollId) { @@ -85,11 +85,10 @@ class ShareApiController extends ApiController { } /** - * get share by token - * Get pollId by token + * Get share by token * @NoAdminRequired - * @NoCSRFRequired * @CORS + * @NoCSRFRequired * @param string $token * @return DataResponse */ @@ -104,7 +103,7 @@ class ShareApiController extends ApiController { } /** - * Write a new share to the db and returns the new share as array + * Add share * @NoAdminRequired * @CORS * @NoCSRFRequired @@ -117,47 +116,46 @@ class ShareApiController extends ApiController { public function add($pollId, $type, $userId = '', $userEmail = '') { try { 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) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (Exception $e) { + return new DataResponse(['error' => $e], Http::STATUS_CONFLICT); } } /** - * SendInvitation - * Sent invitation mails for a share + * Delete share * @NoAdminRequired * @CORS * @NoCSRFRequired * @param string $token * @return DataResponse */ - public function sendInvitation($token) { + + public function delete($token) { try { - return new DataResponse($this->mailService->sendInvitationMail($token), Http::STATUS_OK); - } catch (Exception $e) { + return new DataResponse(['share' => $this->shareService->delete($token)], Http::STATUS_OK); + } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (DoesNotExistException $e) { + return new DataResponse($e, Http::STATUS_NOT_FOUND); } } /** - * delete share + * Sent invitation mails for a share * @NoAdminRequired * @CORS * @NoCSRFRequired * @param string $token * @return DataResponse */ - - public function delete($token) { + public function sendInvitation($token) { try { - return new DataResponse(['share' => $this->shareService->remove($token)], Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + return new DataResponse($this->mailService->sendInvitationMail($token), Http::STATUS_OK); } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } } diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index bd3dd187..bb55c5b8 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -30,54 +30,47 @@ use OCA\Polls\Exceptions\InvalidUsername; use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; - - -use OCA\Polls\Model\Acl; use OCA\Polls\Service\ShareService; use OCA\Polls\Service\MailService; class ShareController extends Controller { - private $logger; + /** @var ShareService */ private $shareService; + + /** @var MailService */ private $mailService; - private $userId; /** * ShareController constructor. * @param string $appName - * @param string $userId * @param IRequest $request - * @param ILogger $logger * @param MailService $mailService * @param ShareService $shareService */ public function __construct( string $appName, - $userId, IRequest $request, - ILogger $logger, MailService $mailService, ShareService $shareService ) { parent::__construct($appName, $request); - $this->logger = $logger; - $this->userId = $userId; $this->shareService = $shareService; $this->mailService = $mailService; } /** - * Write a new share to the db and returns the new share as array + * Add share * @NoAdminRequired - * @NoCSRFRequired * @param int $pollId - * @param Array $share + * @param int $pollId + * @param string $type + * @param string $userId + * @param string $userEmail * @return DataResponse */ public function add($pollId, $type, $userId = '', $userEmail = '') { @@ -91,19 +84,18 @@ class ShareController extends Controller { } /** - * createPersonalShare - * Write a new share to the db and returns the new share as array + * Create a personal share from a public share + * or update an email share with the username * @NoAdminRequired * @PublicPage - * @NoCSRFRequired * @param string $token * @param string $userName * @return DataResponse */ - public function createPersonalShare($token, $userName) { + public function personal($token, $userName) { try { - return new DataResponse($this->shareService->createPersonalShare($token, $userName), Http::STATUS_CREATED); + return new DataResponse($this->shareService->personal($token, $userName), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidUsername $e) { @@ -115,43 +107,36 @@ class ShareController extends Controller { } /** - * SendInvitation - * Sent invitation mails for a share + * Delete share * @NoAdminRequired - * @PublicPage - * @NoCSRFRequired * @param string $token * @return DataResponse */ - public function sendInvitation($token) { + + public function delete($token) { try { - $sentResult = $this->mailService->sendInvitationMail($token); - $share = $this->shareService->get($token); - return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK); - } catch (Exception $e) { + return new DataResponse($this->shareService->delete($token), Http::STATUS_OK); + } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (Exception $e) { + return new DataResponse($e, Http::STATUS_NOT_FOUND); } } /** - * remove - * remove share + * Sent invitation mails for a share * @NoAdminRequired - * @NoCSRFRequired - * @param Share $share + * @PublicPage + * @param string $token * @return DataResponse */ - - public function delete($share) { + public function sendInvitation($token) { try { - return new DataResponse(array( - 'action' => 'deleted', - 'shareId' => $this->shareService->remove($share['token'])->getId() - ), Http::STATUS_OK); - } catch (NotAuthorizedException $e) { - return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + $sentResult = $this->mailService->sendInvitationMail($token); + $share = $this->shareService->get($token); + return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK); } catch (Exception $e) { - return new DataResponse($e, Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } } } diff --git a/lib/Controller/SubscriptionApiController.php b/lib/Controller/SubscriptionApiController.php index 3a23d403..13ffe3eb 100644 --- a/lib/Controller/SubscriptionApiController.php +++ b/lib/Controller/SubscriptionApiController.php @@ -28,7 +28,6 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; @@ -38,25 +37,20 @@ use OCA\Polls\Service\SubscriptionService; class SubscriptionApiController extends ApiController { - private $userId; + /** @var SubscriptionService */ private $subscriptionService; - private $logger; /** - * SubscriptionController constructor. + * SubscriptionApiController 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 + IRequest $request ) { parent::__construct($appName, @@ -64,17 +58,18 @@ class SubscriptionApiController extends ApiController { 'PUT, GET, DELETE', 'Authorization, Content-Type, Accept', 1728000); - $this->userId = $userId; $this->subscriptionService = $subscriptionService; - $this->logger = $logger; } /** + * Get subscription status * @NoAdminRequired * CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse + * @throws DoesNotExistException + * @throws NotAuthorizedException */ public function get($pollId) { try { @@ -88,10 +83,12 @@ class SubscriptionApiController extends ApiController { } /** + * Subscribe to poll * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId + * @throws NotAuthorizedException */ public function subscribe($pollId) { try { @@ -102,10 +99,12 @@ class SubscriptionApiController extends ApiController { } } /** + * Unsubscribe from poll * @NoAdminRequired * @CORS * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId + * @throws NotAuthorizedException */ public function unsubscribe($pollId) { try { diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php index e90f4a66..f2517237 100644 --- a/lib/Controller/SubscriptionController.php +++ b/lib/Controller/SubscriptionController.php @@ -28,7 +28,6 @@ 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; @@ -37,38 +36,32 @@ use OCA\Polls\Service\SubscriptionService; class SubscriptionController extends Controller { - private $userId; + /** @var SubscriptionService */ 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 - + IRequest $request ) { parent::__construct($appName, $request); - $this->userId = $userId; $this->subscriptionService = $subscriptionService; - $this->logger = $logger; } /** + * Get subscription status * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse + * @throws DoesNotExistException + * @throws NotAuthorizedException */ public function get($pollId) { try { @@ -81,9 +74,12 @@ class SubscriptionController extends Controller { } /** + * Switch subscription status * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId + * @param int $subscribed + * @return DataResponse + * @throws NotAuthorizedException */ public function set($pollId, $subscribed) { try { diff --git a/lib/Controller/SystemController.php b/lib/Controller/SystemController.php index 44dfbf8a..b1e2c606 100644 --- a/lib/Controller/SystemController.php +++ b/lib/Controller/SystemController.php @@ -36,16 +36,25 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; -use OCP\ILogger; class SystemController extends Controller { + /** @var string */ private $userId; - private $logger; + + /** @var IConfig */ private $systemConfig; + + /** @var IGroupManager */ private $groupManager; + + /** @var IUserManager */ private $userManager; + + /** @var VoteMapper */ private $voteMapper; + + /** @var ShareMapper */ private $shareMapper; /** @@ -53,7 +62,6 @@ class SystemController extends Controller { * @param string $appName * @param $userId * @param IRequest $request - * @param ILogger $logger * @param IConfig $systemConfig * @param IGroupManager $groupManager * @param IUserManager $userManager @@ -64,7 +72,6 @@ class SystemController extends Controller { string $appName, $userId, IRequest $request, - ILogger $logger, IConfig $systemConfig, IGroupManager $groupManager, IUserManager $userManager, @@ -74,7 +81,6 @@ class SystemController extends Controller { parent::__construct($appName, $request); $this->voteMapper = $voteMapper; $this->shareMapper = $shareMapper; - $this->logger = $logger; $this->userId = $userId; $this->systemConfig = $systemConfig; $this->groupManager = $groupManager; @@ -85,7 +91,7 @@ class SystemController extends Controller { * Validate string as email address * @NoAdminRequired * @param string $query - * @return Boolval + * @return boolval */ private function isValidEmail($email) { return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) ? false : true; @@ -207,7 +213,6 @@ class SystemController extends Controller { } } - } return new DataResponse([ @@ -303,13 +308,13 @@ class SystemController extends Controller { ], Http::STATUS_OK); } - public function getDisplayName() { - $this->userManager = \OC::$server->getUserManager(); - - if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) { - return \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); - } else { - return $this->userId; - } - } + // public function getDisplayName() { + // $this->userManager = \OC::$server->getUserManager(); + // + // if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) { + // return \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); + // } else { + // return $this->userId; + // } + // } } diff --git a/lib/Controller/VoteApiController.php b/lib/Controller/VoteApiController.php index 21d7762d..8f8976ba 100644 --- a/lib/Controller/VoteApiController.php +++ b/lib/Controller/VoteApiController.php @@ -28,7 +28,6 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\IRequest; -use OCP\ILogger; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -37,20 +36,18 @@ use OCA\Polls\Service\VoteService; class VoteApiController extends ApiController { - private $logger; + /** @var VoteService */ private $voteService; /** - * VoteController constructor. + * VoteAPIController 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, @@ -59,16 +56,14 @@ class VoteApiController extends ApiController { '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 + * @NoCSRFRequired + * @param int $pollId * @return DataResponse */ public function list($pollId) { @@ -82,21 +77,19 @@ class VoteApiController extends ApiController { } /** - * set + * Set vote answer * @NoAdminRequired - * @NoCSRFRequired * @CORS - * @param integer $pollId - * @param Array $option - * @param string $userId + * @NoCSRFRequired + * @param int $optionId * @param string $setTo * @return DataResponse */ - public function set($pollId, $pollOptionText, $setTo) { + public function set($optionId, $setTo) { try { - return new DataResponse(['vote' => $this->voteService->set($pollId, $pollOptionText, $setTo)], Http::STATUS_OK); + return new DataResponse(['vote' => $this->voteService->set($optionId, $setTo)], Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option or poll not found'], Http::STATUS_NOT_FOUND); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } diff --git a/lib/Controller/VoteController.php b/lib/Controller/VoteController.php index f666c004..02ccc9a0 100644 --- a/lib/Controller/VoteController.php +++ b/lib/Controller/VoteController.php @@ -27,7 +27,6 @@ namespace OCA\Polls\Controller; use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; -use OCP\ILogger; use OCP\IRequest; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -38,34 +37,29 @@ use OCA\Polls\Service\VoteService; class VoteController extends Controller { + /** @var VoteService */ private $voteService; - private $logger; /** - * VoteController constructor. + * VoteController constructor * @param string $appName * @param IRequest $request - * @param ILogger $logger * @param VoteService $voteService */ public function __construct( string $appName, - ILogger $logger, IRequest $request, VoteService $voteService ) { parent::__construct($appName, $request); - $this->logger = $logger; $this->voteService = $voteService; } /** - * Get all votes of given poll * Read all votes of a poll based on the poll id and return list as array * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return DataResponse */ public function get($pollId) { @@ -82,33 +76,28 @@ class VoteController extends Controller { * set * @NoAdminRequired * @NoCSRFRequired - * @param integer $pollId - * @param Array $option - * @param string $userId + * @param int $optionId * @param string $setTo * @return DataResponse */ - public function set($pollId, $option, $setTo) { + public function set($optionId, $setTo) { try { - return new DataResponse($this->voteService->set($pollId, $option['pollOptionText'], $setTo), Http::STATUS_OK); + return new DataResponse($this->voteService->set($optionId, $setTo), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { - return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'Option or poll not found'], Http::STATUS_NOT_FOUND); } } - /** - * delete + * Remove user from poll * @NoAdminRequired - * @NoCSRFRequired - * @param integer $voteId * @param string $userId - * @param integer $pollId + * @param int $pollId * @return DataResponse */ - public function delete($userId, $pollId) { + public function delete($pollId, $userId) { try { return new DataResponse($this->voteService->delete($pollId, $userId), Http::STATUS_OK); } catch (NotAuthorizedException $e) { @@ -123,18 +112,17 @@ class VoteController extends Controller { */ /** - * setByToken + * Set vote with token * @NoAdminRequired * @PublicPage - * @NoCSRFRequired * @param Array $option * @param string $setTo * @param string $token * @return DataResponse */ - public function setByToken($option, $setTo, $token) { + public function setByToken($optionId, $setTo, $token) { try { - return new DataResponse($this->voteService->set(0, $option['pollOptionText'], $setTo, $token), Http::STATUS_OK); + return new DataResponse($this->voteService->set($optionId, $setTo, $token), Http::STATUS_OK); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (DoesNotExistException $e) { @@ -144,11 +132,9 @@ class VoteController extends Controller { } /** - * getByToken * Read all votes of a poll based on a share token and return list as array * @NoAdminRequired * @PublicPage - * @NoCSRFRequired * @param string $token * @return DataResponse */ diff --git a/lib/Cron/NotificationCron.php b/lib/Cron/NotificationCron.php index e5ddbe13..6aa4fb17 100644 --- a/lib/Cron/NotificationCron.php +++ b/lib/Cron/NotificationCron.php @@ -28,11 +28,10 @@ use OCA\Polls\Service\MailService; class NotificationCron extends TimedJob { - /** @var MailService*/ + /** @var MailService */ private $mailService; - /** @param MailService $mailService - */ + /** @param MailService $mailService */ public function __construct( MailService $mailService ) { diff --git a/lib/Db/Comment.php b/lib/Db/Comment.php index 4935c077..56514570 100644 --- a/lib/Db/Comment.php +++ b/lib/Db/Comment.php @@ -31,9 +31,9 @@ use OCP\IUser; use OCP\AppFramework\Db\Entity; /** - * @method integer getId() + * @method int getId() * @method void setId(integer $value) - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) * @method string getUserId() * @method void setUserId(string $value) diff --git a/lib/Db/Log.php b/lib/Db/Log.php index ce2492f3..92056674 100644 --- a/lib/Db/Log.php +++ b/lib/Db/Log.php @@ -29,13 +29,13 @@ use JsonSerializable; use OCP\AppFramework\Db\Entity; /** - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) - * @method integer getCreated() + * @method int getCreated() * @method void setCreated(integer $value) - * @method integer getProcessed() + * @method int getProcessed() * @method void setProcessed(integer $value) - * @method integer getUserId() + * @method int getUserId() * @method void setUserId(string $value) * @method string getDisplayName() * @method void setDisplayName(string $value) diff --git a/lib/Db/Option.php b/lib/Db/Option.php index 8f4d4fcf..d02f167e 100644 --- a/lib/Db/Option.php +++ b/lib/Db/Option.php @@ -30,17 +30,17 @@ use JsonSerializable; use OCP\AppFramework\Db\Entity; /** - * @method integer getId() + * @method int getId() * @method void setId(integer $value) - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) * @method string getPollOptionText() * @method void setPollOptionText(string $value) - * @method integer getTimestamp() + * @method int getTimestamp() * @method void setTimestamp(integer $value) - * @method integer getOrder() + * @method int getOrder() * @method void setOrder(integer $value) - * @method integer getConfirmed() + * @method int getConfirmed() * @method void setConfirmed(integer $value) */ class Option extends Entity implements JsonSerializable { diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 8fa2d056..d574a481 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -39,29 +39,29 @@ use OCP\AppFramework\Db\Entity; * @method void setDescription(string $value) * @method string getOwner() * @method void setOwner(string $value) - * @method integer getCreated() + * @method int getCreated() * @method void setCreated(integer $value) - * @method integer getExpire() + * @method int getExpire() * @method void setExpire(integer $value) - * @method integer getDeleted() + * @method int getDeleted() * @method void setDeleted(integer $value) * @method string getAccess() * @method void setAccess(string $value) - * @method integer getAnonymous() + * @method int getAnonymous() * @method void setAnonymous(integer $value) - * @method integer getFullAnonymous() + * @method int getFullAnonymous() * @method void setFullAnonymous(integer $value) - * @method integer getAllowMaybe() + * @method int getAllowMaybe() * @method void setAllowMaybe(integer $value) * @method string getOptions() * @method void setOptions(string $value) * @method string getSettings() * @method void setSettings(string $value) - * @method integer getVoteLimit() + * @method int getVoteLimit() * @method void setVoteLimit(integer $value) * @method string getShowResults() * @method void setShowResults(string $value) - * @method integer getAdminAccess() + * @method int getAdminAccess() * @method void setAdminAccess(integer $value) */ class Poll extends Entity implements JsonSerializable { @@ -135,20 +135,6 @@ 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/Db/Share.php b/lib/Db/Share.php index 1a3042d2..04b77733 100644 --- a/lib/Db/Share.php +++ b/lib/Db/Share.php @@ -35,13 +35,13 @@ use OCP\AppFramework\Db\Entity; * @method void setToken(string $value) * @method string getType() * @method void setType(string $value) - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) * @method string getUserId() * @method void setUserId(string $value) * @method string getUserEmail() * @method void setUserEmail(string $value) - * @method integer getInvitationSent() + * @method int getInvitationSent() * @method void setInvitationSent(integer $value) */ class Share extends Entity implements JsonSerializable { diff --git a/lib/Db/Subscription.php b/lib/Db/Subscription.php index 256fcbc1..ace2ebd5 100644 --- a/lib/Db/Subscription.php +++ b/lib/Db/Subscription.php @@ -29,9 +29,9 @@ use JsonSerializable; use OCP\AppFramework\Db\Entity; /** - * @method integer getId() + * @method int getId() * @method void setId(integer $value) - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) * @method string getUserId() * @method void setUserId(string $value) diff --git a/lib/Db/Vote.php b/lib/Db/Vote.php index b6013467..839f154a 100644 --- a/lib/Db/Vote.php +++ b/lib/Db/Vote.php @@ -30,11 +30,11 @@ use OCP\IUser; use OCP\AppFramework\Db\Entity; /** - * @method integer getPollId() + * @method int getPollId() * @method void setPollId(integer $value) * @method string getUserId() * @method void setUserId(string $value) - * @method integer getVoteOptionId() + * @method int getVoteOptionId() * @method void setVoteOptionId(integer $value) * @method string getVoteOptionText() * @method void setVoteOptionText(string $value) diff --git a/lib/Exceptions/BadRequestException.php b/lib/Exceptions/BadRequestException.php new file mode 100644 index 00000000..8c0c25c3 --- /dev/null +++ b/lib/Exceptions/BadRequestException.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 BadRequestException extends \Exception { + /** + * NotAuthorizedException Constructor + * @param string $e exception message + */ + public function __construct($e = 'Not allowed') { + parent::__construct($e); + } + public function getStatus() { + return Http::STATUS_BAD_REQUEST; + } + +} diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index ebc998f2..71319436 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -30,7 +30,6 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\IUserManager; use OCP\IGroupManager; -use OCP\ILogger; use OCP\IUser; use OCA\Polls\Db\Poll; use OCA\Polls\Db\Share; @@ -48,9 +47,6 @@ class Acl implements JsonSerializable { /** @var int */ private $pollId = 0; - /** @var ILogger */ - private $logger; - /** @var array */ private $shares = []; @@ -86,7 +82,6 @@ class Acl implements JsonSerializable { * Acl constructor. * @param string $appName * @param string $userId - * @param ILogger $logger * @param IUserManager $userManager * @param IGroupManager $groupManager * @param PollMapper $pollMapper @@ -97,7 +92,6 @@ class Acl implements JsonSerializable { */ public function __construct( $userId, - ILogger $logger, IUserManager $userManager, IGroupManager $groupManager, PollMapper $pollMapper, @@ -106,7 +100,6 @@ class Acl implements JsonSerializable { Poll $poll ) { $this->userId = $userId; - $this->logger = $logger; $this->userManager = $userManager; $this->groupManager = $groupManager; $this->pollMapper = $pollMapper; @@ -139,7 +132,7 @@ class Acl implements JsonSerializable { /** * @NoAdminRequired - * @return boolean + * @return bool */ public function setPollIdOrToken($pollId = 0, $token = '') { @@ -154,7 +147,7 @@ class Acl implements JsonSerializable { /** * @NoAdminRequired - * @return boolean + * @return bool */ public function checkAuthorize($pollId = 0, $token = '') { @@ -403,14 +396,14 @@ class Acl implements JsonSerializable { * @return string */ public function setToken(string $token): Acl { - $this->logger->debug('Share PollId' . $token); + \OC::$server->getLogger()->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()); + \OC::$server->getLogger()->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/AnonymizeService.php b/lib/Service/AnonymizeService.php index 80f11fa1..27fbdc9b 100644 --- a/lib/Service/AnonymizeService.php +++ b/lib/Service/AnonymizeService.php @@ -31,10 +31,19 @@ use OCA\Polls\Db\CommentMapper; class AnonymizeService { + /** @var VoteMapper */ private $voteMapper; + + /** @var CommentMapper */ private $commentMapper; + + /** @var array */ private $anonList = array(); + + /** @var string */ private $userId; + + /** @var int */ private $pollId; public function __construct( @@ -74,7 +83,7 @@ class AnonymizeService { * Initialize anonymizer with pollId and userId * Creates a mapping list with unique Anonymous strings based on the partcipants of a poll * @NoAdminRequired - * @param integer $pollId + * @param int $pollId * @param string $userId - usernames, which will not be anonymized */ diff --git a/lib/Service/CommentService.php b/lib/Service/CommentService.php index a1cd7131..f2c6ef56 100644 --- a/lib/Service/CommentService.php +++ b/lib/Service/CommentService.php @@ -23,10 +23,9 @@ namespace OCA\Polls\Service; -use \Exception; -use OCP\ILogger; - +use Exception; use OCA\Polls\Exceptions\NotAuthorizedException; + use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; use OCA\Polls\Model\Acl; @@ -36,15 +35,20 @@ use OCA\Polls\Service\AnonymizeService; class CommentService { - private $comment; + /** @var CommentMapper */ private $commentMapper; - private $logger; + + /** @var Comment */ + private $comment; + + /** @var AnonymizeService */ private $anonymizer; + + /** @var Acl */ private $acl; /** * CommentService constructor. - * @param ILogger $logger * @param CommentMapper $commentMapper * @param Comment $comment * @param AnonymizeService $anonymizer @@ -52,7 +56,6 @@ class CommentService { */ public function __construct( - ILogger $logger, CommentMapper $commentMapper, Comment $comment, AnonymizeService $anonymizer, @@ -60,18 +63,18 @@ class CommentService { ) { $this->commentMapper = $commentMapper; $this->comment = $comment; - $this->logger = $logger; $this->anonymizer = $anonymizer; $this->acl = $acl; } /** - * get + * Get comments * Read all comments of a poll based on the poll id and return list as array * @NoAdminRequired - * @param integer $pollId + * @param int $pollId * @param string $token - * @return Array + * @return array + * @throws NotAuthorizedException */ public function list($pollId = 0, $token = '') { @@ -88,12 +91,13 @@ class CommentService { } /** - * Write a new comment to the db and returns the new comment as array + * Add comment * @NoAdminRequired - * @param string $message * @param int $pollId + * @param string $message * @param string $token * @return Comment + * @throws NotAuthorizedException */ public function add($pollId = 0, $message, $token = '') { @@ -114,20 +118,20 @@ class CommentService { throw new NotAuthorizedException; } - } catch (\Exception $e) { - $this->logger->alert('Error writing comment for pollId ' . $pollId . ': '. $e); + } catch (Exception $e) { + \OC::$server->getLogger()->alert('Error writing comment for pollId ' . $pollId . ': '. $e); throw new NotAuthorizedException($e); } } /** - * delete - * Delete Comment + * Delete comment * @NoAdminRequired * @param int $commentId * @param string $token * @return Comment + * @throws NotAuthorizedException */ public function delete($commentId, $token = '') { $this->comment = $this->commentMapper->find($commentId); diff --git a/lib/Service/LogService.php b/lib/Service/LogService.php index d6936c04..950abf44 100644 --- a/lib/Service/LogService.php +++ b/lib/Service/LogService.php @@ -31,36 +31,38 @@ use OCA\Polls\Db\LogMapper; class LogService { - private $mapper; - private $logItem; + /** @var LogMapper */ + private $logMapper; + + /** @var Log */ + private $log; /** * LogService constructor. - * @param LogMapper $mapper - * @param Log $logItem + * @param LogMapper $logMapper + * @param Log $log */ - public function __construct( - LogMapper $mapper, - Log $logItem + LogMapper $logMapper, + Log $log ) { - $this->mapper = $mapper; - $this->logItem = $logItem; + $this->logMapper = $logMapper; + $this->log = $log; } /** * Prevent repetition of the same log event * @NoAdminRequired - * @return Bool + * @return bool */ public function isRepetition() { try { - $lastRecord = $this->mapper->getLastRecord($this->logItem->getPollId()); - return (intval($lastRecord->getPollId()) === intval($this->logItem->getPollId()) - && $lastRecord->getUserId() === $this->logItem->getUserId() - && $lastRecord->getMessageId() === $this->logItem->getMessageId() - && $lastRecord->getMessage() === $this->logItem->getMessage() + $lastRecord = $this->logMapper->getLastRecord($this->log->getPollId()); + return (intval($lastRecord->getPollId()) === intval($this->log->getPollId()) + && $lastRecord->getUserId() === $this->log->getUserId() + && $lastRecord->getMessageId() === $this->log->getMessageId() + && $lastRecord->getMessage() === $this->log->getMessage() ); } catch (DoesNotExistException $e) { return false; @@ -77,23 +79,23 @@ class LogService { * @return Log */ public function setLog($pollId, $messageId, $userId = null, $message = null) { - $this->logItem = new Log(); - $this->logItem->setPollId($pollId); - $this->logItem->setCreated(time()); - $this->logItem->setMessageId($messageId); - $this->logItem->setMessage($message); + $this->log = new Log(); + $this->log->setPollId($pollId); + $this->log->setCreated(time()); + $this->log->setMessageId($messageId); + $this->log->setMessage($message); if ($userId) { - $this->logItem->setUserId($userId); + $this->log->setUserId($userId); } else { - $this->logItem->setUserId(\OC::$server->getUserSession()->getUser()->getUID()); + $this->log->setUserId(\OC::$server->getUserSession()->getUser()->getUID()); } if ($this->isRepetition()) { return null; } else { - return $this->mapper->insert($this->logItem); + return $this->logMapper->insert($this->log); } } diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index fbd40984..54fe8a34 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -34,7 +34,6 @@ use OCP\IL10N; use OCP\L10N\IFactory; use OCP\Mail\IMailer; use OCP\Mail\IEMailTemplate; -use OCP\ILogger; use OCA\Polls\Db\SubscriptionMapper; use OCA\Polls\Db\Subscription; @@ -46,18 +45,37 @@ use OCA\Polls\Db\LogMapper; class MailService { + /** @var IUserManager */ private $userManager; + + /** @var IGroupManager */ private $groupManager; + + /** @var IConfig */ private $config; + + /** @var IURLGenerator */ private $urlGenerator; + + /** @var IL10N */ private $trans; + + /** @var IFactory */ private $transFactory; + + /** @var IMailer */ private $mailer; - private $logger; - private $shareMapper; + /** @var SubscriptionMapper */ private $subscriptionMapper; + + /** @var ShareMapper */ + private $shareMapper; + + /** @var PollMapper */ private $pollMapper; + + /** @var LogMapper */ private $logMapper; /** @@ -69,7 +87,6 @@ class MailService { * @param IL10N $trans * @param IFactory $transFactory * @param IMailer $mailer - * @param ILogger $logger * @param SubscriptionMapper $subscriptionMapper * @param ShareMapper $shareMapper * @param PollMapper $pollMapper @@ -84,7 +101,6 @@ class MailService { IL10N $trans, IFactory $transFactory, IMailer $mailer, - ILogger $logger, ShareMapper $shareMapper, SubscriptionMapper $subscriptionMapper, PollMapper $pollMapper, @@ -97,7 +113,6 @@ class MailService { $this->trans = $trans; $this->transFactory = $transFactory; $this->mailer = $mailer; - $this->logger = $logger; $this->shareMapper = $shareMapper; $this->subscriptionMapper = $subscriptionMapper; $this->pollMapper = $pollMapper; @@ -135,7 +150,7 @@ class MailService { return null; } catch (\Exception $e) { - $this->logger->logException($e, ['app' => 'polls']); + \OC::$server->getLogger()->logException($e, ['app' => 'polls']); throw $e; } @@ -305,7 +320,7 @@ class MailService { $sentMails[] = $recipient; } catch (Exception $e) { $abortedMails[] = $recipient; - $this->logger->alert('Error sending Mail to ' . json_encode($recipient)); + \OC::$server->getLogger()->alert('Error sending Mail to ' . json_encode($recipient)); } } return ['sentMails' => $sentMails, 'abortedMails' => $abortedMails]; @@ -428,7 +443,7 @@ class MailService { try { $this->sendMail($emailTemplate, $subscription->getUserId()); } catch (Exception $e) { - $this->logger->alert('Error sending Mail to ' . $subscription->getUserId()); + \OC::$server->getLogger()->alert('Error sending Mail to ' . $subscription->getUserId()); } } } diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 0afe4b8c..2bab49a8 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -26,23 +26,41 @@ namespace OCA\Polls\Service; use Exception; use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; +use OCA\Polls\Exceptions\BadRequestException; -use OCA\Polls\Db\Option; use OCA\Polls\Db\OptionMapper; +use OCA\Polls\Db\Option; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\Poll; use OCA\Polls\Service\LogService; use OCA\Polls\Model\Acl; class OptionService { + /** @var OptionMapper */ private $optionMapper; + + /** @var Option */ private $option; + + /** @var PollMapper */ + private $pollMapper; + + /** @var Poll */ + private $poll; + + /** @var LogService */ private $logService; + + /** @var Acl */ private $acl; /** * OptionController constructor. * @param OptionMapper $optionMapper * @param Option $option + * @param PollMapper $pollMapper + * @param Poll $poll * @param LogService $logService * @param Acl $acl */ @@ -50,85 +68,94 @@ class OptionService { public function __construct( OptionMapper $optionMapper, Option $option, + PollMapper $pollMapper, + Poll $poll, LogService $logService, Acl $acl ) { $this->optionMapper = $optionMapper; $this->option = $option; + $this->pollMapper = $pollMapper; + $this->poll = $poll; $this->logService = $logService; $this->acl = $acl; } /** - * Set properties from option array + * Get all options of given poll * @NoAdminRequired - * @param Array $option + * @param int $pollId + * @param string $token + * @return array Array of Option objects + * @throws NotAuthorizedException */ - private function set($option) { - - $this->option->setPollId($option['pollId']); - $this->option->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); - $this->option->setTimestamp($option['timestamp']); + public function list($pollId = 0, $token = '') { - if ($option['timestamp']) { - $this->option->setOrder($option['timestamp']); - } else { - $this->option->setOrder($option['order']); + if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { + throw new NotAuthorizedException; } - 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); + try { + return $this->optionMapper->findByPoll($pollId); + } catch (DoesNotExistException $e) { + return []; } } + /** - * Get all options of given poll + * Add a new option * @NoAdminRequired - * @param integer $pollId - * @param string $token - * @return array Array of Option objects + * @param int $pollId + * @param int $timestamp + * @param string $pollOptionText + * @return Option + * @throws NotAuthorizedException */ - public function list($pollId = 0, $token = '') { + public function add($pollId, $timestamp = 0 , $pollOptionText = '') { - if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { + $this->poll = $this->pollMapper->find($pollId); + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } - return $this->optionMapper->findByPoll($pollId); + $this->option = new Option(); + $this->option->setPollId($pollId); + $this->setOption($timestamp, $pollOptionText, 0); + return $this->optionMapper->insert($this->option); } - /** - * Add a new Option to poll + * Update option * @NoAdminRequired - * @param Array $option + * @param int $optionId + * @param int $timestamp + * @param string $pollOptionText + * @param int $order * @return Option + * @throws NotAuthorizedException */ - public function add($option) { + public function update($optionId, $timestamp = 0 , $pollOptionText = '', $order = 0) { - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + $this->option = $this->optionMapper->find($optionId); + $this->poll = $this->pollMapper->find($this->option->getPollId()); + + if (!$this->acl->setPollId($this->option->getPollId())->getAllowEdit()) { throw new NotAuthorizedException; } - $this->option = new Option(); - $this->set($option); - $this->optionMapper->insert($this->option); - $this->logService->setLog($option['pollId'], 'addOption'); + $this->setOption($timestamp, $pollOptionText, $order); - return $this->option; + return $this->optionMapper->update($this->option); } /** - * Remove a single option + * Delete option * @NoAdminRequired - * @param Option $option - * @return array Array of Option objects + * @param int $optionId + * @return Option deleted Option + * @throws NotAuthorizedException */ public function delete($optionId) { $this->option = $this->optionMapper->find($optionId); @@ -140,45 +167,79 @@ class OptionService { $this->optionMapper->delete($this->option); return $this->option; - } /** - * Update poll option + * Switch optoin confirmation * @NoAdminRequired - * @param array $option - * @return Option + * @param int $optionId + * @return Option confirmed Option + * @throws NotAuthorizedException */ - public function update($option) { - if (!$this->acl->setPollId($option['pollId'])->getAllowEdit()) { + public function confirm($optionId) { + $this->option = $this->optionMapper->find($optionId); + + if (!$this->acl->setPollId($this->option->getPollId())->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'); + if ($this->option->setConfirmation()) { + $this->option->setConfirmation(0); + } else { + $this->option->setConfirmation(time()); + } + + return $this->optionMapper->update($this->option); + } + + /** + * Copy options from $fromPoll to $toPoll + * @NoAdminRequired + * @param int $fromPollId + * @param int $toPollId + * @return array Array of Option objects + * @throws NotAuthorizedException + */ + public function clone($fromPollId, $toPollId) { - return $this->option; - } catch (Exception $e) { - return new DoesNotExistException($e); + if (!$this->acl->setPollId($fromPollId)->getAllowView()) { + throw new NotAuthorizedException; } + foreach ($this->optionMapper->findByPoll($fromPollId) as $origin) { + $option = new Option(); + $option->setPollId($toPollId); + $option->setConfirmed(0); + $option->setPollOptionText($origin->getPollOptionText()); + $option->setTimestamp($origin->getTimestamp()); + $option->setOrder($origin->getOrder()); + $this->optionMapper->insert($option); + } + + return $this->optionMapper->findByPoll($toPollId); } /** - * Set order by order of the given array + * Reorder options with the order specified by $options * @NoAdminRequired - * @param array $options + * @param int $pollId + * @param array $options - Array of options * @return array Array of Option objects + * @throws NotAuthorizedException + * @throws BadRequestException */ public function reorder($pollId, $options) { + $this->poll = $this->pollMapper->find($pollId); + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } + if ($this->poll->getType() === 'datePoll') { + throw new BadRequestException("Not allowed in date polls", 1); + } + $i = 0; foreach ($options as $option) { $this->option = $this->optionMapper->find($option['id']); @@ -189,28 +250,116 @@ class OptionService { } return $this->optionMapper->findByPoll($pollId); - } /** - * Set order by order of the given array + * Change order for $optionId and reorder the options * @NoAdminRequired - * @param integer $fromPollId - * @param integer $toPollId + * @param int $optionId + * @param int $newOrder * @return array Array of Option objects + * @throws NotAuthorizedException + * @throws BadRequestException */ - public function clone($fromPollId, $toPollId) { + public function setOrder($optionId, $newOrder) { - if (!$this->acl->setPollId($fromPollId)->getAllowView()) { + $this->option = $this->optionMapper->find($optionId); + $pollId = $this->option->getPollId(); + $this->poll = $this->pollMapper->find($pollId); + + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { throw new NotAuthorizedException; } - foreach ($this->optionMapper->findByPoll($fromPollId) as $option) { - $option->setPollId($toPollId); - $this->optionMapper->insert($option); + if ($this->poll->getType() === 'datePoll') { + throw new BadRequestException("Not allowed in date polls", 1); } - return $this->optionMapper->findByPoll($toPollId); + if ($newOrder < 1) { + $newOrder = 1; + } elseif ($newOrder > getHighestOrder($pollId)) { + $newOrder = getHighestOrder($pollId); + } + + $oldOrder = $this->option->getOrder(); + + foreach ($this->optionMapper->findByPoll($pollId) as $option) { + $currentOrder = $option->getOrder(); + if ( + ($currentOrder < $oldOrder && $currentOrder < $newOrder) + || ($currentOrder > $oldOrder && $currentOrder > $newOrder) + ) { + + continue; + + } elseif ($currentOrder > $oldOrder && $currentOrder <= $newOrder) { + $option->setOrder($currentOrder - 1); + $this->optionMapper->update($option); + + } elseif ( + ($currentOrder < $oldOrder && $currentOrder >= $newOrder) + || ($currentOrder < $oldOrder && $currentOrder = $newOrder) + ) { + + $option->setOrder($currentOrder + 1); + $this->optionMapper->update($option); + + } elseif ($currentOrder === $oldOrder) { + + $option->setOrder($newOrder); + $this->optionMapper->update($option); + + } + } + + return $this->optionMapper->findByPoll($this->option->getPollId()); + } + + /** + * Set option entities validated + * @NoAdminRequired + * @param int $timestamp + * @param string $pollOptionText + * @param int $order + * @throws BadRequestException + */ + private function setOption($timestamp = 0 , $pollOptionText = '', $order = 0) { + if ($this->poll->getType() === 'datePoll') { + if ($timestamp) { + $this->option->setTimestamp($timestamp); + $this->option->setOrder($timestamp); + $this->option->setPollOptionText(date('c', $timestamp)); + } else { + throw new BadRequestException("Date poll must have a timestamp", 1); + } + } elseif ($this->poll->getType() === 'textPoll') { + if ($pollOptionText) { + $this->option->setPollOptionText($pollOptionText); + } else { + throw new BadRequestException("Text poll must have a pollOptionText", 1); + } + + if (!$order && !$this->option->getOrder()) { + $order = $this->getHighestOrder($this->option->getPollId()) + 1; + $this->option->setOrder($order); + } + } + } + + /** + * Get the highest order number in $pollId + * @NoAdminRequired + * @param int $pollId + * @return int Highest order number + */ + private function getHighestOrder($pollId) { + $order = 0; + foreach ($this->optionMapper->findByPoll($pollId) as $option) { + if ($option->getOrder() > $order) { + $order = $option->getOrder(); + } + } + return $order; } } diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index d23c8595..946a66d2 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -31,7 +31,6 @@ use OCA\Polls\Exceptions\InvalidPollTypeException; use OCA\Polls\Exceptions\NotAuthorizedException; - use OCP\ILogger; use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Poll; @@ -40,15 +39,20 @@ class PollService { - private $logger; + /** @var PollMapper */ private $pollMapper; + + /** @var Poll */ private $poll; + + /** @var LogService */ private $logService; + + /** @var Acl */ private $acl; /** * PollController constructor. - * @param ILogger $logger * @param PollMapper $pollMapper * @param Poll $poll * @param LogService $logService @@ -56,13 +60,11 @@ */ public function __construct( - ILogger $logger, PollMapper $pollMapper, Poll $poll, LogService $logService, Acl $acl ) { - $this->logger = $logger; $this->pollMapper = $pollMapper; $this->poll = $poll; $this->logService = $logService; @@ -71,9 +73,10 @@ /** - * list + * Get list of polls * @NoAdminRequired - * @return array + * @return array Array of Poll + * @throws NotAuthorizedException */ public function list() { @@ -97,10 +100,11 @@ } /** - * get + * get poll configuration * @NoAdminRequired - * @param integer $pollId - * @return array + * @param int $pollId + * @return Poll + * @throws NotAuthorizedException */ public function get($pollId) { @@ -113,10 +117,11 @@ } /** - * get + * get poll configuration by token * @NoAdminRequired - * @param integer $pollId - * @return array + * @param int $pollId + * @return Poll + * @throws NotAuthorizedException */ public function getByToken($token) { @@ -129,57 +134,14 @@ } /** - * delete + * Add poll * @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); - } - - /** - * write - * @NoAdminRequired - * @NoCSRFRequired * @param string $type * @param string $title * @return Poll + * @throws NotAuthorizedException + * @throws InvalidPollTypeException + * @throws EmptyTitleException */ public function add($type, $title) { @@ -221,11 +183,15 @@ } /** - * update + * Update poll configuration * @NoAdminRequired - * @NoCSRFRequired - * @param Array $poll + * @param int $pollId + * @param array $poll * @return Poll + * @throws NotAuthorizedException + * @throws EmptyTitleException + * @throws InvalidShowResultsException + * @throws InvalidAccessException */ public function update($pollId, $poll) { @@ -256,36 +222,93 @@ return $this->poll; } + /** - * clone + * Switch deleted status (move to deleted polls) * @NoAdminRequired - * @NoCSRFRequired - * @param integer $pollId + * @param int $pollId * @return Poll + * @throws NotAuthorizedException */ - public function clone($pollId) { - if (!$this->acl->setPollId($this->poll->getId())->getAllowView()) { + 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; + } + + /** + * Delete poll + * @NoAdminRequired + * @param int $pollId + * @return Poll the deleted poll + * @throws NotAuthorizedException + */ + + public function deletePermanently($pollId) { $this->poll = $this->pollMapper->find($pollId); - $this->poll->setCreated(time()); - $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); - $this->poll->setTitle('Clone of ' . $this->poll->getTitle()); - $this->poll->setDeleted(0); - $this->poll->setId(0); + if (!$this->acl->setPollId($pollId)->getAllowEdit() || !$this->poll->getDeleted()) { + throw new NotAuthorizedException; + } - $this->poll = $this->pollMapper->insert($this->poll); - $this->logService->setLog($this->poll->getId(), 'addPoll'); + return $this->pollMapper->delete($this->poll); + } - $this->optionService->clone($pollId, $this->poll->getId()); + /** + * Clone poll + * @NoAdminRequired + * @param int $pollId + * @return Poll + * @throws NotAuthorizedException + */ + public function clone($pollId) { - return $this->poll; + $origin = $this->pollMapper->find($pollId); + if (!$this->acl->setPollId($origin->getId())->getAllowView()) { + throw new NotAuthorizedException; + } + + $this->poll = new Poll(); + $this->poll->setCreated(time()); + $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); + $this->poll->setTitle('Clone of ' . $origin->getTitle()); + $this->poll->setDeleted(0); + $this->poll->setAccess('hidden'); + $this->poll->setType($origin->getType()); + $this->poll->setDescription($origin->getDescription()); + $this->poll->setExpire($origin->getExpire()); + $this->poll->setAnonymous($origin->getAnonymous()); + $this->poll->setFullAnonymous($origin->getFullAnonymous()); + $this->poll->setAllowMaybe($origin->getAllowMaybe()); + $this->poll->setVoteLimit($origin->getVoteLimit()); + $this->poll->setSettings($origin->getSettings()); + $this->poll->setOptions($origin->getOptions()); + $this->poll->setShowResults($origin->getShowResults()); + $this->poll->setAdminAccess($origin->getAdminAccess()); + + return $this->pollMapper->insert($this->poll); } + /** + * Get valid values for configuration options + * @NoAdminRequired + * @return array + */ public function getValidEnum() { return [ 'pollType' => $this->getValidPollType(), @@ -294,14 +317,29 @@ ]; } + /** + * Get valid values for pollType + * @NoAdminRequired + * @return array + */ private function getValidPollType() { return ['datePoll', 'textPoll']; } + /** + * Get valid values for access + * @NoAdminRequired + * @return array + */ private function getValidAccess() { return ['hidden', 'public']; } + /** + * Get valid values for showResult + * @NoAdminRequired + * @return array + */ private function getValidShowResults() { return ['always', 'expired', 'never']; } diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 5d8b47c4..1bd9bcb8 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -24,54 +24,62 @@ namespace OCA\Polls\Service; use Exception; - -use OCP\Security\ISecureRandom; - use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Exceptions\InvalidUsername; -use OCA\Polls\Db\Share; +use OCP\Security\ISecureRandom; + +use OCA\Polls\Controller\SystemController; use OCA\Polls\Db\ShareMapper; +use OCA\Polls\Db\Share; use OCA\Polls\Service\MailService; use OCA\Polls\Model\Acl; -use OCA\Polls\Controller\SystemController; class ShareService { + /** @var SystemController */ + private $systemController; + + /** @var ShareMapper */ private $shareMapper; + + /** @var Share */ private $share; - private $systemController; + + /** @var MailService */ private $mailService; + + /** @var Acl */ private $acl; /** * ShareController constructor. + * @param SystemController $systemController * @param ShareMapper $shareMapper * @param Share $share - * @param SystemController $systemController * @param MailService $mailService * @param Acl $acl */ public function __construct( + SystemController $systemController, ShareMapper $shareMapper, Share $share, - SystemController $systemController, MailService $mailService, Acl $acl ) { + $this->systemController = $systemController; $this->shareMapper = $shareMapper; $this->share = $share; - $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 array + * @param int $pollId + * @return array array of Share + * @throws NotAuthorizedException */ public function list($pollId) { if (!$this->acl->setPollId($pollId)->getAllowEdit()) { @@ -82,8 +90,7 @@ class ShareService { } /** - * getByToken - * Get pollId by token + * Get share by token * @NoAdminRequired * @param string $token * @return Share @@ -93,11 +100,14 @@ class ShareService { } /** - * Write a new share to the db and returns the new share as array + * Add share * @NoAdminRequired * @param int $pollId - * @param string $share - * @return array + * @param string $type + * @param string $userId + * @param string $userEmail + * @return Share + * @throws NotAuthorizedException */ public function add($pollId, $type, $userId, $userEmail = '') { @@ -122,14 +132,16 @@ class ShareService { } /** - * createPersonalShare - * Write a new share to the db and returns the new share as array + * Create a personal share from a public share + * or update an email share with the username * @NoAdminRequired * @param string $token * @param string $userName * @return Share + * @throws NotAuthorizedException + * @throws InvalidUsername */ - public function createPersonalShare($token, $userName) { + public function personal($token, $userName) { $publicShare = $this->shareMapper->findByToken($token); // Return of validatePublicUsername is a DataResponse @@ -142,7 +154,6 @@ class ShareService { if ($publicShare->getType() === 'public') { - $this->share = new Share(); $this->share->setToken(\OC::$server->getSecureRandom()->generate( 16, @@ -169,14 +180,15 @@ class ShareService { } /** - * remove + * Delete share * remove share * @NoAdminRequired * @param string $token * @return Share + * @throws NotAuthorizedException */ - public function remove($token) { + public function delete($token) { $this->share = $this->shareMapper->findByToken($token); if (!$this->acl->setPollId($this->share->getPollId())->getAllowEdit()) { throw new NotAuthorizedException; diff --git a/lib/Service/SubscriptionService.php b/lib/Service/SubscriptionService.php index f378e476..8809f792 100644 --- a/lib/Service/SubscriptionService.php +++ b/lib/Service/SubscriptionService.php @@ -27,7 +27,6 @@ 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; @@ -35,30 +34,29 @@ use OCA\Polls\Model\Acl; class SubscriptionService { + /** @var Acl */ private $acl; + + /** @var SubscriptionMapper */ private $subscriptionMapper; - private $logger; /** * SubscriptionController constructor. * @param SubscriptionMapper $subscriptionMapper - * @param ILogger $logger * @param Acl $acl */ public function __construct( SubscriptionMapper $subscriptionMapper, - ILogger $logger, Acl $acl ) { $this->subscriptionMapper = $subscriptionMapper; $this->acl = $acl; - $this->logger = $logger; } /** * @NoAdminRequired - * @param integer $pollId + * @param int $pollId * @return array */ public function get($pollId) { @@ -77,7 +75,7 @@ class SubscriptionService { /** * @NoAdminRequired - * @param integer $pollId + * @param int $pollId * @return array */ public function set($pollId, $subscribed) { @@ -111,15 +109,15 @@ class SubscriptionService { } 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'); + \OC::$server->getLogger()->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); + \OC::$server->getLogger()->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'); + \OC::$server->getLogger()->debug('Added new subscription'); return $subscription; } else { return ['status' => 'Unsubscribed from poll ' . $pollId]; diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php index 4df01c70..5073b732 100644 --- a/lib/Service/VoteService.php +++ b/lib/Service/VoteService.php @@ -27,8 +27,8 @@ use Exception; use OCP\AppFramework\Db\DoesNotExistException; use OCA\Polls\Exceptions\NotAuthorizedException; -use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; +use OCA\Polls\Db\Vote; use OCA\Polls\Db\OptionMapper; use OCA\Polls\Service\AnonymizeService; use OCA\Polls\Service\LogService; @@ -36,11 +36,22 @@ use OCA\Polls\Model\Acl; class VoteService { + /** @var VoteMapper */ private $voteMapper; + + /** @var Vote */ private $vote; + + /** @var OptionMapper */ private $optionMapper; + + /** @var AnonymizeService */ private $anonymizer; + + /** @var LogService */ private $logService; + + /** @var Acl */ private $acl; /** @@ -69,12 +80,12 @@ class VoteService { } /** - * 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 int $pollId * @param string $token - * @return Vote + * @return array + * @throws NotAuthorizedException */ public function list($pollId = 0, $token = '') { if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { @@ -92,24 +103,29 @@ class VoteService { } /** - * set + * Set vote * @NoAdminRequired - * @param integer $pollId - * @param Array $option + * @param int $optionId * @param string $setTo * @param string $token * @return Vote + * @throws NotAuthorizedException */ - public function set($pollId = 0, $pollOptionText, $setTo, $token = '') { + public function set($optionId, $setTo, $token = '') { + + $option = $this->optionMapper->find($optionId); + $pollId = $option->getPollId(); - if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowVote()) { + if (!$this->acl->setPollIdOrToken($option->getPollId(), $token)->getAllowVote()) { throw new NotAuthorizedException; } - $option = $this->optionMapper->findByPollAndText($this->acl->getpollId(), $pollOptionText); + if (!$option->getPollId() === $this->acl->getPollId()) { + throw new NotAuthorizedException; + } try { - $this->vote = $this->voteMapper->findSingleVote($this->acl->getpollId(), $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); @@ -117,7 +133,7 @@ class VoteService { // Vote does not exist, insert as new Vote $this->vote = new Vote(); - $this->vote->setPollId($this->acl->getpollId()); + $this->vote->setPollId($this->acl->getPollId()); $this->vote->setUserId($this->acl->getUserId()); $this->vote->setVoteOptionText($option->getPollOptionText()); $this->vote->setVoteOptionId($option->getId()); @@ -125,19 +141,19 @@ class VoteService { $this->voteMapper->insert($this->vote); } finally { - $this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId()); + $this->logService->setLog($this->acl->getPollId(), 'setVote', $this->vote->getUserId()); return $this->vote; } } /** - * delete + * Remove user from poll * @NoAdminRequired - * @NoCSRFRequired - * @param integer $voteId + * @param int $voteId * @param string $userId - * @param integer $pollId + * @param int $pollId * @return Vote + * @throws NotAuthorizedException */ public function delete($pollId, $userId) { -- cgit v1.2.3