Add ListAuditLogs method and PublicAuditRecord class for audit log retrieval
This commit is contained in:
parent
bcb5a3c290
commit
bacd4809f4
5 changed files with 244 additions and 2 deletions
|
@ -6,6 +6,7 @@
|
|||
use FederationServer\Methods\Attachments\DeleteAttachment;
|
||||
use FederationServer\Methods\Attachments\DownloadAttachment;
|
||||
use FederationServer\Methods\Attachments\UploadAttachment;
|
||||
use FederationServer\Methods\Audit\ListAuditLogs;
|
||||
use FederationServer\Methods\Operators\CreateOperator;
|
||||
use FederationServer\Methods\Operators\DeleteOperator;
|
||||
use FederationServer\Methods\Operators\EnableOperator;
|
||||
|
@ -18,6 +19,8 @@
|
|||
|
||||
enum Method
|
||||
{
|
||||
case LIST_AUDIT_LOGS;
|
||||
|
||||
case LIST_OPERATORS;
|
||||
case CREATE_OPERATOR;
|
||||
case DELETE_OPERATOR;
|
||||
|
@ -42,6 +45,10 @@
|
|||
{
|
||||
switch($this)
|
||||
{
|
||||
case self::LIST_AUDIT_LOGS:
|
||||
ListAuditLogs::handleRequest();
|
||||
break;
|
||||
|
||||
case self::LIST_OPERATORS:
|
||||
ListOperators::handleRequest();
|
||||
break;
|
||||
|
@ -93,11 +100,11 @@
|
|||
{
|
||||
return match (true)
|
||||
{
|
||||
$requestMethod === 'POST' && $path === '/' => null,
|
||||
$path === '/' && $requestMethod === 'GET' => Method::LIST_AUDIT_LOGS,
|
||||
|
||||
preg_match('#^/attachments/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'GET' => Method::DOWNLOAD_ATTACHMENT,
|
||||
preg_match('#^/attachments/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'DELETE' => Method::DELETE_ATTACHMENT,
|
||||
($requestMethod === 'POST' || $requestMethod === 'PUT') && $path === '/attachments/upload' => Method::UPLOAD_ATTACHMENT,
|
||||
$path === '/attachments' && ($requestMethod === 'POST' || $requestMethod === 'PUT') => Method::UPLOAD_ATTACHMENT,
|
||||
|
||||
$path === '/operators' && ($requestMethod === 'POST' || $requestMethod === 'GET') => Method::LIST_OPERATORS,
|
||||
$path === '/operators/create' && $requestMethod === 'POST' => Method::CREATE_OPERATOR,
|
||||
|
|
65
src/FederationServer/Methods/Audit/ListAuditLogs.php
Normal file
65
src/FederationServer/Methods/Audit/ListAuditLogs.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace FederationServer\Methods\Audit;
|
||||
|
||||
use FederationServer\Classes\Managers\AuditLogManager;
|
||||
use FederationServer\Classes\Managers\EntitiesManager;
|
||||
use FederationServer\Classes\Managers\OperatorManager;
|
||||
use FederationServer\Classes\RequestHandler;
|
||||
use FederationServer\Exceptions\DatabaseOperationException;
|
||||
use FederationServer\Exceptions\RequestException;
|
||||
use FederationServer\FederationServer;
|
||||
use FederationServer\Objects\PublicAuditRecord;
|
||||
|
||||
class ListAuditLogs extends RequestHandler
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function handleRequest(): void
|
||||
{
|
||||
$limit = (int) (FederationServer::getParameter('limit') ?? 100);
|
||||
$page = (int) (FederationServer::getParameter('page') ?? 1);
|
||||
|
||||
if($limit < 1)
|
||||
{
|
||||
$limit = 100;
|
||||
}
|
||||
|
||||
if($page < 1)
|
||||
{
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
$results = [];
|
||||
|
||||
try
|
||||
{
|
||||
$auditLogs = AuditLogManager::getEntries($limit, $page);
|
||||
foreach($auditLogs as $logRecord)
|
||||
{
|
||||
$operatorRecord = null;
|
||||
$entityRecord = null;
|
||||
|
||||
if($logRecord->getOperator() !== null)
|
||||
{
|
||||
$operatorRecord = OperatorManager::getOperator($logRecord->getOperator());
|
||||
}
|
||||
|
||||
if($logRecord->getEntity() !== null)
|
||||
{
|
||||
$entityRecord = EntitiesManager::getEntityByUuid($logRecord->getEntity());
|
||||
}
|
||||
|
||||
$results[] = new PublicAuditRecord($logRecord, $operatorRecord, $entityRecord);
|
||||
}
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new RequestException('Internal Server Error: Unable to retrieve audit logs', 500, $e);
|
||||
}
|
||||
|
||||
self::successResponse(array_map(fn($log) => $log->toArray(), $results));
|
||||
}
|
||||
}
|
||||
|
|
@ -125,6 +125,11 @@
|
|||
return $this->updated;
|
||||
}
|
||||
|
||||
public function toPublicRecord(): PublicOperatorRecord
|
||||
{
|
||||
return new PublicOperatorRecord($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
85
src/FederationServer/Objects/PublicAuditRecord.php
Normal file
85
src/FederationServer/Objects/PublicAuditRecord.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace FederationServer\Objects;
|
||||
|
||||
use FederationServer\Classes\Enums\AuditLogType;
|
||||
use FederationServer\Interfaces\SerializableInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PublicAuditRecord implements SerializableInterface
|
||||
{
|
||||
private string $uuid;
|
||||
private ?PublicOperatorRecord $operator;
|
||||
private ?EntityRecord $entity;
|
||||
private AuditLogType $type;
|
||||
private string $message;
|
||||
private int $timestamp;
|
||||
|
||||
public function __construct(AuditLogRecord $auditLogRecord, OperatorRecord|PublicOperatorRecord|null $operator=null, ?EntityRecord $entity=null)
|
||||
{
|
||||
if($operator instanceof OperatorRecord)
|
||||
{
|
||||
$operator = $operator->toPublicRecord();
|
||||
}
|
||||
|
||||
$this->uuid = $auditLogRecord->getUuid();
|
||||
$this->operator = $operator;
|
||||
$this->entity = $entity;
|
||||
$this->type = $auditLogRecord->getType();
|
||||
$this->message = $auditLogRecord->getMessage();
|
||||
$this->timestamp = $auditLogRecord->getTimestamp();
|
||||
}
|
||||
|
||||
public function getUuid(): string
|
||||
{
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function getOperator(): ?PublicOperatorRecord
|
||||
{
|
||||
return $this->operator;
|
||||
}
|
||||
|
||||
public function getEntity(): ?EntityRecord
|
||||
{
|
||||
return $this->entity;
|
||||
}
|
||||
|
||||
public function getType(): AuditLogType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function getTimestamp(): int
|
||||
{
|
||||
return $this->timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'operator' => $this->operator?->toArray(),
|
||||
'entity' => $this->entity?->toArray(),
|
||||
'type' => $this->type->value,
|
||||
'message' => $this->message,
|
||||
'timestamp' => $this->timestamp
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $array): SerializableInterface
|
||||
{
|
||||
throw new InvalidArgumentException('fromArray() is not implemented in PublicAuditRecord');
|
||||
}
|
||||
}
|
80
src/FederationServer/Objects/PublicOperatorRecord.php
Normal file
80
src/FederationServer/Objects/PublicOperatorRecord.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace FederationServer\Objects;
|
||||
|
||||
use FederationServer\Interfaces\SerializableInterface;
|
||||
|
||||
class PublicOperatorRecord implements SerializableInterface
|
||||
{
|
||||
private string $uuid;
|
||||
private string $name;
|
||||
private bool $isClient;
|
||||
private int $created;
|
||||
private int $updated;
|
||||
|
||||
public function __construct(array|OperatorRecord $data)
|
||||
{
|
||||
if(is_array($data))
|
||||
{
|
||||
$this->uuid = (string)$data['uuid'] ?? '';
|
||||
$this->name = (string)$data['name'] ?? '';
|
||||
$this->isClient = (bool)$data['is_client'];
|
||||
$this->created = (int)$data['created'] ?? 0;
|
||||
$this->updated = (int)$data['updated'] ?? 0;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->uuid = $data->getUuid();
|
||||
$this->name = $data->getName();
|
||||
$this->isClient = $data->isClient();
|
||||
$this->created = $data->getCreated();
|
||||
$this->updated = $data->getUpdated();
|
||||
}
|
||||
|
||||
public function getUuid(): string
|
||||
{
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function isClient(): bool
|
||||
{
|
||||
return $this->isClient;
|
||||
}
|
||||
|
||||
public function getCreated(): int
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
public function getUpdated(): int
|
||||
{
|
||||
return $this->updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'is_client' => $this->isClient,
|
||||
'created' => $this->created,
|
||||
'updated' => $this->updated
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $array): SerializableInterface
|
||||
{
|
||||
return new self($array);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue