Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize attendee status on create and specific changes #47308

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions apps/dav/lib/CalDAV/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@
*/
namespace OCA\DAV\CalDAV;

use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarObject;
use Sabre\DAV\INode;
use Sabre\DAV\Server;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\VObject\Reader;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\ElementList;
use Sabre\VObject\Property;

class Plugin extends \Sabre\CalDAV\Plugin {
public const SYSTEM_CALENDAR_ROOT = 'system-calendars';

/**
* Initializes the plugin
*
* @param Server $server
* @return void
*/
public function initialize(Server $server) {

parent::initialize($server);

$server->on('calendarObjectChange', [$this, 'calendarObjectChange'], 90);

}

/**
* Returns the path to a principal's calendar home.
*
Expand All @@ -34,4 +60,150 @@
return self::SYSTEM_CALENDAR_ROOT . '/calendar-rooms/' . $principalId;
}
}

/**
* @param RequestInterface $request
* @param ResponseInterface $response
* @param VCalendar $vCal
* @param mixed $calendarPath
* @param mixed $modified
* @param mixed $isNew
*/
public function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $alteredObject, $calendarPath, &$modified, $isNew) {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OCA\DAV\CalDAV\Plugin::calendarObjectChange does not have a return type, expecting void

// determine if the calendar has an event
// if there is no event there is nothing to do
if (!$alteredObject->VEVENT) {
return;
}
// determine if altered calendar event is a new
// if calendar event is new sanitize and exit
if ($isNew) {
$this->sanitizeCreatedInstance($alteredObject->VEVENT, $modified);

Check failure on line 82 in apps/dav/lib/CalDAV/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/dav/lib/CalDAV/Plugin.php:82:35: InvalidArgument: Argument 1 of OCA\DAV\CalDAV\Plugin::sanitizeCreatedInstance expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCA\DAV\CalDAV\Plugin::sanitizeCreatedInstance expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property provided
return;
}
// retrieve current calendar event node
/** @var \OCA\DAV\CalDAV\CalendarObject $currentNode */
$currentNode = $this->server->tree->getNodeForPath($request->getPath());
// convert calendar event string data to VCalendar object
/** @var \Sabre\VObject\Component\VCalendar $currentObject */
$currentObject = Reader::read($currentNode->get());
// find what has changed (base, recurrence, both) between altered and current calendar event
$delta = $this->findEventInstanceDelta($alteredObject->VEVENT, $currentObject->VEVENT);

Check failure on line 92 in apps/dav/lib/CalDAV/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/dav/lib/CalDAV/Plugin.php:92:42: InvalidArgument: Argument 1 of OCA\DAV\CalDAV\Plugin::findEventInstanceDelta expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property provided (see https://psalm.dev/004)

Check failure on line 92 in apps/dav/lib/CalDAV/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/dav/lib/CalDAV/Plugin.php:92:66: InvalidArgument: Argument 2 of OCA\DAV\CalDAV\Plugin::findEventInstanceDelta expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property|null provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of OCA\DAV\CalDAV\Plugin::findEventInstanceDelta expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property provided

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 2 of OCA\DAV\CalDAV\Plugin::findEventInstanceDelta expects Sabre\VObject\Component\VEvent, but Sabre\VObject\Property|null provided
//
foreach ($delta as $entry) {
// determine if this instance was created or updated
if ($entry['current'] !== null) {
$this->sanitizeUpdatedInstance($entry['altered'], $entry['current'], $modified);
} else {
$this->sanitizeCreatedInstance($entry['altered'], $modified);
}
}

}

public function sanitizeCreatedInstance(VEvent $altered, $modified): void {

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $modified has no provided type

// sanitize attendees
if (isset($altered->ATTENDEE)) {
$this->sanitizeEventAttendees($altered, $modified);
}

}

public function sanitizeUpdatedInstance(VEvent $altered, VEvent $current, $modified): void {

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $modified has no provided type

// find differences in properties
$delta = $this->findEventPropertyDelta($altered, $current, $modified);

Check failure on line 117 in apps/dav/lib/CalDAV/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TooManyArguments

apps/dav/lib/CalDAV/Plugin.php:117:19: TooManyArguments: Too many arguments for method OCA\DAV\CalDAV\Plugin::findeventpropertydelta - saw 3 (see https://psalm.dev/026)

Check failure

Code scanning / Psalm

TooManyArguments Error

Too many arguments for method OCA\DAV\CalDAV\Plugin::findeventpropertydelta - saw 3
// determine if any important properties have changed sanitize attendees
if (isset($delta['DTSTART']) || isset($delta['DTEND']) || isset($delta['LOCATION']) || isset($delta['RRULE'])) {
$this->sanitizeEventAttendees($altered, $modified);
}

}

public function sanitizeEventAttendees(VEvent $event, $modified): void {

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $modified has no provided type

// iterate thought attendees
foreach ($event->ATTENDEE as $id => $entry) {

Check notice

Code scanning / Psalm

PossiblyNullIterator Note

Cannot iterate over nullable var Sabre\VObject\Property|null
// determine attendee participation status
// if status is missing or NOT set correctly change the status
if (!isset($entry['PARTSTAT']) || $entry['PARTSTAT']->getValue() !== 'NEEDS-ACTION') {
$event->ATTENDEE[$id]['PARTSTAT']->setValue('NEEDS-ACTION');

Check failure on line 132 in apps/dav/lib/CalDAV/Plugin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/dav/lib/CalDAV/Plugin.php:132:27: InvalidArgument: Argument 1 of Sabre\VObject\Node::offsetGet expects int, but 'PARTSTAT' provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 1 of Sabre\VObject\Node::offsetGet expects int, but 'PARTSTAT' provided
$modified = true;
}
}

}

protected function findEventInstanceDelta(VEvent $altered, VEvent $current): array {

$list = [];
// iterate through altered event instances
foreach ($altered as $event) {
// create instance id
if (!isset($event->{'RECURRENCE-ID'})) {
$id = $event->UID->getValue() . ':Base';
} else {
$id = $event->UID->getValue() . ':' . $event->{'RECURRENCE-ID'}->getValue();
}
// add instance to list
$list[$id] = ['altered' => $event, 'current' => null];
}
// iterate through current event instances
foreach ($current as $event) {
// create instance id
if (!isset($event->{'RECURRENCE-ID'})) {
$id = $event->UID->getValue() . ':Base';
} else {
$id = $event->UID->getValue() . ':' . $event->{'RECURRENCE-ID'}->getValue();
}
// determine if id exists in list
if (isset($list[$id])) {
// compare altered instance to current instance
if ($list[$id]['altered']->{'LAST-MODIFIED'}->getValue() == $event->{'LAST-MODIFIED'}->getValue() &&

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method getValue on possibly null value
$list[$id]['altered']->SEQUENCE->getValue() == $event->SEQUENCE->getValue()) {

Check notice

Code scanning / Psalm

PossiblyNullPropertyFetch Note

Cannot get property on possibly null variable $list[$id]['altered'] of type mixed|null

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method getValue on possibly null value
// remove entry from list if instance has not changed
unset($list[$id]);
} else {
// update entry in list with current instance
$list[$id]['current'] = $event;
}
} else {
// add entry to list
$list[$id] = ['altered' => null, 'current' => $event];
}
}

return $list;

}

protected function findEventPropertyDelta(VEvent $altered, VEvent $current): array {

$list = [];
// iterate through altered event properties
foreach ($altered->children() as $property) {
// add property to list
$list[$property->name] = ['altered' => $property->getValue(), 'current' => null];
}
// iterate through altered event properties
foreach ($current->children() as $property) {
if (isset($list[$property->name])) {
if ($list[$property->name]['altered'] == $property->getValue()) {
// remove entry from list if instance has not changed
unset($list[$property->name]);
} else {
// update entry in list with current instance
$list[$property->name]['current'] = $property->getValue();
}
} else {
// add entry to list
$list[$property->name] = ['altered' => null, 'current' => $property->getValue()];
}
}

return $list;
}

}
Loading