Completed
Push — master ( 391ad0...cb32b5 )
by René
05:14 queued 59s
created

Acl::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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