Completed
Push — master ( 798959...da39f8 )
by René
16s queued 10s
created

Acl::getAllowSubscribe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
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 $appName
79
	 * @param string $userId
80
	 * @param IUserManager $userManager
81
	 * @param IGroupManager $groupManager
82
	 * @param PollMapper $pollMapper
83
	 * @param VoteMapper $voteMapper
84
	 * @param ShareMapper $shareMapper
85
	 * @param Poll $poll
86
	 * @param Share $share
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
		Share $share
98
	) {
99
		$this->userId = $userId;
100
		$this->userManager = $userManager;
101
		$this->groupManager = $groupManager;
102
		$this->pollMapper = $pollMapper;
103
		$this->voteMapper = $voteMapper;
104
		$this->shareMapper = $shareMapper;
105
		$this->poll = $poll;
106
		$this->share = $share;
107
	}
108
109
	/**
110
	 * @NoAdminRequired
111
	 * @return bool
112
	 */
113
	public function set($pollId = 0, $token = ''): Acl {
114
		if ($token) {
115
			\OC::$server->getLogger()->debug('Share token: ' . $token);
116
117
			$this->token = $token;
118
			$this->pollId = 0;
119
			$this->userId = null;
120
			$this->share = $this->shareMapper->findByToken($token);
121
122
			if (\OC::$server->getUserSession()->isLoggedIn()) {
123
				if ($this->share->getType() !== 'group' && $this->share->getType() !== 'public') {
124
					throw new NotAuthorizedException;
125
				}
126
127
				$this->userId = \OC::$server->getUserSession()->getUser()->getUID();
128
			} else {
129
				if ($this->share->getType() === 'group' || $this->share->getType() === 'user') {
130
					throw new NotAuthorizedException;
131
				}
132
133
				$this->userId = $this->share->getUserId();
134
			}
135
136
			$this->pollId = $this->share->getPollId();
137
		} elseif ($pollId) {
138
			$this->userId = \OC::$server->getUserSession()->getUser()->getUID();
139
			$this->pollId = $pollId;
140
			$this->share = null;
141
		}
142
143
		$this->poll = $this->pollMapper->find($this->pollId);
144
145
		return $this;
146
	}
147
148
	/**
149
	 * @NoAdminRequired
150
	 * @return string
151
	 */
152
	public function getUserId() {
153
		return $this->userId;
154
	}
155
156
	/**
157
	 * @NoAdminRequired
158
	 * @return string
159
	 */
160
	public function getDisplayName() {
161
		if ($this->userManager->get($this->userId) instanceof IUser) {
162
			return $this->userManager->get($this->userId)->getDisplayName();
163
		} else {
164
			return $this->userId;
165
		}
166
	}
167
168
	/**
169
	 * @NoAdminRequired
170
	 * @return string
171
	 */
172
	public function getIsExternalUser() {
173
		return !($this->userManager->get($this->userId) instanceof IUser);
174
	}
175
176
	/**
177
	 * @NoAdminRequired
178
	 * @return string
179
	 */
180
	public function getLoggedIn() {
181
		return \OC::$server->getUserSession()->isLoggedIn();
182
	}
183
184
	/**
185
	 * @NoAdminRequired
186
	 * @return int
187
	 */
188
	public function getPollId(): int {
189
		return $this->pollId;
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
		return count(
264
			array_filter($this->shareMapper->findByPoll($this->getPollId()), function ($item) {
265
				if (($item->getType() === 'user' || $item->getType() === 'external' || $item->getType() === 'email' || $item->getType() === 'contact') && $item->getUserId() === $this->getUserId()) {
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() === '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
			'externalUser'		=> $this->getIsExternalUser(),
377
			'pollId'            => $this->getPollId(),
378
			'token'             => $this->getToken(),
379
			'isOwner'           => $this->getIsOwner(),
380
			'isAdmin'           => $this->getIsAdmin(),
381
			'allowView'         => $this->getAllowView(),
382
			'allowVote'         => $this->getAllowVote(),
383
			'allowComment'      => $this->getAllowComment(),
384
			'allowEdit'         => $this->getAllowEdit(),
385
			'allowSeeResults'   => $this->getAllowSeeResults(),
386
			'allowSeeUsernames' => $this->getAllowSeeUsernames(),
387
			'allowSubscribe'    => $this->getAllowSubscribe(),
388
			'userHasVoted'		=> $this->getUserHasVoted(),
389
			'groupShare'        => $this->getGroupShare(),
390
			'personalShare'     => $this->getPersonalShare(),
391
			'publicShare'     	=> $this->getPublicShare()
392
		];
393
	}
394
}
395