Completed
Pull Request — master (#1038)
by René
06:51 queued 55s
created

Acl::getIsExternalUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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