From 3e355c4db12caad198abde5e9a9d67afc22f3034 Mon Sep 17 00:00:00 2001 From: yemkareems Date: Thu, 13 Jun 2024 12:59:51 +0530 Subject: [PATCH] fix: rename logged as beforeRename and afterRename, and in both place getInternalPath is used in place of getPath to make it consistent across the logs Signed-off-by: yemkareems --- apps/admin_audit/lib/Actions/Files.php | 40 ++++++++++++++++---- apps/admin_audit/lib/AppInfo/Application.php | 10 ++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/apps/admin_audit/lib/Actions/Files.php b/apps/admin_audit/lib/Actions/Files.php index 5ecfe106b74..1a3d9ca4126 100644 --- a/apps/admin_audit/lib/Actions/Files.php +++ b/apps/admin_audit/lib/Actions/Files.php @@ -8,6 +8,7 @@ declare(strict_types=1); 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; @@ -52,16 +53,14 @@ class Files extends Action { /** * 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(); - $target = $event->getTarget(); $params = [ - 'newid' => $target->getId(), - 'oldpath' => mb_substr($source->getPath(), 5), - 'newpath' => mb_substr($target->getPath(), 5), + 'oldid' => $source->getId(), + 'oldpath' => mb_substr($source->getInternalPath(), 5), ]; } catch (InvalidPathException|NotFoundException $e) { \OCP\Server::get(LoggerInterface::class)->error( @@ -71,12 +70,39 @@ class Files extends Action { } $this->log( - 'File renamed with id "%s" from "%s" to "%s"', + 'File with id "%s" renamed from "%s"', $params, array_keys($params) ); } + /** + * Logs rename actions of files + * + * @param NodeRenamedEvent $event + */ + public function afterRename(NodeRenamedEvent $event): void { + try { + $target = $event->getTarget(); + $params = [ + 'newid' => $target->getId(), + 'newpath' => mb_substr($target->getInternalPath(), 5), + ]; + } 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 with id "%s" renamed to "%s"', + $params, + array_keys($params) + ); + } + + /** * Logs creation of files * diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index 62ab65b95b9..b40a4fc5929 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -32,6 +32,7 @@ use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed; 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; @@ -182,7 +183,14 @@ class Application extends App implements IBootstrap { $eventDispatcher->addListener( NodeRenamedEvent::class, function (NodeRenamedEvent $event) use ($fileActions) { - $fileActions->rename($event); + $fileActions->afterRename($event); + } + ); + + $eventDispatcher->addListener( + BeforeNodeRenamedEvent::class, + function (BeforeNodeRenamedEvent $event) use ($fileActions) { + $fileActions->beforeRename($event); } );