diff --git a/src/FederationServer/Classes/Enums/Method.php b/src/FederationServer/Classes/Enums/Method.php index 1181f39..97eb4f4 100644 --- a/src/FederationServer/Classes/Enums/Method.php +++ b/src/FederationServer/Classes/Enums/Method.php @@ -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, diff --git a/src/FederationServer/Methods/Audit/ListAuditLogs.php b/src/FederationServer/Methods/Audit/ListAuditLogs.php new file mode 100644 index 0000000..3b2efa6 --- /dev/null +++ b/src/FederationServer/Methods/Audit/ListAuditLogs.php @@ -0,0 +1,65 @@ +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)); + } + } + diff --git a/src/FederationServer/Objects/OperatorRecord.php b/src/FederationServer/Objects/OperatorRecord.php index b39635d..4559cd0 100644 --- a/src/FederationServer/Objects/OperatorRecord.php +++ b/src/FederationServer/Objects/OperatorRecord.php @@ -125,6 +125,11 @@ return $this->updated; } + public function toPublicRecord(): PublicOperatorRecord + { + return new PublicOperatorRecord($this); + } + /** * @inheritDoc */ diff --git a/src/FederationServer/Objects/PublicAuditRecord.php b/src/FederationServer/Objects/PublicAuditRecord.php new file mode 100644 index 0000000..1364cae --- /dev/null +++ b/src/FederationServer/Objects/PublicAuditRecord.php @@ -0,0 +1,85 @@ +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'); + } + } \ No newline at end of file diff --git a/src/FederationServer/Objects/PublicOperatorRecord.php b/src/FederationServer/Objects/PublicOperatorRecord.php new file mode 100644 index 0000000..aad66fc --- /dev/null +++ b/src/FederationServer/Objects/PublicOperatorRecord.php @@ -0,0 +1,80 @@ +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); + } + } \ No newline at end of file