Remove unused imports and refactor audit log response handling
This commit is contained in:
parent
7cc13a850e
commit
c1a2384257
6 changed files with 7 additions and 141 deletions
|
@ -8,7 +8,6 @@
|
|||
use FederationServer\Classes\Logger;
|
||||
use FederationServer\Exceptions\DatabaseOperationException;
|
||||
use FederationServer\Objects\AuditLogRecord;
|
||||
use FederationServer\Objects\PublicAuditRecord;
|
||||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
@ -319,31 +318,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an AuditLogRecord to a PublicAuditRecord.
|
||||
*
|
||||
* @param AuditLogRecord $record The AuditLogRecord to convert.
|
||||
* @return PublicAuditRecord The converted PublicAuditRecord.
|
||||
* @throws DatabaseOperationException Thrown if there was a database error while transforming the record.
|
||||
*/
|
||||
public static function toPublicAuditRecord(AuditLogRecord $record): PublicAuditRecord
|
||||
{
|
||||
$operatorRecord = null;
|
||||
$entityRecord = null;
|
||||
|
||||
if($record->getOperator() !== null)
|
||||
{
|
||||
$operatorRecord = OperatorManager::getOperator($record->getOperator());
|
||||
}
|
||||
|
||||
if($record->getEntity() !== null)
|
||||
{
|
||||
$entityRecord = EntitiesManager::getEntityByUuid($record->getEntity());
|
||||
}
|
||||
|
||||
return new PublicAuditRecord($record, $operatorRecord, $entityRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all audit log entries.
|
||||
*
|
||||
|
|
|
@ -50,8 +50,8 @@
|
|||
|
||||
try
|
||||
{
|
||||
$results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log),
|
||||
AuditLogManager::getEntries($limit, $page, $filteredEntries)
|
||||
self::successResponse(array_map(fn($log) => $log->toArray(),
|
||||
AuditLogManager::getEntries($limit, $page, $filteredEntries))
|
||||
);
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
|
@ -59,7 +59,6 @@
|
|||
throw new RequestException('Internal Server Error: Unable to retrieve audit logs', 500, $e);
|
||||
}
|
||||
|
||||
self::successResponse(array_map(fn($log) => $log->toArray(), $results));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,11 @@
|
|||
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\Classes\Validate;
|
||||
use FederationServer\Exceptions\DatabaseOperationException;
|
||||
use FederationServer\Exceptions\RequestException;
|
||||
use FederationServer\FederationServer;
|
||||
use FederationServer\Objects\PublicAuditRecord;
|
||||
|
||||
class ViewAuditEntry extends RequestHandler
|
||||
{
|
||||
|
@ -38,27 +35,12 @@
|
|||
throw new RequestException('Audit log not found', 404);
|
||||
}
|
||||
|
||||
$operatorRecord = null;
|
||||
$entityRecord = null;
|
||||
|
||||
if($logRecord->getOperator() !== null)
|
||||
{
|
||||
$operatorRecord = OperatorManager::getOperator($logRecord->getOperator());
|
||||
}
|
||||
|
||||
if($logRecord->getEntity() !== null)
|
||||
{
|
||||
$entityRecord = EntitiesManager::getEntityByUuid($logRecord->getEntity());
|
||||
}
|
||||
|
||||
$result = new PublicAuditRecord($logRecord, $operatorRecord, $entityRecord);
|
||||
self::successResponse($logRecord->toArray());
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new RequestException('Internal Server Error: Unable to retrieve audit log', 500, $e);
|
||||
}
|
||||
|
||||
self::successResponse($result->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,16 +67,14 @@
|
|||
throw new RequestException('Not Found: Entity with the specified UUID does not exist', 404);
|
||||
}
|
||||
|
||||
$results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log),
|
||||
AuditLogManager::getEntriesByEntity($entityUuid, $limit, $page, $filteredEntries)
|
||||
self::successResponse(array_map(fn($log) => $log->toArray(),
|
||||
AuditLogManager::getEntriesByEntity($entityUuid, $limit, $page, $filteredEntries))
|
||||
);
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,16 +67,14 @@
|
|||
throw new RequestException('Not Found: Operator with the specified UUID does not exist', 404);
|
||||
}
|
||||
|
||||
$results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log),
|
||||
AuditLogManager::getEntriesByOperator($operatorUuid, $limit, $page, $filteredEntries)
|
||||
self::successResponse(array_map(fn($log) => $log->toArray(),
|
||||
AuditLogManager::getEntriesByOperator($operatorUuid, $limit, $page, $filteredEntries))
|
||||
);
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
<?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');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue