|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\CalDAV\Federation; |
| 11 | + |
| 12 | +use OCA\DAV\AppInfo\Application; |
| 13 | +use OCA\DAV\BackgroundJob\FederatedCalendarSyncJob; |
| 14 | +use OCA\Federation\TrustedServers; |
| 15 | +use OCP\App\IAppManager; |
| 16 | +use OCP\BackgroundJob\IJobList; |
| 17 | +use OCP\Federation\ICloudIdManager; |
| 18 | +use OCP\IURLGenerator; |
| 19 | +use OCP\Notification\IManager as INotificationManager; |
| 20 | +use OCP\Server; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +class FederatedCalendarInvitationService { |
| 24 | + public const NOTIFICATION_OBJECT_TYPE = 'federated_calendar_share'; |
| 25 | + public const NOTIFICATION_SUBJECT_NEW_SHARE = 'federated_calendar_share'; |
| 26 | + |
| 27 | + private const PRINCIPAL_PREFIX = 'principals/users/'; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly FederatedCalendarMapper $mapper, |
| 31 | + private readonly IJobList $jobList, |
| 32 | + private readonly INotificationManager $notificationManager, |
| 33 | + private readonly IURLGenerator $url, |
| 34 | + private readonly ICloudIdManager $cloudIdManager, |
| 35 | + private readonly CalendarFederationConfig $config, |
| 36 | + private readonly IAppManager $appManager, |
| 37 | + private readonly LoggerInterface $logger, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function accept(FederatedCalendarEntity $calendar): void { |
| 42 | + $calendar->setState(FederatedCalendarEntity::STATE_ACCEPTED); |
| 43 | + $this->mapper->update($calendar); |
| 44 | + |
| 45 | + $this->jobList->add(FederatedCalendarSyncJob::class, [ |
| 46 | + FederatedCalendarSyncJob::ARGUMENT_ID => $calendar->getId(), |
| 47 | + ]); |
| 48 | + |
| 49 | + $this->dismissNotification($calendar); |
| 50 | + } |
| 51 | + |
| 52 | + public function decline(FederatedCalendarEntity $calendar): void { |
| 53 | + // Dismiss first: a leftover bell notification would offer actions for |
| 54 | + // a share that no longer exists |
| 55 | + $this->dismissNotification($calendar); |
| 56 | + $this->mapper->deleteById($calendar->getId()); |
| 57 | + } |
| 58 | + |
| 59 | + public function notifyAboutNewShare(FederatedCalendarEntity $calendar): void { |
| 60 | + $notification = $this->notificationManager->createNotification(); |
| 61 | + $notification->setApp(Application::APP_ID) |
| 62 | + ->setUser($this->getUserId($calendar)) |
| 63 | + ->setDateTime(new \DateTime()) |
| 64 | + ->setObject(self::NOTIFICATION_OBJECT_TYPE, (string)$calendar->getId()) |
| 65 | + ->setSubject(self::NOTIFICATION_SUBJECT_NEW_SHARE, [ |
| 66 | + 'sharedBy' => $calendar->getSharedBy(), |
| 67 | + 'sharedByDisplayName' => $calendar->getSharedByDisplayName(), |
| 68 | + 'calendarName' => $calendar->getDisplayName(), |
| 69 | + ]); |
| 70 | + |
| 71 | + $endpointUrl = $this->url->linkToOCSRouteAbsolute('dav.federated_calendar.accept', [ |
| 72 | + 'id' => $calendar->getId(), |
| 73 | + ]); |
| 74 | + |
| 75 | + $declineAction = $notification->createAction(); |
| 76 | + $declineAction->setLabel('decline') |
| 77 | + ->setLink($endpointUrl, 'DELETE'); |
| 78 | + $notification->addAction($declineAction); |
| 79 | + |
| 80 | + $acceptAction = $notification->createAction(); |
| 81 | + $acceptAction->setLabel('accept') |
| 82 | + ->setLink($endpointUrl, 'POST'); |
| 83 | + $notification->addAction($acceptAction); |
| 84 | + |
| 85 | + $this->notificationManager->notify($notification); |
| 86 | + } |
| 87 | + |
| 88 | + public function dismissNotification(FederatedCalendarEntity $calendar): void { |
| 89 | + $notification = $this->notificationManager->createNotification(); |
| 90 | + $notification->setApp(Application::APP_ID) |
| 91 | + ->setUser($this->getUserId($calendar)) |
| 92 | + ->setObject(self::NOTIFICATION_OBJECT_TYPE, (string)$calendar->getId()); |
| 93 | + $this->notificationManager->markProcessed($notification); |
| 94 | + } |
| 95 | + |
| 96 | + public function shouldAutoAccept(?string $ownerCloudId, string $remoteUrl): bool { |
| 97 | + if ($ownerCloudId === null || $ownerCloudId === '') { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + if (!$this->config->isTrustedShareAutoAcceptEnabled()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + $trustedServers = $this->getTrustedServers(); |
| 106 | + if ($trustedServers === null) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + $remote = $this->cloudIdManager->resolveCloudId($ownerCloudId)->getRemote(); |
| 112 | + } catch (\InvalidArgumentException) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + if (!$trustedServers->isTrustedServer($remote)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + return $this->isSameHost($remote, $remoteUrl); |
| 121 | + } |
| 122 | + |
| 123 | + private function isSameHost(string $remote, string $remoteUrl): bool { |
| 124 | + $remoteParts = parse_url(str_contains($remote, '://') ? $remote : '//' . $remote); |
| 125 | + $urlParts = parse_url($remoteUrl); |
| 126 | + if (!is_array($remoteParts) || !is_array($urlParts) |
| 127 | + || !isset($remoteParts['host'], $urlParts['host'])) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + return strcasecmp($remoteParts['host'], $urlParts['host']) === 0 |
| 132 | + && ($remoteParts['port'] ?? null) === ($urlParts['port'] ?? null); |
| 133 | + } |
| 134 | + |
| 135 | + protected function getTrustedServers(): ?TrustedServers { |
| 136 | + if (!$this->appManager->isEnabledForAnyone('federation') |
| 137 | + || !class_exists(TrustedServers::class)) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + try { |
| 142 | + return Server::get(TrustedServers::class); |
| 143 | + } catch (\Throwable $e) { |
| 144 | + $this->logger->debug('Failed to create TrustedServers', ['exception' => $e]); |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private function getUserId(FederatedCalendarEntity $calendar): string { |
| 150 | + return substr($calendar->getPrincipaluri(), strlen(self::PRINCIPAL_PREFIX)); |
| 151 | + } |
| 152 | +} |
0 commit comments