Skip to content

Commit

Permalink
Merge pull request #45845 from nextcloud/fix/log-rename-to-use-intern…
Browse files Browse the repository at this point in the history
…al-path

fix: rename split into two as before rename and after rename
  • Loading branch information
yemkareems authored Jun 20, 2024
2 parents 2593f5f + eae2386 commit a05a04c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
29 changes: 25 additions & 4 deletions apps/admin_audit/lib/Actions/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,8 @@
* @package OCA\AdminAudit\Actions
*/
class Files extends Action {

private array $renamedNodes = [];
/**
* Logs file read actions
*
Expand Down Expand Up @@ -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(
Expand All @@ -77,6 +97,7 @@ public function rename(NodeRenamedEvent $event): void {
);
}


/**
* Logs creation of files
*
Expand Down
10 changes: 9 additions & 1 deletion apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
);

Expand Down

0 comments on commit a05a04c

Please sign in to comment.