Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix vote limit check #3822

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading