2016-06-08 09:25:44 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-03-10 13:40:19 -05:00
|
|
|
declare(strict_types=1);
|
2016-06-08 09:25:44 -04:00
|
|
|
/**
|
2024-05-24 13:43:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2017-07-01 05:28:03 -04:00
|
|
|
namespace OCA\AdminAudit\Actions;
|
2016-06-08 09:25:44 -04:00
|
|
|
|
2024-08-06 08:47:33 -04:00
|
|
|
use OC\Files\Node\NonExistingFile;
|
2024-04-09 02:40:12 -04:00
|
|
|
use OCP\Files\Events\Node\BeforeNodeReadEvent;
|
2024-06-13 03:29:51 -04:00
|
|
|
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
|
2024-04-09 02:40:12 -04:00
|
|
|
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
|
|
|
|
|
use OCP\Files\Events\Node\NodeCopiedEvent;
|
|
|
|
|
use OCP\Files\Events\Node\NodeCreatedEvent;
|
|
|
|
|
use OCP\Files\Events\Node\NodeDeletedEvent;
|
|
|
|
|
use OCP\Files\Events\Node\NodeRenamedEvent;
|
|
|
|
|
use OCP\Files\Events\Node\NodeWrittenEvent;
|
|
|
|
|
use OCP\Files\InvalidPathException;
|
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
2016-06-08 09:25:44 -04:00
|
|
|
/**
|
|
|
|
|
* Class Files logs the actions to files
|
|
|
|
|
*
|
2017-07-01 05:28:03 -04:00
|
|
|
* @package OCA\AdminAudit\Actions
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
|
|
|
|
class Files extends Action {
|
2024-06-13 08:05:15 -04:00
|
|
|
|
2024-06-14 00:44:44 -04:00
|
|
|
private array $renamedNodes = [];
|
2016-06-08 09:25:44 -04:00
|
|
|
/**
|
|
|
|
|
* Logs file read actions
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param BeforeNodeReadEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function read(BeforeNodeReadEvent $event): void {
|
|
|
|
|
try {
|
2024-08-06 08:47:33 -04:00
|
|
|
$node = $event->getNode();
|
2024-04-09 02:40:12 -04:00
|
|
|
$params = [
|
2024-08-06 08:47:33 -04:00
|
|
|
'id' => $node instanceof NonExistingFile ? null : $node->getId(),
|
|
|
|
|
'path' => mb_substr($node->getInternalPath(), 5),
|
2024-04-09 02:40:12 -04:00
|
|
|
];
|
|
|
|
|
} catch (InvalidPathException|NotFoundException $e) {
|
|
|
|
|
\OCP\Server::get(LoggerInterface::class)->error(
|
|
|
|
|
'Exception thrown in file read: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File with id "%s" accessed: "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs rename actions of files
|
|
|
|
|
*
|
2024-06-13 03:29:51 -04:00
|
|
|
* @param BeforeNodeRenamedEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-06-13 03:29:51 -04:00
|
|
|
public function beforeRename(BeforeNodeRenamedEvent $event): void {
|
2024-04-09 02:40:12 -04:00
|
|
|
try {
|
|
|
|
|
$source = $event->getSource();
|
2024-06-14 00:44:44 -04:00
|
|
|
$this->renamedNodes[$source->getId()] = $source;
|
2024-06-13 03:29:51 -04:00
|
|
|
} 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
|
|
|
|
|
*
|
|
|
|
|
* @param NodeRenamedEvent $event
|
|
|
|
|
*/
|
|
|
|
|
public function afterRename(NodeRenamedEvent $event): void {
|
|
|
|
|
try {
|
2024-04-09 02:40:12 -04:00
|
|
|
$target = $event->getTarget();
|
2024-06-14 00:44:44 -04:00
|
|
|
$originalSource = $this->renamedNodes[$target->getId()];
|
2024-04-09 02:40:12 -04:00
|
|
|
$params = [
|
|
|
|
|
'newid' => $target->getId(),
|
2024-06-14 00:44:44 -04:00
|
|
|
'oldpath' => mb_substr($originalSource->getInternalPath(), 5),
|
2024-06-13 03:29:51 -04:00
|
|
|
'newpath' => mb_substr($target->getInternalPath(), 5),
|
2024-04-09 02:40:12 -04:00
|
|
|
];
|
|
|
|
|
} catch (InvalidPathException|NotFoundException $e) {
|
|
|
|
|
\OCP\Server::get(LoggerInterface::class)->error(
|
|
|
|
|
'Exception thrown in file rename: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-06-13 08:05:15 -04:00
|
|
|
'File renamed with id "%s" from "%s" to "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 03:29:51 -04:00
|
|
|
|
2016-06-08 09:25:44 -04:00
|
|
|
/**
|
|
|
|
|
* Logs creation of files
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param NodeCreatedEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function create(NodeCreatedEvent $event): void {
|
|
|
|
|
try {
|
|
|
|
|
$params = [
|
|
|
|
|
'id' => $event->getNode()->getId(),
|
|
|
|
|
'path' => mb_substr($event->getNode()->getInternalPath(), 5),
|
|
|
|
|
];
|
|
|
|
|
} 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'] === '') {
|
2017-05-31 05:10:42 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File with id "%s" created: "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs copying of files
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param NodeCopiedEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function copy(NodeCopiedEvent $event): void {
|
|
|
|
|
try {
|
|
|
|
|
$params = [
|
|
|
|
|
'oldid' => $event->getSource()->getId(),
|
|
|
|
|
'newid' => $event->getTarget()->getId(),
|
|
|
|
|
'oldpath' => mb_substr($event->getSource()->getInternalPath(), 5),
|
|
|
|
|
'newpath' => mb_substr($event->getTarget()->getInternalPath(), 5),
|
|
|
|
|
];
|
|
|
|
|
} catch (InvalidPathException|NotFoundException $e) {
|
|
|
|
|
\OCP\Server::get(LoggerInterface::class)->error(
|
|
|
|
|
'Exception thrown in file copy: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File id copied from: "%s" to "%s", path from "%s" to "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs writing of files
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param BeforeNodeWrittenEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function write(BeforeNodeWrittenEvent $event): void {
|
2024-08-15 04:00:22 -04:00
|
|
|
$node = $event->getNode();
|
2024-04-09 02:40:12 -04:00
|
|
|
try {
|
|
|
|
|
$params = [
|
2024-08-15 04:00:22 -04:00
|
|
|
'id' => $node instanceof NonExistingFile ? null : $node->getId(),
|
|
|
|
|
'path' => mb_substr($node->getInternalPath(), 5),
|
2024-04-09 02:40:12 -04:00
|
|
|
];
|
|
|
|
|
} 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'] === '') {
|
2017-05-31 05:10:42 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File with id "%s" written to: "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs update of files
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param NodeWrittenEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function update(NodeWrittenEvent $event): void {
|
|
|
|
|
try {
|
|
|
|
|
$params = [
|
|
|
|
|
'id' => $event->getNode()->getId(),
|
|
|
|
|
'path' => mb_substr($event->getNode()->getInternalPath(), 5),
|
|
|
|
|
];
|
|
|
|
|
} catch (InvalidPathException|NotFoundException $e) {
|
|
|
|
|
\OCP\Server::get(LoggerInterface::class)->error(
|
|
|
|
|
'Exception thrown in file update: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File with id "%s" updated: "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs deletions of files
|
|
|
|
|
*
|
2024-04-09 02:40:12 -04:00
|
|
|
* @param NodeDeletedEvent $event
|
2016-06-08 09:25:44 -04:00
|
|
|
*/
|
2024-04-09 02:40:12 -04:00
|
|
|
public function delete(NodeDeletedEvent $event): void {
|
|
|
|
|
try {
|
|
|
|
|
$params = [
|
|
|
|
|
'id' => $event->getNode()->getId(),
|
|
|
|
|
'path' => mb_substr($event->getNode()->getInternalPath(), 5),
|
|
|
|
|
];
|
|
|
|
|
} catch (InvalidPathException|NotFoundException $e) {
|
|
|
|
|
\OCP\Server::get(LoggerInterface::class)->error(
|
|
|
|
|
'Exception thrown in file delete: '.$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 09:25:44 -04:00
|
|
|
$this->log(
|
2024-04-09 02:40:12 -04:00
|
|
|
'File with id "%s" deleted: "%s"',
|
2016-06-08 09:25:44 -04:00
|
|
|
$params,
|
2024-04-09 02:40:12 -04:00
|
|
|
array_keys($params)
|
2016-06-08 09:25:44 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|