Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 50 additions & 10 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ public function getCalendarsForUser($principalUri) {
->join('s', 'calendars', 'a', $select->expr()->eq('s.resourceid', 'a.id', IQueryBuilder::PARAM_INT))
->where($select->expr()->in('s.principaluri', $select->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY))
->andWhere($select->expr()->eq('s.type', $select->createNamedParameter('calendar', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR))
->andWhere($select->expr()->notIn('a.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY));
->andWhere($select->expr()->notIn('a.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
// calendars in the owner's trash bin are not available to sharees
->andWhere($select->expr()->isNull('a.deleted_at'));

$results = $select->executeQuery();

Expand Down Expand Up @@ -529,6 +531,7 @@ public function getPublicCalendars() {
->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
->andWhere($query->expr()->isNull('a.deleted_at'))
->executeQuery();

while ($row = $result->fetchAssociative()) {
Expand Down Expand Up @@ -587,6 +590,7 @@ public function getPublicCalendar($uri) {
->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri)))
->andWhere($query->expr()->isNull('a.deleted_at'))
->executeQuery();

$row = $result->fetchAssociative();
Expand Down Expand Up @@ -948,7 +952,9 @@ public function deleteCalendar($calendarId, bool $forceDeletePermanently = false
$calendarData = $this->getCalendarById($calendarId);
$shares = $this->getShares($calendarId);

$this->purgeCalendarInvitations($calendarId);
if ($calendarData !== null) {
$this->purgeCalendarInvitations($calendarId, $calendarData['principaluri']);
}

$qbDeleteCalendarObjectProps = $this->db->getQueryBuilder();
$qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable)
Expand Down Expand Up @@ -1557,6 +1563,10 @@ public function createCalendarObject($calendarId, $objectUri, $calendarData, $ca
// RFC 4791 no-uid-conflict (409) reporting the existing object's href.
throw UidConflict::forCalendar($existing['uri']);
}
// The URI may still belong to a trashed object, which is not served as a child of the calendar
if ($this->getCalendarObject($calendarId, $objectUri, $calendarType) !== null) {
throw new Forbidden("A calendar object with URI $objectUri already exists in calendar $calendarId");
}
// The UID may still belong to a trashed object; delete it and replace it with the new object.
$found = $this->findCalendarObjectByUid($calendarId, $extraData['uid'], $calendarType, true);
if ($found !== null) {
Expand Down Expand Up @@ -1785,12 +1795,12 @@ public function deleteCalendarObject($calendarId, $objectUri, $calendarType = se

$this->purgeProperties($calendarId, $data['id']);

$this->purgeObjectInvitations($data['uid']);

if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
$calendarRow = $this->getCalendarById($calendarId);
$shares = $this->getShares($calendarId);

$this->purgeObjectInvitations($data['uid'], $calendarRow['principaluri']);

$this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data));
} else {
$subscriptionRow = $this->getSubscriptionById($calendarId);
Expand Down Expand Up @@ -2804,6 +2814,7 @@ private function findDeletedCalendarObjectForPrincipal(int $id, string $principa
->join('co', 'calendars', 'c', $select->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT))
->andWhere($select->expr()->eq('co.id', $select->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT))
->andWhere($select->expr()->isNotNull('co.deleted_at'))
->andWhere($select->expr()->isNull('c.deleted_at'))
->orderBy('s.access', 'ASC');
$this->applySharedCalendarFilters($select, $principals, $principalUri);

Expand Down Expand Up @@ -4073,20 +4084,27 @@ private function rowToSubscription($row, array $subscription): array {
* @since 31.0.0
*
* @param int $calendarId
* @param string $principalUri owner of the calendar
*
* @return void
*/
protected function purgeCalendarInvitations(int $calendarId): void {
protected function purgeCalendarInvitations(int $calendarId, string $principalUri): void {
$organizer = $this->getInvitationsOrganizer($principalUri);
if ($organizer === null) {
return;
}
// select all calendar object uid's
$cmd = $this->db->getQueryBuilder();
$cmd->select('uid')
->from($this->dbObjectsTable)
->where($cmd->expr()->eq('calendarid', $cmd->createNamedParameter($calendarId)));
->where($cmd->expr()->eq('calendarid', $cmd->createNamedParameter($calendarId)))
->andWhere($cmd->expr()->eq('calendartype', $cmd->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
$allIds = $cmd->executeQuery()->fetchFirstColumn();
// delete all links that match object uid's
// delete all links that match object uid's and were sent by the calendar owner
$cmd = $this->db->getQueryBuilder();
$cmd->delete($this->dbObjectInvitationsTable)
->where($cmd->expr()->in('uid', $cmd->createParameter('uids'), IQueryBuilder::PARAM_STR_ARRAY));
->where($cmd->expr()->in('uid', $cmd->createParameter('uids'), IQueryBuilder::PARAM_STR_ARRAY))
->andWhere($cmd->expr()->eq('organizer', $cmd->createNamedParameter($organizer, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));
foreach (array_chunk($allIds, 1000) as $chunkIds) {
$cmd->setParameter('uids', $chunkIds, IQueryBuilder::PARAM_STR_ARRAY);
$cmd->executeStatement();
Expand All @@ -4099,16 +4117,38 @@ protected function purgeCalendarInvitations(int $calendarId): void {
* @since 31.0.0
*
* @param string $eventId UID of the event
* @param string $principalUri owner of the calendar the event is deleted from
*
* @return void
*/
protected function purgeObjectInvitations(string $eventId): void {
protected function purgeObjectInvitations(string $eventId, string $principalUri): void {
$organizer = $this->getInvitationsOrganizer($principalUri);
if ($organizer === null) {
return;
}
$cmd = $this->db->getQueryBuilder();
$cmd->delete($this->dbObjectInvitationsTable)
->where($cmd->expr()->eq('uid', $cmd->createNamedParameter($eventId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));
->where($cmd->expr()->eq('uid', $cmd->createNamedParameter($eventId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR))
->andWhere($cmd->expr()->eq('organizer', $cmd->createNamedParameter($organizer, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));
$cmd->executeStatement();
}

/**
* UIDs are shared by every attendee's copy of an event, so invitations are attributed
* by organizer. Like Principal::findByUri(), an address of several accounts belongs to none.
*/
private function getInvitationsOrganizer(string $principalUri): ?string {
$principalInformation = $this->principalBackend->getPrincipalPropertiesByPath($principalUri, [
'{http://sabredav.org/ns}email-address',
]);
$email = $principalInformation['{http://sabredav.org/ns}email-address'] ?? null;
if ($email === null || count($this->userManager->getByEmail($email)) !== 1) {
return null;
}
// the iTip broker stores the normalized, lower-cased mailto: address
return 'mailto:' . strtolower($email);
}

public function unshare(IShareable $shareable, string $principal): void {
$this->atomic(function () use ($shareable, $principal): void {
$calendarData = $this->getCalendarById($shareable->getResourceId());
Expand Down
8 changes: 6 additions & 2 deletions apps/dav/lib/CalDAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function propPatch(PropPatch $propPatch) {
public function getChild($name) {
$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name, $this->getCalendarType());

if (!$obj) {
if (!$obj || isset($obj[TrashbinPlugin::PROPERTY_DELETED_AT])) {
throw new NotFound('Calendar object not found');
}

Expand Down Expand Up @@ -330,7 +330,7 @@ public function getMultipleChildren(array $paths) {
#[\Override]
public function childExists($name) {
$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name, $this->getCalendarType());
if (!$obj) {
if (!$obj || isset($obj[TrashbinPlugin::PROPERTY_DELETED_AT])) {
return false;
}
if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
Expand Down Expand Up @@ -417,9 +417,13 @@ public function getChanges($syncToken, $syncLevel, $limit = null) {

/**
* @inheritDoc
* @throws Forbidden
*/
#[\Override]
public function restore(): void {
if ($this->isShared()) {
throw new Forbidden('Only the owner can restore a deleted calendar');
}
$this->caldavBackend->restoreCalendar((int)$this->calendarInfo['id']);
}

Expand Down
5 changes: 3 additions & 2 deletions apps/dav/lib/CalDAV/Federation/FederatedCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\DAV\CalDAV\Federation;

use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Trashbin\Plugin as TrashbinPlugin;
use OCP\Constants;
use Sabre\CalDAV\ICalendar;
use Sabre\CalDAV\Plugin;
Expand Down Expand Up @@ -215,7 +216,7 @@ public function calendarQuery(array $filters): array {
public function getChild($name): INode {
$obj = $this->caldavBackend->getCalendarObject($this->federationInfo->getId(), $name, $this->getCalendarType());

if ($obj === null) {
if ($obj === null || isset($obj[TrashbinPlugin::PROPERTY_DELETED_AT])) {
throw new NotFound('Calendar object not found');
}

Expand Down Expand Up @@ -260,7 +261,7 @@ public function getMultipleChildren(array $paths): array {
#[\Override]
public function childExists($name): bool {
$obj = $this->caldavBackend->getCalendarObject($this->federationInfo->getId(), $name, $this->getCalendarType());
return $obj !== null;
return $obj !== null && !isset($obj[TrashbinPlugin::PROPERTY_DELETED_AT]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/CalDAV/PublicCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace OCA\DAV\CalDAV;

use OCA\DAV\CalDAV\Trashbin\Plugin as TrashbinPlugin;
use Sabre\DAV\Exception\NotFound;

class PublicCalendar extends Calendar {
Expand All @@ -20,7 +21,7 @@ class PublicCalendar extends Calendar {
public function getChild($name) {
$obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);

if (!$obj) {
if (!$obj || isset($obj[TrashbinPlugin::PROPERTY_DELETED_AT])) {
throw new NotFound('Calendar object not found');
}
if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
Expand Down
9 changes: 7 additions & 2 deletions apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function schedule(Message $iTipMessage) {
return;
}

if (strcasecmp($iTipMessage->method, self::METHOD_CANCEL) === 0) {
$this->imipService->deleteInvitationTokens($iTipMessage);
}

// don't send out mails for events that already took place
$lastOccurrence = $this->imipService->getLastOccurrence($iTipMessage->message);
$currentTime = $this->timeFactory->getTime();
Expand Down Expand Up @@ -400,7 +404,7 @@ private function getSenderNameFor(string $sender): ?string {
}

/**
* Profile email addresses are part of the user's calendar-user-address-set
* Verified profile email addresses are part of the user's calendar-user-address-set
* and therefore valid sender addresses next to the system email address.
*/
private function isAddressOfUser(string $address, IUser $user): bool {
Expand All @@ -411,7 +415,8 @@ private function isAddressOfUser(string $address, IUser $user): bool {
$emailCollection = $this->accountManager->getAccount($user)
->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
foreach ($emailCollection->getProperties() as $property) {
if (strcasecmp($property->getValue(), $address) === 0) {
if ($property->getLocallyVerified() === IAccountManager::VERIFIED
&& strcasecmp($property->getValue(), $address) === 0) {
return true;
}
}
Expand Down
30 changes: 30 additions & 0 deletions apps/dav/lib/CalDAV/Schedule/IMipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\DAV\CalDAV\EventReader;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Config\IUserConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\IL10N;
Expand Down Expand Up @@ -1259,6 +1260,35 @@ public function createInvitationToken(Message $iTipMessage, VEvent $vevent, int
return $token;
}

/**
* Delete the invitation tokens a CANCEL message revokes: all tokens of the
* recipient for the event if the series is cancelled, otherwise only those
* for the cancelled occurrences
*/
public function deleteInvitationTokens(Message $iTipMessage): void {
$recurrenceIds = [];
foreach ($this->eventInstances($iTipMessage->message) as $vEvent) {
if (!isset($vEvent->{'RECURRENCE-ID'})) {
$recurrenceIds = null;
break;
}
$recurrenceIds[] = $vEvent->{'RECURRENCE-ID'}->serialize();
}
if ($recurrenceIds === []) {
return;
}

$query = $this->db->getQueryBuilder();
$query->delete('calendar_invitations')
->where($query->expr()->eq('uid', $query->createNamedParameter($iTipMessage->uid)))
->andWhere($query->expr()->eq('attendee', $query->createNamedParameter($iTipMessage->recipient)))
->andWhere($query->expr()->eq('organizer', $query->createNamedParameter($iTipMessage->sender)));
if ($recurrenceIds !== null) {
$query->andWhere($query->expr()->in('recurrenceid', $query->createNamedParameter($recurrenceIds, IQueryBuilder::PARAM_STR_ARRAY)));
}
$query->executeStatement();
}

/**
* @param IEMailTemplate $template
* @param $token
Expand Down
6 changes: 5 additions & 1 deletion apps/dav/lib/CalDAV/Sharing/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use OCP\ICacheFactory;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager;
use Psr\Log\LoggerInterface;

class Backend extends SharingBackend {
Expand All @@ -28,7 +30,9 @@ public function __construct(
private Service $service,
private FederationSharingService $federationSharingService,
private LoggerInterface $logger,
private IShareManager $shareManager,
private IUserSession $userSession,
) {
parent::__construct($this->userManager, $this->groupManager, $this->principalBackend, $this->remoteUserPrincipalBackend, $this->cacheFactory, $this->service, $this->federationSharingService, $this->logger);
parent::__construct($this->userManager, $this->groupManager, $this->principalBackend, $this->remoteUserPrincipalBackend, $this->cacheFactory, $this->service, $this->federationSharingService, $this->logger, $this->shareManager, $this->userSession);
}
}
4 changes: 4 additions & 0 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function getAddressBooksForUser($principalUri) {
if ($row['principaluri'] === $principalUri) {
continue;
}
// system address books can not be shared, ignore rows stored before that was enforced
if ($row['principaluri'] === 'principals/system/system') {
continue;
}

$readOnly = (int)$row['access'] === Backend::ACCESS_READ;
if (isset($addressBooks[$row['id']])) {
Expand Down
6 changes: 5 additions & 1 deletion apps/dav/lib/CardDAV/Sharing/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use OCP\ICacheFactory;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager;
use Psr\Log\LoggerInterface;

class Backend extends SharingBackend {
Expand All @@ -27,7 +29,9 @@ public function __construct(
private Service $service,
private FederationSharingService $federationSharingService,
private LoggerInterface $logger,
private IShareManager $shareManager,
private IUserSession $userSession,
) {
parent::__construct($this->userManager, $this->groupManager, $this->principalBackend, $this->remoteUserPrincipalBackend, $this->cacheFactory, $this->service, $this->federationSharingService, $this->logger);
parent::__construct($this->userManager, $this->groupManager, $this->principalBackend, $this->remoteUserPrincipalBackend, $this->cacheFactory, $this->service, $this->federationSharingService, $this->logger, $this->shareManager, $this->userSession);
}
}
Loading
Loading