Add DisableOperator class and update Method.php for operator disabling functionality

This commit is contained in:
netkas 2025-06-03 17:10:24 -04:00
parent 005bf2e1f8
commit 852a2b03de
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
3 changed files with 88 additions and 34 deletions

View file

@ -10,6 +10,7 @@
use FederationServer\Methods\Audit\ViewAuditEntry;
use FederationServer\Methods\Operators\CreateOperator;
use FederationServer\Methods\Operators\DeleteOperator;
use FederationServer\Methods\Operators\DisableOperator;
use FederationServer\Methods\Operators\EnableOperator;
use FederationServer\Methods\Operators\GetOperator;
use FederationServer\Methods\Operators\ListOperators;
@ -27,6 +28,7 @@
case CREATE_OPERATOR;
case DELETE_OPERATOR;
case ENABLE_OPERATOR;
case DISABLE_OPERATOR;
case GET_OPERATOR;
case REFRESH_OPERATOR_API_KEY;
case MANAGE_OPERATORS_PERMISSION;
@ -89,6 +91,9 @@
case self::ENABLE_OPERATOR:
EnableOperator::handleRequest();
break;
case self::DISABLE_OPERATOR:
DisableOperator::handleRequest();
break;
case self::REFRESH_OPERATOR_API_KEY:
RefreshOperatorApiKey::handleRequest();
break;
@ -130,6 +135,7 @@
preg_match('#^/operators/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'GET' => Method::GET_OPERATOR,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'DELETE' => Method::DELETE_OPERATOR,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})/enable$#', $path) && $requestMethod === 'POST' => Method::ENABLE_OPERATOR,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})/disable$#', $path) && $requestMethod === 'POST' => Method::DISABLE_OPERATOR,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})/refresh$#', $path) && $requestMethod === 'POST' => Method::REFRESH_OPERATOR_API_KEY,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})/manage_operators$#', $path) && $requestMethod === 'POST' => Method::MANAGE_OPERATORS_PERMISSION,
preg_match('#^/operators/([a-fA-F0-9\-]{36,})/manage_blacklist$#', $path) && $requestMethod === 'POST' => Method::MANAGE_BLACKLIST_PERMISSION,

View file

@ -0,0 +1,70 @@
<?php
namespace FederationServer\Methods\Operators;
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 DisableOperator 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(!preg_match('#^/operators/([a-fA-F0-9\-]{36,})/disable$#', FederationServer::getPath(), $matches))
{
throw new RequestException('Bad Request: Operator UUID is required', 400);
}
$operatorUuid = $matches[1];
if(!$operatorUuid)
{
throw new RequestException('Bad Request: Operator UUID is required', 400);
}
try
{
$existingOperator = OperatorManager::getOperator($operatorUuid);
if($existingOperator === null)
{
throw new RequestException('Operator Not Found', 404);
}
if($existingOperator->isDisabled())
{
throw new RequestException('Operator is already enabled', 400);
}
OperatorManager::disableOperator($operatorUuid);
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 disablinf the operator: %s', $e->getMessage()), $e);
throw new RequestException('Internal Server Error: Unable to disable operator', 500, $e);
}
// Respond with the UUID of the newly created operator.
self::successResponse();
}
}

View file

@ -37,17 +37,6 @@
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($operatorUuid);
@ -56,34 +45,23 @@
throw new RequestException('Operator Not Found', 404);
}
if($enabled)
if(!$existingOperator->isDisabled())
{
OperatorManager::enableOperator($operatorUuid);
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($operatorUuid);
AuditLogManager::createEntry(AuditLogType::OPERATOR_DISABLED, sprintf('Operator %s (%s) disabled by %s (%s)',
$existingOperator->getName(),
$existingOperator->getUuid(),
$authenticatedOperator->getName(),
$authenticatedOperator->getUuid()
), $authenticatedOperator->getUuid());
throw new RequestException('Operator is already enabled', 400);
}
OperatorManager::enableOperator($operatorUuid);
AuditLogManager::createEntry(AuditLogType::OPERATOR_ENABLED, sprintf('Operator %s (%s) enabled 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);
Logger::log()->error(sprintf('Database error while enabling the operator: %s', $e->getMessage()), $e);
throw new RequestException('Internal Server Error: Unable to enable operator', 500, $e);
}
// Respond with the UUID of the newly created operator.