diff options
author | dartcafe <github@dartcafe.de> | 2022-04-13 09:30:31 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2022-04-13 09:30:31 +0300 |
commit | 93c0e98afcdc0c0f81922e8d5f5700a467047230 (patch) | |
tree | eabcffb92a953387677375afb5b886a7f8eb7147 /lib | |
parent | 9a4fa40b50823d952451d32c9df0737254f8c4aa (diff) |
fix locked vote cells and add locked icon
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/PollApiController.php | 38 | ||||
-rw-r--r-- | lib/Controller/PollController.php | 7 | ||||
-rw-r--r-- | lib/Model/Acl.php | 33 | ||||
-rw-r--r-- | lib/Service/PollService.php | 20 | ||||
-rw-r--r-- | lib/Service/VoteService.php | 25 |
5 files changed, 79 insertions, 44 deletions
diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index a4b2dc18..df482848 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -21,29 +21,35 @@ * */ - namespace OCA\Polls\Controller; +namespace OCA\Polls\Controller; - use OCP\AppFramework\Db\DoesNotExistException; - use OCA\Polls\Exceptions\Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\Exception; - use OCP\IRequest; - use OCP\AppFramework\ApiController; - use OCP\AppFramework\Http; - use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; - use OCA\Polls\Service\PollService; +use OCA\Polls\Model\Acl; +use OCA\Polls\Service\PollService; - class PollApiController extends ApiController { +class PollApiController extends ApiController { - /** @var PollService */ + /** @var Acl */ + private $acl; + + /** @var PollService */ private $pollService; public function __construct( string $appName, + Acl $acl, IRequest $request, PollService $pollService ) { parent::__construct($appName, $request); + $this->acl = $acl; $this->pollService = $pollService; } @@ -101,8 +107,14 @@ */ public function update(int $pollId, array $poll): DataResponse { try { - return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK); - } catch (DoesNotExistException $e) { + $this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT); + + return new DataResponse([ + 'poll' => $this->pollService->update($pollId, $poll), + 'acl' => $this->acl, + ], Http::STATUS_OK); + + } catch (DoesNotExistException $e) { return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); } catch (Exception $e) { return new DataResponse(['message' => $e->getMessage()], $e->getStatus()); @@ -182,4 +194,4 @@ public function enum(): DataResponse { return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK); } - } +} diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index fff5f04d..2deb5cc7 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -113,7 +113,12 @@ class PollController extends Controller { public function update(int $pollId, array $poll): DataResponse { return $this->response(function () use ($pollId, $poll) { - return $this->pollService->update($pollId, $poll); + $this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT); + + return [ + 'poll' => $this->pollService->update($pollId, $poll), + 'acl' => $this->acl, + ]; }); } diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 79dba8d5..a1817c6c 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -29,6 +29,7 @@ use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Model\Settings\AppSettings; use OCA\Polls\Db\Poll; use OCA\Polls\Db\Share; +use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\VoteMapper; use OCA\Polls\Db\ShareMapper; @@ -72,6 +73,9 @@ class Acl implements JsonSerializable { /** @var IGroupManager */ private $groupManager; + /** @var OptionMapper */ + private $optionMapper; + /** @var PollMapper */ private $pollMapper; @@ -91,6 +95,7 @@ class Acl implements JsonSerializable { IUserManager $userManager, IUserSession $userSession, IGroupManager $groupManager, + OptionMapper $optionMapper, PollMapper $pollMapper, VoteMapper $voteMapper, ShareMapper $shareMapper @@ -98,6 +103,7 @@ class Acl implements JsonSerializable { $this->userManager = $userManager; $this->userSession = $userSession; $this->groupManager = $groupManager; + $this->optionMapper = $optionMapper; $this->pollMapper = $pollMapper; $this->voteMapper = $voteMapper; $this->shareMapper = $shareMapper; @@ -254,6 +260,32 @@ class Acl implements JsonSerializable { } } + public function getIsVoteLimitExceeded(): bool { + // return true, if no vote limit is set + if ($this->getPoll()->getVoteLimit() < 1) { + return false; + } + + // Only count votes, which match to an actual existing option. + // Explanation: If an option is deleted, the corresponding votes are not deleted. + $pollOptionTexts = array_map(function ($option) { + return $option->getPollOptionText(); + }, $this->optionMapper->findByPoll($this->getPollId())); + + $voteCount = 0; + $votes = $this->voteMapper->getYesVotesByParticipant($this->getPollId(), $this->getUserId()); + foreach ($votes as $vote) { + if (in_array($vote->getVoteOptionText(), $pollOptionTexts)) { + $voteCount++; + } + } + + if ($this->getPoll()->getVoteLimit() <= $voteCount) { + return true; + } + return false; + } + public function jsonSerialize(): array { return [ 'allowAddOptions' => $this->getIsAllowed(self::PERMISSION_OPTIONS_ADD), @@ -272,6 +304,7 @@ class Acl implements JsonSerializable { 'allowVote' => $this->getIsAllowed(self::PERMISSION_VOTE_EDIT), 'displayName' => $this->getDisplayName(), 'isOwner' => $this->getIsOwner(), + 'isVoteLimitExceeded' => $this->getIsVoteLimitExceeded(), 'loggedIn' => $this->getIsLoggedIn(), 'pollId' => $this->getPollId(), 'token' => $this->getToken(), diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index 85a45bb6..d08eb25c 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -244,24 +244,28 @@ class PollService { * @return Poll */ public function update(int $pollId, array $poll): Poll { - $this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT); - $this->poll = $this->acl->getPoll(); + // $this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT); + // $this->poll = $this->acl->getPoll(); + $this->poll = $this->pollMapper->find($pollId); // Validate valuess if (isset($poll['showResults']) && !in_array($poll['showResults'], $this->getValidShowResults())) { throw new InvalidShowResultsException('Invalid value for prop showResults'); } - if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) { - throw new InvalidAccessException('Invalid value for prop access ' . $poll['access']); - } if (isset($poll['title']) && !$poll['title']) { throw new EmptyTitleException('Title must not be empty'); } - if (isset($poll['access']) && $poll['access'] === (Poll::ACCESS_OPEN)) { - $this->acl->request(Acl::PERMISSION_ALL_ACCESS); - } + if (isset($poll['access']) && !in_array($poll['access'], $this->getValidAccess())) { + if (!in_array($poll['access'], $this->getValidAccess())) { + throw new InvalidAccessException('Invalid value for prop access ' . $poll['access']); + } + + if ($poll['access'] === (Poll::ACCESS_OPEN)) { + $this->acl->setPollId($pollId, Acl::PERMISSION_ALL_ACCESS); + } + } // Set the expiry time to the actual servertime to avoid an // expiry misinterpration when using acl diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php index b6b68185..4fe9f0e2 100644 --- a/lib/Service/VoteService.php +++ b/lib/Service/VoteService.php @@ -97,7 +97,7 @@ class VoteService { } } - private function checkLimits(Option $option, string $userId):void { + private function checkLimits(Option $option, string $userId): void { // check, if the optionlimit is reached or exceeded, if one is set if ($this->acl->getPoll()->getOptionLimit() > 0) { @@ -105,29 +105,10 @@ class VoteService { throw new VoteLimitExceededException; } } - - // exit, if no vote limit is set - if ($this->acl->getPoll()->getVoteLimit() < 1) { - return; - } - - // Only count votes, which match to an actual existing option. - // Explanation: If an option is deleted, the corresponding votes are not deleted. - $pollOptionTexts = array_map(function ($option) { - return $option->getPollOptionText(); - }, $this->optionMapper->findByPoll($option->getPollId())); - - $votecount = 0; - $votes = $this->voteMapper->getYesVotesByParticipant($option->getPollId(), $userId); - foreach ($votes as $vote) { - if (in_array($vote->getVoteOptionText(), $pollOptionTexts)) { - $votecount++; - } - } - - if ($this->acl->getPoll()->getVoteLimit() <= $votecount) { + if ($this->acl->getIsVoteLimitExceeded()) { throw new VoteLimitExceededException; } + return; } /** |