Skip to content

Commit

Permalink
feat: include file id in audit logs
Browse files Browse the repository at this point in the history
Signed-off-by: yemkareems <yemkareems@gmail.com>
  • Loading branch information
yemkareems committed Apr 29, 2024
1 parent c53e365 commit 7b0b54f
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 101 deletions.
202 changes: 145 additions & 57 deletions apps/admin_audit/lib/Actions/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
*/
namespace OCA\AdminAudit\Actions;

use OCP\Files\Events\Node\BeforeNodeReadEvent;
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;
use OCP\Files\NotFoundException;
use OCP\Preview\BeforePreviewFetchedEvent;
use Psr\Log\LoggerInterface;

/**
* Class Files logs the actions to files
*
Expand All @@ -36,134 +48,210 @@ class Files extends Action {
/**
* Logs file read actions
*
* @param array $params
* @param BeforeNodeReadEvent $event
*/
public function read(array $params): void {
public function read(BeforeNodeReadEvent $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 read: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File accessed: "%s"',
'File with id "%s" accessed: "%s"',
$params,
[
'path',
]
array_keys($params)
);
}

/**
* Logs rename actions of files
*
* @param array $params
* @param NodeRenamedEvent $event
*/
public function rename(array $params): void {
public function rename(NodeRenamedEvent $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),
];
} 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 renamed: "%s" to "%s"',
'File renamed with id "%s" from "%s" to "%s"',
$params,
[
'oldpath',
'newpath',
]
array_keys($params)
);
}

/**
* Logs creation of files
*
* @param array $params
* @param NodeCreatedEvent $event
*/
public function create(array $params): void {
if ($params['path'] === '/' || $params['path'] === '' || $params['path'] === null) {
public function create(NodeCreatedEvent $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 create: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
if ($params['path'] === '/' || $params['path'] === '') {
return;
}

$this->log(
'File created: "%s"',
'File with id "%s" created: "%s"',
$params,
[
'path',
]
array_keys($params)
);
}

/**
* Logs copying of files
*
* @param array $params
* @param NodeCopiedEvent $event
*/
public function copy(array $params): void {
public function copy(NodeCopiedEvent $event): void {
try {
$params = [
'oldid' => $event->getSource()->getId(),
'newid' => $event->getTarget()->getId(),
'oldpath' => mb_substr($event->getSource()->getInternalPath(), 5),
'newpath' => mb_substr($event->getTarget()->getInternalPath(), 5),
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
"Exception thrown in file copy: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File copied: "%s" to "%s"',
'File id copied from: "%s" to "%s", path from "%s" to "%s"',
$params,
[
'oldpath',
'newpath',
]
array_keys($params)
);
}

/**
* Logs writing of files
*
* @param array $params
* @param BeforeNodeWrittenEvent $event
*/
public function write(array $params): void {
if ($params['path'] === '/' || $params['path'] === '' || $params['path'] === null) {
public function write(BeforeNodeWrittenEvent $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 write: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
if ($params['path'] === '/' || $params['path'] === '') {
return;
}

$this->log(
'File written to: "%s"',
'File with id "%s" written to: "%s"',
$params,
[
'path',
]
array_keys($params)
);
}

/**
* Logs update of files
*
* @param array $params
* @param NodeWrittenEvent $event
*/
public function update(array $params): void {
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 updated: "%s"',
'File with id "%s" updated: "%s"',
$params,
[
'path',
]
array_keys($params)
);
}

/**
* Logs deletions of files
*
* @param array $params
* @param NodeDeletedEvent $event
*/
public function delete(array $params): void {
public function delete(NodeDeletedEvent $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 delete: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'File deleted: "%s"',
'File with id "%s" deleted: "%s"',
$params,
[
'path',
]
array_keys($params)
);
}

/**
* Logs preview access to a file
*
* @param array $params
* @param BeforePreviewFetchedEvent $event
*/
public function preview(array $params): void {
public function preview(BeforePreviewFetchedEvent $event): void {
try {
$file = $event->getNode();
$params = [
'id' => $file->getId(),
'width' => $event->getWidth(),
'height' => $event->getHeight(),
'crop' => $event->isCrop(),
'mode' => $event->getMode(),
'path' => mb_substr($file->getInternalPath(), 5)
];
} catch (InvalidPathException|NotFoundException $e) {
\OCP\Server::get(LoggerInterface::class)->error(
"Exception thrown in file preview: ".$e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
$this->log(
'Preview accessed: "%s" (width: "%s", height: "%s" crop: "%s", mode: "%s")',
'Preview accessed: (id: "%s", width: "%s", height: "%s" crop: "%s", mode: "%s", path: "%s")',
$params,
[
'path',
'width',
'height',
'crop',
'mode'
]
array_keys($params)
);
}
}
Loading

0 comments on commit 7b0b54f

Please sign in to comment.