Skip to content

Commit e09cff4

Browse files
committed
feat(CalDAV): Add function to check if calendar is publicly accessible with the provided token
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
1 parent 8c6d314 commit e09cff4

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

apps/dav/lib/CalDAV/CalendarImpl.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
use OCP\Calendar\Exceptions\CalendarException;
1717
use OCP\Calendar\ICalendarExport;
1818
use OCP\Calendar\ICalendarIsEnabled;
19+
use OCP\Calendar\ICalendarIsPublic;
1920
use OCP\Calendar\ICalendarIsShared;
2021
use OCP\Calendar\ICalendarIsWritable;
2122
use OCP\Calendar\ICreateFromString;
2223
use OCP\Calendar\IHandleImipMessage;
2324
use OCP\Constants;
2425
use Sabre\CalDAV\Xml\Property\ScheduleCalendarTransp;
2526
use Sabre\DAV\Exception\Conflict;
27+
use Sabre\DAV\Exception\NotFound;
2628
use Sabre\VObject\Component\VCalendar;
2729
use Sabre\VObject\Component\VTimeZone;
2830
use Sabre\VObject\ITip\Message;
@@ -32,7 +34,7 @@
3234

3335
use function Sabre\Uri\split as uriSplit;
3436

35-
class CalendarImpl implements ICreateFromString, IHandleImipMessage, ICalendarIsWritable, ICalendarIsShared, ICalendarExport, ICalendarIsEnabled {
37+
class CalendarImpl implements ICreateFromString, IHandleImipMessage, ICalendarIsWritable, ICalendarIsShared, ICalendarExport, ICalendarIsEnabled, ICalendarIsPublic {
3638
public function __construct(
3739
private Calendar $calendar,
3840
/** @var array<string, mixed> */
@@ -168,6 +170,19 @@ public function isShared(): bool {
168170
return $this->calendar->isShared();
169171
}
170172

173+
/**
174+
* @since 33.0.1, 32.0.7, 31.0.14.1, 30.0.17.8
175+
*/
176+
public function matchesToken(string $token): bool {
177+
try {
178+
$this->backend->getPublicCalendar($token);
179+
180+
return true;
181+
} catch (NotFound) {
182+
return false;
183+
}
184+
}
185+
171186
/**
172187
* @throws CalendarException
173188
*/
@@ -336,5 +351,4 @@ public function export(?CalendarExportOptions $options = null): Generator {
336351
}
337352
}
338353
}
339-
340354
}

apps/dav/tests/unit/CalDAV/CalendarImplTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function setUp(): void {
3333
$this->backend = $this->createMock(CalDavBackend::class);
3434
$this->calendar = $this->createMock(Calendar::class);
3535
$this->calendarInfo = [
36-
'id' => 'fancy_id_123',
36+
'id' => 123,
3737
'{DAV:}displayname' => 'user readable name 123',
3838
'{http://apple.com/ns/ical/}calendar-color' => '#AABBCC',
3939
'uri' => '/this/is/a/uri',
@@ -62,7 +62,7 @@ protected function setUp(): void {
6262

6363

6464
public function testGetKey(): void {
65-
$this->assertEquals($this->calendarImpl->getKey(), 'fancy_id_123');
65+
$this->assertEquals($this->calendarImpl->getKey(), '123');
6666
}
6767

6868
public function testGetDisplayname(): void {
@@ -73,6 +73,28 @@ public function testGetDisplayColor(): void {
7373
$this->assertEquals($this->calendarImpl->getDisplayColor(), '#AABBCC');
7474
}
7575

76+
public function testMatchesTokenWithValidToken(): void {
77+
$testToken = 'abc123';
78+
79+
$this->backend->expects($this->once())
80+
->method('getPublicCalendar')
81+
->with($testToken)
82+
->willReturn(true);
83+
84+
$this->assertTrue($this->calendarImpl->matchesToken($testToken));
85+
}
86+
87+
public function testMatchesTokenWithInvalidToken(): void {
88+
$testToken = 'abc123';
89+
90+
$this->backend->expects($this->once())
91+
->method('getPublicCalendar')
92+
->with($testToken)
93+
->willReturn(false);
94+
95+
$this->assertTrue($this->calendarImpl->matchesToken($testToken));
96+
}
97+
7698
public function testSearch(): void {
7799
$this->backend->expects($this->once())
78100
->method('search')
@@ -266,5 +288,4 @@ public function getHrefs(): array {
266288

267289
$calendarImpl->handleIMipMessage('fakeUser', $vObject->serialize());
268290
}
269-
270291
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
'OCP\\Calendar\\ICalendarEventBuilder' => $baseDir . '/lib/public/Calendar/ICalendarEventBuilder.php',
223223
'OCP\\Calendar\\ICalendarExport' => $baseDir . '/lib/public/Calendar/ICalendarExport.php',
224224
'OCP\\Calendar\\ICalendarIsEnabled' => $baseDir . '/lib/public/Calendar/ICalendarIsEnabled.php',
225+
'OCP\\Calendar\\ICalendarIsPublic' => $baseDir . '/lib/public/Calendar/ICalendarIsPublic.php',
225226
'OCP\\Calendar\\ICalendarIsShared' => $baseDir . '/lib/public/Calendar/ICalendarIsShared.php',
226227
'OCP\\Calendar\\ICalendarIsWritable' => $baseDir . '/lib/public/Calendar/ICalendarIsWritable.php',
227228
'OCP\\Calendar\\ICalendarProvider' => $baseDir . '/lib/public/Calendar/ICalendarProvider.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
263263
'OCP\\Calendar\\ICalendarEventBuilder' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarEventBuilder.php',
264264
'OCP\\Calendar\\ICalendarExport' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarExport.php',
265265
'OCP\\Calendar\\ICalendarIsEnabled' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarIsEnabled.php',
266+
'OCP\\Calendar\\ICalendarIsPublic' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarIsPublic.php',
266267
'OCP\\Calendar\\ICalendarIsShared' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarIsShared.php',
267268
'OCP\\Calendar\\ICalendarIsWritable' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarIsWritable.php',
268269
'OCP\\Calendar\\ICalendarProvider' => __DIR__ . '/../../..' . '/lib/public/Calendar/ICalendarProvider.php',
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
namespace OCP\Calendar;
10+
11+
/**
12+
* @since 33.0.1, 32.0.7, 31.0.14.1, 30.0.17.8
13+
*/
14+
interface ICalendarIsPublic {
15+
/**
16+
* Indicates whether the calendar is public
17+
* and accessible via the provided token.
18+
*
19+
* @since 33.0.1, 32.0.7, 31.0.14.1, 30.0.17.8
20+
*/
21+
public function matchesToken(string $token): bool;
22+
}

0 commit comments

Comments
 (0)