Completed
Pull Request — master (#1038)
by René
06:20
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 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 18
cp 0
rs 10
cc 1
nc 1
nop 8
crap 2

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