|
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\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\IRequest; |
|
30
|
|
|
use OCP\ILogger; |
|
31
|
|
|
use OCP\IL10N; |
|
32
|
|
|
use OCP\AppFramework\Controller; |
|
33
|
|
|
use OCP\AppFramework\Http; |
|
34
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
35
|
|
|
|
|
36
|
|
|
use OCP\IGroupManager; |
|
37
|
|
|
use OCP\IUser; |
|
38
|
|
|
use OCP\IUserManager; |
|
39
|
|
|
use OCP\Security\ISecureRandom; |
|
40
|
|
|
|
|
41
|
|
|
use OCA\Polls\Db\Comment; |
|
42
|
|
|
use OCA\Polls\Db\CommentMapper; |
|
43
|
|
|
use OCA\Polls\Db\Poll; |
|
44
|
|
|
use OCA\Polls\Db\PollMapper; |
|
45
|
|
|
use OCA\Polls\Db\Option; |
|
46
|
|
|
use OCA\Polls\Db\OptionMapper; |
|
47
|
|
|
use OCA\Polls\Db\Share; |
|
48
|
|
|
use OCA\Polls\Db\ShareMapper; |
|
49
|
|
|
use OCA\Polls\Db\Vote; |
|
50
|
|
|
use OCA\Polls\Db\VoteMapper; |
|
51
|
|
|
use OCA\Polls\Service\LogService; |
|
52
|
|
|
use OCA\Polls\Service\MailService; |
|
53
|
|
|
use OCA\Polls\Service\AnonymizeService; |
|
54
|
|
|
use OCA\Polls\Model\Acl; |
|
55
|
|
|
|
|
56
|
|
|
class PollController extends Controller { |
|
57
|
|
|
|
|
58
|
|
|
private $userId; |
|
59
|
|
|
private $commentMapper; |
|
60
|
|
|
private $pollMapper; |
|
61
|
|
|
private $optionMapper; |
|
62
|
|
|
private $shareMapper; |
|
63
|
|
|
private $voteMapper; |
|
64
|
|
|
private $trans; |
|
65
|
|
|
private $logger; |
|
66
|
|
|
private $groupManager; |
|
67
|
|
|
private $userManager; |
|
68
|
|
|
private $poll; |
|
69
|
|
|
private $logService; |
|
70
|
|
|
private $mailService; |
|
71
|
|
|
private $anonymizer; |
|
72
|
|
|
private $acl; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* CommentController constructor. |
|
76
|
|
|
* @param string $appName |
|
77
|
|
|
* @param $userId |
|
78
|
|
|
* @param IRequest $request |
|
79
|
|
|
* @param ILogger $logger |
|
80
|
|
|
* @param IL10N $trans |
|
81
|
|
|
* @param OptionMapper $optionMapper |
|
82
|
|
|
* @param PollMapper $pollMapper |
|
83
|
|
|
* @param IGroupManager $groupManager |
|
84
|
|
|
* @param IUserManager $userManager |
|
85
|
|
|
* @param LogService $logService |
|
86
|
|
|
* @param MailService $mailService |
|
87
|
|
|
* @param AnonymizeService $anonymizer |
|
88
|
|
|
* @param Acl $acl |
|
89
|
|
|
*/ |
|
90
|
|
|
|
|
91
|
|
|
public function __construct( |
|
92
|
|
|
string $appName, |
|
93
|
|
|
$userId, |
|
94
|
|
|
IRequest $request, |
|
95
|
|
|
ILogger $logger, |
|
96
|
|
|
IL10N $trans, |
|
97
|
|
|
CommentMapper $commentMapper, |
|
98
|
|
|
OptionMapper $optionMapper, |
|
99
|
|
|
PollMapper $pollMapper, |
|
100
|
|
|
ShareMapper $shareMapper, |
|
101
|
|
|
VoteMapper $voteMapper, |
|
102
|
|
|
Poll $poll, |
|
103
|
|
|
IGroupManager $groupManager, |
|
104
|
|
|
IUserManager $userManager, |
|
105
|
|
|
LogService $logService, |
|
106
|
|
|
MailService $mailService, |
|
107
|
|
|
AnonymizeService $anonymizer, |
|
108
|
|
|
Acl $acl |
|
109
|
|
|
) { |
|
110
|
|
|
parent::__construct($appName, $request); |
|
111
|
|
|
$this->userId = $userId; |
|
112
|
|
|
$this->trans = $trans; |
|
113
|
|
|
$this->commentMapper = $commentMapper; |
|
114
|
|
|
$this->pollMapper = $pollMapper; |
|
115
|
|
|
$this->optionMapper = $optionMapper; |
|
116
|
|
|
$this->shareMapper = $shareMapper; |
|
117
|
|
|
$this->voteMapper = $voteMapper; |
|
118
|
|
|
$this->logger = $logger; |
|
119
|
|
|
$this->groupManager = $groupManager; |
|
120
|
|
|
$this->userManager = $userManager; |
|
121
|
|
|
$this->poll = $poll; |
|
122
|
|
|
$this->logService = $logService; |
|
123
|
|
|
$this->mailService = $mailService; |
|
124
|
|
|
$this->anonymizer = $anonymizer; |
|
125
|
|
|
$this->acl = $acl; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* list |
|
131
|
|
|
* @NoAdminRequired |
|
132
|
|
|
* @NoCSRFRequired |
|
133
|
|
|
* @return DataResponse |
|
134
|
|
|
*/ |
|
135
|
|
|
|
|
136
|
|
|
public function list() { |
|
137
|
|
|
if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
138
|
|
|
$pollList = []; |
|
139
|
|
|
|
|
140
|
|
|
try { |
|
141
|
|
|
|
|
142
|
|
|
$polls = $this->pollMapper->findAll(); |
|
143
|
|
|
// TODO: Not the elegant way. Improvement neccessary |
|
144
|
|
|
foreach ($polls as $poll) { |
|
145
|
|
|
$combinedPoll = (object) array_merge( |
|
146
|
|
|
(array) json_decode(json_encode($poll)), (array) json_decode(json_encode($this->acl->setPollId($poll->getId())))); |
|
147
|
|
|
if ($combinedPoll->allowView) { |
|
148
|
|
|
$pollList[] = $combinedPoll; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return new DataResponse($pollList, Http::STATUS_OK); |
|
153
|
|
|
} catch (DoesNotExistException $e) { |
|
154
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
155
|
|
|
} |
|
156
|
|
|
} else { |
|
157
|
|
|
return new DataResponse([], Http::STATUS_OK); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* get |
|
165
|
|
|
* @NoAdminRequired |
|
166
|
|
|
* @NoCSRFRequired |
|
167
|
|
|
* @param integer $pollId |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
public function get($pollId) { |
|
171
|
|
|
|
|
172
|
|
|
try { |
|
173
|
|
|
if (!$this->acl->getFoundByToken()) { |
|
174
|
|
|
$this->acl->setPollId($pollId); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->poll = $this->pollMapper->find($pollId); |
|
178
|
|
|
|
|
179
|
|
|
if (!$this->acl->getAllowView()) { |
|
180
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$options = $this->optionMapper->findByPoll($pollId); |
|
184
|
|
|
|
|
185
|
|
|
if ($this->acl->getAllowEdit()) { |
|
186
|
|
|
$shares = $this->shareMapper->findByPoll($pollId); |
|
187
|
|
|
} else { |
|
188
|
|
|
$shares = []; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ($this->acl->getAllowSeeUsernames()) { |
|
192
|
|
|
$comments = $this->commentMapper->findByPoll($pollId); |
|
193
|
|
|
|
|
194
|
|
|
if ($this->acl->getAllowSeeResults()) { |
|
195
|
|
|
$votes = $this->voteMapper->findByPoll($pollId); |
|
196
|
|
|
} else { |
|
197
|
|
|
$votes = $this->voteMapper->findByPollAndUser($pollId, $this->acl->getUserId()); |
|
198
|
|
|
} |
|
199
|
|
|
} else { |
|
200
|
|
|
$this->anonymizer->set($pollId, $this->acl->getUserId()); |
|
201
|
|
|
$comments = $this->anonymizer->getComments(); |
|
202
|
|
|
$votes = $this->anonymizer->getVotes(); |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return new DataResponse([ |
|
207
|
|
|
'acl' => $this->acl, |
|
208
|
|
|
'comments' => $comments, |
|
209
|
|
|
'options' => $options, |
|
210
|
|
|
'poll' => $this->poll, |
|
211
|
|
|
'shares' => $shares, |
|
212
|
|
|
'votes' => $votes |
|
213
|
|
|
], Http::STATUS_OK); |
|
214
|
|
|
|
|
215
|
|
|
} catch (DoesNotExistException $e) { |
|
216
|
|
|
$this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']); |
|
217
|
|
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* getByToken |
|
223
|
|
|
* Read all options of a poll based on a share token and return list as array |
|
224
|
|
|
* @NoAdminRequired |
|
225
|
|
|
* @PublicPage |
|
226
|
|
|
* @NoCSRFRequired |
|
227
|
|
|
* @param string $token |
|
228
|
|
|
* @return DataResponse |
|
229
|
|
|
*/ |
|
230
|
|
|
public function getByToken($token) { |
|
231
|
|
|
try { |
|
232
|
|
|
return $this->get($this->acl->setToken($token)->getPollId()); |
|
233
|
|
|
} catch (DoesNotExistException $e) { |
|
234
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* delete |
|
241
|
|
|
* @NoAdminRequired |
|
242
|
|
|
* @param Array $poll |
|
243
|
|
|
* @return DataResponse |
|
244
|
|
|
*/ |
|
245
|
|
|
|
|
246
|
|
|
public function delete($pollId) { |
|
247
|
|
|
|
|
248
|
|
|
try { |
|
249
|
|
|
// Find existing poll |
|
250
|
|
|
$this->poll = $this->pollMapper->find($pollId); |
|
251
|
|
|
$this->acl->setPollId($this->poll->getId()); |
|
252
|
|
|
|
|
253
|
|
|
if (!$this->acl->getAllowEdit()) { |
|
254
|
|
|
$this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); |
|
255
|
|
|
return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
if ($this->poll->getDeleted()) { |
|
259
|
|
|
$this->poll->setDeleted(0); |
|
260
|
|
|
} else { |
|
261
|
|
|
$this->poll->setDeleted(time()); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->pollMapper->update($this->poll); |
|
265
|
|
|
$this->logService->setLog($this->poll->getId(), 'deletePoll'); |
|
266
|
|
|
return new DataResponse(['deleted' => $pollId], Http::STATUS_OK); |
|
267
|
|
|
|
|
268
|
|
|
} catch (Exception $e) { |
|
269
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* deletePermanently |
|
275
|
|
|
* @NoAdminRequired |
|
276
|
|
|
* @param Array $poll |
|
277
|
|
|
* @return DataResponse |
|
278
|
|
|
*/ |
|
279
|
|
|
|
|
280
|
|
|
public function deletePermanently($pollId) { |
|
281
|
|
|
|
|
282
|
|
|
try { |
|
283
|
|
|
// Find existing poll |
|
284
|
|
|
$this->poll = $this->pollMapper->find($pollId); |
|
285
|
|
|
$this->acl->setPollId($this->poll->getId()); |
|
286
|
|
|
|
|
287
|
|
|
if (!$this->acl->getAllowEdit()) { |
|
288
|
|
|
$this->logger->alert('Unauthorized delete attempt from user ' . $this->userId); |
|
289
|
|
|
return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
if (!$this->poll->getDeleted()) { |
|
293
|
|
|
$this->logger->alert('user ' . $this->userId . ' trying to permanently delete active poll'); |
|
294
|
|
|
return new DataResponse(['message' => 'Permanent deletion of active poll.'], Http::STATUS_CONFLICT); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$this->pollMapper->delete($this->poll); |
|
298
|
|
|
return new DataResponse([], Http::STATUS_OK); |
|
299
|
|
|
|
|
300
|
|
|
} catch (Exception $e) { |
|
301
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* write |
|
307
|
|
|
* @NoAdminRequired |
|
308
|
|
|
* @param Array $poll |
|
309
|
|
|
* @return DataResponse |
|
310
|
|
|
*/ |
|
311
|
|
|
|
|
312
|
|
|
public function write($poll) { |
|
313
|
|
|
|
|
314
|
|
|
try { |
|
315
|
|
|
// Find existing poll |
|
316
|
|
|
$this->poll = $this->pollMapper->find($poll['id']); |
|
317
|
|
|
$this->acl->setPollId($this->poll->getId()); |
|
318
|
|
|
if (!$this->acl->getAllowEdit()) { |
|
319
|
|
|
$this->logger->alert('Unauthorized write attempt from user ' . $this->userId); |
|
320
|
|
|
return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
} catch (Exception $e) { |
|
324
|
|
|
$this->poll = new Poll(); |
|
325
|
|
|
|
|
326
|
|
|
$this->poll->setType($poll['type']); |
|
327
|
|
|
$this->poll->setOwner($this->userId); |
|
328
|
|
|
$this->poll->setCreated(time()); |
|
329
|
|
|
} finally { |
|
330
|
|
|
$this->poll->setTitle($poll['title']); |
|
331
|
|
|
$this->poll->setDescription($poll['description']); |
|
332
|
|
|
$this->poll->setAccess($poll['access']); |
|
333
|
|
|
$this->poll->setExpire($poll['expire']); |
|
334
|
|
|
$this->poll->setAnonymous(intval($poll['anonymous'])); |
|
335
|
|
|
$this->poll->setFullAnonymous(0); |
|
336
|
|
|
$this->poll->setAllowMaybe(intval($poll['allowMaybe'])); |
|
337
|
|
|
$this->poll->setVoteLimit(intval($poll['voteLimit'])); |
|
338
|
|
|
$this->poll->setSettings(''); |
|
339
|
|
|
$this->poll->setOptions(''); |
|
340
|
|
|
$this->poll->setShowResults($poll['showResults']); |
|
341
|
|
|
$this->poll->setDeleted($poll['deleted']); |
|
342
|
|
|
$this->poll->setAdminAccess($poll['adminAccess']); |
|
343
|
|
|
|
|
344
|
|
|
if ($this->poll->getId() > 0) { |
|
345
|
|
|
$this->pollMapper->update($this->poll); |
|
346
|
|
|
$this->logService->setLog($this->poll->getId(), 'updatePoll'); |
|
347
|
|
|
} else { |
|
348
|
|
|
$this->pollMapper->insert($this->poll); |
|
349
|
|
|
$this->logService->setLog($this->poll->getId(), 'addPoll'); |
|
350
|
|
|
} |
|
351
|
|
|
return $this->get($this->poll->getId()); |
|
352
|
|
|
// $this->acl->setPollId($this->poll->getId()); |
|
353
|
|
|
// return new DataResponse([ |
|
354
|
|
|
// 'poll' => $this->poll, |
|
355
|
|
|
// 'acl' => $this->acl |
|
356
|
|
|
// ], Http::STATUS_OK); |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* clone |
|
362
|
|
|
* @NoAdminRequired |
|
363
|
|
|
* @param integer $pollId |
|
364
|
|
|
* @return DataResponse |
|
365
|
|
|
*/ |
|
366
|
|
|
public function clone($pollId) { |
|
367
|
|
|
$this->poll = $this->pollMapper->find($pollId); |
|
368
|
|
|
|
|
369
|
|
|
$clonePoll = new Poll(); |
|
370
|
|
|
$clonePoll->setOwner($this->userId); |
|
371
|
|
|
$clonePoll->setCreated(time()); |
|
372
|
|
|
$clonePoll->setTitle('Clone of ' . $this->poll->getTitle()); |
|
373
|
|
|
$clonePoll->setDeleted(0); |
|
374
|
|
|
|
|
375
|
|
|
$clonePoll->setType($this->poll->getType()); |
|
376
|
|
|
$clonePoll->setDescription($this->poll->getDescription()); |
|
377
|
|
|
$clonePoll->setAccess($this->poll->getAccess()); |
|
378
|
|
|
$clonePoll->setExpire($this->poll->getExpire()); |
|
379
|
|
|
$clonePoll->setAnonymous(intval($this->poll->getAnonymous())); |
|
380
|
|
|
$clonePoll->setFullAnonymous(0); |
|
381
|
|
|
$clonePoll->setAllowMaybe(intval($this->poll->getAllowMaybe())); |
|
382
|
|
|
$clonePoll->setVoteLimit(intval($this->poll->getVoteLimit())); |
|
383
|
|
|
$clonePoll->setSettings(''); |
|
384
|
|
|
$clonePoll->setOptions(''); |
|
385
|
|
|
$clonePoll->setShowResults($this->poll->getShowResults()); |
|
386
|
|
|
$clonePoll->setAdminAccess($this->poll->getAdminAccess()); |
|
387
|
|
|
|
|
388
|
|
|
$this->pollMapper->insert($clonePoll); |
|
389
|
|
|
$this->logService->setLog($clonePoll->getId(), 'addPoll'); |
|
390
|
|
|
|
|
391
|
|
|
foreach ($this->optionMapper->findByPoll($pollId) as $option) { |
|
392
|
|
|
$newOption = new Option(); |
|
393
|
|
|
$newOption->setPollId($clonePoll->getId()); |
|
394
|
|
|
$newOption->setPollOptionText($option->getPollOptionText()); |
|
395
|
|
|
$newOption->setTimestamp($option->getTimestamp()); |
|
396
|
|
|
|
|
397
|
|
|
$this->optionMapper->insert($newOption); |
|
398
|
|
|
} |
|
399
|
|
|
return new DataResponse(['pollId' => $clonePoll->getId()], Http::STATUS_OK); |
|
400
|
|
|
|
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
} |
|
404
|
|
|
|