Add ManageBlacklistPermission method to handle blacklist permission management with validation

This commit is contained in:
netkas 2025-06-03 00:40:44 -04:00
parent 7ab602eef0
commit 9358366f3c
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
2 changed files with 56 additions and 0 deletions

View file

@ -3,6 +3,7 @@
namespace FederationServer\Classes\Enums;
use FederationServer\Exceptions\RequestException;
use FederationServer\Methods\ManageBlacklistPermission;
use FederationServer\Methods\ManageOperatorsPermission;
use FederationServer\Methods\CreateOperator;
use FederationServer\Methods\DeleteOperator;
@ -20,6 +21,7 @@
case GET_OPERATOR;
case REFRESH_OPERATOR_API_KEY;
case MANAGE_OPERATORS_PERMISSION;
case MANAGE_BLACKLIST_PERMISSION;
case UPLOAD_ATTACHMENT;
case DOWNLOAD_ATTACHMENT;
@ -52,6 +54,9 @@
case self::MANAGE_OPERATORS_PERMISSION:
ManageOperatorsPermission::handleRequest();
break;
case self::MANAGE_BLACKLIST_PERMISSION:
ManageBlacklistPermission::handleRequest();
break;
case self::UPLOAD_ATTACHMENT:
UploadAttachment::handleRequest();
@ -84,6 +89,7 @@
$requestMethod === 'POST' && $path === '/operators/enable' => Method::ENABLE_OPERATOR,
$requestMethod === 'POST' && $path === '/operators/refresh' => Method::REFRESH_OPERATOR_API_KEY,
$requestMethod === 'POST' && $path === '/operators/permissions/manage_operators' => Method::MANAGE_OPERATORS_PERMISSION,
$requestMethod === 'POST' && $path === '/operators/permissions/manage_blacklist' => Method::MANAGE_BLACKLIST_PERMISSION,
default => null,
};

View file

@ -0,0 +1,50 @@
<?php
namespace FederationServer\Methods;
use FederationServer\Classes\Logger;
use FederationServer\Classes\Managers\OperatorManager;
use FederationServer\Classes\RequestHandler;
use FederationServer\Classes\Validate;
use FederationServer\Exceptions\DatabaseOperationException;
use FederationServer\Exceptions\RequestException;
use FederationServer\FederationServer;
class ManageBlacklistPermission extends RequestHandler
{
/**
* @inheritDoc
*/
public static function handleRequest(): void
{
$authenticatedOperator = FederationServer::getAuthenticatedOperator();
if(!$authenticatedOperator->canManageOperators())
{
throw new RequestException('Unauthorized: Insufficient permissions manage permissions', 403);
}
$operatorUuid = FederationServer::getParameter('uuid');
$enabled = (bool)filter_var(FederationServer::getParameter('enabled'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if($operatorUuid === null)
{
throw new RequestException('Bad Request: Missing required parameters', 400);
}
if(!Validate::uuid($operatorUuid))
{
throw new RequestException('Bad Request: Invalid operator UUID', 400);
}
try
{
OperatorManager::setManageBlacklist($operatorUuid, $enabled);
}
catch(DatabaseOperationException $e)
{
Logger::log()->error('Database error while managing operator\'s permissions: ' . $e->getMessage(), $e);
throw new RequestException('Internal Server Error: Unable to manage operator\'s permissions', 500, $e);
}
self::successResponse();
}
}