Passed
Pull Request — master (#966)
by René
04:09
created

Acl::setPollId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
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
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 && !\OC::$server->getUserSession()->isLoggedIn()) {
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
		try {
392
393
			$this->token = $token;
394
			$share = $this->shareMapper->findByToken($token);
395
			$this->foundByToken = true;
396
			$this->setPollId($share->getPollId());
397
398
			if (($share->getType() === 'group' || $share->getType() === 'user') && !\OC::$server->getUserSession()->isLoggedIn()) {
399
				// User must be logged in for shareType user and group
400
				$this->setPollId(0);
401
				$this->setUserId(null);
402
				$this->token = '';
403
				$this->foundByToken = false;
404
			} else if (($share->getType() === 'group' || $share->getType() === 'public') && \OC::$server->getUserSession()->isLoggedIn()) {
405
				// Use user name of authorized user shareType public and group if user is logged in
406
				$this->setUserId($this->userId);
407
			} else {
408
				$this->setUserId($share->getUserId());
409
			}
410
411
412
		} catch (DoesNotExistException $e) {
413
			$this->setPollId(0);
414
			$this->setUserId(null);
415
			$this->token = '';
416
			$this->foundByToken = false;
417
		}
418
		return $this;
419
	}
420
421
	/**
422
	 * @return array
423
	 */
424
	public function jsonSerialize(): array {
425
		return	[
426
			'userId'            => $this->getUserId(),
427
			'displayName'       => $this->getDisplayName(),
428
			'loggedIn'			=> $this->getLoggedIn(),
429
			'pollId'            => $this->getPollId(),
430
			'token'             => $this->getToken(),
431
			'isOwner'           => $this->getIsOwner(),
432
			'isAdmin'           => $this->getIsAdmin(),
433
			'allowView'         => $this->getAllowView(),
434
			'allowVote'         => $this->getAllowVote(),
435
			'allowComment'      => $this->getAllowComment(),
436
			'allowEdit'         => $this->getAllowEdit(),
437
			'allowSeeResults'   => $this->getAllowSeeResults(),
438
			'allowSeeUsernames' => $this->getAllowSeeUsernames(),
439
			'allowSeeAllVotes'  => $this->getAllowSeeAllVotes(),
440
			'userHasVoted'		=> $this->getUserHasVoted(),
441
			'groupShare'        => $this->getGroupShare(),
442
			'personalShare'     => $this->getPersonalShare(),
443
			'publicShare'     	=> $this->getPublicShare(),
444
			'foundByToken'      => $this->getFoundByToken()
445
		];
446
	}
447
}
448