Files
nextcloud-server-mirror/apps/admin_audit/lib/Actions/Files.php
Ghassen kefi 03b51ed1ae feat(admin_audit): add stable operation identifier to audit log entries
Every audit log entry now carries a machine-readable "operation" field
(e.g. "files.file.read") in its data, next to the human-readable message.
Log consumers can filter on it instead of matching message text, which
may change between releases.

The names are defined once in the new OCA\AdminAudit\Operation enum.
Operations passed by other apps through CriticalActionPerformedEvent are
written as given.

Refs nextcloud-gmbh/governance#199

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
2026-10-06 15:56:05 +02:00

187 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AdminAudit\Actions;
use OC\Files\Node\NonExistingFile;
use OCA\AdminAudit\Operation;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeReadEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Server;
use Psr\Log\LoggerInterface;
/**
* Class Files logs the actions to files
*
* @package OCA\AdminAudit\Actions
*/
class Files extends Action {
/**
* Logs file read actions
*/
public function read(BeforeNodeReadEvent $event): void {
try {
$node = $event->getNode();
$params = [
'id' => $node instanceof NonExistingFile ? 'not-yet-assigned' : $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file read: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
Operation::FileRead,
'File with id "%s" accessed: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs rename actions of files
*/
public function afterRename(NodeRenamedEvent $event): void {
try {
$target = $event->getTarget();
$source = $event->getSource();
$params = [
'newid' => $target->getId(),
'oldpath' => $source->getPath(),
'newpath' => $target->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file rename: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
Operation::FileRenamed,
'File renamed with id "%s" from "%s" to "%s"',
$params,
array_keys($params)
);
}
/**
* Logs creation of files
*/
public function create(NodeCreatedEvent $event): void {
try {
$node = $event->getNode();
$params = [
'id' => $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file create: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
if ($params['path'] === '/' || $params['path'] === '') {
return;
}
$this->log(
Operation::FileCreated,
'File with id "%s" created: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs copying of files
*/
public function copy(NodeCopiedEvent $event): void {
try {
$source = $event->getSource();
$target = $event->getTarget();
$params = [
'oldid' => $source->getId(),
'newid' => $target->getId(),
'oldpath' => $source->getPath(),
'newpath' => $target->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file copy: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
Operation::FileCopied,
'File id copied from: "%s" to "%s", path from "%s" to "%s"',
$params,
array_keys($params)
);
}
/**
* Logs writing of files
*/
public function write(NodeWrittenEvent $event): void {
try {
$node = $event->getNode();
$params = [
'id' => $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file write: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
if ($params['path'] === '/' || $params['path'] === '') {
return;
}
$this->log(
Operation::FileWritten,
'File with id "%s" written to: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs deletions of files
*/
public function delete(BeforeNodeDeletedEvent $event): void {
try {
$node = $event->getNode();
$params = [
'id' => $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file delete: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
Operation::FileDeleted,
'File with id "%s" deleted: "%s"',
$params,
array_keys($params)
);
}
}