Signed-off-by: dartcafe <github@dartcafe.de>
This commit is contained in:
dartcafe 2021-05-11 23:46:47 +02:00
Родитель 34fe2e9e43
Коммит c4a907d41a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CCE73CEF3035D3C8
4 изменённых файлов: 69 добавлений и 92 удалений

Просмотреть файл

@ -95,9 +95,14 @@ class Acl implements JsonSerializable {
/**
* load share via token and than call setShare
*/
public function setToken(string $token = '', string $permission = self::PERMISSION_POLL_VIEW): Acl {
public function setToken(string $token = '', string $permission = self::PERMISSION_POLL_VIEW, int $pollIdToValidate = 0): Acl {
try {
$this->share = $this->shareMapper->findByToken($token);
if ($pollIdToValidate && $this->share->getPollId() !== $pollIdToValidate) {
throw new NotAuthorizedException;
}
$this->poll = $this->pollMapper->find($this->share->getPollId());
$this->validateShareAccess();
$this->request($permission);

Просмотреть файл

@ -105,7 +105,7 @@ class CommentService {
$this->comment = $this->commentMapper->find($commentId);
if ($token) {
$this->acl->setToken($token);
$this->acl->setToken($token, Acl::PERMISSION_COMMENT_ADD, $this->comment->getPollId());
} else {
$this->acl->setPollId($this->comment->getPollId());
}

Просмотреть файл

@ -68,9 +68,6 @@ class OptionService {
/** @var OptionMapper */
private $optionMapper;
/** @var PollMapper */
private $pollMapper;
/** @var VoteMapper */
private $voteMapper;
@ -83,7 +80,6 @@ class OptionService {
Acl $acl,
Option $option,
OptionMapper $optionMapper,
PollMapper $pollMapper,
VoteMapper $voteMapper,
WatchService $watchService
) {
@ -92,7 +88,6 @@ class OptionService {
$this->acl = $acl;
$this->option = $option;
$this->optionMapper = $optionMapper;
$this->pollMapper = $pollMapper;
$this->voteMapper = $voteMapper;
$this->watchService = $watchService;
}
@ -110,15 +105,14 @@ class OptionService {
} else {
$this->acl->setPollId($pollId);
}
$this->poll = $this->acl->getPoll();
try {
$this->options = $this->optionMapper->findByPoll($this->poll->getId());
$this->votes = $this->voteMapper->findByPoll($this->poll->getId());
$this->options = $this->optionMapper->findByPoll($this->acl->getPollId());
$this->votes = $this->voteMapper->findByPoll($this->acl->getPollId());
$this->calculateVotes();
if ($this->poll->getHideBookedUp() && !$this->acl->getIsAllowed(Acl::PERMISSION_POLL_EDIT)) {
if ($this->acl->getPoll()->getHideBookedUp() && !$this->acl->getIsAllowed(Acl::PERMISSION_POLL_EDIT)) {
// hide booked up options except the user has edit permission
$this->filterBookedUp();
} elseif ($this->acl->getIsAllowed(Acl::PERMISSION_POLL_RESULTS_VIEW)) {
@ -155,11 +149,9 @@ class OptionService {
$this->acl->setPollId($pollId, Acl::PERMISSION_OPTIONS_ADD);
}
$this->poll = $this->acl->getPoll();
$this->option = new Option();
$this->option->setPollId($this->poll->getId());
$this->option->setOrder($this->getHighestOrder($this->poll->getId()) + 1);
$this->option->setPollId($this->acl->getPollId());
$this->option->setOrder($this->getHighestOrder($this->acl->getPollId()) + 1);
$this->setOption($timestamp, $pollOptionText, $duration);
if (!$this->acl->getIsOwner()) {
@ -168,7 +160,7 @@ class OptionService {
try {
$this->option = $this->optionMapper->insert($this->option);
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
} catch (UniqueConstraintViolationException $e) {
throw new DuplicateEntryException('This option already exists');
}
@ -184,8 +176,6 @@ class OptionService {
$this->option = $this->optionMapper->find($optionId);
$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
$this->setOption($timestamp, $pollOptionText, $duration);
$this->option = $this->optionMapper->update($this->option);
@ -202,19 +192,17 @@ class OptionService {
$this->option = $this->optionMapper->find($optionId);
if ($token) {
$this->acl->setToken($token);
$this->acl->setToken($token,Acl::PERMISSION_POLL_VIEW, $this->option->getPollId());
} else {
$this->acl->setPollId($this->option->getPollId());
}
$this->poll = $this->acl->getPoll();
if ($this->option->getOwner() !== $this->acl->getUserId()) {
$this->acl->request(Acl::PERMISSION_POLL_EDIT);
}
$this->optionMapper->delete($this->option);
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
return $this->option;
}
@ -227,11 +215,12 @@ class OptionService {
public function confirm(int $optionId): Option {
$this->option = $this->optionMapper->find($optionId);
$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
$this->option->setConfirmed($this->option->getConfirmed() ? 0 : time());
$this->option = $this->optionMapper->update($this->option);
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
return $this->option;
}
@ -250,14 +239,12 @@ class OptionService {
$this->option = $this->optionMapper->find($optionId);
$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
if ($this->poll->getType() !== Poll::TYPE_DATE) {
if ($this->acl->getPoll()->getType() !== Poll::TYPE_DATE) {
throw new InvalidPollTypeException('Sequences are only available in date polls');
}
if ($step === 0) {
return $this->optionMapper->findByPoll($this->poll->getId());
return $this->optionMapper->findByPoll($this->acl->getPollId());
}
$baseDate = new DateTime;
@ -265,7 +252,7 @@ class OptionService {
for ($i = 0; $i < $amount; $i++) {
$clonedOption = new Option();
$clonedOption->setPollId($this->poll->getId());
$clonedOption->setPollId($this->acl->getPollId());
$clonedOption->setDuration($this->option->getDuration());
$clonedOption->setConfirmed(0);
$clonedOption->setTimestamp($baseDate->modify($step . ' ' . $unit)->getTimestamp());
@ -280,8 +267,8 @@ class OptionService {
}
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
return $this->optionMapper->findByPoll($this->poll->getId());
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
return $this->optionMapper->findByPoll($this->acl->getPollId());
}
/**
@ -296,13 +283,12 @@ class OptionService {
*/
public function shift(int $pollId, int $step, string $unit): array {
$this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
if ($this->poll->getType() !== Poll::TYPE_DATE) {
if ($this->acl->getPoll()->getType() !== Poll::TYPE_DATE) {
throw new InvalidPollTypeException('Shifting is only available in date polls');
}
$this->options = $this->optionMapper->findByPoll($this->poll->getId());
$this->options = $this->optionMapper->findByPoll($this->acl->getPollId());
if ($step > 0) {
// avoid UniqueConstraintViolationException
@ -316,7 +302,7 @@ class OptionService {
$this->optionMapper->update($option);
}
return $this->optionMapper->findByPoll($this->poll->getId());
return $this->optionMapper->findByPoll($this->acl->getPollId());
}
/**
@ -352,24 +338,23 @@ class OptionService {
*/
public function reorder(int $pollId, array $options): array {
$this->acl->setPollId($pollId, Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
if ($this->poll->getType() === Poll::TYPE_DATE) {
if ($this->acl->getPoll()->getType() === Poll::TYPE_DATE) {
throw new InvalidPollTypeException('Not allowed in date polls');
}
$i = 0;
foreach ($options as $option) {
$this->option = $this->optionMapper->find($option['id']);
if ($this->poll->getId() === intval($this->option->getPollId())) {
if ($this->acl->getPollId() === intval($this->option->getPollId())) {
$this->option->setOrder(++$i);
$this->optionMapper->update($this->option);
}
}
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
return $this->optionMapper->findByPoll($this->poll->getId());
return $this->optionMapper->findByPoll($this->acl->getPollId());
}
/**
@ -384,25 +369,23 @@ class OptionService {
$this->option = $this->optionMapper->find($optionId);
$this->acl->setPollId($this->option->getPollId(), Acl::PERMISSION_POLL_EDIT);
$this->poll = $this->acl->getPoll();
if ($this->poll->getType() === Poll::TYPE_DATE) {
if ($this->acl->getPoll()->getType() === Poll::TYPE_DATE) {
throw new InvalidPollTypeException('Not allowed in date polls');
}
if ($newOrder < 1) {
$newOrder = 1;
} elseif ($newOrder > $this->getHighestOrder($this->poll->getId())) {
$newOrder = $this->getHighestOrder($this->poll->getId());
} elseif ($newOrder > $this->getHighestOrder($this->acl->getPollId())) {
$newOrder = $this->getHighestOrder($this->acl->getPollId());
}
foreach ($this->optionMapper->findByPoll($this->poll->getId()) as $option) {
foreach ($this->optionMapper->findByPoll($this->acl->getPollId()) as $option) {
$option->setOrder($this->moveModifier($this->option->getOrder(), $newOrder, $option->getOrder()));
$this->optionMapper->update($option);
}
$this->watchService->writeUpdate($this->poll->getId(), Watch::OBJECT_OPTIONS);
return $this->optionMapper->findByPoll($this->poll->getId());
$this->watchService->writeUpdate($this->acl->getPollId(), Watch::OBJECT_OPTIONS);
return $this->optionMapper->findByPoll($this->acl->getPollId());
}
/**
@ -435,7 +418,7 @@ class OptionService {
*/
private function setOption(int $timestamp = 0, ?string $pollOptionText = '', ?int $duration = 0): void {
if ($this->poll->getType() === Poll::TYPE_DATE) {
if ($timestamp) {
$this->option->setTimestamp($timestamp);
$this->option->setOrder($timestamp);
$this->option->setDuration($duration ?? 0);
@ -515,7 +498,7 @@ class OptionService {
})
);
$option->isBookedUp = $this->poll->getOptionLimit() ? $this->poll->getOptionLimit() <= $option->yes : false;
$option->isBookedUp = $this->acl->getPoll()->getOptionLimit() ? $this->acl->getPoll()->getOptionLimit() <= $option->yes : false;
// remove details, if the results shall be hidden
if (!$this->acl->getIsAllowed(Acl::PERMISSION_POLL_RESULTS_VIEW)) {
@ -524,14 +507,14 @@ class OptionService {
$option->maybe = 0;
$option->realNo = 0;
} else {
$option->no = count($this->voteMapper->findParticipantsByPoll($this->poll->getId())) - $option->maybe - $option->yes;
$option->no = count($this->voteMapper->findParticipantsByPoll($this->acl->getPollId())) - $option->maybe - $option->yes;
$option->no = $this->countParticipants() - $option->maybe - $option->yes;
}
}
}
private function countParticipants(): int {
return count($this->voteMapper->findParticipantsByPoll($this->poll->getId()));
return count($this->voteMapper->findParticipantsByPoll($this->acl->getPollId()));
}
/**

Просмотреть файл

@ -29,8 +29,8 @@ use OCA\Polls\Exceptions\VoteLimitExceededException;
use OCA\Polls\Db\Log;
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\Option;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\Watch;
use OCA\Polls\Model\Acl;
@ -49,9 +49,6 @@ class VoteService {
/** @var OptionMapper */
private $optionMapper;
/** @var PollMapper */
private $pollMapper;
/** @var Vote */
private $vote;
@ -67,7 +64,6 @@ class VoteService {
AnonymizeService $anonymizer,
LogService $logService,
OptionMapper $optionMapper,
PollMapper $pollMapper,
Vote $vote,
VoteMapper $voteMapper,
WatchService $watchService
@ -76,7 +72,6 @@ class VoteService {
$this->anonymizer = $anonymizer;
$this->logService = $logService;
$this->optionMapper = $optionMapper;
$this->pollMapper = $pollMapper;
$this->vote = $vote;
$this->voteMapper = $voteMapper;
$this->watchService = $watchService;
@ -95,53 +90,50 @@ class VoteService {
try {
if (!$this->acl->getIsAllowed(Acl::PERMISSION_POLL_RESULTS_VIEW)) {
return $this->voteMapper->findByPollAndUser($this->acl->getpollId(), $this->acl->getUserId());
} elseif (!$this->acl->getIsAllowed(Acl::PERMISSION_POLL_USERNAMES_VIEW)) {
}
if (!$this->acl->getIsAllowed(Acl::PERMISSION_POLL_USERNAMES_VIEW)) {
$this->anonymizer->set($this->acl->getpollId(), $this->acl->getUserId());
return $this->anonymizer->getVotes();
} else {
return $this->voteMapper->findByPoll($this->acl->getpollId());
}
return $this->voteMapper->findByPoll($this->acl->getpollId());
} catch (DoesNotExistException $e) {
return [];
}
}
private function checkLimits(int $optionId, string $userId):void {
$option = $this->optionMapper->find($optionId);
$poll = $this->pollMapper->find($option->getPollId());
private function checkLimits(Option $option, string $userId):void {
// check, if the optionlimit is reached or exceeded, if one is set
if ($poll->getOptionLimit() > 0) {
if ($poll->getOptionLimit() <= count($this->voteMapper->getYesVotesByOption($option->getPollId(), $option->getPollOptionText()))) {
if ($this->acl->getPoll()->getOptionLimit() > 0) {
if ($this->acl->getPoll()->getOptionLimit() <= count($this->voteMapper->getYesVotesByOption($option->getPollId(), $option->getPollOptionText()))) {
throw new VoteLimitExceededException;
}
}
// check if the votelimit for the user is reached or exceeded, if one is set
if ($poll->getVoteLimit() > 0) {
$pollOptionTexts = [];
$votecount = 0;
// exit, if no vote limit is set
if ($this->acl->getPoll()->getVoteLimit() < 1) {
return;
}
$options = $this->optionMapper->findByPoll($option->getPollId());
$votes = $this->voteMapper->getYesVotesByParticipant($option->getPollId(), $userId);
// 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()));
// Only count votes, which match to an actual existing option.
// Explanation: If an option is deleted, the corresponding votes are not deleted.
// create an array of pollOptionTexts
foreach ($options as $element) {
$pollOptionTexts[] = $element->getPollOptionText();
$votecount = 0;
$votes = $this->voteMapper->getYesVotesByParticipant($option->getPollId(), $userId);
foreach ($votes as $vote) {
if (in_array($vote->getVoteOptionText(), $pollOptionTexts)) {
$votecount++;
}
}
// only count relevant votes for the limit
foreach ($votes as $vote) {
if (in_array($vote->getVoteOptionText(), $pollOptionTexts)) {
$votecount++;
}
}
if ($poll->getVoteLimit() <= $votecount) {
throw new VoteLimitExceededException;
}
if ($this->acl->getPoll()->getVoteLimit() <= $votecount) {
throw new VoteLimitExceededException;
}
}
@ -152,16 +144,13 @@ class VoteService {
$option = $this->optionMapper->find($optionId);
if ($token) {
$this->acl->setToken($token, Acl::PERMISSION_VOTE_EDIT);
if (intval($option->getPollId()) !== $this->acl->getPollId()) {
throw new NotAuthorizedException;
}
$this->acl->setToken($token, Acl::PERMISSION_VOTE_EDIT, $option->getPollId());
} else {
$this->acl->setPollId($option->getPollId(), Acl::PERMISSION_VOTE_EDIT);
}
if ($setTo === 'yes') {
$this->checkLimits($optionId, $this->acl->getUserId());
$this->checkLimits($option, $this->acl->getUserId());
}
try {