Completed
Pull Request — master (#1038)
by René
04:22
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
use OCA\Polls\Exceptions\NotAuthorizedException;
31
32
use OCP\IUserManager;
33
use OCP\IGroupManager;
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 array */
52
	private $shares = [];
0 ignored issues
show
introduced by
The private property $shares is not used, and could be removed.
Loading history...
53
54
	/** @var string */
55
	private $token = '';
56
57
	/** @var string */
58
	private $userId;
59
60
	/** @var IUserManager */
61
	private $userManager;
62
63
	/** @var IGroupManager */
64
	private $groupManager;
65
66
	/** @var PollMapper */
67
	private $pollMapper;
68
69
	/** @var VoteMapper */
70
	private $voteMapper;
71
72
	/** @var ShareMapper */
73
	private $shareMapper;
74
75
	/** @var Poll */
76
	private $poll;
77
78
	/** @var Share */
79
	private $share;
80
81
	/**
82
	 * Acl constructor.
83
	 * @param string $appName
84
	 * @param string $userId
85
	 * @param IUserManager $userManager
86
	 * @param IGroupManager $groupManager
87
	 * @param PollMapper $pollMapper
88
	 * @param VoteMapper $voteMapper
89
	 * @param ShareMapper $shareMapper
90
	 * @param Poll $poll
91
	 * @param Share $share
92
	 *
93
	 */
94
	public function __construct(
95
		$userId,
96
		IUserManager $userManager,
97
		IGroupManager $groupManager,
98
		PollMapper $pollMapper,
99
		VoteMapper $voteMapper,
100
		ShareMapper $shareMapper,
101
		Poll $poll,
102
		Share $share
103
	) {
104
		$this->userId = $userId;
105
		$this->userManager = $userManager;
106
		$this->groupManager = $groupManager;
107
		$this->pollMapper = $pollMapper;
108
		$this->voteMapper = $voteMapper;
109
		$this->shareMapper = $shareMapper;
110
		$this->poll = $poll;
111
		$this->share = $share;
112
	}
113
114
	/**
115
	 * @NoAdminRequired
116
	 * @return bool
117
	 */
118
	public function set($pollId = 0, $token = ''): Acl {
119
120
		if ($token) {
121
			\OC::$server->getLogger()->debug('Share token: ' . $token);
122
123
			$this->token = $token;
124
			$this->pollId = 0;
125
			$this->userId = null;
126
			$this->share = $this->shareMapper->findByToken($token);
127
128
			if (\OC::$server->getUserSession()->isLoggedIn()) {
129
				if ($this->share->getType() !== 'group' && $this->share->getType() !== 'public') {
130
					throw new NotAuthorizedException;
131
				}
132
133
				$this->userId = \OC::$server->getUserSession()->getUser()->getUID();
134
			} else {
135
				if ($this->share->getType() === 'group' || $this->share->getType() === 'user') {
136
					throw new NotAuthorizedException;
137
				}
138
139
				$this->userId = $this->share->getUserId();
140
			}
141
142
			$this->pollId = $this->share->getPollId();
143
		} elseif ($pollId) {
144
			$this->user = \OC::$server->getUserSession()->getUser()->getUID();
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
145
			$this->pollId = $pollId;
146
			$this->share = null;
147
		}
148
149
		$this->poll = $this->pollMapper->find($this->pollId);
150
151
		return $this;
152
	}
153
154
	/**
155
	 * @NoAdminRequired
156
	 * @return string
157
	 */
158
	 public function getUserId() {
159
		return $this->userId;
160
	}
161
162
	/**
163
	 * @NoAdminRequired
164
	 * @return string
165
	 */
166
	public function getDisplayName() {
167
		if ($this->userManager->get($this->userId) instanceof IUser) {
168
			return $this->userManager->get($this->userId)->getDisplayName();
169
		} else {
170
			return $this->userId;
171
		}
172
	}
173
174
	/**
175
	 * @NoAdminRequired
176
	 * @return string
177
	 */
178
	public function getIsExternalUser() {
179
		return !($this->userManager->get($this->userId) instanceof IUser);
180
	}
181
182
	/**
183
	 * @NoAdminRequired
184
	 * @return string
185
	 */
186
	public function getLoggedIn() {
187
		return \OC::$server->getUserSession()->isLoggedIn();
188
	}
189
190
	/**
191
	 * @NoAdminRequired
192
	 * @return int
193
	 */
194
	public function getPollId(): int {
195
		return $this->pollId;
196
	}
197
198
	/**
199
	 * @NoAdminRequired
200
	 * @return bool
201
	 */
202
	public function getIsOwner(): bool {
203
		if (\OC::$server->getUserSession()->isLoggedIn()) {
204
			return ($this->poll->getOwner() === $this->userId);
205
		} else {
206
			return false;
207
		}
208
	}
209
210
	/**
211
	 * @NoAdminRequired
212
	 * @return bool
213
	 */
214
	public function getIsAdmin(): bool {
215
		if (\OC::$server->getUserSession()->isLoggedIn()) {
216
			return ($this->groupManager->isAdmin($this->userId) && $this->poll->getAdminAccess());
217
		} else {
218
			return false;
219
		}
220
	}
221
222
	/**
223
	 * @NoAdminRequired
224
	 * @return bool
225
	 */
226
	public function getAllowView(): bool {
227
		return (
228
			   $this->getIsOwner()
229
			|| ($this->getIsAdmin() && $this->poll->getAdminAccess())
230
			|| !$this->poll->getDeleted() && (
231
				   $this->getUserHasVoted()
232
				|| $this->getGroupShare()
233
				|| $this->getPersonalShare()
234
				|| $this->getPublicShare()
235
				|| ($this->poll->getAccess() !== 'hidden' && !$this->getPublicShare())
236
			)
237
		);
238
	}
239
240
	/**
241
	 * @NoAdminRequired
242
	 * @return bool
243
	 */
244
	public function getGroupShare(): bool {
245
		return count(
246
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
247
				if ($item->getType() === 'group' && $this->groupManager->isInGroup($this->getUserId(), $item->getUserId())) {
248
					return true;
249
				}
250
			})
251
		);
