|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author René Gieling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Polls\Service; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
28
|
|
|
|
|
29
|
|
|
use OCA\Polls\Db\Vote; |
|
30
|
|
|
use OCA\Polls\Db\VoteMapper; |
|
31
|
|
|
use OCA\Polls\Db\OptionMapper; |
|
32
|
|
|
use OCA\Polls\Service\AnonymizeService; |
|
33
|
|
|
use OCA\Polls\Service\LogService; |
|
34
|
|
|
use OCA\Polls\Model\Acl; |
|
35
|
|
|
|
|
36
|
|
|
class VoteService { |
|
37
|
|
|
|
|
38
|
|
|
private $voteMapper; |
|
39
|
|
|
private $vote; |
|
40
|
|
|
private $optionMapper; |
|
41
|
|
|
private $anonymizer; |
|
42
|
|
|
private $logService; |
|
43
|
|
|
private $acl; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* VoteController constructor. |
|
47
|
|
|
* @param VoteMapper $voteMapper |
|
48
|
|
|
* @param Vote $vote |
|
49
|
|
|
* @param OptionMapper $optionMapper |
|
50
|
|
|
* @param AnonymizeService $anonymizer |
|
51
|
|
|
* @param LogService $logService |
|
52
|
|
|
* @param Acl $acl |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
VoteMapper $voteMapper, |
|
56
|
|
|
Vote $vote, |
|
57
|
|
|
OptionMapper $optionMapper, |
|
58
|
|
|
AnonymizeService $anonymizer, |
|
59
|
|
|
LogService $logService, |
|
60
|
|
|
Acl $acl |
|
61
|
|
|
) { |
|
62
|
|
|
$this->voteMapper = $voteMapper; |
|
63
|
|
|
$this->vote = $vote; |
|
64
|
|
|
$this->optionMapper = $optionMapper; |
|
65
|
|
|
$this->anonymizer = $anonymizer; |
|
66
|
|
|
$this->logService = $logService; |
|
67
|
|
|
$this->acl = $acl; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get all votes of given poll |
|
72
|
|
|
* Read all votes of a poll based on the poll id and return list as array |
|
73
|
|
|
* @NoAdminRequired |
|
74
|
|
|
* @param integer $pollId |
|
75
|
|
|
* @param string $token |
|
76
|
|
|
* @return DataResponse |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function list($pollId = 0, $token = '') { |
|
79
|
|
|
if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowView()) { |
|
80
|
|
|
throw new NotAuthorizedException; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$this->acl->getAllowSeeResults()) { |
|
84
|
|
|
return $this->voteMapper->findByPollAndUser($this->acl->getpollId(), $this->acl->getUserId()); |
|
85
|
|
|
} elseif (!$this->acl->getAllowSeeUsernames()) { |
|
86
|
|
|
$this->anonymizer->set($this->acl->getpollId(), $this->acl->getUserId()); |
|
87
|
|
|
return $this->anonymizer->getVotes(); |
|
88
|
|
|
} else { |
|
89
|
|
|
return $this->voteMapper->findByPoll($this->acl->getpollId()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* set |
|
95
|
|
|
* @NoAdminRequired |
|
96
|
|
|
* @param integer $pollId |
|
97
|
|
|
* @param Array $option |
|
98
|
|
|
* @param string $setTo |
|
99
|
|
|
* @param string $token |
|
100
|
|
|
* @return DataResponse |
|
101
|
|
|
*/ |
|
102
|
|
|
public function set($pollId = 0, $pollOptionText, $setTo, $token = '') { |
|
103
|
|
|
|
|
104
|
|
|
if (!$this->acl->setPollIdOrToken($pollId, $token)->getAllowVote()) { |
|
105
|
|
|
throw new NotAuthorizedException; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$option = $this->optionMapper->findByPollAndText($this->acl->getpollId(), $pollOptionText); |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
$this->vote = $this->voteMapper->findSingleVote($this->acl->getpollId(), $option->getPollOptionText(), $this->acl->getUserId()); |
|
112
|
|
|
$this->vote->setVoteAnswer($setTo); |
|
113
|
|
|
$this->voteMapper->update($this->vote); |
|
114
|
|
|
|
|
115
|
|
|
} catch (DoesNotExistException $e) { |
|
116
|
|
|
// Vote does not exist, insert as new Vote |
|
117
|
|
|
$this->vote = new Vote(); |
|
118
|
|
|
|
|
119
|
|
|
$this->vote->setPollId($this->acl->getpollId()); |
|
120
|
|
|
$this->vote->setUserId($this->acl->getUserId()); |
|
121
|
|
|
$this->vote->setVoteOptionText($option->getPollOptionText()); |
|
122
|
|
|
$this->vote->setVoteOptionId($option->getId()); |
|
123
|
|
|
$this->vote->setVoteAnswer($setTo); |
|
124
|
|
|
$this->voteMapper->insert($this->vote); |
|
125
|
|
|
|
|
126
|
|
|
} finally { |
|
127
|
|
|
$this->logService->setLog($this->vote->getPollId(), 'setVote', $this->vote->getUserId()); |
|
128
|
|
|
return $this->vote; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* delete |
|
134
|
|
|
* @NoAdminRequired |
|
135
|
|
|
* @NoCSRFRequired |
|
136
|
|
|
* @param integer $voteId |
|
137
|
|
|
* @param string $userId |
|
138
|
|
|
* @param integer $pollId |
|
139
|
|
|
* @return DataResponse |
|
140
|
|
|
*/ |
|
141
|
|
|
public function delete($pollId, $userId) { |
|
142
|
|
|
|
|
143
|
|
|
if (!$this->acl->setPollId($pollId)->getAllowEdit()) { |
|
144
|
|
|
throw new NotAuthorizedException; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$votes = $this->voteMapper->deleteByPollAndUser($pollId, $userId); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths