Passed
Pull Request — master (#823)
by René
04:05
created

PollController::deletePermanently()   A

Complexity

Conditions 4
Paths 13

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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