Files
Ghassen kefi 2be108cf79 feat(dav): log calendar and address book sharing and lifecycle
CalDavBackend and CardDavBackend write calendar and address book
creation, trashing, deletion, restore, publishing and share changes to
the audit log through CriticalActionPerformedEvent where the change is
made. The generic dav and calendar events can be dispatched by other
apps as well, so they are not used as the source of these entries.
The public link of a published calendar is not logged.

Calendar export, accepting and declining federated calendars, creating
direct download links and enabling or disabling birthday calendars are
logged where they happen. Direct link tokens are never logged.

Granting and revoking calendar delegation (read or write access to all
calendars of a user, set through the calendar-proxy-read and
calendar-proxy-write principals) is logged once per delegate whose
access actually changed. Setting the same access again is not logged.

Events and contacts created, updated, deleted, moved to the trash,
restored or moved to another calendar or address book are logged with
the object URI, never their content. The birthday calendar and the
system address book are skipped, as the server generates them from
contacts and user accounts; subscribed calendars are not logged either.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
2026-11-05 16:34:43 +01:00

125 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Controller;
use NCU\AppFramework\Http\Attribute\TokenScope;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\CalDAV\Export\ExportService;
use OCA\DAV\TokenScopes;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\StreamTraversableResponse;
use OCP\AppFramework\OCSController;
use OCP\Calendar\CalendarExportOptions;
use OCP\Calendar\ICalendarExport;
use OCP\Calendar\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Log\Audit\CriticalActionPerformedEvent;
class CalendarExportController extends OCSController {
public function __construct(
IRequest $request,
private IUserSession $userSession,
private IUserManager $userManager,
private IGroupManager $groupManager,
private IManager $calendarManager,
private ExportService $exportService,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct(Application::APP_ID, $request);
}
/**
* Export calendar data
*
* @param string $target calendar id
* @param string|null $type data format
* @param array{rangeStart:string,rangeCount:positive-int} $options configuration options
* @param string|null $user system user id
*
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
*
* 200: data in requested format
* 400: invalid parameters
* 401: user not authorized
*/
#[ApiRoute(verb: 'POST', url: '/export', root: '/calendar')]
#[UserRateLimit(limit: 1, period: 60)]
#[NoAdminRequired]
#[TokenScope(scope: TokenScopes::CALENDAR_READ)]
public function export(string $target, ?string $type = null, ?array $options = null, ?string $user = null) {
$calendarId = $target;
$format = $type ?? 'ical';
$rangeStart = isset($options['rangeStart']) ? (string)$options['rangeStart'] : null;
$rangeCount = isset($options['rangeCount']) ? (int)$options['rangeCount'] : null;
// evaluate if user is logged in and has permissions
if (!$this->userSession->isLoggedIn()) {
return new DataResponse([], Http::STATUS_UNAUTHORIZED);
}
if ($user !== null) {
if ($this->userSession->getUser()->getUID() !== $user
&& $this->groupManager->isAdmin($this->userSession->getUser()->getUID()) === false) {
return new DataResponse([], Http::STATUS_UNAUTHORIZED);
}
if (!$this->userManager->userExists($user)) {
return new DataResponse(['error' => 'user not found'], Http::STATUS_BAD_REQUEST);
}
} else {
$userId = $this->userSession->getUser()->getUID();
}
// retrieve calendar and evaluate if export is supported
$calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $userId, [$calendarId]);
if ($calendars === []) {
return new DataResponse(['error' => 'calendar not found'], Http::STATUS_BAD_REQUEST);
}
$calendar = $calendars[0];
if (!$calendar instanceof ICalendarExport) {
return new DataResponse(['error' => 'calendar export not supported'], Http::STATUS_BAD_REQUEST);
}
// construct options object
$options = new CalendarExportOptions();
$options->setRangeStart($rangeStart);
$options->setRangeCount($rangeCount);
// evaluate if provided format is supported
if (!in_array($format, ExportService::FORMATS, true)) {
return new DataResponse(['error' => "Format <$format> is not valid."], Http::STATUS_BAD_REQUEST);
}
$options->setFormat($format);
// construct response
$contentType = match (strtolower($options->getFormat())) {
'jcal' => 'application/calendar+json; charset=UTF-8',
'xcal' => 'application/calendar+xml; charset=UTF-8',
default => 'text/calendar; charset=UTF-8'
};
$response = new StreamTraversableResponse($this->exportService->export($calendar, $options), Http::STATUS_OK, ['Content-Type' => $contentType]);
$response->cacheFor(0);
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent(
'Calendar "%s" of "%s" was exported in format "%s"',
[
'calendarId' => $calendarId,
'owner' => $userId,
'format' => $format,
],
false,
'dav.calendar.exported',
));
return $response;
}
}