diff --git a/apps/admin_audit/lib/Actions/Files.php b/apps/admin_audit/lib/Actions/Files.php index 5ecfe106b744b..ee40a4e14767f 100644 --- a/apps/admin_audit/lib/Actions/Files.php +++ b/apps/admin_audit/lib/Actions/Files.php @@ -8,6 +8,7 @@ namespace OCA\AdminAudit\Actions; use OCP\Files\Events\Node\BeforeNodeReadEvent; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; use OCP\Files\Events\Node\BeforeNodeWrittenEvent; use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\Events\Node\NodeCreatedEvent; @@ -25,6 +26,8 @@ * @package OCA\AdminAudit\Actions */ class Files extends Action { + + private array $renamedNodes = []; /** * Logs file read actions * @@ -52,16 +55,33 @@ public function read(BeforeNodeReadEvent $event): void { /** * Logs rename actions of files * - * @param NodeRenamedEvent $event + * @param BeforeNodeRenamedEvent $event */ - public function rename(NodeRenamedEvent $event): void { + 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 + * + * @param NodeRenamedEvent $event + */ + public function afterRename(NodeRenamedEvent $event): void { + try { $target = $event->getTarget(); + $originalSource = $this->renamedNodes[$target->getId()]; $params = [ 'newid' => $target->getId(), - 'oldpath' => mb_substr($source->getPath(), 5), - 'newpath' => mb_substr($target->getPath(), 5), + 'oldpath' => mb_substr($originalSource->getInternalPath(), 5), + 'newpath' => mb_substr($target->getInternalPath(), 5), ]; } catch (InvalidPathException|NotFoundException $e) { \OCP\Server::get(LoggerInterface::class)->error( @@ -77,6 +97,7 @@ public function rename(NodeRenamedEvent $event): void { ); } + /** * Logs creation of files * diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index 62ab65b95b9f7..b8b80841539dd 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -32,6 +32,7 @@ use OCP\Console\ConsoleEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Events\Node\BeforeNodeReadEvent; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; use OCP\Files\Events\Node\BeforeNodeWrittenEvent; use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\Events\Node\NodeCreatedEvent; @@ -179,10 +180,17 @@ function (BeforePreviewFetchedEvent $event) use ($fileActions) { } ); + $eventDispatcher->addListener( + BeforeNodeRenamedEvent::class, + function (BeforeNodeRenamedEvent $event) use ($fileActions) { + $fileActions->beforeRename($event); + } + ); + $eventDispatcher->addListener( NodeRenamedEvent::class, function (NodeRenamedEvent $event) use ($fileActions) { - $fileActions->rename($event); + $fileActions->afterRename($event); } );