-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathPermissionService.php
More file actions
380 lines (345 loc) · 12.2 KB
/
PermissionService.php
File metadata and controls
380 lines (345 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Service;
use OCA\Circles\Model\Member;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\FederatedUser;
use OCA\Deck\Db\IPermissionMapper;
use OCA\Deck\Db\User;
use OCA\Deck\NoPermissionException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\Cache\CappedMemoryCache;
use OCP\Federation\ICloudIdManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;
class PermissionService {
/** @var array<string, array<string, User|FederatedUser>> */
private $users = [];
// accessToken to check permission for federated shares
private ?string $accessToken = null;
private CappedMemoryCache $boardCache;
/** @var CappedMemoryCache<array<Acl::PERMISSION_*, bool>> */
private CappedMemoryCache $permissionCache;
public function __construct(
private LoggerInterface $logger,
private CirclesService $circlesService,
private AclMapper $aclMapper,
private BoardMapper $boardMapper,
private IUserManager $userManager,
private ConfigService $configService,
private IGroupManager $groupManager,
private IManager $shareManager,
private IConfig $config,
private ICloudIdManager $cloudIdManager,
private ?string $userId,
) {
$this->boardCache = new CappedMemoryCache();
$this->permissionCache = new CappedMemoryCache();
}
/**
* Set AccessToken for requests from federation shares
*/
public function setAccessToken(string $token) {
$this->accessToken = $token;
}
/**
* Get current user permissions for a board by id
*
* @return array<Acl::PERMISSION_*, bool>
*/
public function getPermissions(int $boardId, ?string $userId = null, bool $allowDeleted = false): array {
if ($userId === null) {
$userId = $this->userId;
}
// check chache if not a federated request
if ($this->accessToken === null) {
$cacheKey = $boardId . '-' . $userId;
if ($cached = $this->permissionCache->get($cacheKey)) {
/** @var array<Acl::PERMISSION_*, bool> $cached */
return $cached;
}
}
$board = $this->getBoard($boardId, $allowDeleted);
if ($this->accessToken !== null) {
$acls = $board->getDeletedAt() === 0 ? $this->aclMapper->findAll($boardId) : [];
$permissions = [
Acl::PERMISSION_READ => $this->externalUserCan($acls, Acl::PERMISSION_READ, $this->accessToken),
Acl::PERMISSION_EDIT => $this->externalUserCan($acls, Acl::PERMISSION_EDIT, $this->accessToken),
Acl::PERMISSION_MANAGE => $this->externalUserCan($acls, Acl::PERMISSION_MANAGE, $this->accessToken),
Acl::PERMISSION_SHARE => $this->externalUserCan($acls, Acl::PERMISSION_SHARE, $this->accessToken)
];
return $permissions;
}
try {
$owner = $this->userIsBoardOwner($boardId, $userId, $allowDeleted);
$acls = $board->getDeletedAt() === 0 ? $this->aclMapper->findAll($boardId) : [];
} catch (MultipleObjectsReturnedException|DoesNotExistException $e) {
$owner = false;
$acls = [];
}
$permissions = [
Acl::PERMISSION_READ => $owner || $this->userCan($acls, Acl::PERMISSION_READ, $userId),
Acl::PERMISSION_EDIT => $owner || $this->userCan($acls, Acl::PERMISSION_EDIT, $userId),
Acl::PERMISSION_MANAGE => $owner || $this->userCan($acls, Acl::PERMISSION_MANAGE, $userId),
Acl::PERMISSION_SHARE => ($owner || $this->userCan($acls, Acl::PERMISSION_SHARE, $userId))
&& (!$this->shareManager->sharingDisabledForUser($userId))
];
$this->permissionCache->set($cacheKey, $permissions);
return $permissions;
}
/**
* Get current user permissions for a board
*
* @param Board $board
* @return array|bool
* @internal param $boardId
*/
public function matchPermissions(Board $board) {
$owner = $this->userIsBoardOwner($board->getId());
$acls = $board->getAcl() ?? [];
if ($this->accessToken !== null) {
return [
Acl::PERMISSION_READ => $this->externalUserCan($acls, Acl::PERMISSION_READ, $this->accessToken),
Acl::PERMISSION_EDIT => $this->externalUserCan($acls, Acl::PERMISSION_EDIT, $this->accessToken),
Acl::PERMISSION_MANAGE => $this->externalUserCan($acls, Acl::PERMISSION_MANAGE, $this->accessToken),
Acl::PERMISSION_SHARE => $this->externalUserCan($acls, Acl::PERMISSION_SHARE, $this->accessToken)
];
}
return [
Acl::PERMISSION_READ => $owner || $this->userCan($acls, Acl::PERMISSION_READ),
Acl::PERMISSION_EDIT => $owner || $this->userCan($acls, Acl::PERMISSION_EDIT),
Acl::PERMISSION_MANAGE => $owner || $this->userCan($acls, Acl::PERMISSION_MANAGE),
Acl::PERMISSION_SHARE => ($owner || $this->userCan($acls, Acl::PERMISSION_SHARE))
&& (!$this->shareManager->sharingDisabledForUser($this->userId))
];
}
/**
* check permissions for replacing dark magic middleware
*
* @throws NoPermissionException
*/
public function checkPermission(?IPermissionMapper $mapper, $id, int $permission, $userId = null, bool $allowDeletedCard = false, bool $allowDeletedBoard = false): bool {
$boardId = (int)$id;
if ($mapper instanceof IPermissionMapper && !($mapper instanceof BoardMapper)) {
$boardId = $mapper->findBoardId($id);
}
// (int)null === 0 so we have to check if any of these are null
if ($boardId === null || $id === null) {
// Throw NoPermission to not leak information about existing entries
throw new NoPermissionException('Permission denied');
}
$permissions = $this->getPermissions($boardId, $userId, $allowDeletedBoard);
if ($permissions[$permission] === true) {
if (!$allowDeletedCard && $mapper instanceof CardMapper) {
try {
$card = $mapper->find((int)$id, false);
} catch (DoesNotExistException $e) {
throw new NoPermissionException('Permission denied');
}
if ($card->getDeletedAt() > 0) {
throw new NoPermissionException('Card is deleted');
}
}
return true;
}
// Throw NoPermission to not leak information about existing entries
throw new NoPermissionException('Permission denied');
}
/**
* @param $boardId
* @return bool
*/
public function userIsBoardOwner($boardId, $userId = null, bool $allowDeleted = false) {
if ($userId === null) {
$userId = $this->userId;
}
try {
$board = $this->getBoard($boardId, $allowDeleted);
return $userId === $board->getOwner();
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
}
return false;
}
/**
* @throws MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
private function getBoard(int $boardId, bool $allowDeleted = false): Board {
if (!isset($this->boardCache[(string)$boardId])) {
$this->boardCache[(string)$boardId] = $this->boardMapper->find($boardId, false, true, $allowDeleted);
}
return $this->boardCache[(string)$boardId];
}
public function externalUserCan(array $acls, int $permission, string $shareToken):bool {
$this->configService->ensureFederationEnabled();
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_REMOTE) {
if ($acl->getToken() === $shareToken) {
return $acl->getPermission($permission);
}
}
}
return false;
}
/**
* Check if permission matches the acl rules for current user and groups
*
* @param Acl[] $acls
* @param $permission
* @return bool
*/
public function userCan(array $acls, $permission, $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
// check for users
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) {
return $acl->getPermission($permission);
}
if ($this->circlesService->isCirclesEnabled() && $acl->getType() === Acl::PERMISSION_TYPE_CIRCLE) {
try {
if ($this->circlesService->isUserInCircle($acl->getParticipant(), $userId) && $acl->getPermission($permission)) {
return true;
}
} catch (\Exception $e) {
$this->logger->info('Member not found in circle that was accessed. This should not happen.');
}
}
}
// check for groups
$hasGroupPermission = false;
foreach ($acls as $acl) {
if (!$hasGroupPermission && $acl->getType() === Acl::PERMISSION_TYPE_GROUP && $this->groupManager->isInGroup($userId, $acl->getParticipant())) {
$hasGroupPermission = $acl->getPermission($permission);
}
}
return $hasGroupPermission;
}
public function getUserId(): ?string {
if ($this->userId === null && $this->accessToken === null) {
return null;
}
return $this->userId ?? $this->aclMapper->findByAccessToken($this->accessToken)->getParticipant();
}
/**
* Find a list of all users (including the ones from groups)
* Required to allow assigning them to cards
*
* @param $boardId
* @param $refresh
* @return array<string, User | FederatedUser>
* */
public function findUsers($boardId, $refresh = false) {
// cache users of a board so we don't query them for every cards
if (array_key_exists((string)$boardId, $this->users) && !$refresh) {
return $this->users[(string)$boardId];
}
try {
$board = $this->boardMapper->find($boardId);
} catch (DoesNotExistException $e) {
return [];
} catch (MultipleObjectsReturnedException $e) {
return [];
}
/** @var array<string, User> */
$users = [];
if (!$this->userManager->userExists($board->getOwner())) {
$this->logger->info('No owner found for board ' . $board->getId());
} else {
$users[(string)$board->getOwner()] = new User($board->getOwner(), $this->userManager);
}
$acls = $this->aclMapper->findAll($boardId);
/** @var Acl $acl */
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
if (!$this->userManager->userExists($acl->getParticipant())) {
$this->logger->info('No user found for acl rule ' . $acl->getId());
continue;
}
$users[(string)$acl->getParticipant()] = new User($acl->getParticipant(), $this->userManager);
}
if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
$group = $this->groupManager->get($acl->getParticipant());
if ($group === null) {
$this->logger->info('No group found for acl rule ' . $acl->getId());
continue;
}
foreach ($group->getUsers() as $user) {
$users[(string)$user->getUID()] = new User($user->getUID(), $this->userManager);
}
}
if ($acl->getType() === Acl::PERMISSION_TYPE_REMOTE) {
$users[(string)$acl->getParticipant()] = new FederatedUser($this->cloudIdManager->resolveCloudId($acl->getParticipant()));
}
if ($this->circlesService->isCirclesEnabled() && $acl->getType() === Acl::PERMISSION_TYPE_CIRCLE) {
try {
$circle = $this->circlesService->getCircle($acl->getParticipant());
if ($circle === null) {
$this->logger->info('No circle found for acl rule ' . $acl->getId());
continue;
}
foreach ($circle->getInheritedMembers() as $member) {
if ($member->getUserType() !== 1 || $member->getLevel() < Member::LEVEL_MEMBER) {
// deck currently only supports user members in circles
continue;
}
$user = $this->userManager->get($member->getUserId());
if ($user === null) {
$this->logger->info('No user found for circle member ' . $member->getUserId());
} else {
$users[(string)$member->getUserId()] = new User($member->getUserId(), $this->userManager);
}
}
} catch (\Exception $e) {
$this->logger->info('Member not found in circle that was accessed. This should not happen.');
}
}
}
$this->users[(string)$boardId] = $users;
return $this->users[(string)$boardId];
}
public function canCreate() {
if ($this->userId === null) {
return false;
}
$groups = $this->getGroupLimitList();
if (count($groups) === 0) {
return true;
}
foreach ($groups as $group) {
if ($this->groupManager->isInGroup($this->userId, $group)) {
return true;
}
}
return false;
}
private function getGroupLimitList() {
$value = $this->config->getAppValue('deck', 'groupLimit', '');
$groups = explode(',', $value);
if ($value === '') {
return [];
}
return $groups;
}
/**
* Set a different user than the current one, e.g. when no user is available in occ
*
* @param string $userId
*/
public function setUserId(string $userId): void {
$this->userId = $userId;
$this->permissionCache->clear();
}
}