Completed
Push — master ( abdf01...4c1664 )
by Julius
05:21 queued 03:00
created

BoardService::deleteUndo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 * @author Maxence Lange <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Deck\Service;
26
27
use OCA\Deck\Activity\ActivityManager;
28
use OCA\Deck\Activity\ChangeSet;
29
use OCA\Deck\Collaboration\Resources\ResourceProvider;
30
use OCA\Deck\Db\Acl;
31
use OCA\Deck\Db\AclMapper;
32
use OCA\Deck\Db\AssignedUsersMapper;
33
use OCA\Deck\Db\ChangeHelper;
34
use OCA\Deck\Db\IPermissionMapper;
35
use OCA\Deck\Db\Label;
36
use OCA\Deck\Db\StackMapper;
37
use OCA\Deck\NoPermissionException;
38
use OCA\Deck\Notification\NotificationHelper;
39
use OCP\AppFramework\Db\DoesNotExistException;
40
use OCP\IGroupManager;
41
use OCP\IL10N;
42
use OCA\Deck\Db\Board;
43
use OCA\Deck\Db\BoardMapper;
44
use OCA\Deck\Db\LabelMapper;
45
use OCP\IUserManager;
46
use OCA\Deck\BadRequestException;
47
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
48
use Symfony\Component\EventDispatcher\GenericEvent;
49
50
51
class BoardService {
52
53
	private $boardMapper;
54
	private $stackMapper;
55
	private $labelMapper;
56
	private $aclMapper;
57
	private $l10n;
58
	private $permissionService;
59
	private $notificationHelper;
60
	private $assignedUsersMapper;
61
	private $userManager;
62
	private $groupManager;
63
	private $userId;
64
	private $activityManager;
65
	/** @var EventDispatcherInterface */
66
	private $eventDispatcher;
67
	private $changeHelper;
68
69 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
		BoardMapper $boardMapper,
71
		StackMapper $stackMapper,
72
		IL10N $l10n,
73
		LabelMapper $labelMapper,
74
		AclMapper $aclMapper,
75
		PermissionService $permissionService,
76
		NotificationHelper $notificationHelper,
77
		AssignedUsersMapper $assignedUsersMapper,
78
		IUserManager $userManager,
79
		IGroupManager $groupManager,
80
		ActivityManager $activityManager,
81
		EventDispatcherInterface $eventDispatcher,
82
		ChangeHelper $changeHelper,
83
		$userId
84
	) {
85
		$this->boardMapper = $boardMapper;
86
		$this->stackMapper = $stackMapper;
87
		$this->labelMapper = $labelMapper;
88
		$this->aclMapper = $aclMapper;
89
		$this->l10n = $l10n;
90
		$this->permissionService = $permissionService;
91
		$this->notificationHelper = $notificationHelper;
92
		$this->assignedUsersMapper = $assignedUsersMapper;
93
		$this->userManager = $userManager;
94
		$this->groupManager = $groupManager;
95
		$this->activityManager = $activityManager;
96
		$this->eventDispatcher = $eventDispatcher;
97
		$this->changeHelper = $changeHelper;
98
		$this->userId = $userId;
99
	}
100
101
	/**
102
	 * Set a different user than the current one, e.g. when no user is available in occ
103
	 *
104
	 * @param string $userId
105
	 */
106
	public function setUserId(string $userId): void {
107
		$this->userId = $userId;
108
	}
109
110
	/**
111
	 * @return array
112
	 */
113
	public function findAll($since = -1, $details = null) {
114
		$userInfo = $this->getBoardPrerequisites();
115
		$userBoards = $this->boardMapper->findAllByUser($userInfo['user'], null, null, $since);
116
		$groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups'],null, null,  $since);
0 ignored issues
show
Unused Code introduced by
The call to BoardMapper::findAllByGroups() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
117
		$circleBoards = $this->boardMapper->findAllByCircles($userInfo['user'], null, null,  $since);
0 ignored issues
show
Unused Code introduced by
The call to BoardMapper::findAllByCircles() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
118
		$complete = array_merge($userBoards, $groupBoards, $circleBoards);
119
		$result = [];
120
		/** @var Board $item */
121
		foreach ($complete as &$item) {
122
			if (!array_key_exists($item->getId(), $result)) {
123
				$this->boardMapper->mapOwner($item);
124
				if ($item->getAcl() !== null) {
125
					foreach ($item->getAcl() as &$acl) {
0 ignored issues
show
Bug introduced by
The expression $item->getAcl() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
126
						$this->boardMapper->mapAcl($acl);
127
					}
128
				}
129
				if ($details !== null) {
130
					$this->enrichWithStacks($item);
131
					$this->enrichWithLabels($item);
132
					$this->enrichWithUsers($item);
133
				}
134
				$permissions = $this->permissionService->matchPermissions($item);
135
				$item->setPermissions([
136
					'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
137
					'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
138
					'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
139
					'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
140
				]);
141
				$result[$item->getId()] = $item;
142
			}
143
		}
144
		return array_values($result);
145
	}
