diff options
author | yemkareems <yemkareems@gmail.com> | 2024-06-13 12:59:51 +0530 |
---|---|---|
committer | yemkareems <yemkareems@gmail.com> | 2024-06-20 10:54:45 +0530 |
commit | 3e355c4db12caad198abde5e9a9d67afc22f3034 (patch) | |
tree | 75afac770744a24156da48fb8ee5a9758b59a230 /apps/admin_audit | |
parent | f06338ca3676c38cee351b6058b99f426c079cc3 (diff) | |
download | nextcloud-server-3e355c4db12caad198abde5e9a9d67afc22f3034.tar.gz nextcloud-server-3e355c4db12caad198abde5e9a9d67afc22f3034.zip |
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 <yemkareems@gmail.com>
Diffstat (limited to 'apps/admin_audit')
-rw-r--r-- | apps/admin_audit/lib/Actions/Files.php | 36 | ||||
-rw-r--r-- | apps/admin_audit/lib/AppInfo/Application.php | 10 |
2 files changed, 40 insertions, 6 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,40 @@ 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(); + $params = [ + 'oldid' => $source->getId(), + 'oldpath' => mb_substr($source->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 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(), - 'oldpath' => mb_substr($source->getPath(), 5), - 'newpath' => mb_substr($target->getPath(), 5), + 'newpath' => mb_substr($target->getInternalPath(), 5), ]; } catch (InvalidPathException|NotFoundException $e) { \OCP\Server::get(LoggerInterface::class)->error( @@ -71,12 +96,13 @@ class Files extends Action { } $this->log( - 'File renamed with id "%s" from "%s" to "%s"', + '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); } ); |