252
	}
253
254
	/**
255
	 * @NoAdminRequired
256
	 * @return bool
257
	 */
258
	public function getUserHasVoted(): bool {
259
		return count(
260
			$this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId())
261
		);
262
	}
263
264
	/**
265
	 * @NoAdminRequired
266
	 * @return bool
267
	 */
268
	public function getPersonalShare(): bool {
269
270
		return count(
271
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
272
				if (($item->getType() === 'user' || $item->getType() === 'external' || $item->getType() === 'email' || $item->getType() === 'contact') && $item->getUserId() === $this->getUserId()) {
273
					return true;
274
				}
275
			})
276
		);
277
	}
278
279
	/**
280
	 * @NoAdminRequired
281
	 * @return bool
282
	 */
283
	public function getPublicShare(): bool {
284
285
		return count(
286
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function($item) {
287
				if ($item->getType() === 'public' && $item->getToken() === $this->getToken()) {
288
					return true;
289
				}
290
			})
291
		);
292
	}
293
294
	/**
295
	 * @NoAdminRequired
296
	 * @return bool
297
	 */
298
	public function getExpired(): bool {
299
		return (
300
			   $this->poll->getExpire() > 0
301
			&& $this->poll->getExpire() < time()
302
		);
303
	}
304
305
	/**
306
	 * @NoAdminRequired
307
	 * @return bool
308
	 */
309
	public function getAllowVote(): bool {
310
		return ($this->getAllowView() || $this->getToken())
311
			&& !$this->getExpired()
312
			&& !$this->poll->getDeleted()
313
			&& $this->userId;
314
	}
315
316
	/**
317
	 * @NoAdminRequired
318
	 * @return bool
319
	 */
320
	public function getAllowSubscribe(): bool {
321
		return ($this->hasEmail())
322
			&& !$this->poll->getDeleted()
323
			&& $this->getAllowView();
324
	}
325
326
	/**
327
	 * @NoAdminRequired
328
	 * @return bool
329
	 */
330
	public function getAllowComment(): bool {
331
		return !$this->poll->getDeleted() && boolval($this->userId);
332
	}
333
334
	/**
335
	 * @NoAdminRequired
336
	 * @return bool
337
	 */
338
	public function getAllowEdit(): bool {
339
		return ($this->getIsOwner() || $this->getIsAdmin());
340
	}
341
342
	/**
343
	 * @NoAdminRequired
344
	 * @return bool
345
	 */
346
	public function getAllowSeeResults(): bool {
347
		 return $this->poll->getShowResults() === 'always'
348
			|| ($this->poll->getShowResults() === 'expired' && $this->getExpired())
349
			|| $this->getIsOwner();
350
	}
351
352
	/**
353
	 * @NoAdminRequired
354
	 * @return bool
355
	 */
356
	public function getAllowSeeUsernames(): bool {
357
		return !$this->poll->getAnonymous() || $this->getIsOwner();
358
	}
359
360
	/**
361
	 * @NoAdminRequired
362
	 * @return string
363
	 */
364
	public function getToken(): string {
365
		return $this->token;
366
	}
367
368
	private function hasEmail():bool {
369
		if ($this->share) {
370
			return strlen($this->share->getUserEmail()) > 0;
371
		} else {
372
			return \OC::$server->getUserSession()->isLoggedIn();
373
		}
374
	}
375
376
	/**
377
	 * @return array
378
	 */
379
	public function jsonSerialize(): array {
380
		return	[
381
			'userId'            => $this->getUserId(),
382
			'displayName'       => $this->getDisplayName(),
383
			'loggedIn'			=> $this->getLoggedIn(),
384
			'externalUser'		=> $this->getIsExternalUser(),
385
			'pollId'            => $this->getPollId(),
386
			'token'             => $this->getToken(),
387
			'isOwner'           => $this->getIsOwner(),
388
			'isAdmin'           => $this->getIsAdmin(),
389
			'allowView'         => $this->getAllowView(),
390
			'allowVote'         => $this->getAllowVote(),
391
			'allowComment'      => $this->getAllowComment(),
392
			'allowEdit'         => $this->getAllowEdit(),
393
			'allowSeeResults'   => $this->getAllowSeeResults(),
394
			'allowSeeUsernames' => $this->getAllowSeeUsernames(),
395
			'allowSubscribe'    => $this->getAllowSubscribe(),
396
			'userHasVoted'		=> $this->getUserHasVoted(),
397
			'groupShare'        => $this->getGroupShare(),
398
			'personalShare'     => $this->getPersonalShare(),
399
			'publicShare'     	=> $this->getPublicShare()
400
		];
401
	}
402
}
403