Passed
Pull Request — master (#966)
by René
03:56
created

Acl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
ccs 0
cts 18
cp 0
crap 2
rs 10
c 2
b 1
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
25
namespace OCA\Polls\Model;
26
27
use JsonSerializable;
28
use Exception;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
31
use OCP\IUserManager;
32
use OCP\IGroupManager;
33
use OCP\ILogger;
34
use OCP\IUser;
35
use OCA\Polls\Db\Poll;
36
use OCA\Polls\Db\Share;
37
use OCA\Polls\Db\PollMapper;
38
use OCA\Polls\Db\VoteMapper;
39
use OCA\Polls\Db\ShareMapper;
40
41
/**
42
 * Class Acl
43
 *
44
 * @package OCA\Polls\Model\Acl
45
 */
46
class Acl implements JsonSerializable {
47
48
	/** @var int */
49
	private $pollId = 0;
50
51
	/** @var ILogger */
52
	private $logger;
53
54
	/** @var array */
55
	private $shares = [];
56
57
	/** @var string */
58
	private $token = '';
59
60
	/** @var bool */
61
	private $foundByToken = false;
62
63
	/** @var string */
64
	private $userId;
65
66
	/** @var IUserManager */
67
	private $userManager;
68
69
	/** @var IGroupManager */
70
	private $groupManager;
71
72
	/** @var PollMapper */
73
	private $pollMapper;
74
75
	/** @var VoteMapper */
76
	private $voteMapper;
77
78
	/** @var ShareMapper */
79
	private $shareMapper;
80
81
	/** @var Poll */
82
	private $poll;
83
84
85
	/**
86
	 * Acl constructor.
87
	 * @param string $appName
88
	 * @param string $userId
89
	 * @param ILogger $logger
90
	 * @param IUserManager $userManager
91
	 * @param IGroupManager $groupManager
92
	 * @param PollMapper $pollMapper
93
	 * @param VoteMapper $voteMapper
94
	 * @param ShareMapper $shareMapper
95
	 * @param Poll $pollMapper
96
	 *
97
	 */
98
	public function __construct(
99
		$userId,
100
		ILogger $logger,
101
		IUserManager $userManager,
102
		IGroupManager $groupManager,
103
		PollMapper $pollMapper,
104
		VoteMapper $voteMapper,
105
		ShareMapper $shareMapper,
106
		Poll $poll
107
	) {
108
		$this->userId = $userId;
109
		$this->logger = $logger;
110
		$this->userManager = $userManager;
111
		$this->groupManager = $groupManager;
112
		$this->pollMapper = $pollMapper;
113
		$this->voteMapper = $voteMapper;
114
		$this->shareMapper = $shareMapper;
115
		$this->poll = $poll;
116
	}
117
118
119
	/**
120
	 * @NoAdminRequired
121
	 * @return string
122
	 */
123
	 public function getUserId() {
124
		return $this->userId;
125
	}
126
127
	/**
128
	 * @NoAdminRequired
129
	 * @return string
130
	 */
131
	public function getDisplayName() {
132
		if ($this->userManager->get($this->userId) instanceof IUser) {
133
			return $this->userManager->get($this->userId)->getDisplayName();
134
		} else {
135
			return $this->userId;
136
		}
137
	}
138
139
140
	/**
141
	 * @NoAdminRequired
142
	 * @return boolean
143
	 */
144
	public function checkAuthorize($pollId = 0, $token = '') {
145
146
		if ($token) {
147
			$this->setToken($token);
148
		} elseif ($pollId) {
149
			$this->setPollId($pollId);
150
		}
151
152
		return ($this->userId && $this->poll->getId());
153
	}
154
155
	/**
156
	 * @NoAdminRequired
157
	 * @return string
158
	 */
159
	public function setUserId($userId): Acl {
160
		$this->userId = $userId;
161
		return $this;
162
	}
163
164
	/**
165
	 * @NoAdminRequired
166
	 * @return string
167
	 */
168
	public function getLoggedIn() {
169
		return \OC::$server->getUserSession()->isLoggedIn();
170
	}
171
172
	/**
173
	 * @NoAdminRequired
174
	 * @return int
175
	 */
176
	public function getPollId(): int {
177
		return $this->pollId;
178
	}
179
180
	/**
181
	 * @NoAdminRequired
182
	 * @return int
183
	 */
184
	public function setPollId(int $pollId): Acl {
185
		$this->pollId = $pollId;
186
		$this->poll = $this->pollMapper->find($this->pollId);
187
		$this->shares = $this->shareMapper->findByPoll($this->pollId);
188
189
		return $this;
190
	}
191
192
	/**
193
	 * @NoAdminRequired
194
	 * @return bool
195
	 */
196
	public function getIsOwner(): bool {
197
		if (\OC::$server->getUserSession()->isLoggedIn()) {
198
			return ($this->poll->getOwner() === $this->userId);
199
		} else {
200
			return false;
201
		}
202
	}
203
204
	/**
205
	 * @NoAdminRequired
206
	 * @return bool
207
	 */
208
	public function getIsAdmin(): bool {
209
		if (\OC::$server->getUserSession()->isLoggedIn()) {
210
			return ($this->groupManager->isAdmin($this->userId) && $this->poll->getAdminAccess());
211
		} else {
212
			return false;
213
		}
214
	}
215
216
	/**
217
	 * @NoAdminRequired
218
	 * @return bool
219
	 */
220
	public function getAllowView(): bool {
221
		return (
222
			   $this->getIsOwner()
223
			|| ($this->getIsAdmin() && $this->poll->getAdminAccess())
224
			|| !$this->poll->getDeleted() && (
225
				   $this->getUserHasVoted()
226
				|| $this->getGroupShare()
227
				|| $this->getPersonalShare()
228
				|| $this->getPublicShare()
229
				|| ($this->poll->getAccess() !== 'hidden' && !$this->getPublicShare())
230
			)
231
		);
232
	}
233
234
	/**
235
	 * @NoAdminRequired
236
	 * @return bool
237
	 */
238
	public function getGroupShare(): bool {
239
		return count(
240
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
241
				if ($item->getType() === 'group' && $this->groupManager->isInGroup($this->getUserId(), $item->getUserId())) {
242
					return true;
243
				}
244
			})
245
		);
246
	}