146
147
	/**
148
	 * @param $boardId
149
	 * @return Board
150
	 * @throws DoesNotExistException
151
	 * @throws \OCA\Deck\NoPermissionException
152
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
153
	 * @throws BadRequestException
154
	 */
155
	public function find($boardId) {
156
157
		if ( is_numeric($boardId) === false ) {
158
			throw new BadRequestException('board id must be a number');
159
		}
160
161
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
162
		/** @var Board $board */
163
		$board = $this->boardMapper->find($boardId, true, true);
164
		$this->boardMapper->mapOwner($board);
165
		foreach ($board->getAcl() as &$acl) {
0 ignored issues
show
Bug introduced by
The expression $board->getAcl() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
166
			if ($acl !== null) {
167
				$this->boardMapper->mapAcl($acl);
168
			}
169
		}
170
		$permissions = $this->permissionService->matchPermissions($board);
171
		$board->setPermissions([
172
			'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
173
			'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
174
			'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
175
			'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
176
		]);
177
		$this->enrichWithUsers($board);
178
		return $board;
179
	}
180
181
	/**
182
	 * @return array
183
	 */
184
	private function getBoardPrerequisites() {
185
		$groups = $this->groupManager->getUserGroupIds(
186
			$this->userManager->get($this->userId)
0 ignored issues
show
Bug introduced by
It seems like $this->userManager->get($this->userId) can be null; however, getUserGroupIds() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
187
		);
188
		return [
189
			'user' => $this->userId,
190
			'groups' => $groups
191
		];
192
	}
193
194
	/**
195
	 * @param $mapper
196
	 * @param $id
197
	 * @return bool
198
	 * @throws DoesNotExistException
199
	 * @throws \OCA\Deck\NoPermissionException
200
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
201
	 * @throws BadRequestException
202
	 */
203
	public function isArchived($mapper, $id) {
204
205
		if (is_numeric($id) === false)  {
206
			throw new BadRequestException('id must be a number');
207
		}
208
209
		try {
210
			$boardId = $id;
211
			if ($mapper instanceof IPermissionMapper) {
212
				$boardId = $mapper->findBoardId($id);
213
			}
214
			if ($boardId === null) {
215
				return false;
216
			}
217
		} catch (DoesNotExistException $exception) {
218
			return false;
219
		}
220
		$board = $this->find($boardId);
221
		return $board->getArchived();
222
	}
223
224
	/**
225
	 * @param $mapper
226
	 * @param $id
227
	 * @return bool
228
	 * @throws DoesNotExistException
229
	 * @throws \OCA\Deck\NoPermissionException
230
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
231
	 * @throws BadRequestException
232
	 */
233
	public function isDeleted($mapper, $id) {
234
235
		if ($mapper === false || $mapper === null) {
236
			throw new BadRequestException('mapper must be provided');
237
		}
238
239
		if (is_numeric($id) === false)  {
240
			throw new BadRequestException('id must be a number');
241
		}
242
243
		try {
244
			$boardId = $id;
245
			if ($mapper instanceof IPermissionMapper) {
246
				$boardId = $mapper->findBoardId($id);
247
			}
248
			if ($boardId === null) {
249
				return false;
250
			}
251
		} catch (DoesNotExistException $exception) {
252
			return false;
253
		}
254
		$board = $this->find($boardId);
255
		return $board->getDeletedAt() > 0;
256
	}
257
258
259
	/**
260
	 * @param $title
261
	 * @param $userId
262
	 * @param $color
263
	 * @return \OCP\AppFramework\Db\Entity
264
	 * @throws BadRequestException
265
	 */
