Completed
Pull Request — master (#794)
by René
04:12
created

Acl::getPersonalShare()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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