Skip to content

Commit 23f0b16

Browse files
committed
Handle board exceptions more gracefully
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 3f29cd9 commit 23f0b16

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

lib/Command/TransferOwnership.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace OCA\Deck\Command;
44

5+
use OCA\Deck\Db\BoardMapper;
56
use OCA\Deck\Service\BoardService;
7+
use OCA\Deck\Service\PermissionService;
68
use Symfony\Component\Console\Command\Command;
79
use Symfony\Component\Console\Helper\QuestionHelper;
810
use Symfony\Component\Console\Input\InputArgument;
@@ -13,12 +15,16 @@
1315

1416
final class TransferOwnership extends Command {
1517
protected $boardService;
18+
protected $boardMapper;
19+
protected $permissionService;
1620
protected $questionHelper;
1721

18-
public function __construct(BoardService $boardService, QuestionHelper $questionHelper) {
22+
public function __construct(BoardService $boardService, BoardMapper $boardMapper, PermissionService $permissionService, QuestionHelper $questionHelper) {
1923
parent::__construct();
2024

2125
$this->boardService = $boardService;
26+
$this->boardMapper = $boardMapper;
27+
$this->permissionService = $permissionService;
2228
$this->questionHelper = $questionHelper;
2329
}
2430

@@ -57,7 +63,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5763

5864
$remapAssignment = $input->getOption('remap');
5965

60-
$board = $boardId ? $this->boardService->find($boardId) : null;
66+
$this->boardService->setUserId($owner);
67+
$this->permissionService->setUserId($owner);
68+
69+
try {
70+
$board = $boardId ? $this->boardMapper->find($boardId) : null;
71+
} catch (\Exception $e) {
72+
$output->writeln("Could not find a board for the provided id.");
73+
return 1;
74+
}
6175

6276
if ($boardId !== null && $board->getOwner() !== $owner) {
6377
$output->writeln("$owner is not the owner of the board $boardId (" . $board->getTitle() . ")");

lib/Controller/BoardController.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ public function clone($boardId) {
161161

162162
/**
163163
* @NoAdminRequired
164-
* @param $boardId
165-
* @param $owner
166-
* @param $newOwner
167-
* * @return null|void
168164
*/
169165
public function transferOwner(int $boardId, string $newOwner): DataResponse {
170166
if ($this->permissionService->userIsBoardOwner($boardId, $this->userId)) {

lib/Service/BoardService.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public function addAcl($boardId, $type, $participant, $edit, $share, $manage) {
524524
$acl->setPermissionManage($manage);
525525
$newAcl = $this->aclMapper->insert($acl);
526526

527-
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE);
527+
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE, [], $this->userId);
528528
$this->notificationHelper->sendBoardShared((int)$boardId, $acl);
529529
$this->boardMapper->mapAcl($newAcl);
530530
$this->changeHelper->boardChanged($boardId);
@@ -689,17 +689,18 @@ public function transferBoardOwnership(int $boardId, string $newOwner, bool $cha
689689
$previousOwner = $board->getOwner();
690690
$this->clearBoardFromCache($board);
691691
$this->aclMapper->deleteParticipantFromBoard($boardId, Acl::PERMISSION_TYPE_USER, $newOwner);
692+
if (!$changeContent) {
693+
try {
694+
$this->addAcl($boardId, Acl::PERMISSION_TYPE_USER, $previousOwner, true, true, true);
695+
} catch (UniqueConstraintViolationException $e) {
696+
}
697+
}
692698
$this->boardMapper->transferOwnership($previousOwner, $newOwner, $boardId);
693699

694700
// Optionally also change user assignments and card owner information
695701
if ($changeContent) {
696702
$this->assignedUsersMapper->remapAssignedUser($boardId, $previousOwner, $newOwner);
697703
$this->cardMapper->remapCardOwner($boardId, $previousOwner, $newOwner);
698-
} else {
699-
try {
700-
$this->addAcl($boardId, Acl::PERMISSION_TYPE_USER, $previousOwner, true, true, true);
701-
} catch (UniqueConstraintViolationException $e) {
702-
}
703704
}
704705
\OC::$server->getDatabaseConnection()->commit();
705706
return $this->boardMapper->find($boardId);

lib/Service/PermissionService.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,13 @@ private function getGroupLimitList() {
333333
}
334334
return $groups;
335335
}
336+
337+
/**
338+
* Set a different user than the current one, e.g. when no user is available in occ
339+
*
340+
* @param string $userId
341+
*/
342+
public function setUserId(string $userId): void {
343+
$this->userId = $userId;
344+
}
336345
}

0 commit comments

Comments
 (0)