Completed
Push — master ( 527ecb...217492 )
by René
04:32
created

PollController::write()   A

Complexity

Conditions 4
Paths 73

Size

Total Lines 45
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 34
nc 73
nop 1
dl 0
loc 45
ccs 0
cts 37
cp 0
crap 20
rs 9.376
c 0
b 0
f 0
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\Poll;
41
use OCA\Polls\Db\PollMapper;
42
use OCA\Polls\Service\PollService;
43
use OCA\Polls\Service\LogService;
44
use OCA\Polls\Service\MailService;
45
use OCA\Polls\Model\Acl;
46
47
class PollController extends Controller {
48
49
	private $userId;
50
	private $mapper;
51
	private $logger;
52
	private $groupManager;
53
	private $userManager;
54
	private $pollService;
55
	private $poll;
56
	private $logService;
57
	private $MailService;
0 ignored issues
show
introduced by
The private property $MailService is not used, and could be removed.
Loading history...
58
	private $acl;
59
60
	/**
61
	 * CommentController constructor.
62
	 * @param string $appName
63
	 * @param $userId
64
	 * @param IRequest $request
65
	 * @param ILogger $logger
66
	 * @param PollMapper $mapper
67
	 * @param IGroupManager $groupManager
68
	 * @param IUserManager $userManager
69
	 * @param PollService $pollService
70
	 * @param LogService $logService
71
	 * @param MailService $mailService
72
	 * @param Acl $acl
73
	 */
74
75
	public function __construct(
76
		string $appName,
77
		$userId,
78
		IRequest $request,
79
		ILogger $logger,
80
		PollMapper $mapper,
81
		Poll $poll,
82
		IGroupManager $groupManager,
83
		IUserManager $userManager,
84
		PollService $pollService,
85
		LogService $logService,
86
		MailService $mailService,
87
		Acl $acl
88
	) {
89
		parent::__construct($appName, $request);
90
		$this->userId = $userId;
91
		$this->mapper = $mapper;
92
		$this->logger = $logger;
93
		$this->groupManager = $groupManager;
94
		$this->userManager = $userManager;
95
		$this->pollService = $pollService;
96
		$this->poll = $poll;
97
		$this->logService = $logService;
98
		$this->mailService = $mailService;
0 ignored issues
show
Bug Best Practice introduced by
The property mailService does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
99
		$this->acl = $acl;
100
	}
101
102
	/**
103
	 * Get all polls
104
	 * @NoAdminRequired
105
	 * @NoCSRFRequired
106
	 * @PublicPage
107
	 * @return DataResponse
108
	 */
109
110
	public function list() {
111
		$polls = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $polls is dead and can be removed.
Loading history...
112
		// TODO: Remove this, because it's just for easy testing purposes
113
		// $this->mailService->sendNotifications();
114
		if (\OC::$server->getUserSession()->isLoggedIn()) {
115
			try {
116
				$polls = array_values(array_filter($this->mapper->findAll(), function($item) {
117
					return $this->acl->setPollId($item->getId())->getAllowView();
118
    			}));
119
				return new DataResponse($polls, Http::STATUS_OK);
120
			} catch (DoesNotExistException $e) {
121
				$polls = [];
122
			}
123
		}
124
	}
125
126
	/**
127
	 * Read an entire poll based on poll id
128
	 * @NoAdminRequired
129
	 * @NoCSRFRequired
130
	 * @PublicPage
131
	 * @param integer $pollId
132
	 * @return array
133
	 */
134
 	public function get($pollId) {
135
136
 		try {
137
			if (!$this->acl->getFoundByToken()) {
138
				$this->acl->setPollId($pollId);
139
			}
140
141
			$this->poll = $this->mapper->find($pollId);
142
			// if ($this->poll->getType() == 0) {
143
			// 	$pollType = 'datePoll';
144
			// } else {
145
			// 	$pollType = 'textPoll';
146
			// }
147
148
			// TODO: add migration for this
149
			// if ($this->poll->getAccess() === 'public' || $this->poll->getAccess() === 'registered') {
150
			// 	$this->poll->setAccess('public');
151
			// } else {
152
			// 	$this->poll->setAccess('hidden');
153
			// }
154
155
			return new DataResponse((object)
156
				$this->poll
157
			,
158
			Http::STATUS_OK);
159
160
		} catch (DoesNotExistException $e) {
161
			$this->logger->info('Poll ' . $pollId . ' not found!', ['app' => 'polls']);
162
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
163
 		}
164
 	}
165
166
	/**
167
	 * getByToken
168
	 * Read all options of a poll based on a share token and return list as array
169
	 * @NoAdminRequired
170
	 * @PublicPage
171
	 * @NoCSRFRequired
172
	 * @param string $token
173
	 * @return DataResponse
174
	 */
175
	public function getByToken($token) {
176
177
		try {
178
			$this->acl->setToken($token);
179
			return $this->get($this->acl->getPollId());
180
		} catch (DoesNotExistException $e) {
181
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
182
		}
183
184
	}
185
186
	/**
187
	 * Write poll (create/update)
188
	 * @NoAdminRequired
189
	 * @param Array $poll
190
	 * @return DataResponse
191
	 */
192
193
	public function write($poll) {
194
195
		try {
196
			// Find existing poll
197
			$this->poll = $this->mapper->find($poll['id']);
198
			$this->acl->setPollId($this->poll->getId());
199
200
			if (!$this->acl->getAllowEdit()) {
201
				$this->logger->alert('Unauthorized write attempt from user ' . $this->userId);
202
				return new DataResponse(['message' => 'Unauthorized write attempt.'], Http::STATUS_UNAUTHORIZED);
203
			}
204
205
			$logMessageId = 'updatePoll';
206
207
		} catch (Exception $e) {
208
209
			$this->poll = new Poll();
210
211
			$this->poll->setType($poll['type']);
212
			$this->poll->setOwner($this->userId);
213
			$this->poll->setCreated(time());
214
			$this->acl->setPollId(0);
215
		} finally {
216
			$this->poll->setTitle($poll['title']);
217
			$this->poll->setDescription($poll['description']);
218
			$this->poll->setAccess($poll['access']);
219
			$this->poll->setExpire($poll['expire']);
220
			$this->poll->setAnonymous(intval($poll['anonymous']));
221
			$this->poll->setFullAnonymous(intval($poll['fullAnonymous']));
222
			$this->poll->setAllowMaybe(intval($poll['allowMaybe']));
223
			$this->poll->setVoteLimit(intval($poll['voteLimit']));
224
			$this->poll->setSettings(json_encode($poll));
225
			$this->poll->setOptions($poll['options']);
226
			$this->poll->setShowResults($poll['showResults']);
227
			$this->poll->setDeleted($poll['deleted']);
228
			$this->poll->setAdminAccess($poll['adminAccess']);
229
230
			if ($this->acl->getPollId() > 0) {
231
				$this->mapper->update($this->poll);
232
				$this->logService->setLog($this->poll->getId(), $logMessageId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $logMessageId does not seem to be defined for all execution paths leading up to this point.
Loading history...
233
			} else {
234
				$this->mapper->insert($this->poll);
235
				$this->logService->setLog($this->poll->getId(), 'addPoll');
236
			}
237
			return new DataResponse($this->poll, Http::STATUS_OK);
238
		}
239
	}
240
}
241