Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer ownership #1955

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ ifeq (, $(shell which phpunit 2> /dev/null))
php $(build_tools_directory)/phpunit.phar -c tests/phpunit.xml --coverage-clover build/php-unit.coverage.xml
php $(build_tools_directory)/phpunit.phar -c tests/phpunit.integration.xml --coverage-clover build/php-integration.coverage.xml
else
phpunit -c tests/phpunit.xml --coverage-clover build/php-unit.coverage.xml
phpunit -c tests/phpunit.integration.xml --coverage-clover build/php-integration.coverage.xml
phpunit -c tests/phpunit.integration.xml --testsuite=integration-database --coverage-clover build/php-integration.coverage.xml
endif

test-integration:
Expand Down
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</repair-steps>
<commands>
<command>OCA\Deck\Command\UserExport</command>
<command>OCA\Deck\Command\TransferOwnership</command>
</commands>
<activity>
<settings>
Expand Down
4 changes: 4 additions & 0 deletions docs/User_documentation_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Overall, Deck is easy to use. You can create boards, add users, share the Deck,
3. [Handle cards options](#3-handle-cards-options)
4. [Archive old tasks](#4-archive-old-tasks)
5. [Manage your board](#5-manage-your-board)
6. [New owner for the deck entities](#6-new-owner-for-the-deck-entities)

### 1. Create my first board
In this example, we're going to create a board and share it with an other nextcloud user.
Expand Down Expand Up @@ -67,3 +68,6 @@ The **sharing tab** allows you to add users or even groups to your boards.
**Deleted objects** allows you to return previously deleted stacks or cards.
The **Timeline** allows you to see everything that happened in your boards. Everything!

### 6. New owner for the deck entities
You can transfer ownership of boards, cards, etc to a new user, using `occ` command `deck:transfer-ownership`
`$ php occ deck:transfer-ownership owner newOwner`
46 changes: 46 additions & 0 deletions lib/Command/TransferOwnership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace OCA\Deck\Command;

use OCA\Deck\Service\BoardService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class TransferOwnership extends Command {
protected $boardService;

public function __construct(BoardService $boardService) {
parent::__construct();

$this->boardService = $boardService;
}

protected function configure() {
$this
->setName('deck:transfer-ownership')
->setDescription('Change owner of deck entities')
->addArgument(
'owner',
InputArgument::REQUIRED,
'Owner uid'
)
->addArgument(
'newOwner',
InputArgument::REQUIRED,
'New owner uid'
);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$owner = $input->getArgument('owner');
$newOwner = $input->getArgument('newOwner');

$output->writeln("Transfer deck entities from $owner to $newOwner");

$this->boardService->transferOwnership($owner, $newOwner);

$output->writeln("Transfer deck entities from $owner to $newOwner completed");
}
}
36 changes: 36 additions & 0 deletions lib/Db/AclMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ public function findByParticipant($type, $participant) {
$sql = 'SELECT * from *PREFIX*deck_board_acl WHERE type = ? AND participant = ?';
return $this->findEntities($sql, [$type, $participant]);
}

/**
* @param $ownerId
* @param $newOwnerId
* @return void
*/
public function transferOwnership($ownerId, $newOwnerId) {
$params = [
'owner' => $ownerId,
'newOwner' => $newOwnerId,
'type' => Acl::PERMISSION_TYPE_USER
];
//We want preserve permissions from both users
$sql = "UPDATE `{$this->tableName}` AS `source`
LEFT JOIN `{$this->tableName}` AS `target`
ON `target`.`participant` = :newOwner AND `target`.`type` = :type
SET `source`.`permission_edit` =(`source`.`permission_edit` || `target`.`permission_edit`),
`source`.`permission_share` =(`source`.`permission_share` || `target`.`permission_share`),
`source`.`permission_manage` =(`source`.`permission_manage` || `target`.`permission_manage`)
WHERE `source`.`participant` = :owner AND `source`.`type` = :type";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
//We can't transfer acl if target already in acl
$sql = "DELETE FROM `{$this->tableName}`
WHERE `participant` = :newOwner
AND `type` = :type
AND EXISTS (SELECT `id` FROM (SELECT `id` FROM `{$this->tableName}`
WHERE `participant` = :owner AND `type` = :type) as tmp)";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
//Now we can transfer without errors
$sqlUpdate = "UPDATE `{$this->tableName}`
SET `participant` = :newOwner WHERE `participant` = :owner AND `type` = :type";
$stmt = $this->execute($sqlUpdate, $params);
$stmt->closeCursor();
}
}
23 changes: 23 additions & 0 deletions lib/Db/AssignedUsersMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,27 @@ private function getOrigin(AssignedUsers $assignment) {
}
return null;
}

/**
* @param $ownerId
* @param $newOwnerId
* @return void
*/
public function transferOwnership($ownerId, $newOwnerId) {
$params = [
'newOwner' => $newOwnerId,
'type' => AssignedUsers::TYPE_USER
];
$sql = "DELETE FROM `{$this->tableName}` WHERE `participant` = :newOwner AND `type`= :type";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
$params = [
'owner' => $ownerId,
'newOwner' => $newOwnerId,
'type' => AssignedUsers::TYPE_USER
];
$sql = "UPDATE `{$this->tableName}` SET `participant` = :newOwner WHERE `participant` = :owner AND `type`= :type";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
}
}
15 changes: 15 additions & 0 deletions lib/Db/BoardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,19 @@ public function mapOwner(Board &$board) {
return null;
});
}

/**
* @param $ownerId
* @param $newOwnerId
* @return void
*/
public function transferOwnership($ownerId, $newOwnerId) {
$params = [
'owner' => $ownerId,
'newOwner' => $newOwnerId
];
$sql = "UPDATE `{$this->tableName}` SET `owner` = :newOwner WHERE `owner` = :owner";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
}
}
15 changes: 15 additions & 0 deletions lib/Db/CardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,19 @@ public function mapOwner(Card &$card) {
return null;
});
}

/**
* @param $ownerId
* @param $newOwnerId
* @return void
*/
public function transferOwnership($ownerId, $newOwnerId) {
$params = [
'owner' => $ownerId,
'newOwner' => $newOwnerId
];
$sql = "UPDATE `{$this->tableName}` SET `owner` = :newOwner WHERE `owner` = :owner";
$stmt = $this->execute($sql, $params);
$stmt->closeCursor();
}
}
16 changes: 16 additions & 0 deletions lib/Service/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Db\AssignedUsersMapper;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\ChangeHelper;
use OCA\Deck\Db\IPermissionMapper;
use OCA\Deck\Db\Label;
Expand Down Expand Up @@ -63,6 +64,7 @@ class BoardService {
/** @var EventDispatcherInterface */
private $eventDispatcher;
private $changeHelper;
private $cardMapper;

public function __construct(
BoardMapper $boardMapper,
Expand All @@ -73,6 +75,7 @@ public function __construct(
PermissionService $permissionService,
NotificationHelper $notificationHelper,
AssignedUsersMapper $assignedUsersMapper,
CardMapper $cardMapper,
IUserManager $userManager,
IGroupManager $groupManager,
ActivityManager $activityManager,
Expand All @@ -94,6 +97,7 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
$this->changeHelper = $changeHelper;
$this->userId = $userId;
$this->cardMapper = $cardMapper;
}

/**
Expand Down Expand Up @@ -669,6 +673,18 @@ public function clone($id, $userId) {
return $newBoard;
}

/**
* @param $ownerId
* @param $newOwnerId
* @return void
*/
public function transferOwnership($owner, $newOwner) {
$this->boardMapper->transferOwnership($owner, $newOwner);
$this->aclMapper->transferOwnership($owner, $newOwner);
$this->assignedUsersMapper->transferOwnership($owner, $newOwner);
$this->cardMapper->transferOwnership($owner, $newOwner);
}

private function enrichWithStacks($board, $since = -1) {
$stacks = $this->stackMapper->findAll($board->getId(), null, null, $since);

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ default:
- '%paths.base%/../features/'
contexts:
- FeatureContext:
baseUrl: http://localhost:8080/index.php/ocs/
baseUrl: http://localhost:9090/index.php/ocs/
admin:
- admin
- admin
regular_user_password: 123456
regular_user_password: 123456
Loading