247
248
	/**
249
	 * @NoAdminRequired
250
	 * @return bool
251
	 */
252
	public function getUserHasVoted(): bool {
253
		return count(
254
			$this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId())
255
		);
256
	}
257
258
	/**
259
	 * @NoAdminRequired
260
	 * @return bool
261
	 */
262
	public function getPersonalShare(): bool {
263
264
		return count(
265
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
266
				if (($item->getType() === 'user' || $item->getType() === 'external' || $item->getType() === 'email' || $item->getType() === 'contact') && $item->getUserId() === $this->getUserId()) {
267
					return true;
268
				}
269
			})
270
		);
271
	}
272
273
	/**
274
	 * @NoAdminRequired
275
	 * @return bool
276
	 */
277
	public function getPublicShare(): bool {
278
279
		return count(
280
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
281
				if ($item->getType() === 'public' && $item->getToken() === $this->getToken()) {
282
					return true;
283
				}
284
			})
285
		);
286
	}
287
288
	/**
289
	 * @NoAdminRequired
290
	 * @return bool
291
	 */
292
	public function getExpired(): bool {
293
		return (
294
			   $this->poll->getExpire() > 0
295
			&& $this->poll->getExpire() < time()
296
		);
297
	}
298
299
	/**
300
	 * @NoAdminRequired
301
	 * @return bool
302
	 */
303
	public function getAllowVote(): bool {
304
		if (
305
			   ($this->getAllowView() || $this->getFoundByToken())
306
			&& !$this->getExpired()
307
			&& !$this->poll->getDeleted()
308
			&& $this->userId
309
310
		) {
311
			return true;
312
		} else {
313
			return false;
314
		}
315
	}
316
317
	/**
318
	 * @NoAdminRequired
319
	 * @return bool
320
	 */
321
	public function getAllowComment(): bool {
322
		return !$this->poll->getDeleted() && boolval($this->userId);
323
	}
324
325
	/**
326
	 * @NoAdminRequired
327
	 * @return bool
328
	 */
329
	public function getAllowEdit(): bool {
330
		return ($this->getIsOwner() || $this->getIsAdmin());
331
	}
332
333
	/**
334
	 * @NoAdminRequired
335
	 * @return bool
336
	 */
337
	public function getAllowSeeResults(): bool {
338
		if ($this->poll->getShowResults() === 'always' || $this->getIsOwner()) {
339
			return true;
340
		} elseif ($this->poll->getShowResults() === 'never') {
341
			return false;
342
		} elseif ($this->poll->getShowResults() === 'expired') {
343
			return $this->getExpired();
344
		} else {
345
			return false;
346
		}
347
	}
348
349
	/**
350
	 * @NoAdminRequired
351
	 * @return bool
352
	 */
353
	public function getAllowSeeUsernames(): bool {
354
		return !($this->poll->getAnonymous() && !$this->getIsOwner()); ;
355
	}
356
357
	/**
358
	 * @NoAdminRequired
359
	 * @return bool
360
	 */
361
	public function getAllowSeeAllVotes(): bool {
362
		// TODO: preparation for polls without displaying other votes
363
		if ($this->pollId) {
364
			return true;
365
		} else {
366
			return false;
367
		}
368
	}
369
370
	/**
371
	 * @NoAdminRequired
372
	 * @return bool
373
	 */
374
	public function getFoundByToken(): bool {
375
		return $this->foundByToken;
376
	}
377
378
	/**
379
	 * @NoAdminRequired
380
	 * @return string
381
	 */
382
	public function getToken(): string {
383
		return $this->token;
384
	}
385
386
	/**
387
	 * @NoAdminRequired
388
	 * @return string
389
	 */
390
	public function setToken(string $token): Acl {
391
		$this->logger->debug('Share PollId' . $token);
392
		try {
393
394
			$this->token = $token;
395
			$share = $this->shareMapper->findByToken($token);
396
			$this->foundByToken = true;
397
			$this->setPollId($share->getPollId());
398
			$this->logger->debug('Share PollId' . $share->getPollId());
399
400
			if (($share->getType() === 'group' || $share->getType() === 'user') && !\OC::$server->getUserSession()->isLoggedIn()) {
401
				// User must be logged in for shareType user and group
402
				$this->setPollId(0);
403
				$this->setUserId(null);
404
				$this->token = '';
405
				$this->foundByToken = false;
406
			} else if (($share->getType() === 'group' || $share->getType() === 'public') && \OC::$server->getUserSession()->isLoggedIn()) {
407
				// Use user name of authorized user shareType public and group if user is logged in
408
				$this->setUserId($this->userId);
409
			} else {
410
				$this->setUserId($share->getUserId());
411
			}
412
413
414
		} catch (DoesNotExistException $e) {
415
			$this->setPollId(0);
416
			$this->setUserId(null);
417
			$this->token = '';
418
			$this->foundByToken = false;
419
		}
420
		return $this;
421
	}
422
423
	/**
424
	 * @return array
425
	 */
426
	public function jsonSerialize(): array {
427
		return	[
428
			'userId'            => $this->getUserId(),
429
			'displayName'       => $this->getDisplayName(),
430
			'loggedIn'			=> $this->getLoggedIn(),
431
			'pollId'            => $this->getPollId(),
432
			'token'             => $this->getToken(),
433
			'isOwner'           => $this->getIsOwner(),
434
			'isAdmin'           => $this->getIsAdmin(),
435
			'allowView'         => $this->getAllowView(),
436
			'allowVote'         => $this->getAllowVote(),
437
			'allowComment'      => $this->getAllowComment(),
438
			'allowEdit'         => $this->getAllowEdit(),
439
			'allowSeeResults'   => $this->getAllowSeeResults(),
440
			'allowSeeUsernames' => $this->getAllowSeeUsernames(),
441
			'allowSeeAllVotes'  => $this->getAllowSeeAllVotes(),
442
			'userHasVoted'		=> $this->getUserHasVoted(),
443
			'groupShare'        => $this->getGroupShare(),
444
			'personalShare'     => $this->getPersonalShare(),
445
			'publicShare'     	=> $this->getPublicShare(),
446
			'foundByToken'      => $this->getFoundByToken()
447
		];
448
	}
449
}
450