0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-04 14:01:10 +00:00
nextcloud_server/apps/admin_audit/lib/Actions/Files.php
Ferdinand Thiessen 832f79ac93
chore: apply code style
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
2025-04-30 19:04:59 +02:00

223 lines
5.6 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 OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeReadEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
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\Preview\BeforePreviewFetchedEvent;
use Psr\Log\LoggerInterface;
/**
* Class Files logs the actions to files
*
* @package OCA\AdminAudit\Actions
*/
class Files extends Action {
private array $renamedNodes = [];
/**
* Logs file read actions
*/
public function read(BeforeNodeReadEvent $event): void {
try {
$node = $event->getNode();
$params = [
'id' => $node instanceof NonExistingFile ? null : $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file read: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File with id "%s" accessed: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs rename actions of files
*/
public function beforeRename(BeforeNodeRenamedEvent $event): void {
try {
$source = $event->getSource();
$this->renamedNodes[$source->getId()] = $source;
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file rename: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
}
/**
* Logs rename actions of files
*/
public function afterRename(NodeRenamedEvent $event): void {
try {
$target = $event->getTarget();
$originalSource = $this->renamedNodes[$target->getId()];
$params = [
'newid' => $target->getId(),
'oldpath' => $originalSource->getPath(),
'newpath' => $target->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file rename: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File renamed with id "%s" from "%s" to "%s"',
$params,
array_keys($params)
);
}
/**
* Logs creation of files
*/
public function create(NodeCreatedEvent $event): void {
try {
$params = [
'id' => $event->getNode()->getId(),
'path' => $event->getNode()->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\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(
'File with id "%s" created: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs copying of files
*/
public function copy(NodeCopiedEvent $event): void {
try {
$params = [
'oldid' => $event->getSource()->getId(),
'newid' => $event->getTarget()->getId(),
'oldpath' => $event->getSource()->getPath(),
'newpath' => $event->getTarget()->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file copy: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'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 {
$node = $event->getNode();
try {
$params = [
'id' => $node->getId(),
'path' => $node->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\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(
'File with id "%s" written to: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs deletions of files
*/
public function delete(BeforeNodeDeletedEvent $event): void {
try {
$params = [
'id' => $event->getNode()->getId(),
'path' => $event->getNode()->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file delete: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File with id "%s" deleted: "%s"',
$params,
array_keys($params)
);
}
/**
* Logs preview access to a file
*
* @param BeforePreviewFetchedEvent $event
*/
public function preview(BeforePreviewFetchedEvent $event): void {
try {
$file = $event->getNode();
$params = [
'id' => $file->getId(),
'width' => $event->getWidth(),
'height' => $event->getHeight(),
'crop' => $event->isCrop(),
'mode' => $event->getMode(),
'path' => $file->getPath(),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
'Exception thrown in file preview: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'Preview accessed: (id: "%s", width: "%s", height: "%s" crop: "%s", mode: "%s", path: "%s")',
$params,
array_keys($params)
);
}
}