diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-01-28 17:35:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-28 17:35:13 +0100 |
commit | 554c78c9b1641351626f0bbcceb44826dfe854a5 (patch) | |
tree | 2fa333c23daca2037240b3cf59dca78a219da5be /apps/dav/lib | |
parent | cfdece7833ed407cfee63b73c9ae20fde848ffc3 (diff) | |
parent | 3180ddd2023c54c984f6999ade489de3d9b4fb72 (diff) | |
download | nextcloud-server-554c78c9b1641351626f0bbcceb44826dfe854a5.tar.gz nextcloud-server-554c78c9b1641351626f0bbcceb44826dfe854a5.zip |
Merge pull request #5881 from nextcloud/command-move-calendar
Add command to move a calendar from an user to another
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/CalDAV/CalDavBackend.php | 17 | ||||
-rw-r--r-- | apps/dav/lib/Command/ListCalendars.php | 108 | ||||
-rw-r--r-- | apps/dav/lib/Command/MoveCalendar.php | 185 |
3 files changed, 310 insertions, 0 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 187ba4ecdcf..88ee778e82c 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -2522,6 +2522,23 @@ 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(); + } + + /** * read VCalendar data into a VCalendar object * * @param string $objectData diff --git a/apps/dav/lib/Command/ListCalendars.php b/apps/dav/lib/Command/ListCalendars.php new file mode 100644 index 00000000000..6c2f5bdb506 --- /dev/null +++ b/apps/dav/lib/Command/ListCalendars.php @@ -0,0 +1,108 @@ +<?php +/** + * @copyright Copyright (c) 2018, Georg Ehrke + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * @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\BirthdayService; +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\Connector\Sabre\Principal; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Share\IManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class ListCalendars extends Command { + + /** @var IUserManager */ + protected $userManager; + + /** @var CalDavBackend */ + private $caldav; + + /** + * @param IUserManager $userManager + * @param CalDavBackend $caldav + */ + function __construct(IUserManager $userManager, CalDavBackend $caldav) { + parent::__construct(); + $this->userManager = $userManager; + $this->caldav = $caldav; + } + + protected function configure() { + $this + ->setName('dav:list-calendars') + ->setDescription('List all calendars of a user') + ->addArgument('uid', + InputArgument::REQUIRED, + 'User for whom all calendars will be listed'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $input->getArgument('uid'); + if (!$this->userManager->userExists($user)) { + throw new \InvalidArgumentException("User <$user> is unknown."); + } + + $calendars = $this->caldav->getCalendarsForUser("principals/users/$user"); + + $calendarTableData = []; + foreach($calendars as $calendar) { + // skip birthday calendar + if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) { + continue; + } + + $readOnly = false; + $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; + if (isset($calendar[$readOnlyIndex])) { + $readOnly = $calendar[$readOnlyIndex]; + } + + $calendarTableData[] = [ + $calendar['uri'], + $calendar['{DAV:}displayname'], + $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'], + $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'], + $readOnly ? ' x ' : ' ✓ ', + ]; + } + + if (count($calendarTableData) > 0) { + $table = new Table($output); + $table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable']) + ->setRows($calendarTableData); + + $table->render(); + } else { + $output->writeln("<info>User <$user> has no calendars</info>"); + } + } + +} diff --git a/apps/dav/lib/Command/MoveCalendar.php b/apps/dav/lib/Command/MoveCalendar.php new file mode 100644 index 00000000000..a2c7ca8c4d8 --- /dev/null +++ b/apps/dav/lib/Command/MoveCalendar.php @@ -0,0 +1,185 @@ +<?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\IConfig; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\IL10N; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Share\IManager as IShareManager; +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 */ + private $userManager; + + /** @var IGroupManager */ + private $groupManager; + + /** @var IShareManager */ + private $shareManager; + + /** @var IConfig $config */ + private $config; + + /** @var IL10N */ + private $l10n; + + /** @var SymfonyStyle */ + private $io; + + /** @var CalDavBackend */ + private $calDav; + + const URI_USERS = 'principals/users/'; + + /** + * @param IUserManager $userManager + * @param IGroupManager $groupManager + * @param IShareManager $shareManager + * @param IConfig $config + * @param IL10N $l10n + * @param CalDavBackend $calDav + */ + function __construct( + IUserManager $userManager, + IGroupManager $groupManager, + IShareManager $shareManager, + IConfig $config, + IL10N $l10n, + CalDavBackend $calDav + ) { + parent::__construct(); + $this->userManager = $userManager; + $this->groupManager = $groupManager; + $this->shareManager = $shareManager; + $this->config = $config; + $this->l10n = $l10n; + $this->calDav = $calDav; + } + + 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('sourceuid', + InputArgument::REQUIRED, + 'User who currently owns the calendar') + ->addArgument('destinationuid', + InputArgument::REQUIRED, + 'User who will receive the calendar') + ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares"); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $userOrigin = $input->getArgument('sourceuid'); + $userDestination = $input->getArgument('destinationuid'); + + $this->io = new SymfonyStyle($input, $output); + + if (!$this->userManager->userExists($userOrigin)) { + throw new \InvalidArgumentException("User <$userOrigin> is unknown."); + } + + if (!$this->userManager->userExists($userDestination)) { + throw new \InvalidArgumentException("User <$userDestination> is unknown."); + } + + $name = $input->getArgument('name'); + + $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name); + + if (null === $calendar) { + throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user."); + } + + if (null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) { + throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>."); + } + + $this->checkShares($calendar, $userOrigin, $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 moving the calendar won't break shares + * + * @param array $calendar + * @param string $userOrigin + * @param string $userDestination + * @param bool $force + */ + private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false) + { + $shares = $this->calDav->getShares($calendar['id']); + foreach ($shares as $share) { + list(, $prefix, $userOrGroup) = explode('/', $share['href'], 3); + + /** + * 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 + */ + if ($this->shareManager->shareWithGroupMembersOnly() === true && 'groups' === $prefix && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) { + if ($force) { + $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/groups/' . $userOrGroup]); + } else { + throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$userOrGroup> with whom the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share."); + } + } + + /** + * Check that calendar isn't already shared with user destination + */ + if ($userOrGroup === $userDestination) { + if ($force) { + $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/users/' . $userOrGroup]); + } else { + throw new \InvalidArgumentException("The calendar <" . $calendar['uri'] . "> is already shared to user <$userDestination>.You may use -f to move the calendar while deleting this share."); + } + } + } + /** + * Warn that share links have changed if there are shares + */ + if (count($shares) > 0) { + $this->io->note([ + "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.", + "Sharees will need to change \"example.com/remote.php/dav/calendars/uid/" . $calendar['uri'] . "_shared_by_$userOrigin\" to \"example.com/remote.php/dav/calendars/uid/" . $calendar['uri'] . "_shared_by_$userDestination\"" + ]); + } + } +} |