266
	public function create($title, $userId, $color) {
267
268
		if ($title === false || $title === null) {
269
			throw new BadRequestException('title must be provided');
270
		}
271
272
		if ($userId === false || $userId === null) {
273
			throw new BadRequestException('userId must be provided');
274
		}
275
276
		if ($color === false || $color === null) {
277
			throw new BadRequestException('color must be provided');
278
		}
279
280
		if (!$this->permissionService->canCreate()) {
281
			throw new NoPermissionException('Creating boards has been disabled for your account.');
282
		}
283
284
		$board = new Board();
285
		$board->setTitle($title);
286
		$board->setOwner($userId);
287
		$board->setColor($color);
288
		$new_board = $this->boardMapper->insert($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
289
290
		// create new labels
291
		$default_labels = [
292
			'31CC7C' => $this->l10n->t('Finished'),
293
			'317CCC' => $this->l10n->t('To review'),
294
			'FF7A66' => $this->l10n->t('Action needed'),
295
			'F1DB50' => $this->l10n->t('Later')
296
		];
297
		$labels = [];
298
		foreach ($default_labels as $labelColor => $labelTitle) {
299
			$label = new Label();
300
			$label->setColor($labelColor);
301
			$label->setTitle($labelTitle);
302
			$label->setBoardId($new_board->getId());
303
			$labels[] = $this->labelMapper->insert($label);
304
		}
305
		$new_board->setLabels($labels);
306
		$this->boardMapper->mapOwner($new_board);
307
		$permissions = $this->permissionService->matchPermissions($new_board);
308
		$new_board->setPermissions([
309
			'PERMISSION_READ' => $permissions[Acl::PERMISSION_READ],
310
			'PERMISSION_EDIT' => $permissions[Acl::PERMISSION_EDIT],
311
			'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE],
312
			'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE]
313
		]);
314
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $new_board, ActivityManager::SUBJECT_BOARD_CREATE);
315
		$this->changeHelper->boardChanged($new_board->getId());
316
317
		$this->eventDispatcher->dispatch(
318
			'\OCA\Deck\Board::onCreate',
319
			new GenericEvent(
320
				null, ['id' => $new_board->getId(), 'userId' => $userId, 'board' => $new_board]
321
			)
322
		);
323
324
		return $new_board;
325
	}
326
327
	/**
328
	 * @param $id
329
	 * @return Board
330
	 * @throws DoesNotExistException
331
	 * @throws \OCA\Deck\NoPermissionException
332
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
333
	 * @throws BadRequestException
334
	 */
335
	public function delete($id) {
336
337
		if (is_numeric($id) === false) {
338
			throw new BadRequestException('board id must be a number');
339
		}
340
341
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
342
		$board = $this->find($id);
343
		if ($board->getDeletedAt() > 0) {
344
			throw new BadRequestException('This board has already been deleted');
345
		}
346
		$board->setDeletedAt(time());
347
		$board = $this->boardMapper->update($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
348
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $board, ActivityManager::SUBJECT_BOARD_DELETE);
349
		$this->changeHelper->boardChanged($board->getId());
350
351
		$this->eventDispatcher->dispatch(
352
			'\OCA\Deck\Board::onDelete', new GenericEvent(null, ['id' => $id])
353
		);
354
355
		return $board;
356
	}
357
358
	/**
359
	 * @param $id
360
	 * @return \OCP\AppFramework\Db\Entity
361
	 * @throws DoesNotExistException
362
	 * @throws \OCA\Deck\NoPermissionException
363
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
364
	 */
365 View Code Duplication
	public function deleteUndo($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
367
		if (is_numeric($id) === false) {
368
			throw new BadRequestException('board id must be a number');
369
		}
370
371
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
372
		$board = $this->find($id);
373
		$board->setDeletedAt(0);
374
		$board = $this->boardMapper->update($board);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
375
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $board, ActivityManager::SUBJECT_BOARD_RESTORE);
376
		$this->changeHelper->boardChanged($board->getId());
377
378
		$this->eventDispatcher->dispatch(
379
			'\OCA\Deck\Board::onUpdate', new GenericEvent(null, ['id' => $id, 'board' => $board])
380
		);
381
382
		return $board;
383
	}
384
385
	/**
386
	 * @param $id
387
	 * @return \OCP\AppFramework\Db\Entity
388
	 * @throws DoesNotExistException
389
	 * @throws \OCA\Deck\NoPermissionException
390
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
391
	 * @throws BadRequestException
392
	 */
393
	public function deleteForce($id) {
394
		if (is_numeric($id) === false)  {
395
			throw new BadRequestException('id must be a number');
396
		}
397
398
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ);
399
		$board = $this->find($id);
400
		$delete = $this->boardMapper->delete($board);
401
402
		$this->eventDispatcher->dispatch(
403
			'\OCA\Deck\Board::onDelete', new GenericEvent(null, ['id' => $id])
404
		);
405
406
		return $delete;
407
	}
408
409
	/**
410
	 * @param $id
411
	 * @param $title
412
	 * @param $color
413
	 * @param $archived
414
	 * @return \OCP\AppFramework\Db\Entity
415
	 * @throws DoesNotExistException
416
	 * @throws \OCA\Deck\NoPermissionException
417
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
418
	 * @throws BadRequestException
419
	 */
420
	public function update($id, $title, $color, $archived) {
421
422
		if (is_numeric($id) === false) {
423
			throw new BadRequestException('board id must be a number');
424
		}
425
426
		if ($title === false || $title === null) {
427
			throw new BadRequestException('color must be provided');
428
		}
429
430
		if ($color === false || $color === null) {
431
			throw new BadRequestException('color must be provided');
432
		}
433
434
		if ( is_bool($archived) === false ) {
435
			throw new BadRequestException('archived must be a boolean');
436
		}
437
438
		$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
439
		$board = $this->find($id);
440
		$changes = new ChangeSet($board);
441
		$board->setTitle($title);
442
		$board->setColor($color);
443
		$board->setArchived($archived);
444
		$changes->setAfter($board);
445
		$this->boardMapper->update($board); // operate on clone so we can check for updated fields
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
446
		$this->boardMapper->mapOwner($board);
447
		$this->activityManager->triggerUpdateEvents(ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_BOARD_UPDATE);
448
		$this->changeHelper->boardChanged($board->getId());
449
450
		$this->eventDispatcher->dispatch(
451
			'\OCA\Deck\Board::onUpdate', new GenericEvent(null, ['id' => $id, 'board' => $board])
452
		);
453
454
		return $board;
455
	}
456
457
458
	/**
459
	 * @param $boardId
460
	 * @param $type
461
	 * @param $participant
462
	 * @param $edit
463
	 * @param $share
464
	 * @param $manage
465
	 * @return \OCP\AppFramework\Db\Entity
466
	 * @throws BadRequestException
467
	 * @throws \OCA\Deck\NoPermissionException
468
	 */
469
	public function addAcl($boardId, $type, $participant, $edit, $share, $manage) {
470
471
		if (is_numeric($boardId) === false) {
472
			throw new BadRequestException('board id must be a number');
473
		}
474
475
		if ($type === false || $type === null) {
476
			throw new BadRequestException('type must be provided');
477
		}
478
479
		if ($participant === false || $participant === null) {
480
			throw new BadRequestException('participant must be provided');
481
		}
482
483
		if ($edit === null) {
484
			throw new BadRequestException('edit must be provided');
485
		}
486
487
		if ($share === null) {
488
			throw new BadRequestException('share must be provided');
489
		}
490
491
		if ($manage === null) {
492
			throw new BadRequestException('manage must be provided');
493
		}
494
495
		$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_SHARE);
496
		$acl = new Acl();
497
		$acl->setBoardId($boardId);
498
		$acl->setType($type);
499
		$acl->setParticipant($participant);
500
		$acl->setPermissionEdit($edit);
501
		$acl->setPermissionShare($share);
502
		$acl->setPermissionManage($manage);
503
504
		/* Notify users about the shared board */
505
		$this->notificationHelper->sendBoardShared($boardId, $acl);
506
507
		$newAcl = $this->aclMapper->insert($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
508
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE);
509
		$this->boardMapper->mapAcl($newAcl);
510
		$this->changeHelper->boardChanged($boardId);
511
512
		// TODO: use the dispatched event for this
513
		$version = \OC_Util::getVersion()[0];
514 View Code Duplication
		if ($version >= 16) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
515
			try {
516
				$resourceProvider = \OC::$server->query(\OCA\Deck\Collaboration\Resources\ResourceProvider::class);
517
				$resourceProvider->invalidateAccessCache($boardId);
518
			} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
519
		}
520
521
		$this->eventDispatcher->dispatch(
522
			'\OCA\Deck\Board::onShareNew', new GenericEvent(null, ['id' => $newAcl->getId(), 'acl' => $newAcl, 'boardId' => $boardId])
523
		);
524
525
		return $newAcl;
526
	}
