Add EnableOperator method to manage operator status and log actions
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
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:
parent
bf36bb6847
commit
aaa2b5ab70
2 changed files with 97 additions and 3 deletions
|
@ -6,12 +6,14 @@
|
||||||
use FederationServer\Methods\CreateOperator;
|
use FederationServer\Methods\CreateOperator;
|
||||||
use FederationServer\Methods\DeleteOperator;
|
use FederationServer\Methods\DeleteOperator;
|
||||||
use FederationServer\Methods\DownloadAttachment;
|
use FederationServer\Methods\DownloadAttachment;
|
||||||
|
use FederationServer\Methods\EnableOperator;
|
||||||
use FederationServer\Methods\UploadAttachment;
|
use FederationServer\Methods\UploadAttachment;
|
||||||
|
|
||||||
enum Method
|
enum Method
|
||||||
{
|
{
|
||||||
case CREATE_OPERATOR;
|
case CREATE_OPERATOR;
|
||||||
case DELETE_OPERATOR;
|
case DELETE_OPERATOR;
|
||||||
|
case ENABLE_OPERATOR;
|
||||||
|
|
||||||
case UPLOAD_ATTACHMENT;
|
case UPLOAD_ATTACHMENT;
|
||||||
case DOWNLOAD_ATTACHMENT;
|
case DOWNLOAD_ATTACHMENT;
|
||||||
|
@ -32,6 +34,9 @@
|
||||||
case self::DELETE_OPERATOR:
|
case self::DELETE_OPERATOR:
|
||||||
DeleteOperator::handleRequest();
|
DeleteOperator::handleRequest();
|
||||||
break;
|
break;
|
||||||
|
case self::ENABLE_OPERATOR:
|
||||||
|
EnableOperator::handleRequest();
|
||||||
|
break;
|
||||||
|
|
||||||
case self::UPLOAD_ATTACHMENT:
|
case self::UPLOAD_ATTACHMENT:
|
||||||
UploadAttachment::handleRequest();
|
UploadAttachment::handleRequest();
|
||||||
|
@ -54,10 +59,13 @@
|
||||||
return match (true)
|
return match (true)
|
||||||
{
|
{
|
||||||
$requestMethod === 'POST' && $path === '/' => null,
|
$requestMethod === 'POST' && $path === '/' => null,
|
||||||
|
|
||||||
preg_match('#^/attachment/([a-fA-F0-9\-]{36,})$#', $path) => Method::DOWNLOAD_ATTACHMENT,
|
preg_match('#^/attachment/([a-fA-F0-9\-]{36,})$#', $path) => Method::DOWNLOAD_ATTACHMENT,
|
||||||
($requestMethod === 'POST' | $requestMethod === 'PUT') && $path === '/uploadAttachment' => Method::UPLOAD_ATTACHMENT,
|
($requestMethod === 'POST' | $requestMethod === 'PUT') && $path === '/attachment/upload' => Method::UPLOAD_ATTACHMENT,
|
||||||
$requestMethod === 'POST' && $path === '/createOperator' => Method::CREATE_OPERATOR,
|
|
||||||
$requestMethod === 'DELETE' && $path === '/deleteOperator' => Method::DELETE_OPERATOR,
|
$requestMethod === 'POST' && $path === '/operators/create' => Method::CREATE_OPERATOR,
|
||||||
|
$requestMethod === 'DELETE' && $path === '/operators/delete' => Method::DELETE_OPERATOR,
|
||||||
|
$requestMethod === 'POST' && $path === '/operators/enable' => Method::ENABLE_OPERATOR,
|
||||||
default => null,
|
default => null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
86
src/FederationServer/Methods/EnableOperator.php
Normal file
86
src/FederationServer/Methods/EnableOperator.php
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FederationServer\Methods;
|
||||||
|
|
||||||
|
use FederationServer\Classes\Enums\AuditLogType;
|
||||||
|
use FederationServer\Classes\Logger;
|
||||||
|
use FederationServer\Classes\Managers\AuditLogManager;
|
||||||
|
use FederationServer\Classes\Managers\OperatorManager;
|
||||||
|
use FederationServer\Classes\RequestHandler;
|
||||||
|
use FederationServer\Exceptions\DatabaseOperationException;
|
||||||
|
use FederationServer\Exceptions\RequestException;
|
||||||
|
use FederationServer\FederationServer;
|
||||||
|
|
||||||
|
class EnableOperator extends RequestHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public static function handleRequest(): void
|
||||||
|
{
|
||||||
|
$authenticatedOperator = FederationServer::getAuthenticatedOperator();
|
||||||
|
|
||||||
|
// Ensure the authenticated operator has permission to delete operators.
|
||||||
|
if(!$authenticatedOperator->canManageOperators())
|
||||||
|
{
|
||||||
|
throw new RequestException('Unauthorized: Insufficient permissions to enable/disable operators', 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!FederationServer::getParameter('uuid'))
|
||||||
|
{
|
||||||
|
throw new RequestException('Bad Request: Operator UUID is required', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!FederationServer::getParameter('enabled'))
|
||||||
|
{
|
||||||
|
throw new RequestException('Bad Request: Enabled status is required', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled = filter_var(FederationServer::getParameter('enabled'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||||
|
if($enabled === null)
|
||||||
|
{
|
||||||
|
throw new RequestException('Bad Request: Invalid enabled status', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$existingOperator = OperatorManager::getOperator(FederationServer::getParameter('uuid'));
|
||||||
|
if($existingOperator === null)
|
||||||
|
{
|
||||||
|
throw new RequestException('Operator Not Found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($enabled)
|
||||||
|
{
|
||||||
|
OperatorManager::enableOperator(FederationServer::getParameter('uuid'));
|
||||||
|
AuditLogManager::createEntry(AuditLogType::OPERATOR_ENABLED, sprintf('Operator %s (%s) enabled by %s (%s)',
|
||||||
|
$existingOperator->getName(),
|
||||||
|
$existingOperator->getUuid(),
|
||||||
|
$authenticatedOperator->getName(),
|
||||||
|
$authenticatedOperator->getUuid()
|
||||||
|
), $authenticatedOperator->getUuid());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OperatorManager::disableOperator(FederationServer::getParameter('uuid'));
|
||||||
|
AuditLogManager::createEntry(AuditLogType::OPERATOR_DISABLED, sprintf('Operator %s (%s) disabled by %s (%s)',
|
||||||
|
$existingOperator->getName(),
|
||||||
|
$existingOperator->getUuid(),
|
||||||
|
$authenticatedOperator->getName(),
|
||||||
|
$authenticatedOperator->getUuid()
|
||||||
|
), $authenticatedOperator->getUuid());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(DatabaseOperationException $e)
|
||||||
|
{
|
||||||
|
Logger::log()->error(sprintf('Database error while %s the operator: %s',
|
||||||
|
$enabled ? 'enabling' : 'disabling',
|
||||||
|
$e->getMessage()), $e
|
||||||
|
);
|
||||||
|
throw new RequestException('Internal Server Error: Unable to ' . ($enabled ? 'enable' : 'disable') . ' operator', 500, $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Respond with the UUID of the newly created operator.
|
||||||
|
self::successResponse();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue