Passed
Pull Request — master (#896)
by René
03:54
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 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 8
c 3
b 1
f 0
nc 1
nop 8
dl 0
loc 18
ccs 0
cts 18
cp 0
crap 2
rs 10

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
		// $this->userManager = \OC::$server->getUserManager();
133
134
		if ($this->userManager->get($this->userId) instanceof IUser) {
135
			return $this->userManager->get($this->userId)->getDisplayName();
136
		} else {
137
			return $this->userId;
138
		}
139
	}
140
141
	/**
142
	 * @NoAdminRequired
143
	 * @return string
144
	 */
145
	public function setUserId($userId): Acl {
146
		$this->userId = $userId;
147
		return $this;
148
	}
149
150
	/**
151
	 * @NoAdminRequired
152
	 * @return string
153
	 */
154
	public function getLoggedIn() {
155
		return \OC::$server->getUserSession()->isLoggedIn();
156
	}
157
158
	/**
159
	 * @NoAdminRequired
160
	 * @return int
161
	 */
162
	public function getPollId(): int {
163
		return $this->pollId;
164
	}
165
166
	/**
167
	 * @NoAdminRequired
168
	 * @return int
169
	 */
170
	public function setPollId(int $pollId): Acl {
171
		$this->pollId = $pollId;
172
		$this->poll = $this->pollMapper->find($this->pollId);
173
		$this->shares = $this->shareMapper->findByPoll($this->pollId);
174
175
		return $this;
176
	}
177
178
	/**
179
	 * @NoAdminRequired
180
	 * @return bool
181
	 */
182
	public function getIsOwner(): bool {
183
		if (\OC::$server->getUserSession()->isLoggedIn()) {
184
			return ($this->poll->getOwner() === $this->userId);
185
		} else {
186
			return false;
187
		}
188
	}
189
190
	/**
191
	 * @NoAdminRequired
192
	 * @return bool
193
	 */
194
	public function getIsAdmin(): bool {
195
		if (\OC::$server->getUserSession()->isLoggedIn()) {
196
			return ($this->groupManager->isAdmin($this->userId) && $this->poll->getAdminAccess());
197
		} else {
198
			return false;
199
		}
200
	}
201
202
	/**
203
	 * @NoAdminRequired
204
	 * @return bool
205
	 */
206
	public function getAllowView(): bool {
207
		return (
208
			   $this->getIsOwner()
209
			|| ($this->getIsAdmin() && $this->poll->getAdminAccess())
210
			|| !$this->poll->getDeleted() && (
211
				   $this->getUserHasVoted()
212
				|| $this->getGroupShare()
213
				|| $this->getPersonalShare()
214
				|| $this->getPublicShare()
215
				|| ($this->poll->getAccess() !== 'hidden' && !$this->getPublicShare())
216
			)
217
		);
218
	}
219
220
	/**
221
	 * @NoAdminRequired
222
	 * @return bool
223
	 */
224
	public function getGroupShare(): bool {
225
		return count(
226
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
227
				if ($item->getType() === 'group' && $this->groupManager->isInGroup($this->getUserId(), $item->getUserId())) {
228
					return true;
229
				}
230
			})
231
		);
232
	}
233
234
	/**
235
	 * @NoAdminRequired
236
	 * @return bool
237
	 */
238
	public function getUserHasVoted(): bool {
239
		return count(
240
			$this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId())
241
		);
242
	}
243
244
	/**
245
	 * @NoAdminRequired
246
	 * @return bool
247
	 */
248
	public function getPersonalShare(): bool {
249
250
		return count(
251
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
252
				if (($item->getType() === 'user' || $item->getType() === 'external' || $item->getType() === 'email') && $item->getUserId() === $this->getUserId()) {
253
					return true;
254
				}
255
			})
256
		);
257
	}
258
259
	/**
260
	 * @NoAdminRequired
261
	 * @return bool
262
	 */
263
	public function getPublicShare(): bool {
264
265
		return count(
266
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
267
				if ($item->getType() === 'public' && $item->getToken() === $this->getToken()) {
268
					return true;
269
				}
270
			})
271
		);
272
	}
273
274
	/**
275
	 * @NoAdminRequired
276
	 * @return bool
277
	 */
278
	public function getExpired(): bool {
279
		return (
280
			   $this->poll->getExpire() > 0
281
			&& $this->poll->getExpire() < time()
282
		);
283
	}
284
285
	/**
286
	 * @NoAdminRequired
287
	 * @return bool
288
	 */
289
	public function getAllowVote(): bool {
290
		if (
291
			   ($this->getAllowView() || $this->getFoundByToken())
292
			&& !$this->getExpired()
293
			&& !$this->poll->getDeleted()
294
			&& $this->userId
295
296
		) {
297
			return true;
298
		} else {
299
			return false;
300
		}
301
	}
302
303
	/**
304
	 * @NoAdminRequired
305
	 * @return bool
306
	 */
307
	public function getAllowComment(): bool {
308
		return !$this->poll->getDeleted() && boolval($this->userId);
309
	}
310
311
	/**
312
	 * @NoAdminRequired
313
	 * @return bool
314
	 */
315
	public function getAllowEdit(): bool {
316
		return ($this->getIsOwner() || $this->getIsAdmin());
317
	}
318
319
	/**
320
	 * @NoAdminRequired
321
	 * @return bool
322
	 */
323
	public function getAllowSeeResults(): bool {
324
		if ($this->poll->getShowResults() === 'always' || $this->getIsOwner()) {
325
		// if ($this->poll->getShowResults() === 'always') {
326
			return true;
327
		} elseif ($this->poll->getShowResults() === 'never') {
328
			return false;
329
		} elseif ($this->poll->getShowResults() === 'expired') {
330
			return $this->getExpired();
331
		} else {
332
			return false;
333
		}
334
	}
335
336
	/**
337
	 * @NoAdminRequired
338
	 * @return bool
339
	 */
340
	public function getAllowSeeUsernames(): bool {
341
		return !($this->poll->getAnonymous() && !$this->getIsOwner()); ;
342
	}
343
344
	/**
345
	 * @NoAdminRequired
346
	 * @return bool
347
	 */
348
	public function getAllowSeeAllVotes(): bool {
349
		// TODO: preparation for polls without displaying other votes
350
		if ($this->pollId) {
351
			return true;
352
		} else {
353
			return false;
354
		}
355
	}
356
357
	/**
358
	 * @NoAdminRequired
359
	 * @return bool
360
	 */
361
	public function getFoundByToken(): bool {
362
		return $this->foundByToken;
363
	}
364
365
	/**
366
	 * @NoAdminRequired
367
	 * @return string
368
	 */
369
	public function getToken(): string {
370
		return $this->token;
371
	}
372
373
	/**
374
	 * @NoAdminRequired
375
	 * @return string
376
	 */
377
	public function setToken(string $token): Acl {
378
		try {
379
380
			$this->token = $token;
381
			$share = $this->shareMapper->findByToken($token);
382
			$this->foundByToken = true;
383
			$this->setPollId($share->getPollId());
384
385
			if (($share->getType() === 'group' || $share->getType() === 'user') && !\OC::$server->getUserSession()->isLoggedIn()) {
386
				// User must be logged in for shareType user and group
387
				$this->setPollId(0);
388
				$this->setUserId(null);
389
				$this->token = '';
390
				$this->foundByToken = false;
391
			} else if (($share->getType() === 'group' || $share->getType() === 'public') && \OC::$server->getUserSession()->isLoggedIn()) {
392
				// Use user name of authorized user shareType public and group if user is logged in
393
				$this->setUserId($this->userId);
394
			} else {
395
				$this->setUserId($share->getUserId());
396
			}
397
398
399
		} catch (DoesNotExistException $e) {
400
			$this->setPollId(0);
401
			$this->setUserId(null);
402
			$this->token = '';
403
			$this->foundByToken = false;
404
		}
405
		return $this;
406
	}
407
408
	/**
409
	 * @return array
410
	 */
411
	public function jsonSerialize(): array {
412
		return	[
413
			'userId'            => $this->getUserId(),
414
			'displayName'       => $this->getDisplayName(),
415
			'loggedIn'			=> $this->getLoggedIn(),
416
			'pollId'            => $this->getPollId(),
417
			'token'             => $this->getToken(),
418
			'isOwner'           => $this->getIsOwner(),
419
			'isAdmin'           => $this->getIsAdmin(),
420
			'allowView'         => $this->getAllowView(),
421
			'allowVote'         => $this->getAllowVote(),
422
			'allowComment'      => $this->getAllowComment(),
423
			'allowEdit'         => $this->getAllowEdit(),
424
			'allowSeeResults'   => $this->getAllowSeeResults(),
425
			'allowSeeUsernames' => $this->getAllowSeeUsernames(),
426
			'allowSeeAllVotes'  => $this->getAllowSeeAllVotes(),
427
			'userHasVoted'		=> $this->getUserHasVoted(),
428
			'groupShare'        => $this->getGroupShare(),
429
			'personalShare'     => $this->getPersonalShare(),
430
			'publicShare'     	=> $this->getPublicShare(),
431
			'foundByToken'      => $this->getFoundByToken()
432
		];
433
	}
434
}
435