|
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\AppFramework\Controller; |
|
32
|
|
|
use OCP\AppFramework\Http; |
|
33
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
34
|
|
|
|
|
35
|
|
|
use OCP\IGroupManager; |
|
36
|
|
|
use OCP\IUser; |
|
37
|
|
|
use OCP\IUserManager; |
|
38
|
|
|
use OCP\Security\ISecureRandom; |
|
39
|
|
|
|
|
40
|
|
|
use OCA\Polls\Db\Event; |
|
41
|
|
|
use OCA\Polls\Db\EventMapper; |
|
42
|
|
|
use OCA\Polls\Service\EventService; |
|
43
|
|
|
use OCA\Polls\Model\Acl; |
|
44
|
|
|
|
|
45
|
|
|
class EventController extends Controller { |
|
46
|
|
|
|
|
47
|
|
|
private $userId; |
|
48
|
|
|
private $mapper; |
|
49
|
|
|
private $logger; |
|
50
|
|
|
private $groupManager; |
|
51
|
|
|
private $userManager; |
|
52
|
|
|
private $eventService; |
|
53
|
|
|
private $event; |
|
54
|
|
|
private $acl; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* CommentController constructor. |
|
58
|
|
|
* @param string $appName |
|
59
|
|
|
* @param $userId |
|
60
|
|
|
* @param IRequest $request |
|
61
|
|
|
* @param ILogger $logger |
|
62
|
|
|
* @param EventMapper $mapper |
|
63
|
|
|
* @param IGroupManager $groupManager |
|
64
|
|
|
* @param IUserManager $userManager |
|
65
|
|
|
* @param EventService $eventService |
|
66
|
|
|
* @param Acl $acl |
|
67
|
|
|
*/ |
|
68
|
|
|
|
|
69
|
|
|
public function __construct( |
|
70
|
|
|
string $appName, |
|
71
|
|
|
$userId, |
|
72
|
|
|
IRequest $request, |
|
73
|
|
|
ILogger $logger, |
|
74
|
|
|
EventMapper $mapper, |
|
75
|
|
|
Event $event, |
|
76
|
|
|
IGroupManager $groupManager, |
|
77
|
|
|
IUserManager $userManager, |
|
78
|
|
|
EventService $eventService, |
|
79
|
|
|
Acl $acl |
|
80
|
|
|
) { |
|
81
|
|
|
parent::__construct($appName, $request); |
|
82
|
|
|
$this->userId = $userId; |
|
83
|
|
|
$this->mapper = $mapper; |
|
84
|
|
|
$this->logger = $logger; |
|
85
|
|
|
$this->groupManager = $groupManager; |
|
86
|
|
|
$this->userManager = $userManager; |
|
87
|
|
|
$this->eventService = $eventService; |
|
88
|
|
|
$this->event = $event; |
|
89
|
|
|
$this->acl = $acl; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get all polls |
|
94
|
|
|
* @NoAdminRequired |
|
95
|
|
|
* @NoCSRFRequired |
|
96
|
|
|
* @return DataResponse |
|
97
|
|
|
*/ |
|
98
|
|
|
|
|
99
|
|
|
public function list() { |
|
100
|
|
|
$events = []; |
|
101
|
|
|
if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
102
|
|
|
try { |
|
103
|
|
|
|
|
104
|
|
|
$events = array_filter($this->mapper->findAll(), function($item) { |
|
105
|
|
|
if ($this->acl->setPollId($item->getId())->getAllowView()) { |
|
106
|
|
|
return true; |
|
107
|
|
|
} else { |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
}); |
|
111
|
|
|
} catch (DoesNotExistException $e) { |
|
112
|
|
|
$events = []; |
|
113
|
|
|
// return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return new DataResponse($events, Http::STATUS_OK); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Read an entire poll based on poll id |
|
121
|
|
|
* @NoAdminRequired |
|
122
|
|
|
* @NoCSRFRequired |
|
123
|
|
|
* @PublicPage |
|
124
|
|
|
* @param integer $pollId |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function get($pollId) { |
|
128
|
|
|
|
|
129
|
|
|
try { |
|
130
|
|
|
if (!$this->acl->getFoundByToken()) { |
|
131
|
|
|
$this->acl->setPollId($pollId); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->event = $this->mapper->find($pollId); |
|
135
|
|
|
|
|
136
|
|
|
} catch (DoesNotExistException $e) { |
|
137
|
|
|
$this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']); |
|
138
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($this->event->getType() == 0) { |
|
142
|
|
|
$pollType = 'datePoll'; |
|
143
|
|
|
} else { |
|
144
|
|
|
$pollType = 'textPoll'; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// TODO: add migration for this |
|
148
|
|
|
if ($this->event->getAccess() === 'public' || $this->event->getAccess() === 'registered') { |
|
149
|
|
|
$this->event->setAccess('public'); |
|
150
|
|
|
} else { |
|
151
|
|
|
$this->event->setAccess('hidden'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return new DataResponse((object) [ |
|
155
|
|
|
'id' => $this->event->getId(), |
|
156
|
|
|
'type' => $pollType, |
|
157
|
|
|
'title' => $this->event->getTitle(), |
|
158
|
|
|
'description' => $this->event->getDescription(), |
|
159
|
|
|
'owner' => $this->event->getOwner(), |
|
160
|
|
|
'created' => $this->event->getCreated(), |
|
161
|
|
|
'access' => $this->event->getAccess(), |
|
162
|
|
|
'expire' => $this->event->getExpire(), |
|
163
|
|
|
'expiration' => $this->event->getExpiration(), |
|
164
|
|
|
'isAnonymous' => boolval($this->event->getIsAnonymous()), |
|
165
|
|
|
'fullAnonymous' => boolval($this->event->getFullAnonymous()), |
|
166
|
|
|
'allowMaybe' => boolval($this->event->getAllowMaybe()), |
|
167
|
|
|
'voteLimit' => $this->event->getVoteLimit(), |
|
168
|
|
|
'showResults' => $this->event->getShowResults(), |
|
169
|
|
|
'deleted' => boolval($this->event->getDeleted()), |
|
170
|
|
|
'deleteDate' => $this->event->getDeleteDate() |
|
171
|
|
|
], |
|
172
|
|
|
Http::STATUS_OK); |
|
173
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* getByToken |
|
178
|
|
|
* Read all options of a poll based on a share token and return list as array |
|
179
|
|
|
* @NoAdminRequired |
|
180
|
|
|
* @PublicPage |
|
181
|
|
|
* @NoCSRFRequired |
|
182
|
|
|
* @param string $token |
|
183
|
|
|
* @return DataResponse |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getByToken($token) { |
|
186
|
|
|
|
|
187
|
|
|
try { |
|
188
|
|
|
$this->acl->setToken($token); |
|
189
|
|
|
} catch (DoesNotExistException $e) { |
|
190
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
191
|
|
|
} |
|
192
|
|
|
return $this->get($this->acl->getPollId()); |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Write poll (create/update) |
|
198
|
|
|
* @NoAdminRequired |
|
199
|
|
|
* @param Array $event |
|
200
|
|
|
* @return DataResponse |
|
201
|
|
|
*/ |
|
202
|
|
|
|
|
203
|
|
|
public function write($event) { |
|
204
|
|
|
|
|
205
|
|
|
try { |
|
206
|
|
|
// Find existing poll |
|
207
|
|
|
$this->event = $this->mapper->find($event['id']); |
|
208
|
|
|
$this->acl->setPollId($this->event->getId()); |
|
209
|
|
|
|
|
210
|
|
|
if (!$this->acl->getAllowEdit()) { |
|
211
|
|
|
$this->logger->alert('Unauthorized write attempt from user ' . $this->userId); |
|
212
|
|
|
return new DataResponse('Unauthorized write attempt.', Http::STATUS_UNAUTHORIZED); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
if ($this->event->getDeleted() !== $event['deleted']) { |
|
216
|
|
|
if ($event['deleted']) { |
|
217
|
|
|
$this->event->setDeleteDate(date('Y-m-d')); |
|
|
|
|
|
|
218
|
|
|
} else { |
|
219
|
|
|
$this->event->setDeleteDate('0'); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
$this->event->setDeleted($event['deleted']); |
|
223
|
|
|
} catch (Exception $e) { |
|
224
|
|
|
$this->event = new Event(); |
|
225
|
|
|
$this->acl->setPollId(0); |
|
226
|
|
|
|
|
227
|
|
|
if ($event['type'] === 'datePoll') { |
|
228
|
|
|
$this->event->setType(0); |
|
229
|
|
|
} elseif ($event['type'] === 'textPoll') { |
|
230
|
|
|
$this->event->setType(1); |
|
231
|
|
|
} else { |
|
232
|
|
|
$this->event->setType($event['type']); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$this->event->setOwner($this->userId); |
|
236
|
|
|
$this->event->setCreated(date('Y-m-d H:i:s',time())); |
|
237
|
|
|
} finally { |
|
238
|
|
|
|
|
239
|
|
|
$this->event->setTitle($event['title']); |
|
240
|
|
|
$this->event->setDescription($event['description']); |
|
241
|
|
|
|
|
242
|
|
|
$this->event->setAccess($event['access']); |
|
243
|
|
|
$this->event->setExpiration($event['expiration']); |
|
244
|
|
|
$this->event->setExpire(date('Y-m-d H:i:s', strtotime($event['expire']))); |
|
245
|
|
|
$this->event->setIsAnonymous(intval($event['isAnonymous'])); |
|
246
|
|
|
$this->event->setFullAnonymous(intval($event['fullAnonymous'])); |
|
247
|
|
|
$this->event->setAllowMaybe(intval($event['allowMaybe'])); |
|
248
|
|
|
// $this->event->setDeleteDate(time()); |
|
249
|
|
|
$this->event->setVoteLimit(intval($event['voteLimit'])); |
|
250
|
|
|
$this->event->setShowResults($event['showResults']); |
|
251
|
|
|
|
|
252
|
|
|
if ($this->acl->getPollId() > 0) { |
|
253
|
|
|
$this->mapper->update($this->event); |
|
254
|
|
|
} else { |
|
255
|
|
|
$this->mapper->insert($this->event); |
|
256
|
|
|
} |
|
257
|
|
|
$this->event = $this->get($this->event->getId()); |
|
258
|
|
|
return new DataResponse($this->event, Http::STATUS_OK); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|