Add getOperators method to retrieve a paginated list of operators with error handling
Some checks are pending
CI / release (push) Waiting to run
CI / debug (push) Waiting to run
CI / check-phpunit (push) Waiting to run
CI / check-phpdoc (push) Waiting to run
CI / generate-phpdoc (push) Blocked by required conditions
CI / test (push) Blocked by required conditions
CI / release-documentation (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions

This commit is contained in:
netkas 2025-06-03 01:11:38 -04:00
parent 459cd1e27e
commit 9a6f9471f2
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
3 changed files with 90 additions and 2 deletions

View file

@ -9,6 +9,7 @@
use FederationServer\Methods\Operators\DeleteOperator; use FederationServer\Methods\Operators\DeleteOperator;
use FederationServer\Methods\Operators\EnableOperator; use FederationServer\Methods\Operators\EnableOperator;
use FederationServer\Methods\Operators\GetOperator; use FederationServer\Methods\Operators\GetOperator;
use FederationServer\Methods\Operators\ListOperators;
use FederationServer\Methods\Operators\ManageBlacklistPermission; use FederationServer\Methods\Operators\ManageBlacklistPermission;
use FederationServer\Methods\Operators\ManageClientPermission; use FederationServer\Methods\Operators\ManageClientPermission;
use FederationServer\Methods\Operators\ManageOperatorsPermission; use FederationServer\Methods\Operators\ManageOperatorsPermission;
@ -40,8 +41,7 @@
switch($this) switch($this)
{ {
case self::LIST_OPERATORS: case self::LIST_OPERATORS:
// This method does not have a dedicated handler, it is handled by the main request handler ListOperators::handleRequest();
// in FederationServer::handleRequest()
break; break;
case self::GET_OPERATOR: case self::GET_OPERATOR:
GetOperator::handleRequest(); GetOperator::handleRequest();

View file

@ -317,4 +317,42 @@
throw new DatabaseOperationException(sprintf("Failed to set client status for operator with UUID '%s'", $uuid), 0, $e); throw new DatabaseOperationException(sprintf("Failed to set client status for operator with UUID '%s'", $uuid), 0, $e);
} }
} }
/**
* Retrieve a list of operators with pagination support.
*
* @param int $limit The maximum number of operators to retrieve.
* @param int $page The page number for pagination.
* @return OperatorRecord[] An array of OperatorRecord objects representing the operators.
* @throws DatabaseOperationException If there is an error during the database operation.
*/
public static function getOperators(int $limit=100, int $page=1): array
{
if($limit < 1 || $page < 1)
{
throw new InvalidArgumentException('Limit and page must be greater than 0.');
}
$offset = ($page - 1) * $limit;
try
{
$stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM operators LIMIT :limit OFFSET :offset");
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$operators = [];
while($data = $stmt->fetch())
{
$operators[] = new OperatorRecord($data);
}
return $operators;
}
catch (PDOException $e)
{
throw new DatabaseOperationException('Failed to retrieve operators', 0, $e);
}
}
} }

View file

@ -0,0 +1,50 @@
<?php
namespace FederationServer\Methods\Operators;
use FederationServer\Classes\Managers\OperatorManager;
use FederationServer\Classes\RequestHandler;
use FederationServer\Exceptions\DatabaseOperationException;
use FederationServer\Exceptions\RequestException;
use FederationServer\FederationServer;
class ListOperators extends RequestHandler
{
/**
* @inheritDoc
*/
public static function handleRequest(): void
{
$authenticatedOperator = FederationServer::getAuthenticatedOperator();
if(!$authenticatedOperator->canManageOperators())
{
throw new RequestException('Unauthorized: Insufficient permissions to list operators', 403);
}
$limit = (int) (FederationServer::getParameter('limit') ?? 100);
$page = (int) (FederationServer::getParameter('page') ?? 1);
if($limit < 1)
{
$limit = 100;
}
if($page < 1)
{
$page = 1;
}
try
{
$operators = OperatorManager::getOperators($limit, $page);
}
catch (DatabaseOperationException $e)
{
throw new RequestException('Internal Server Error: Unable to retrieve operators', 500, $e);
}
$result = array_map(fn($op) => $op->toArray(), $operators);
self::successResponse($result);
}
}