From aaa2b5ab70c016a200995544d70b0cdb9d4bbb58 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 2 Jun 2025 21:30:53 -0400 Subject: [PATCH] Add EnableOperator method to manage operator status and log actions --- src/FederationServer/Classes/Enums/Method.php | 14 ++- .../Methods/EnableOperator.php | 86 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/FederationServer/Methods/EnableOperator.php diff --git a/src/FederationServer/Classes/Enums/Method.php b/src/FederationServer/Classes/Enums/Method.php index e98acdc..1334dae 100644 --- a/src/FederationServer/Classes/Enums/Method.php +++ b/src/FederationServer/Classes/Enums/Method.php @@ -6,12 +6,14 @@ use FederationServer\Methods\CreateOperator; use FederationServer\Methods\DeleteOperator; use FederationServer\Methods\DownloadAttachment; + use FederationServer\Methods\EnableOperator; use FederationServer\Methods\UploadAttachment; enum Method { case CREATE_OPERATOR; case DELETE_OPERATOR; + case ENABLE_OPERATOR; case UPLOAD_ATTACHMENT; case DOWNLOAD_ATTACHMENT; @@ -32,6 +34,9 @@ case self::DELETE_OPERATOR: DeleteOperator::handleRequest(); break; + case self::ENABLE_OPERATOR: + EnableOperator::handleRequest(); + break; case self::UPLOAD_ATTACHMENT: UploadAttachment::handleRequest(); @@ -54,10 +59,13 @@ return match (true) { $requestMethod === 'POST' && $path === '/' => null, + preg_match('#^/attachment/([a-fA-F0-9\-]{36,})$#', $path) => Method::DOWNLOAD_ATTACHMENT, - ($requestMethod === 'POST' | $requestMethod === 'PUT') && $path === '/uploadAttachment' => Method::UPLOAD_ATTACHMENT, - $requestMethod === 'POST' && $path === '/createOperator' => Method::CREATE_OPERATOR, - $requestMethod === 'DELETE' && $path === '/deleteOperator' => Method::DELETE_OPERATOR, + ($requestMethod === 'POST' | $requestMethod === 'PUT') && $path === '/attachment/upload' => Method::UPLOAD_ATTACHMENT, + + $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, }; diff --git a/src/FederationServer/Methods/EnableOperator.php b/src/FederationServer/Methods/EnableOperator.php new file mode 100644 index 0000000..fd75f02 --- /dev/null +++ b/src/FederationServer/Methods/EnableOperator.php @@ -0,0 +1,86 @@ +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(); + } + } \ No newline at end of file