Passed
Pull Request — master (#1193)
by René
04:17
created

Acl::getPersonalShare()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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