-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Write critical operations to the audit log
Signed-off-by: Hoang Pham <hoangmaths96@gmail.com>
- Loading branch information
Showing
14 changed files
with
368 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Event; | ||
|
||
use OCA\Tables\Db\Row2; | ||
use OCP\EventDispatcher\Event; | ||
|
||
final class RowDeletedEvent extends Event | ||
{ | ||
public function __construct(protected Row2 $row, protected string $userId) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function getRow(): Row2 | ||
{ | ||
return $this->row; | ||
} | ||
|
||
public function getUserId(): string | ||
{ | ||
return $this->userId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Event; | ||
|
||
use OCA\Tables\Db\Table; | ||
use OCP\EventDispatcher\Event; | ||
|
||
final class TableDeletedEvent extends Event | ||
{ | ||
public function __construct(protected Table $table, protected string $userId) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function getTable(): Table | ||
{ | ||
return $this->table; | ||
} | ||
|
||
public function getUserId(): string | ||
{ | ||
return $this->userId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Event; | ||
|
||
use OCA\Tables\Db\Table; | ||
use OCP\EventDispatcher\Event; | ||
|
||
final class TableOwnershipTransferredEvent extends Event | ||
{ | ||
public function __construct(protected Table $table, protected string $toUserId, protected ?string $fromUserId = null) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function getTable(): Table | ||
{ | ||
return $this->table; | ||
} | ||
|
||
public function getFromUserId(): string | ||
{ | ||
return $this->fromUserId; | ||
} | ||
|
||
public function getToUserId(): string | ||
{ | ||
return $this->toUserId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Event; | ||
|
||
use OCA\Tables\Db\View; | ||
use OCP\EventDispatcher\Event; | ||
|
||
final class ViewDeletedEvent extends Event | ||
{ | ||
public function __construct(protected View $view, protected string $userId) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function getView(): View | ||
{ | ||
return $this->view; | ||
} | ||
|
||
public function getUserId(): string | ||
{ | ||
return $this->userId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Listener; | ||
|
||
use OCA\Tables\Event\RowDeletedEvent; | ||
use OCA\Tables\Service\Support\AuditLogServiceInterface; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
|
||
final class WhenRowDeletedAuditLogListener implements IEventListener | ||
{ | ||
public function __construct(protected AuditLogServiceInterface $auditLogService) | ||
{ | ||
} | ||
|
||
public function handle(Event $event): void | ||
{ | ||
if (!($event instanceof RowDeletedEvent)) { | ||
return; | ||
} | ||
|
||
$row = $event->getRow(); | ||
$userId = $event->getUserId(); | ||
$rowId = $row->getId(); | ||
|
||
$this->auditLogService->log("Row with ID: $rowId was deleted by user with ID: $userId", [ | ||
'row' => $row->jsonSerialize(), | ||
'userId' => $userId, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Listener; | ||
|
||
use OCA\Tables\Event\TableDeletedEvent; | ||
use OCA\Tables\Service\Support\AuditLogServiceInterface; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
|
||
final class WhenTableDeletedAuditLogListener implements IEventListener | ||
{ | ||
public function __construct(protected AuditLogServiceInterface $auditLogService) | ||
{ | ||
} | ||
|
||
public function handle(Event $event): void | ||
{ | ||
if (!($event instanceof TableDeletedEvent)) { | ||
return; | ||
} | ||
|
||
$table = $event->getTable(); | ||
$userId = $event->getUserId(); | ||
|
||
$this->auditLogService->log("Table with ID: $table->id was deleted by user with ID: $userId", [ | ||
'table' => $table->jsonSerialize(), | ||
'userId' => $userId, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Listener; | ||
|
||
use OCA\Tables\Event\TableOwnershipTransferredEvent; | ||
use OCA\Tables\Service\Support\AuditLogServiceInterface; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
|
||
final class WhenTableTransferredAuditLogListener implements IEventListener | ||
{ | ||
public function __construct(protected AuditLogServiceInterface $auditLogService) | ||
{ | ||
} | ||
|
||
public function handle(Event $event): void | ||
{ | ||
if (!($event instanceof TableOwnershipTransferredEvent)) { | ||
return; | ||
} | ||
|
||
$table = $event->getTable(); | ||
$fromUserId = $event->getFromUserId(); | ||
$toUserId = $event->getToUserId(); | ||
|
||
$this->auditLogService->log("Table with ID: $table->id was transferred from user with ID: $fromUserId to user with ID: $toUserId", [ | ||
'table' => $table->jsonSerialize(), | ||
'fromUserId' => $fromUserId, | ||
'toUserId' => $toUserId, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Listener; | ||
|
||
use OCA\Tables\Event\ViewDeletedEvent; | ||
use OCA\Tables\Service\Support\AuditLogServiceInterface; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
|
||
final class WhenViewDeletedAuditLogListener implements IEventListener | ||
{ | ||
public function __construct(protected AuditLogServiceInterface $auditLogService) | ||
{ | ||
} | ||
|
||
public function handle(Event $event): void | ||
{ | ||
if (!($event instanceof ViewDeletedEvent)) { | ||
return; | ||
} | ||
|
||
$view = $event->getView(); | ||
$userId = $event->getUserId(); | ||
|
||
$this->auditLogService->log("View with ID: $view->id was deleted by user with ID: $userId", [ | ||
'view' => $view->jsonSerialize(), | ||
'userId' => $userId, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Service\Support; | ||
|
||
interface AuditLogServiceInterface | ||
{ | ||
public function log(string $message, array $context): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Tables\Service\Support; | ||
|
||
use OCP\EventDispatcher\IEventDispatcher; | ||
use OCP\Log\Audit\CriticalActionPerformedEvent; | ||
|
||
final class DefaultAuditLogService implements AuditLogServiceInterface | ||
{ | ||
public function __construct(private IEventDispatcher $eventDispatcher) | ||
{ | ||
} | ||
|
||
public function log(string $message, array $context): void | ||
{ | ||
$auditEvent = new CriticalActionPerformedEvent($message, $context); | ||
|
||
$this->eventDispatcher->dispatchTyped($auditEvent); | ||
} | ||
} |
Oops, something went wrong.