Skip to content

Commit

Permalink
Support iMIP invitations from Mail
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Larch <anna@nextcloud.com>
  • Loading branch information
miaulalala committed Jul 25, 2022
1 parent 0a956e2 commit 43f7bec
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 12 deletions.
62 changes: 62 additions & 0 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,68 @@ public function getCalendarObjectByUID($principalUri, $uid) {
return null;
}

/**
* Searches through all of a users calendars and calendar objects to find
* an object with a specific UID.
*
* This method should return the path to this object, relative to the
* calendar home, so this path usually only contains two parts:
*
* calendarpath/objectpath.ics
*
* If the uid is not found, return null.
*
* This method should only consider * objects that the principal owns, so
* any calendars owned by other principals that also appear in this
* collection should be ignored.
*
* @param string $principalUri
* @param string $uid
* @return string|null
*/
public function searchByUid(string $principalUri, string $uid): ?string {
$query = $this->db->getQueryBuilder();
$query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi')
->from('calendarobjects', 'co')
->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
->andWhere($query->expr()->isNull('co.deleted_at'));
$stmt = $query->executeQuery();
$row = $stmt->fetch();
$stmt->closeCursor();
if ($row) {
return $this->readBlob($row['calendardata']);
}

return null;
}

/**
* Searches through all of a users calendars and calendar objects to find
* an object with a specific UID and will return the complete object
*
* @param string $principalUri
* @param string $uid
* @return string|null
*/
public function searchPrincipalByUid(string $principalUri, string $uid): ?array {
$query = $this->db->getQueryBuilder();
$query->select('c.*', 'co.calendardata', 'co.calendarid', 'co.uid')
->from('calendarobjects', 'co')
->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
->andWhere($query->expr()->isNull('co.deleted_at'));
$stmt = $query->executeQuery();
$row = $stmt->fetch();
$stmt->closeCursor();
if ($row) {
return $row;
}
return null;
}

public function getCalendarObjectById(string $principalUri, int $id): ?array {
$query = $this->db->getQueryBuilder();
$query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at'])
Expand Down
5 changes: 4 additions & 1 deletion apps/dav/lib/CalDAV/CalendarHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ public function calendarSearch(array $filters, $limit = null, $offset = null) {
return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
}


public function enableCachedSubscriptionsForThisRequest() {
$this->returnCachedSubscriptions = true;
}

public function searchPrincipalByUid(string $principalUri, string $uid): ?array {
return $this->caldavBackend->searchPrincipalByUid($principalUri, $uid);
}
}
35 changes: 28 additions & 7 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@

use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Calendar\Exceptions\CalendarException;
use OCP\Calendar\ICreateFromString;
use OCP\Constants;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Conflict;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Document;
use Sabre\VObject\ITip\Message;
use Sabre\VObject\Property\VCard\DateTime;
use Sabre\VObject\Reader;
use function Sabre\Uri\split as uriSplit;

class CalendarImpl implements ICreateFromString {
Expand All @@ -46,13 +55,6 @@ class CalendarImpl implements ICreateFromString {
/** @var array */
private $calendarInfo;

/**
* CalendarImpl constructor.
*
* @param Calendar $calendar
* @param array $calendarInfo
* @param CalDavBackend $backend
*/
public function __construct(Calendar $calendar,
array $calendarInfo,
CalDavBackend $backend) {
Expand Down Expand Up @@ -177,4 +179,23 @@ public function createFromString(string $name, string $calendarData): void {
fclose($stream);
}
}

public function handleIMipMessage(Message $iTipMessage): void {
$server = new InvitationResponseServer(false);

/** @var CustomPrincipalPlugin $plugin */
$plugin = $server->server->getPlugin('auth');
// we're working around the previous implementation
// that only allowed the public system principal to be used
// so set the custom principal here
$plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());

if (empty($this->calendarInfo['uri'])) {
throw new CalendarException('Could not write to calendar as URI parameter is missing');
}
// Force calendar change URI
/** @var Schedule\Plugin $schedulingPlugin */
$schedulingPlugin = $server->server->getPlugin('caldav-schedule');
$schedulingPlugin->scheduleLocalDelivery($iTipMessage);
}
}
10 changes: 9 additions & 1 deletion apps/dav/lib/CalDAV/CalendarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\DAV\CalDAV;

use OCP\Calendar\ICalendarProvider;
use OCP\Calendar\ICreateFromString;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
Expand All @@ -51,6 +52,9 @@ public function __construct(CalDavBackend $calDavBackend, IL10N $l10n, IConfig $
$this->logger = $logger;
}

/**
* @return ICreateFromString[]
*/
public function getCalendars(string $principalUri, array $calendarUris = []): array {
$calendarInfos = [];
if (empty($calendarUris)) {
Expand All @@ -69,9 +73,13 @@ public function getCalendars(string $principalUri, array $calendarUris = []): ar
$iCalendars[] = new CalendarImpl(
$calendar,
$calendarInfo,
$this->calDavBackend,
$this->calDavBackend
);
}
return $iCalendars;
}

public function provideCalendarHome(string $principalUri): CalendarHome {
return new CalendarHome($this->calDavBackend, $principalUri, $this->logger);
}
}
2 changes: 2 additions & 0 deletions apps/dav/lib/Controller/InvitationResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IDBConnection;
Expand Down
Loading

0 comments on commit 43f7bec

Please sign in to comment.