Add command to move a calendar from an user to another
Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add a basic check for displaynames in case we fail to get calendar from uri and put some sf console styles & refactor a bit Signed-off-by: Thomas Citharel <tcit@tcit.fr> basic Tests Signed-off-by: Thomas Citharel <tcit@tcit.fr> add forgotten createNamedParameter() Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
cff89c2a44
commit
943d48cb3e
5 changed files with 344 additions and 0 deletions
|
@ -41,6 +41,7 @@
|
|||
<commands>
|
||||
<command>OCA\DAV\Command\CreateAddressBook</command>
|
||||
<command>OCA\DAV\Command\CreateCalendar</command>
|
||||
<command>OCA\DAV\Command\MoveCalendar</command>
|
||||
<command>OCA\DAV\Command\SyncBirthdayCalendar</command>
|
||||
<command>OCA\DAV\Command\SyncSystemAddressBook</command>
|
||||
<command>OCA\DAV\Command\RemoveInvalidShares</command>
|
||||
|
|
|
@ -2521,6 +2521,49 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a calendar from one user to another
|
||||
*
|
||||
* @param string $uriName
|
||||
* @param string $uriOrigin
|
||||
* @param string $uriDestination
|
||||
*/
|
||||
public function moveCalendar($uriName, $uriOrigin, $uriDestination)
|
||||
{
|
||||
$query = $this->db->getQueryBuilder();
|
||||
$query->update('calendars')
|
||||
->set('principaluri', $query->createNamedParameter($uriDestination))
|
||||
->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
|
||||
->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $displayName
|
||||
* @param string|null $principalUri
|
||||
* @return array
|
||||
*/
|
||||
public function findCalendarsUrisByDisplayName($displayName, $principalUri = null)
|
||||
{
|
||||
// Ideally $displayName would be sanitized the same way as stringUtility.js does in calendar
|
||||
|
||||
$query = $this->db->getQueryBuilder();
|
||||
$query->select('uri')
|
||||
->from('calendars')
|
||||
->where($query->expr()->iLike('displayname',
|
||||
$query->createNamedParameter('%'.$this->db->escapeLikeParameter($displayName).'%')));
|
||||
if ($principalUri) {
|
||||
$query->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)));
|
||||
}
|
||||
$result = $query->execute();
|
||||
|
||||
$calendarUris = [];
|
||||
while($row = $result->fetch()) {
|
||||
$calendarUris[] = $row['uri'];
|
||||
}
|
||||
return $calendarUris;
|
||||
}
|
||||
|
||||
/**
|
||||
* read VCalendar data into a VCalendar object
|
||||
*
|
||||
|
|
162
apps/dav/lib/Command/MoveCalendar.php
Normal file
162
apps/dav/lib/Command/MoveCalendar.php
Normal file
|
@ -0,0 +1,162 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Thomas Citharel <tcit@tcit.fr>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
namespace OCA\DAV\Command;
|
||||
|
||||
use OCA\DAV\CalDAV\CalDavBackend;
|
||||
use OCA\DAV\CalDAV\Calendar;
|
||||
use OCA\DAV\Connector\Sabre\Principal;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\IL10N;
|
||||
use OCP\IUserManager;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class MoveCalendar extends Command {
|
||||
|
||||
/** @var IUserManager */
|
||||
protected $userManager;
|
||||
|
||||
/** @var IGroupManager $groupManager */
|
||||
private $groupManager;
|
||||
|
||||
/** @var \OCP\IDBConnection */
|
||||
protected $dbConnection;
|
||||
|
||||
/** @var IL10N */
|
||||
protected $l10n;
|
||||
|
||||
/** @var SymfonyStyle */
|
||||
private $io;
|
||||
|
||||
/** @var CalDavBackend */
|
||||
private $caldav;
|
||||
|
||||
const URI_USERS = 'principals/users/';
|
||||
|
||||
/**
|
||||
* @param IUserManager $userManager
|
||||
* @param IGroupManager $groupManager
|
||||
* @param IDBConnection $dbConnection
|
||||
* @param IL10N $l10n
|
||||
*/
|
||||
function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection, IL10N $l10n) {
|
||||
parent::__construct();
|
||||
$this->userManager = $userManager;
|
||||
$this->groupManager = $groupManager;
|
||||
$this->dbConnection = $dbConnection;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
protected function configure() {
|
||||
$this
|
||||
->setName('dav:move-calendar')
|
||||
->setDescription('Move a calendar from an user to another')
|
||||
->addArgument('name',
|
||||
InputArgument::REQUIRED,
|
||||
'Name of the calendar to move')
|
||||
->addArgument('userorigin',
|
||||
InputArgument::REQUIRED,
|
||||
'User who currently owns the calendar')
|
||||
->addArgument('userdestination',
|
||||
InputArgument::REQUIRED,
|
||||
'User who will receive the calendar')
|
||||
->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing shares with groups that the destination user is not in");
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output) {
|
||||
$userOrigin = $input->getArgument('userorigin');
|
||||
$userDestination = $input->getArgument('userdestination');
|
||||
|
||||
$this->io = new SymfonyStyle($input, $output);
|
||||
|
||||
if (in_array('system', [$userOrigin, $userDestination], true)) {
|
||||
throw new \InvalidArgumentException("User can't be system");
|
||||
}
|
||||
|
||||
if (!$this->userManager->userExists($userOrigin)) {
|
||||
throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
|
||||
}
|
||||
|
||||
|
||||
if (!$this->userManager->userExists($userDestination)) {
|
||||
throw new \InvalidArgumentException("User <$userDestination> is unknown.");
|
||||
}
|
||||
|
||||
$principalBackend = new Principal(
|
||||
$this->userManager,
|
||||
$this->groupManager
|
||||
);
|
||||
$random = \OC::$server->getSecureRandom();
|
||||
$dispatcher = \OC::$server->getEventDispatcher();
|
||||
|
||||
$name = $input->getArgument('name');
|
||||
$this->caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $random, $dispatcher);
|
||||
|
||||
$calendar = $this->caldav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
|
||||
|
||||
if (null === $calendar) {
|
||||
/** If we got no matching calendar with URI, let's try the display names */
|
||||
$suggestedUris = $this->caldav->findCalendarsUrisByDisplayName($name, self::URI_USERS . $userOrigin);
|
||||
if (count($suggestedUris) > 0) {
|
||||
$this->io->note('No calendar with this URI was found, but you may want to try with these?');
|
||||
$this->io->listing($suggestedUris);
|
||||
}
|
||||
throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>.");
|
||||
}
|
||||
|
||||
if (null !== $this->caldav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) {
|
||||
throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
|
||||
}
|
||||
|
||||
$this->checkShares($calendar, $userDestination, $input->getOption('force'));
|
||||
|
||||
$this->caldav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination);
|
||||
|
||||
$this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that user destination is member of the groups which whom the calendar was shared
|
||||
* If we ask to force the migration, the share with the group is dropped
|
||||
*
|
||||
* @param $calendar
|
||||
* @param $userDestination
|
||||
* @param bool $force
|
||||
*/
|
||||
private function checkShares($calendar, $userDestination, $force = false)
|
||||
{
|
||||
$shares = $this->caldav->getShares($calendar['id']);
|
||||
foreach ($shares as $share) {
|
||||
list($prefix, $group) = str_split($share['href'], 28);
|
||||
if ('principal:principals/groups/' === $prefix && !$this->groupManager->isInGroup($userDestination, $group)) {
|
||||
if ($force) {
|
||||
$this->caldav->updateShares(new Calendar($this->caldav, $calendar, $this->l10n), [], ['href' => 'principal:principals/groups/' . $group]);
|
||||
} else {
|
||||
throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$group> with which the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -983,4 +983,21 @@ EOD;
|
|||
|
||||
$this->assertEquals(null, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION));
|
||||
}
|
||||
|
||||
public function testCalendarMovement()
|
||||
{
|
||||
$this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
|
||||
|
||||
$this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
|
||||
|
||||
$calendarInfoUser = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
|
||||
|
||||
$this->backend->moveCalendar('Example', self::UNIT_TEST_USER, self::UNIT_TEST_USER1);
|
||||
$this->assertCount(0, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
|
||||
$this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1));
|
||||
|
||||
$calendarInfoUser1 = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1)[0];
|
||||
$this->assertEquals($calendarInfoUser['id'], $calendarInfoUser1['id']);
|
||||
$this->assertEquals($calendarInfoUser['uri'], $calendarInfoUser1['uri']);
|
||||
}
|
||||
}
|
||||
|
|
121
apps/dav/tests/unit/Command/MoveCalendarTest.php
Normal file
121
apps/dav/tests/unit/Command/MoveCalendarTest.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Thomas Citharel <tcit@tcit.fr>
|
||||
*
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Tests\Command;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use OCA\DAV\Command\MoveCalendar;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\IL10N;
|
||||
use OCP\IUserManager;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
use Test\TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Class MoveCalendarTest
|
||||
*
|
||||
* @package OCA\DAV\Tests\Command
|
||||
* @group DB
|
||||
*/
|
||||
class MoveCalendarTest extends TestCase {
|
||||
|
||||
/** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */
|
||||
private $userManager;
|
||||
|
||||
/** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */
|
||||
private $groupManager;
|
||||
|
||||
/** @var \OCP\IDBConnection|\PHPUnit_Framework_MockObject_MockObject $dbConnection */
|
||||
private $dbConnection;
|
||||
|
||||
/** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
|
||||
private $l10n;
|
||||
|
||||
/** @var MoveCalendar */
|
||||
private $command;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->groupManager = $this->createMock(IGroupManager::class);
|
||||
$this->dbConnection = $this->createMock(IDBConnection::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
|
||||
$this->command = new MoveCalendar(
|
||||
$this->userManager,
|
||||
$this->groupManager,
|
||||
$this->dbConnection,
|
||||
$this->l10n
|
||||
);
|
||||
}
|
||||
|
||||
public function dataExecute() {
|
||||
return [
|
||||
[false, true],
|
||||
[true, false]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataExecute
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @param $userOriginExists
|
||||
* @param $userDestinationExists
|
||||
*/
|
||||
public function testWithBadUserOrigin($userOriginExists, $userDestinationExists)
|
||||
{
|
||||
$this->userManager->expects($this->at(0))
|
||||
->method('userExists')
|
||||
->with('user')
|
||||
->willReturn($userOriginExists);
|
||||
|
||||
if (!$userDestinationExists) {
|
||||
$this->userManager->expects($this->at(1))
|
||||
->method('userExists')
|
||||
->with('user2')
|
||||
->willReturn($userDestinationExists);
|
||||
}
|
||||
|
||||
$commandTester = new CommandTester($this->command);
|
||||
$commandTester->execute([
|
||||
'name' => $this->command->getName(),
|
||||
'userorigin' => 'user',
|
||||
'userdestination' => 'user2',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage User can't be system
|
||||
*/
|
||||
public function testTryToMoveToOrFromSystem()
|
||||
{
|
||||
$commandTester = new CommandTester($this->command);
|
||||
$commandTester->execute([
|
||||
'name' => $this->command->getName(),
|
||||
'userorigin' => 'system',
|
||||
'userdestination' => 'user2',
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue