diff --git a/src/FederationServer/Classes/Enums/Method.php b/src/FederationServer/Classes/Enums/Method.php index 623899b..4187729 100644 --- a/src/FederationServer/Classes/Enums/Method.php +++ b/src/FederationServer/Classes/Enums/Method.php @@ -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, diff --git a/src/FederationServer/Methods/Operators/DisableOperator.php b/src/FederationServer/Methods/Operators/DisableOperator.php new file mode 100644 index 0000000..f49e1f8 --- /dev/null +++ b/src/FederationServer/Methods/Operators/DisableOperator.php @@ -0,0 +1,70 @@ +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(); + } + } \ No newline at end of file diff --git a/src/FederationServer/Methods/Operators/EnableOperator.php b/src/FederationServer/Methods/Operators/EnableOperator.php index d58f0d5..2bdc8c9 100644 --- a/src/FederationServer/Methods/Operators/EnableOperator.php +++ b/src/FederationServer/Methods/Operators/EnableOperator.php @@ -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.