diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-06-28 15:33:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 15:33:18 +0200 |
commit | 41e6e0c646862e7d9958f202d994200ebc929047 (patch) | |
tree | 6b90dcc3d9dada9895c5e8166bec49efe1b51ff6 /apps/dav | |
parent | 64bff27c99fe387c79d5eecd19e549f61c3f8d4a (diff) | |
parent | f38e0600900ddecf9a3eeef86f66099ea9c3d88b (diff) | |
download | nextcloud-server-41e6e0c646862e7d9958f202d994200ebc929047.tar.gz nextcloud-server-41e6e0c646862e7d9958f202d994200ebc929047.zip |
Merge pull request #33007 from nextcloud/cleanup/federation-app
Summer cleanup of the federation app
Diffstat (limited to 'apps/dav')
-rw-r--r-- | apps/dav/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/dav/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | apps/dav/lib/AppInfo/Application.php | 15 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/CardDavBackend.php | 8 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/SyncService.php | 88 | ||||
-rw-r--r-- | apps/dav/lib/Listener/TrustedServerRemovedListener.php | 50 |
6 files changed, 74 insertions, 89 deletions
diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index c620de3fe4a..8c1bcf17516 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -246,6 +246,7 @@ return array( 'OCA\\DAV\\Listener\\CardListener' => $baseDir . '/../lib/Listener/CardListener.php', 'OCA\\DAV\\Listener\\ClearPhotoCacheListener' => $baseDir . '/../lib/Listener/ClearPhotoCacheListener.php', 'OCA\\DAV\\Listener\\SubscriptionListener' => $baseDir . '/../lib/Listener/SubscriptionListener.php', + 'OCA\\DAV\\Listener\\TrustedServerRemovedListener' => $baseDir . '/../lib/Listener/TrustedServerRemovedListener.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => $baseDir . '/../lib/Migration/BuildSocialSearchIndex.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 21f94cf71ce..29085a868de 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -261,6 +261,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Listener\\CardListener' => __DIR__ . '/..' . '/../lib/Listener/CardListener.php', 'OCA\\DAV\\Listener\\ClearPhotoCacheListener' => __DIR__ . '/..' . '/../lib/Listener/ClearPhotoCacheListener.php', 'OCA\\DAV\\Listener\\SubscriptionListener' => __DIR__ . '/..' . '/../lib/Listener/SubscriptionListener.php', + 'OCA\\DAV\\Listener\\TrustedServerRemovedListener' => __DIR__ . '/..' . '/../lib/Listener/TrustedServerRemovedListener.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndex.php', diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 10f0c52c79c..fe8405e09e2 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -71,6 +71,7 @@ use OCA\DAV\Events\CardDeletedEvent; use OCA\DAV\Events\CardUpdatedEvent; use OCA\DAV\Events\SubscriptionCreatedEvent; use OCA\DAV\Events\SubscriptionDeletedEvent; +use OCP\Federation\Events\TrustedServerRemovedEvent; use OCA\DAV\HookManager; use OCA\DAV\Listener\ActivityUpdaterListener; use OCA\DAV\Listener\AddressbookListener; @@ -83,6 +84,7 @@ use OCA\DAV\Listener\CalendarShareUpdateListener; use OCA\DAV\Listener\CardListener; use OCA\DAV\Listener\ClearPhotoCacheListener; use OCA\DAV\Listener\SubscriptionListener; +use OCA\DAV\Listener\TrustedServerRemovedListener; use OCA\DAV\Search\ContactsSearchProvider; use OCA\DAV\Search\EventsSearchProvider; use OCA\DAV\Search\TasksSearchProvider; @@ -182,6 +184,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class); $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class); $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class); + $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class); $context->registerNotifierService(Notifier::class); @@ -235,18 +238,6 @@ class Application extends App implements IBootstrap { // Here we should recalculate if reminders should be sent to new or old sharees }); - $dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove', - function (GenericEvent $event) { - /** @var CardDavBackend $cardDavBackend */ - $cardDavBackend = \OC::$server->query(CardDavBackend::class); - $addressBookUri = $event->getSubject(); - $addressBook = $cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri); - if (!is_null($addressBook)) { - $cardDavBackend->deleteAddressBook($addressBook['id']); - } - } - ); - $eventHandler = function () use ($container, $serverContainer): void { try { /** @var UpdateCalendarResourcesRoomsBackgroundJob $job */ diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index 2c99e6084c1..a26307b02a8 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -296,18 +296,14 @@ class CardDavBackend implements BackendInterface, SyncSupport { return $addressBook; } - /** - * @param $addressBookUri - * @return array|null - */ - public function getAddressBooksByUri($principal, $addressBookUri) { + public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { $query = $this->db->getQueryBuilder(); $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) ->from('addressbooks') ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) ->setMaxResults(1) - ->execute(); + ->executeQuery(); $row = $result->fetch(); $result->closeCursor(); diff --git a/apps/dav/lib/CardDAV/SyncService.php b/apps/dav/lib/CardDAV/SyncService.php index 169dbc79e0f..5094b7f3f5c 100644 --- a/apps/dav/lib/CardDAV/SyncService.php +++ b/apps/dav/lib/CardDAV/SyncService.php @@ -40,27 +40,13 @@ use Sabre\HTTP\ClientHttpException; use Sabre\VObject\Reader; class SyncService { - - /** @var CardDavBackend */ - private $backend; - - /** @var IUserManager */ - private $userManager; - + private CardDavBackend $backend; + private IUserManager $userManager; private LoggerInterface $logger; + private ?array $localSystemAddressBook = null; + private Converter $converter; + protected string $certPath; - /** @var array */ - private $localSystemAddressBook; - - /** @var Converter */ - private $converter; - - /** @var string */ - protected $certPath; - - /** - * SyncService constructor. - */ public function __construct(CardDavBackend $backend, IUserManager $userManager, LoggerInterface $logger, @@ -73,20 +59,11 @@ class SyncService { } /** - * @param string $url - * @param string $userName - * @param string $addressBookUrl - * @param string $sharedSecret - * @param string $syncToken - * @param int $targetBookId - * @param string $targetPrincipal - * @param array $targetProperties - * @return string * @throws \Exception */ - public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { + public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken, string $targetBookHash, string $targetPrincipal, array $targetProperties): string { // 1. create addressbook - $book = $this->ensureSystemAddressBookExists($targetPrincipal, (string)$targetBookId, $targetProperties); + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookHash, $targetProperties); $addressBookId = $book['id']; // 2. query changes @@ -122,28 +99,23 @@ class SyncService { } /** - * @param string $principal - * @param string $id - * @param array $properties - * @return array|null * @throws \Sabre\DAV\Exception\BadRequest */ - public function ensureSystemAddressBookExists($principal, $id, $properties) { - $book = $this->backend->getAddressBooksByUri($principal, $id); + public function ensureSystemAddressBookExists(string $principal, string $uri, array $properties): ?array { + $book = $this->backend->getAddressBooksByUri($principal, $uri); if (!is_null($book)) { return $book; } - $this->backend->createAddressBook($principal, $id, $properties); + // FIXME This might break in clustered DB setup + $this->backend->createAddressBook($principal, $uri, $properties); - return $this->backend->getAddressBooksByUri($principal, $id); + return $this->backend->getAddressBooksByUri($principal, $uri); } /** * Check if there is a valid certPath we should use - * - * @return string */ - protected function getCertPath() { + protected function getCertPath(): string { // we already have a valid certPath if ($this->certPath !== '') { @@ -159,14 +131,7 @@ class SyncService { return $this->certPath; } - /** - * @param string $url - * @param string $userName - * @param string $addressBookUrl - * @param string $sharedSecret - * @return Client - */ - protected function getClient($url, $userName, $sharedSecret) { + protected function getClient(string $url, string $userName, string $sharedSecret): Client { $settings = [ 'baseUri' => $url . '/', 'userName' => $userName, @@ -183,15 +148,7 @@ class SyncService { return $client; } - /** - * @param string $url - * @param string $userName - * @param string $addressBookUrl - * @param string $sharedSecret - * @param string $syncToken - * @return array - */ - protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { + protected function requestSyncReport(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken): array { $client = $this->getClient($url, $userName, $sharedSecret); $body = $this->buildSyncCollectionRequestBody($syncToken); @@ -203,23 +160,12 @@ class SyncService { return $this->parseMultiStatus($response['body']); } - /** - * @param string $url - * @param string $userName - * @param string $sharedSecret - * @param string $resourcePath - * @return array - */ - protected function download($url, $userName, $sharedSecret, $resourcePath) { + protected function download(string $url, string $userName, string $sharedSecret, string $resourcePath): array { $client = $this->getClient($url, $userName, $sharedSecret); return $client->request('GET', $resourcePath); } - /** - * @param string|null $syncToken - * @return string - */ - private function buildSyncCollectionRequestBody($syncToken) { + private function buildSyncCollectionRequestBody(?string $syncToken): string { $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $root = $dom->createElementNS('DAV:', 'd:sync-collection'); diff --git a/apps/dav/lib/Listener/TrustedServerRemovedListener.php b/apps/dav/lib/Listener/TrustedServerRemovedListener.php new file mode 100644 index 00000000000..29ff050983b --- /dev/null +++ b/apps/dav/lib/Listener/TrustedServerRemovedListener.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Carl Schwan <carl@carlschwan.eu> + * + * @author Carl Schwan <carl@carlschwan.eu> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\DAV\Listener; + +use OCA\DAV\CardDAV\CardDavBackend; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Federation\Events\TrustedServerRemovedEvent; + +class TrustedServerRemovedListener implements IEventListener { + private CardDavBackend $cardDavBackend; + + public function __construct(CardDavBackend $cardDavBackend) { + $this->cardDavBackend = $cardDavBackend; + } + + public function handle(Event $event): void { + if (!$event instanceof TrustedServerRemovedEvent) { + return; + } + $addressBookUri = $event->getUrlHash(); + $addressBook = $this->cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri); + if (!is_null($addressBook)) { + $this->cardDavBackend->deleteAddressBook($addressBook['id']); + } + } +} |