diff options
author | Anna Larch <anna@nextcloud.com> | 2021-12-06 20:01:22 +0100 |
---|---|---|
committer | Anna Larch <anna@nextcloud.com> | 2022-03-16 12:48:50 +0100 |
commit | 0745fc50126e92406ec95265ef1ff5d4b5575d3e (patch) | |
tree | 452e44b917c0d242669ee93d58fc84d5210802e5 /apps/dav/lib | |
parent | a626307da8e9a4e5124da1c35429892f745b2de9 (diff) | |
download | nextcloud-server-0745fc50126e92406ec95265ef1ff5d4b5575d3e.tar.gz nextcloud-server-0745fc50126e92406ec95265ef1ff5d4b5575d3e.zip |
Move calendar objects between calendars instead of deleting and recreating them
Signed-off-by: Anna Larch <anna@nextcloud.com>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/CalDAV/CalDavBackend.php | 53 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/Calendar.php | 31 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarHome.php | 11 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarManager.php | 9 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarObject.php | 8 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarProvider.php | 9 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/CalendarRoot.php | 15 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/PublicCalendarRoot.php | 9 | ||||
-rw-r--r-- | apps/dav/lib/Command/DeleteCalendar.php | 12 | ||||
-rw-r--r-- | apps/dav/lib/Command/MoveCalendar.php | 12 | ||||
-rw-r--r-- | apps/dav/lib/RootCollection.php | 10 |
11 files changed, 157 insertions, 22 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index fa6e6a7ecbb..d949f32c1fc 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -63,6 +63,7 @@ use OCA\DAV\Events\CalendarUpdatedEvent; use OCA\DAV\Events\SubscriptionCreatedEvent; use OCA\DAV\Events\SubscriptionDeletedEvent; use OCA\DAV\Events\SubscriptionUpdatedEvent; +use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; @@ -1122,7 +1123,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * * @param string $principalUri * @return array - * @throws \OCP\DB\Exception + * @throws Exception */ public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { $query = $this->db->getQueryBuilder(); @@ -1416,6 +1417,56 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription } /** + * Moves a calendar object from calendar to calendar. + * + * @param int $sourceCalendarId + * @param int $targetCalendarId + * @param int $objectId + * @param string $principalUri + * @param int $calendarType + * @return bool + * @throws Exception + */ + public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $principalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { + $object = $this->getCalendarObjectById($principalUri, $objectId); + if (empty($object)) { + return false; + } + + $query = $this->db->getQueryBuilder(); + $query->update('calendarobjects') + ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) + ->where($query->expr()->eq('id', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) + ->executeStatement(); + + $this->purgeProperties($sourceCalendarId, $objectId); + $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); + + $this->addChange($sourceCalendarId, $object['uri'], 1, $calendarType); + $this->addChange($targetCalendarId, $object['uri'], 3, $calendarType); + + $object = $this->getCalendarObjectById($principalUri, $objectId); + // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client + if (empty($object)) { + return false; + } + + $calendarRow = $this->getCalendarById($targetCalendarId); + // the calendar this event is being moved to does not exist any longer + if (empty($calendarRow)) { + return false; + } + + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { + $shares = $this->getShares($targetCalendarId); + $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($targetCalendarId, $calendarRow, $shares, $object)); + } + return true; + } + + + /** * @param int $calendarObjectId * @param int $classification */ diff --git a/apps/dav/lib/CalDAV/Calendar.php b/apps/dav/lib/CalDAV/Calendar.php index fa29fc8d588..75c815c3b0a 100644 --- a/apps/dav/lib/CalDAV/Calendar.php +++ b/apps/dav/lib/CalDAV/Calendar.php @@ -33,11 +33,15 @@ use DateTimeInterface; use OCA\DAV\CalDAV\Trashbin\Plugin as TrashbinPlugin; use OCA\DAV\DAV\Sharing\IShareable; use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException; +use OCP\DB\Exception; use OCP\IConfig; use OCP\IL10N; +use Psr\Log\LoggerInterface; use Sabre\CalDAV\Backend\BackendInterface; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; +use Sabre\DAV\IMoveTarget; +use Sabre\DAV\INode; use Sabre\DAV\PropPatch; /** @@ -46,7 +50,7 @@ use Sabre\DAV\PropPatch; * @package OCA\DAV\CalDAV * @property CalDavBackend $caldavBackend */ -class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable { +class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable, IMoveTarget { /** @var IConfig */ private $config; @@ -57,6 +61,9 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable /** @var bool */ private $useTrashbin = true; + /** @var LoggerInterface */ + private $logger; + /** * Calendar constructor. * @@ -65,7 +72,7 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable * @param IL10N $l10n * @param IConfig $config */ - public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config) { + public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config, LoggerInterface $logger) { // Convert deletion date to ISO8601 string if (isset($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) { $calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] = (new DateTimeImmutable()) @@ -85,6 +92,7 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable $this->config = $config; $this->l10n = $l10n; + $this->logger = $logger; } /** @@ -415,6 +423,9 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable return parent::getChanges($syncToken, $syncLevel, $limit); } + /** + * @inheritDoc + */ public function restore(): void { $this->caldavBackend->restoreCalendar((int) $this->calendarInfo['id']); } @@ -422,4 +433,20 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable public function disableTrashbin(): void { $this->useTrashbin = false; } + + /** + * @inheritDoc + */ + public function moveInto($targetName, $sourcePath, INode $sourceNode) { + if (!($sourceNode instanceof CalendarObject)) { + return false; + } + + try { + return $this->caldavBackend->moveCalendarObject($sourceNode->getCalendarId(), (int)$this->calendarInfo['id'], $sourceNode->getId(), $sourceNode->getPrincipalUri()); + } catch (Exception $e) { + $this->logger->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]); + return false; + } + } } diff --git a/apps/dav/lib/CalDAV/CalendarHome.php b/apps/dav/lib/CalDAV/CalendarHome.php index 3a1ee2883c5..ceeba31800e 100644 --- a/apps/dav/lib/CalDAV/CalendarHome.php +++ b/apps/dav/lib/CalDAV/CalendarHome.php @@ -30,6 +30,7 @@ use OCA\DAV\AppInfo\PluginManager; use OCA\DAV\CalDAV\Integration\ExternalCalendar; use OCA\DAV\CalDAV\Integration\ICalendarProvider; use OCA\DAV\CalDAV\Trashbin\TrashbinHome; +use Psr\Log\LoggerInterface; use Sabre\CalDAV\Backend\BackendInterface; use Sabre\CalDAV\Backend\NotificationSupport; use Sabre\CalDAV\Backend\SchedulingSupport; @@ -55,7 +56,10 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome { /** @var bool */ private $returnCachedSubscriptions = false; - public function __construct(BackendInterface $caldavBackend, $principalInfo) { + /** @var LoggerInterface */ + private $logger; + + public function __construct(BackendInterface $caldavBackend, $principalInfo, LoggerInterface $logger) { parent::__construct($caldavBackend, $principalInfo); $this->l10n = \OC::$server->getL10N('dav'); $this->config = \OC::$server->getConfig(); @@ -63,6 +67,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome { \OC::$server, \OC::$server->getAppManager() ); + $this->logger = $logger; } /** @@ -95,7 +100,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome { $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']); $objects = []; foreach ($calendars as $calendar) { - $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); + $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger); } if ($this->caldavBackend instanceof SchedulingSupport) { @@ -157,7 +162,7 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome { // Calendars foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) { if ($calendar['uri'] === $name) { - return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); + return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger); } } diff --git a/apps/dav/lib/CalDAV/CalendarManager.php b/apps/dav/lib/CalDAV/CalendarManager.php index 0377979a8a8..daa96a51392 100644 --- a/apps/dav/lib/CalDAV/CalendarManager.php +++ b/apps/dav/lib/CalDAV/CalendarManager.php @@ -27,6 +27,7 @@ namespace OCA\DAV\CalDAV; use OCP\Calendar\IManager; use OCP\IConfig; use OCP\IL10N; +use Psr\Log\LoggerInterface; class CalendarManager { @@ -39,6 +40,9 @@ class CalendarManager { /** @var IConfig */ private $config; + /** @var LoggerInterface */ + private $logger; + /** * CalendarManager constructor. * @@ -46,10 +50,11 @@ class CalendarManager { * @param IL10N $l10n * @param IConfig $config */ - public function __construct(CalDavBackend $backend, IL10N $l10n, IConfig $config) { + public function __construct(CalDavBackend $backend, IL10N $l10n, IConfig $config, LoggerInterface $logger) { $this->backend = $backend; $this->l10n = $l10n; $this->config = $config; + $this->logger = $logger; } /** @@ -67,7 +72,7 @@ class CalendarManager { */ private function register(IManager $cm, array $calendars) { foreach ($calendars as $calendarInfo) { - $calendar = new Calendar($this->backend, $calendarInfo, $this->l10n, $this->config); + $calendar = new Calendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger); $cm->registerCalendar(new CalendarImpl( $calendar, $calendarInfo, diff --git a/apps/dav/lib/CalDAV/CalendarObject.php b/apps/dav/lib/CalDAV/CalendarObject.php index 1582e8f2bfc..d87ea571d1d 100644 --- a/apps/dav/lib/CalDAV/CalendarObject.php +++ b/apps/dav/lib/CalDAV/CalendarObject.php @@ -154,4 +154,12 @@ class CalendarObject extends \Sabre\CalDAV\CalendarObject { } return true; } + + public function getCalendarId(): int { + return (int)$this->objectData['calendarId']; + } + + public function getPrincipalUri(): string { + return $this->objectData['principaluri']; + } } diff --git a/apps/dav/lib/CalDAV/CalendarProvider.php b/apps/dav/lib/CalDAV/CalendarProvider.php index 85ead6117a6..f29c601db2d 100644 --- a/apps/dav/lib/CalDAV/CalendarProvider.php +++ b/apps/dav/lib/CalDAV/CalendarProvider.php @@ -28,6 +28,7 @@ namespace OCA\DAV\CalDAV; use OCP\Calendar\ICalendarProvider; use OCP\IConfig; use OCP\IL10N; +use Psr\Log\LoggerInterface; class CalendarProvider implements ICalendarProvider { @@ -40,10 +41,14 @@ class CalendarProvider implements ICalendarProvider { /** @var IConfig */ private $config; - public function __construct(CalDavBackend $calDavBackend, IL10N $l10n, IConfig $config) { + /** @var LoggerInterface */ + private $logger; + + public function __construct(CalDavBackend $calDavBackend, IL10N $l10n, IConfig $config, LoggerInterface $logger) { $this->calDavBackend = $calDavBackend; $this->l10n = $l10n; $this->config = $config; + $this->logger = $logger; } public function getCalendars(string $principalUri, array $calendarUris = []): array { @@ -60,7 +65,7 @@ class CalendarProvider implements ICalendarProvider { $iCalendars = []; foreach ($calendarInfos as $calendarInfo) { - $calendar = new Calendar($this->calDavBackend, $calendarInfo, $this->l10n, $this->config); + $calendar = new Calendar($this->calDavBackend, $calendarInfo, $this->l10n, $this->config, $this->logger); $iCalendars[] = new CalendarImpl( $calendar, $calendarInfo, diff --git a/apps/dav/lib/CalDAV/CalendarRoot.php b/apps/dav/lib/CalDAV/CalendarRoot.php index 5b2bfa36027..e08264b8f59 100644 --- a/apps/dav/lib/CalDAV/CalendarRoot.php +++ b/apps/dav/lib/CalDAV/CalendarRoot.php @@ -25,9 +25,22 @@ */ namespace OCA\DAV\CalDAV; +use Psr\Log\LoggerInterface; +use Sabre\CalDAV\Backend; +use Sabre\DAVACL\PrincipalBackend; + class CalendarRoot extends \Sabre\CalDAV\CalendarRoot { + + /** @var LoggerInterface */ + private $logger; + + public function __construct(PrincipalBackend\BackendInterface $principalBackend, Backend\BackendInterface $caldavBackend, $principalPrefix = 'principals', LoggerInterface $logger) { + parent::__construct($principalBackend, $caldavBackend, $principalPrefix); + $this->logger = $logger; + } + public function getChildForPrincipal(array $principal) { - return new CalendarHome($this->caldavBackend, $principal); + return new CalendarHome($this->caldavBackend, $principal, $this->logger); } public function getName() { diff --git a/apps/dav/lib/CalDAV/PublicCalendarRoot.php b/apps/dav/lib/CalDAV/PublicCalendarRoot.php index aafbb48ae8e..4f7dfea2682 100644 --- a/apps/dav/lib/CalDAV/PublicCalendarRoot.php +++ b/apps/dav/lib/CalDAV/PublicCalendarRoot.php @@ -27,6 +27,7 @@ namespace OCA\DAV\CalDAV; use OCP\IConfig; use OCP\IL10N; +use Psr\Log\LoggerInterface; use Sabre\DAV\Collection; class PublicCalendarRoot extends Collection { @@ -40,6 +41,9 @@ class PublicCalendarRoot extends Collection { /** @var \OCP\IConfig */ protected $config; + /** @var LoggerInterface */ + private $logger; + /** * PublicCalendarRoot constructor. * @@ -48,10 +52,11 @@ class PublicCalendarRoot extends Collection { * @param IConfig $config */ public function __construct(CalDavBackend $caldavBackend, IL10N $l10n, - IConfig $config) { + IConfig $config, LoggerInterface $logger) { $this->caldavBackend = $caldavBackend; $this->l10n = $l10n; $this->config = $config; + $this->logger = $logger; } /** @@ -66,7 +71,7 @@ class PublicCalendarRoot extends Collection { */ public function getChild($name) { $calendar = $this->caldavBackend->getPublicCalendar($name); - return new PublicCalendar($this->caldavBackend, $calendar, $this->l10n, $this->config); + return new PublicCalendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger); } /** diff --git a/apps/dav/lib/Command/DeleteCalendar.php b/apps/dav/lib/Command/DeleteCalendar.php index 42aa0e3278f..dd5f11c740f 100644 --- a/apps/dav/lib/Command/DeleteCalendar.php +++ b/apps/dav/lib/Command/DeleteCalendar.php @@ -30,6 +30,7 @@ use OCA\DAV\CalDAV\Calendar; use OCP\IConfig; use OCP\IL10N; use OCP\IUserManager; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -49,6 +50,9 @@ class DeleteCalendar extends Command { /** @var IUserManager */ private $userManager; + /** @var LoggerInterface */ + private $logger; + /** * @param CalDavBackend $calDav * @param IConfig $config @@ -59,13 +63,15 @@ class DeleteCalendar extends Command { CalDavBackend $calDav, IConfig $config, IL10N $l10n, - IUserManager $userManager + IUserManager $userManager, + LoggerInterface $logger ) { parent::__construct(); $this->calDav = $calDav; $this->config = $config; $this->l10n = $l10n; $this->userManager = $userManager; + $this->logger = $logger; } protected function configure(): void { @@ -123,7 +129,9 @@ class DeleteCalendar extends Command { $this->calDav, $calendarInfo, $this->l10n, - $this->config); + $this->config, + $this->logger + ); $force = $input->getOption('force'); if ($force) { diff --git a/apps/dav/lib/Command/MoveCalendar.php b/apps/dav/lib/Command/MoveCalendar.php index e6cba3b3330..320fe8aeac6 100644 --- a/apps/dav/lib/Command/MoveCalendar.php +++ b/apps/dav/lib/Command/MoveCalendar.php @@ -33,6 +33,7 @@ use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserManager; use OCP\Share\IManager as IShareManager; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -63,6 +64,9 @@ class MoveCalendar extends Command { /** @var CalDavBackend */ private $calDav; + /** @var LoggerInterface */ + private $logger; + public const URI_USERS = 'principals/users/'; /** @@ -79,7 +83,8 @@ class MoveCalendar extends Command { IShareManager $shareManager, IConfig $config, IL10N $l10n, - CalDavBackend $calDav + CalDavBackend $calDav, + LoggerInterface $logger ) { parent::__construct(); $this->userManager = $userManager; @@ -88,6 +93,7 @@ class MoveCalendar extends Command { $this->config = $config; $this->l10n = $l10n; $this->calDav = $calDav; + $this->logger = $logger; } protected function configure() { @@ -218,7 +224,7 @@ class MoveCalendar extends Command { */ 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]); + $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['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."); } @@ -229,7 +235,7 @@ class MoveCalendar extends Command { */ if ($userOrGroup === $userDestination) { if ($force) { - $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/users/' . $userOrGroup]); + $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['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."); } diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index d307b5d2488..2e5952f6efd 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -47,6 +47,7 @@ use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; +use Psr\Log\LoggerInterface; use Sabre\DAV\SimpleCollection; class RootCollection extends SimpleCollection { @@ -54,6 +55,7 @@ class RootCollection extends SimpleCollection { $l10n = \OC::$server->getL10N('dav'); $random = \OC::$server->getSecureRandom(); $logger = \OC::$server->getLogger(); + $psrLogger = \OC::$server->get(LoggerInterface::class); $userManager = \OC::$server->getUserManager(); $userSession = \OC::$server->getUserSession(); $groupManager = \OC::$server->getGroupManager(); @@ -107,15 +109,15 @@ class RootCollection extends SimpleCollection { $legacyDispatcher, $config ); - $userCalendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users'); + $userCalendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users', $psrLogger); $userCalendarRoot->disableListing = $disableListing; - $resourceCalendarRoot = new CalendarRoot($calendarResourcePrincipalBackend, $caldavBackend, 'principals/calendar-resources'); + $resourceCalendarRoot = new CalendarRoot($calendarResourcePrincipalBackend, $caldavBackend, 'principals/calendar-resources', $psrLogger); $resourceCalendarRoot->disableListing = $disableListing; - $roomCalendarRoot = new CalendarRoot($calendarRoomPrincipalBackend, $caldavBackend, 'principals/calendar-rooms'); + $roomCalendarRoot = new CalendarRoot($calendarRoomPrincipalBackend, $caldavBackend, 'principals/calendar-rooms', $psrLogger); $roomCalendarRoot->disableListing = $disableListing; - $publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $l10n, $config); + $publicCalendarRoot = new PublicCalendarRoot($caldavBackend, $l10n, $config, $psrLogger); $publicCalendarRoot->disableListing = $disableListing; $systemTagCollection = new SystemTag\SystemTagsByIdCollection( |