Skip to content

Commit a017371

Browse files
fixup! fix(dav): apply stored additional properties to calendar rows
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent 266db1c commit a017371

2 files changed

Lines changed: 98 additions & 10 deletions

File tree

‎apps/dav/lib/CalDAV/CalDavBackend.php‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,15 +4019,17 @@ private function convertPrincipal($principalUri, $toV2) {
40194019
/**
40204020
* Adds the properties the user whose calendar home a calendar is in has stored on it
40214021
*
4022-
* Stored properties only fill in values the calendar does not provide itself. On calendars
4023-
* shared with the user, the properties a sharee can customize override the owner's values.
4022+
* Only allow-listed properties with a valid value are applied: the enabled state on all
4023+
* calendars, and on calendars shared with the user the properties a sharee can customize,
4024+
* which override the owner's values.
40244025
*
40254026
* @param array[] $calendars
40264027
* @return array[]
40274028
*/
40284029
private function addAdditionalPropertiesToCalendars(array $calendars): array {
40294030
$ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
4030-
4031+
$enabledKey = '{http://owncloud.org/ns}calendar-enabled';
4032+
40314033
$paths = [];
40324034
$calendarPaths = [];
40334035
foreach ($calendars as $key => $calendar) {
@@ -4045,20 +4047,35 @@ private function addAdditionalPropertiesToCalendars(array $calendars): array {
40454047

40464048
$properties = $this->customPropertiesService->getProperties($paths);
40474049
foreach ($calendarPaths as $key => [$userId, $path]) {
4048-
$storedProperties = $properties[$userId][$path] ?? [];
4049-
if ($storedProperties === []) {
4050-
continue;
4051-
}
4052-
$calendar = array_merge($storedProperties, $calendars[$key]);
4050+
$calendar = $calendars[$key];
4051+
$allowedProperties = [$enabledKey];
40534052
if (isset($calendar[$ownerPrincipalKey]) && $calendar[$ownerPrincipalKey] !== $calendar['principaluri']) {
4054-
$calendar = array_merge($calendar, array_intersect_key($storedProperties, array_flip(CustomPropertiesBackend::SHARED_CALENDAR_PROPERTIES)));
4053+
$allowedProperties = array_merge($allowedProperties, CustomPropertiesBackend::SHARED_CALENDAR_PROPERTIES);
4054+
}
4055+
foreach ($allowedProperties as $name) {
4056+
if (isset($properties[$userId][$path][$name]) && $this->isValidAdditionalProperty($name, $properties[$userId][$path][$name])) {
4057+
$calendar[$name] = $properties[$userId][$path][$name];
4058+
}
40554059
}
40564060
$calendars[$key] = $calendar;
40574061
}
40584062

40594063
return $calendars;
40604064
}
40614065

4066+
/**
4067+
* Checks that a stored calendar property has the type its consumers expect
4068+
*/
4069+
private function isValidAdditionalProperty(string $name, mixed $value): bool {
4070+
return match ($name) {
4071+
'{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp' => $value instanceof ScheduleCalendarTransp,
4072+
'{http://apple.com/ns/ical/}calendar-order' => is_numeric($value),
4073+
'{http://owncloud.org/ns}calendar-enabled',
4074+
'{http://nextcloud.com/ns}disable-alarm-notifications' => is_scalar($value),
4075+
default => is_string($value),
4076+
};
4077+
}
4078+
40624079
/**
40634080
* adds information about an owner to the calendar data
40644081
*

‎apps/dav/tests/unit/CalDAV/CalDavBackendTest.php‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Sabre\DAV\Exception\BadRequest;
3131
use Sabre\DAV\Exception\NotFound;
3232
use Sabre\DAV\PropPatch;
33+
use Sabre\DAV\Xml\Property\Complex;
3334
use Sabre\DAV\Xml\Property\Href;
3435
use Sabre\DAVACL\IACL;
3536
use function time;
@@ -1722,7 +1723,7 @@ public function testGetCalendarsAddsAdditionalProperties(): void {
17221723
$sharedPath = 'calendars/caldav-unit-test/calendar-uri-sharer_shared_by_caldav-unit-test1';
17231724
$ownPath = 'calendars/caldav-unit-test/calendar-uri-sharee';
17241725
try {
1725-
// a sharee can override the transparency, other properties only fill in missing values
1726+
// a sharee can override the transparency, read-only is never applied
17261727
$this->insertProperty('caldav-unit-test', $sharedPath, $transparencyKey, new ScheduleCalendarTransp('opaque'));
17271728
$this->insertProperty('caldav-unit-test', $sharedPath, $enabledKey, '0');
17281729
$this->insertProperty('caldav-unit-test', $sharedPath, $readOnlyKey, '0');
@@ -1767,6 +1768,76 @@ public function testGetCalendarsAddsAdditionalProperties(): void {
17671768
}
17681769
}
17691770

1771+
public function testGetCalendarsIgnoresUnlistedAndInvalidAdditionalProperties(): void {
1772+
$this->userManager->expects($this->any())
1773+
->method('userExists')
1774+
->willReturn(true);
1775+
$this->principal->expects(self::atLeastOnce())
1776+
->method('findByUri')
1777+
->willReturn(self::UNIT_TEST_USER);
1778+
1779+
$sharee = self::UNIT_TEST_USER;
1780+
$sharer = self::UNIT_TEST_USER1;
1781+
$ownerPrincipalKey = '{http://owncloud.org/ns}owner-principal';
1782+
$publicKey = '{http://owncloud.org/ns}public';
1783+
$displayNameKey = '{DAV:}displayname';
1784+
$colorKey = '{http://apple.com/ns/ical/}calendar-color';
1785+
1786+
$this->backend->createCalendar($sharee, 'calendar-uri-sharee', [
1787+
$displayNameKey => 'Own',
1788+
$colorKey => '#000000',
1789+
]);
1790+
$this->backend->createCalendar($sharer, 'calendar-uri-sharer', [
1791+
$displayNameKey => 'Shared',
1792+
$colorKey => '#000000',
1793+
]);
1794+
$sharerCalendar = new Calendar($this->backend, $this->backend->getCalendarsForUser($sharer)[0], $this->createMock(IL10N::class), $this->createMock(IConfig::class), new NullLogger());
1795+
$this->backend->updateShares($sharerCalendar, [
1796+
[
1797+
'href' => 'principal:' . $sharee,
1798+
'readOnly' => true,
1799+
],
1800+
], []);
1801+
1802+
$sharedPath = 'calendars/caldav-unit-test/calendar-uri-sharer_shared_by_caldav-unit-test1';
1803+
$ownPath = 'calendars/caldav-unit-test/calendar-uri-sharee';
1804+
try {
1805+
$this->insertProperty('caldav-unit-test', $ownPath, $ownerPrincipalKey, $sharer);
1806+
$this->insertProperty('caldav-unit-test', $ownPath, $publicKey, '1');
1807+
$this->insertProperty('caldav-unit-test', $ownPath, $displayNameKey, 'Stored');
1808+
$this->insertProperty('caldav-unit-test', $ownPath, $colorKey, '#ff0000');
1809+
$this->insertProperty('caldav-unit-test', $sharedPath, $publicKey, '1');
1810+
$this->insertProperty('caldav-unit-test', $sharedPath, $displayNameKey, new Complex('<x/>'));
1811+
$this->insertProperty('caldav-unit-test', $sharedPath, $colorKey, '#00ff00');
1812+
1813+
$ownCalendars = [
1814+
$this->backend->getCalendarByUri($sharee, 'calendar-uri-sharee'),
1815+
$this->backend->getUsersOwnCalendars($sharee)[0],
1816+
];
1817+
foreach ($this->backend->getCalendarsForUser($sharee) as $calendar) {
1818+
if ($calendar['uri'] === 'calendar-uri-sharee') {
1819+
$ownCalendars[] = $calendar;
1820+
} else {
1821+
$shared = $calendar;
1822+
}
1823+
}
1824+
1825+
$this->assertCount(3, $ownCalendars);
1826+
foreach ($ownCalendars as $own) {
1827+
$this->assertNotSame($sharer, $own[$ownerPrincipalKey] ?? null);
1828+
$this->assertArrayNotHasKey($publicKey, $own);
1829+
$this->assertSame('Own', $own[$displayNameKey]);
1830+
$this->assertSame('#000000', $own[$colorKey]);
1831+
}
1832+
1833+
$this->assertArrayNotHasKey($publicKey, $shared);
1834+
$this->assertIsString($shared[$displayNameKey]);
1835+
$this->assertSame('#00ff00', $shared[$colorKey]);
1836+
} finally {
1837+
$this->deleteProperties('caldav-unit-test', 'caldav-unit-test1');
1838+
}
1839+
}
1840+
17701841
/**
17711842
* @throws \OCP\DB\Exception
17721843
* @throws \Sabre\DAV\Exception\BadRequest

0 commit comments

Comments
 (0)