Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(admin_audit): Listen to the right events #48548

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 5 additions & 43 deletions apps/admin_audit/lib/Actions/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
namespace OCA\AdminAudit\Actions;

use OC\Files\Node\NonExistingFile;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
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;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\InvalidPathException;
Expand All @@ -28,10 +27,9 @@
class Files extends Action {

private array $renamedNodes = [];

/**
* Logs file read actions
*
* @param BeforeNodeReadEvent $event
*/
public function read(BeforeNodeReadEvent $event): void {
try {
Expand All @@ -55,8 +53,6 @@ public function read(BeforeNodeReadEvent $event): void {

/**
* Logs rename actions of files
*
* @param BeforeNodeRenamedEvent $event
*/
public function beforeRename(BeforeNodeRenamedEvent $event): void {
try {
Expand All @@ -72,8 +68,6 @@ public function beforeRename(BeforeNodeRenamedEvent $event): void {

/**
* Logs rename actions of files
*
* @param NodeRenamedEvent $event
*/
public function afterRename(NodeRenamedEvent $event): void {
try {
Expand Down Expand Up @@ -101,8 +95,6 @@ public function afterRename(NodeRenamedEvent $event): void {

/**
* Logs creation of files
*
* @param NodeCreatedEvent $event
*/
public function create(NodeCreatedEvent $event): void {
try {
Expand All @@ -128,8 +120,6 @@ public function create(NodeCreatedEvent $event): void {

/**
* Logs copying of files
*
* @param NodeCopiedEvent $event
*/
public function copy(NodeCopiedEvent $event): void {
try {
Expand All @@ -154,14 +144,12 @@ public function copy(NodeCopiedEvent $event): void {

/**
* Logs writing of files
*
* @param BeforeNodeWrittenEvent $event
*/
public function write(BeforeNodeWrittenEvent $event): void {
public function write(NodeWrittenEvent $event): void {
$node = $event->getNode();
try {
$params = [
'id' => $node instanceof NonExistingFile ? null : $node->getId(),
'id' => $node->getId(),
'path' => mb_substr($node->getInternalPath(), 5),
];
} catch (InvalidPathException|NotFoundException $e) {
Expand All @@ -181,36 +169,10 @@ public function write(BeforeNodeWrittenEvent $event): void {
);
}

/**
* Logs update of files
*
* @param NodeWrittenEvent $event
*/
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;
}
$this->log(
'File with id "%s" updated: "%s"',
$params,
array_keys($params)
);
}

/**
* Logs deletions of files
*
* @param NodeDeletedEvent $event
*/
public function delete(NodeDeletedEvent $event): void {
public function delete(BeforeNodeDeletedEvent $event): void {
try {
$params = [
'id' => $event->getNode()->getId(),
Expand Down
16 changes: 4 additions & 12 deletions apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
use OCP\Console\ConsoleEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
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;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Group\Events\GroupCreatedEvent;
Expand Down Expand Up @@ -194,17 +193,10 @@ function (NodeCopiedEvent $event) use ($fileActions): void {
}
);

$eventDispatcher->addListener(
BeforeNodeWrittenEvent::class,
function (BeforeNodeWrittenEvent $event) use ($fileActions): void {
$fileActions->write($event);
}
);

$eventDispatcher->addListener(
NodeWrittenEvent::class,
function (NodeWrittenEvent $event) use ($fileActions): void {
$fileActions->update($event);
$fileActions->write($event);
}
);

Expand All @@ -216,8 +208,8 @@ function (BeforeNodeReadEvent $event) use ($fileActions): void {
);

$eventDispatcher->addListener(
NodeDeletedEvent::class,
function (NodeDeletedEvent $event) use ($fileActions): void {
BeforeNodeDeletedEvent::class,
function (BeforeNodeDeletedEvent $event) use ($fileActions): void {
$fileActions->delete($event);
}
);
Expand Down
Loading