Skip to content

Commit

Permalink
Merge pull request #3822 from nextcloud/fix/vote-limit-check/master
Browse files Browse the repository at this point in the history
fix vote limit check
  • Loading branch information
dartcafe authored Dec 28, 2024
2 parents 732b8e9 + 9125618 commit 4a88c1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/Service/VoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Event\VoteSetEvent;
use OCA\Polls\Exceptions\NotFoundException;
use OCA\Polls\Exceptions\VoteLimitExceededException;
use OCA\Polls\Model\Acl as Acl;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function __construct(
public function list(int $pollId): array {
$poll = $this->pollMapper->find($pollId);
$poll->request(Poll::PERMISSION_POLL_VIEW);

if (!$poll->getIsAllowed(Poll::PERMISSION_POLL_RESULTS_VIEW)) {
// Just return the participants votes, no further anoymizing or obfuscating is nessecary
return $this->voteMapper->findByPollAndUser($pollId, ($this->acl->getCurrentUserId()));
Expand All @@ -65,6 +66,18 @@ private function checkLimits(Option $option): void {
return;
}

private function checkVoteLimit(Option $option): void {
// check, if the optionlimit is reached or exceeded, if one is set
if ($option->getIsLockedByOptionLimit()) {
throw new VoteLimitExceededException();
}

if ($option->getIsLockedByVotesLimit()) {
throw new VoteLimitExceededException;
}
return;
}

/**
* Set vote
*/
Expand All @@ -73,6 +86,11 @@ public function set(int $optionId, string $setTo): ?Vote {
$poll = $this->pollMapper->find($option->getPollId());
$poll->request(Poll::PERMISSION_VOTE_EDIT);

if ($option->getIsLocked()) {
$this->checkVoteLimit($option);
throw new NotFoundException();
}

try {
$this->vote = $this->voteMapper->findSingleVote($poll->getId(), $option->getPollOptionText(), $this->acl->getCurrentUserId());

Expand Down
4 changes: 4 additions & 0 deletions src/js/components/VoteTable/VoteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export default {
})
showSuccess(t('polls', 'Vote saved'), { timeout: 2000 })
} catch (e) {
if (e.response.status === 409 && e.response.data.message === 'Vote limit exceeded') {
showError(t('polls', 'Vote already booked out'))
return
}
showError(t('polls', 'Error saving vote'))
}
},
Expand Down
1 change: 1 addition & 0 deletions src/js/store/modules/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const actions = {
context.dispatch('list')
context.dispatch('options/list', null, { root: true })
context.dispatch('poll/get', null, { root: true })
throw error
} else {
Logger.error('Error setting vote', { error, payload })
throw error
Expand Down

0 comments on commit 4a88c1d

Please sign in to comment.