Skip to content

Commit

Permalink
Merge pull request #45800 from nextcloud/backport/45775/stable29
Browse files Browse the repository at this point in the history
[stable29] fix(caldav): event links in shared calendar notifications
  • Loading branch information
AndyScherzinger authored Jun 12, 2024
2 parents 2dd1c74 + a794f15 commit 095774b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
28 changes: 18 additions & 10 deletions apps/dav/lib/CalDAV/Activity/Provider/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(IFactory $languageFactory, IURLGenerator $url, IMana
* @param array $eventData
* @return array
*/
protected function generateObjectParameter(array $eventData) {
protected function generateObjectParameter(array $eventData, string $affectedUser): array {
if (!isset($eventData['id']) || !isset($eventData['name'])) {
throw new \InvalidArgumentException();
}
Expand All @@ -97,7 +97,15 @@ protected function generateObjectParameter(array $eventData) {
// The calendar app needs to be manually loaded for the routes to be loaded
OC_App::loadApp('calendar');
$linkData = $eventData['link'];
$objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $linkData['calendar_uri'] . '/' . $linkData['object_uri']);
if ($affectedUser === $linkData['owner']) {
$objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $linkData['calendar_uri'] . '/' . $linkData['object_uri']);
} else {
// Can't use the "real" owner and calendar names here because we create a custom
// calendar for incoming shares with the name "<calendar>_shared_by_<sharer>".
// Hack: Fix the link by generating it for the incoming shared calendar instead,
// as seen from the affected user.
$objectId = base64_encode($this->url->getWebroot() . '/remote.php/dav/calendars/' . $affectedUser . '/' . $linkData['calendar_uri'] . '_shared_by_' . $linkData['owner'] . '/' . $linkData['object_uri']);
}
$link = [
'view' => 'dayGridMonth',
'timeRange' => 'now',
Expand Down Expand Up @@ -189,7 +197,7 @@ protected function getParameters(IEvent $event) {
return [
'actor' => $this->generateUserParameter($parameters['actor']),
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_ADD . '_event_self':
case self::SUBJECT_OBJECT_DELETE . '_event_self':
Expand All @@ -198,7 +206,7 @@ protected function getParameters(IEvent $event) {
case self::SUBJECT_OBJECT_RESTORE . '_event_self':
return [
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
];
}
}
Expand All @@ -210,13 +218,13 @@ protected function getParameters(IEvent $event) {
'actor' => $this->generateUserParameter($parameters['actor']),
'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_MOVE . '_event_self':
return [
'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
'event' => $this->generateClassifiedObjectParameter($parameters['object'], $event->getAffectedUser()),
];
}
}
Expand All @@ -233,22 +241,22 @@ protected function getParameters(IEvent $event) {
return [
'actor' => $this->generateUserParameter($parameters[0]),
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
'event' => $this->generateObjectParameter($parameters[2]),
'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_ADD . '_event_self':
case self::SUBJECT_OBJECT_DELETE . '_event_self':
case self::SUBJECT_OBJECT_UPDATE . '_event_self':
return [
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
'event' => $this->generateObjectParameter($parameters[2]),
'event' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
];
}

throw new \InvalidArgumentException();
}

private function generateClassifiedObjectParameter(array $eventData) {
$parameter = $this->generateObjectParameter($eventData);
private function generateClassifiedObjectParameter(array $eventData, string $affectedUser): array {
$parameter = $this->generateObjectParameter($eventData, $affectedUser);
if (!empty($eventData['classified'])) {
$parameter['name'] = $this->l->t('Busy');
}
Expand Down
12 changes: 6 additions & 6 deletions apps/dav/lib/CalDAV/Activity/Provider/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function getParameters(IEvent $event) {
return [
'actor' => $this->generateUserParameter($parameters['actor']),
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
'todo' => $this->generateObjectParameter($parameters['object']),
'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_ADD . '_todo_self':
case self::SUBJECT_OBJECT_DELETE . '_todo_self':
Expand All @@ -113,7 +113,7 @@ protected function getParameters(IEvent $event) {
case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
return [
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
'todo' => $this->generateObjectParameter($parameters['object']),
'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
];
}
}
Expand All @@ -125,13 +125,13 @@ protected function getParameters(IEvent $event) {
'actor' => $this->generateUserParameter($parameters['actor']),
'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
'todo' => $this->generateObjectParameter($parameters['object']),
'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_MOVE . '_todo_self':
return [
'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
'todo' => $this->generateObjectParameter($parameters['object']),
'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
];
}
}
Expand All @@ -150,7 +150,7 @@ protected function getParameters(IEvent $event) {
return [
'actor' => $this->generateUserParameter($parameters[0]),
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
'todo' => $this->generateObjectParameter($parameters[2]),
'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
];
case self::SUBJECT_OBJECT_ADD . '_todo_self':
case self::SUBJECT_OBJECT_DELETE . '_todo_self':
Expand All @@ -159,7 +159,7 @@ protected function getParameters(IEvent $event) {
case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
return [
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
'todo' => $this->generateObjectParameter($parameters[2]),
'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
];
}

Expand Down
39 changes: 37 additions & 2 deletions apps/dav/tests/unit/CalDAV/Activity/Provider/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public function dataGenerateObjectParameter() {
* @param bool $calendarAppEnabled
*/
public function testGenerateObjectParameter(int $id, string $name, ?array $link, bool $calendarAppEnabled = true): void {
$affectedUser = 'otheruser';
if ($link) {
$affectedUser = $link['owner'];
$generatedLink = [
'view' => 'dayGridMonth',
'timeRange' => 'now',
Expand Down Expand Up @@ -141,7 +143,40 @@ public function testGenerateObjectParameter(int $id, string $name, ?array $link,
if ($link && $calendarAppEnabled) {
$result['link'] = 'fullLink';
}
$this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter]));
$this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, $affectedUser]));
}

public function testGenerateObjectParameterWithSharedCalendar(): void {
$link = [
'object_uri' => 'someuuid.ics',
'calendar_uri' => 'personal',
'owner' => 'sharer'
];
$generatedLink = [
'view' => 'dayGridMonth',
'timeRange' => 'now',
'mode' => 'sidebar',
'objectId' => base64_encode('/remote.php/dav/calendars/sharee/' . $link['calendar_uri'] . '_shared_by_sharer/' . $link['object_uri']),
'recurrenceId' => 'next'
];
$this->appManager->expects($this->once())
->method('isEnabledForUser')
->with('calendar')
->willReturn(true);
$this->url->expects($this->once())
->method('getWebroot');
$this->url->expects($this->once())
->method('linkToRouteAbsolute')
->with('calendar.view.indexview.timerange.edit', $generatedLink)
->willReturn('fullLink');
$objectParameter = ['id' => 42, 'name' => 'calendar', 'link' => $link];
$result = [
'type' => 'calendar-event',
'id' => 42,
'name' => 'calendar',
'link' => 'fullLink',
];
$this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, 'sharee']));
}

public function dataGenerateObjectParameterThrows() {
Expand All @@ -160,6 +195,6 @@ public function dataGenerateObjectParameterThrows() {
public function testGenerateObjectParameterThrows($eventData, string $exception = InvalidArgumentException::class): void {
$this->expectException($exception);

$this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData]);
$this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData, 'no_user']);
}
}

0 comments on commit 095774b

Please sign in to comment.