527
528
	/**
529
	 * @param $id
530
	 * @param $edit
531
	 * @param $share
532
	 * @param $manage
533
	 * @return \OCP\AppFramework\Db\Entity
534
	 * @throws DoesNotExistException
535
	 * @throws \OCA\Deck\NoPermissionException
536
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
537
	 * @throws BadRequestException
538
	 */
539
	public function updateAcl($id, $edit, $share, $manage) {
540
541
		if (is_numeric($id) === false) {
542
			throw new BadRequestException('id must be a number');
543
		}
544
545
		if ($edit === null) {
546
			throw new BadRequestException('edit must be provided');
547
		}
548
549
		if ($share === null) {
550
			throw new BadRequestException('share must be provided');
551
		}
552
553
		if ($manage === null) {
554
			throw new BadRequestException('manage must be provided');
555
		}
556
557
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
558
		/** @var Acl $acl */
559
		$acl = $this->aclMapper->find($id);
560
		$acl->setPermissionEdit($edit);
561
		$acl->setPermissionShare($share);
562
		$acl->setPermissionManage($manage);
563
		$this->boardMapper->mapAcl($acl);
564
		$board = $this->aclMapper->update($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
565
		$this->changeHelper->boardChanged($acl->getBoardId());
566
567
		$this->eventDispatcher->dispatch(
568
			'\OCA\Deck\Board::onShareEdit', new GenericEvent(null, ['id' => $id, 'boardId' => $acl->getBoardId(), 'acl' => $acl])
569
		);
570
571
		return $board;
572
	}
573
574
	/**
575
	 * @param $id
576
	 * @return \OCP\AppFramework\Db\Entity
577
	 * @throws DoesNotExistException
578
	 * @throws \OCA\Deck\NoPermissionException
579
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
580
	 * @throws BadRequestException
581
	 */
582
	public function deleteAcl($id) {
583
584
		if (is_numeric($id) === false) {
585
			throw new BadRequestException('id must be a number');
586
		}
587
588
		$this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE);
589
		/** @var Acl $acl */
590
		$acl = $this->aclMapper->find($id);
591
		$this->boardMapper->mapAcl($acl);
592
		if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
593
			$assignements = $this->assignedUsersMapper->findByUserId($acl->getParticipant());
594
			foreach ($assignements as $assignement) {
595
				$this->assignedUsersMapper->delete($assignement);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
596
			}
597
		}
598
		$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $acl, ActivityManager::SUBJECT_BOARD_UNSHARE);
599
		$this->changeHelper->boardChanged($acl->getBoardId());
600
601
		$version = \OC_Util::getVersion()[0];
602 View Code Duplication
		if ($version >= 16) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
603
			try {
604
				$resourceProvider = \OC::$server->query(\OCA\Deck\Collaboration\Resources\ResourceProvider::class);
605
				$resourceProvider->invalidateAccessCache($acl->getBoardId());
606
			} catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
607
		}
608
		$delete = $this->aclMapper->delete($acl);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
609
610
		$this->eventDispatcher->dispatch(
611
			'\OCA\Deck\Board::onShareDelete', new GenericEvent(null, ['id' => $id, 'boardId' => $acl->getBoardId(), 'acl' => $acl])
612
		);
613
614
		return $delete;
615
	}
616
617
	private function enrichWithStacks($board, $since = -1) {
618
		$stacks = $this->stackMapper->findAll($board->getId(), null, null, $since);
0 ignored issues
show
Unused Code introduced by
The call to StackMapper::findAll() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
619
620
		if(\count($stacks) === 0) {
621
			return;
622
		}
623
624
		$board->setStacks($stacks);
625
	}
626
627
	private function enrichWithLabels($board, $since = -1) {
628
		$labels = $this->labelMapper->findAll($board->getId(), null, null, $since);
0 ignored issues
show
Unused Code introduced by
The call to LabelMapper::findAll() has too many arguments starting with $since.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
629
630
		if(\count($labels) === 0) {
631
			return;
632
		}
633
634
		$board->setLabels($labels);
635
	}
636
637
	private function enrichWithUsers($board, $since = -1) {
0 ignored issues
show
Unused Code introduced by
The parameter $since is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
638
		$boardUsers = $this->permissionService->findUsers($board->getId());
639
		if(\count($boardUsers) === 0) {
640
			return;
641
		}
642
		$board->setUsers(array_values($boardUsers));
643
	}
644
645
}
646