Add PushEntity class and enhance EntitiesManager with UUID handling and existence checks
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
d43258af4c
commit
14effd7ef8
3 changed files with 147 additions and 2 deletions
|
@ -10,6 +10,7 @@
|
|||
use FederationServer\Methods\Audit\ViewAuditEntry;
|
||||
use FederationServer\Methods\Entities\GetEntity;
|
||||
use FederationServer\Methods\Entities\ListEntities;
|
||||
use FederationServer\Methods\Entities\PushEntity;
|
||||
use FederationServer\Methods\Entities\QueryEntity;
|
||||
use FederationServer\Methods\Operators\CreateOperator;
|
||||
use FederationServer\Methods\Operators\DeleteOperator;
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
class EntitiesManager
|
||||
{
|
||||
|
@ -19,7 +20,7 @@
|
|||
* @throws InvalidArgumentException If the ID exceeds 255 characters or if the domain is invalid.
|
||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||
*/
|
||||
public static function registerEntity(string $id, ?string $domain=null): void
|
||||
public static function registerEntity(string $id, ?string $domain=null): string
|
||||
{
|
||||
if(strlen($id) > 255)
|
||||
{
|
||||
|
@ -34,9 +35,12 @@
|
|||
throw new InvalidArgumentException("Domain cannot exceed 255 characters.");
|
||||
}
|
||||
|
||||
$uuid = Uuid::v4()->toRfc4122();
|
||||
|
||||
try
|
||||
{
|
||||
$stmt = DatabaseConnection::getConnection()->prepare("INSERT INTO entities (id, domain) VALUES (:id, :domain)");
|
||||
$stmt = DatabaseConnection::getConnection()->prepare("INSERT INTO entities (uuid, id, domain) VALUES (:uuid, :id, :domain)");
|
||||
$stmt->bindParam(':uuid', $uuid);
|
||||
$stmt->bindParam(':id', $id);
|
||||
$stmt->bindParam(':domain', $domain);
|
||||
$stmt->execute();
|
||||
|
@ -45,6 +49,8 @@
|
|||
{
|
||||
throw new DatabaseOperationException("Failed to register entity: " . $e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,6 +137,78 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an entity exists by its ID and domain.
|
||||
*
|
||||
* @param string $id The ID of the entity.
|
||||
* @param string|null $domain The domain of the entity, can be null.
|
||||
* @return bool True if the entity exists, false otherwise.
|
||||
* @throws InvalidArgumentException If the ID is not provided or is invalid.
|
||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||
*/
|
||||
public static function entityExists(string $id, ?string $domain): bool
|
||||
{
|
||||
if(strlen($id) < 1)
|
||||
{
|
||||
throw new InvalidArgumentException("Entity ID must be provided.");
|
||||
}
|
||||
|
||||
if(!is_null($domain) && !filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME))
|
||||
{
|
||||
throw new InvalidArgumentException("Invalid domain format.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(is_null($domain))
|
||||
{
|
||||
$stmt = DatabaseConnection::getConnection()->prepare("SELECT COUNT(*) FROM entities WHERE id = :id");
|
||||
$stmt->bindParam(':id', $id);
|
||||
}
|
||||
else
|
||||
{
|
||||
$stmt = DatabaseConnection::getConnection()->prepare("SELECT COUNT(*) FROM entities WHERE id = :id AND domain = :domain");
|
||||
$stmt->bindParam(':id', $id);
|
||||
$stmt->bindParam(':domain', $domain);
|
||||
}
|
||||
|
||||
$stmt->execute();
|
||||
return (bool)$stmt->fetchColumn();
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
throw new DatabaseOperationException("Failed to check entity existence: " . $e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an entity exists by its UUID.
|
||||
*
|
||||
* @param string $uuid The UUID of the entity.
|
||||
* @return bool True if the entity exists, false otherwise.
|
||||
* @throws InvalidArgumentException If the UUID is not provided or is invalid.
|
||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||
*/
|
||||
public static function entityExistsByUuid(string $uuid): bool
|
||||
{
|
||||
if(strlen($uuid) < 1)
|
||||
{
|
||||
throw new InvalidArgumentException("Entity UUID must be provided.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$stmt = DatabaseConnection::getConnection()->prepare("SELECT COUNT(*) FROM entities WHERE uuid = :uuid");
|
||||
$stmt->bindParam(':uuid', $uuid);
|
||||
$stmt->execute();
|
||||
return (bool)$stmt->fetchColumn();
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
throw new DatabaseOperationException("Failed to check entity existence by UUID: " . $e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an entity by its UUID.
|
||||
*
|
||||
|
|
66
src/FederationServer/Methods/Entities/PushEntity.php
Normal file
66
src/FederationServer/Methods/Entities/PushEntity.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace FederationServer\Methods\Entities;
|
||||
|
||||
use FederationServer\Classes\Managers\EntitiesManager;
|
||||
use FederationServer\Classes\RequestHandler;
|
||||
use FederationServer\Exceptions\DatabaseOperationException;
|
||||
use FederationServer\Exceptions\RequestException;
|
||||
use FederationServer\FederationServer;
|
||||
|
||||
class PushEntity extends RequestHandler
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function handleRequest(): void
|
||||
{
|
||||
$authenticatedOperator = FederationServer::getAuthenticatedOperator();
|
||||
if(!$authenticatedOperator->isClient() && !$authenticatedOperator->canManageOperators())
|
||||
{
|
||||
throw new RequestException('Unauthorized: Insufficient permissions to push entities', 403);
|
||||
}
|
||||
|
||||
$id = FederationServer::getParameter('id');
|
||||
$domain = FederationServer::getParameter('domain') ?? null;
|
||||
|
||||
if(!$id)
|
||||
{
|
||||
throw new RequestException('Bad Request: Entity ID is required', 400);
|
||||
}
|
||||
|
||||
if(strlen($id) > 255)
|
||||
{
|
||||
throw new RequestException('Bad Request: Entity ID exceeds maximum length of 255 characters', 400);
|
||||
}
|
||||
|
||||
if(!is_null($domain) && !filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME))
|
||||
{
|
||||
throw new RequestException('Bad Request: Invalid domain format', 400);
|
||||
}
|
||||
|
||||
if(!is_null($domain) && strlen($domain) > 255)
|
||||
{
|
||||
throw new RequestException('Bad Request: Domain exceeds maximum length of 255 characters', 400);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(!EntitiesManager::entityExists($id, $domain))
|
||||
{
|
||||
$entityUuid = EntitiesManager::registerEntity($id, $domain);
|
||||
}
|
||||
else
|
||||
{
|
||||
$entityUuid = EntitiesManager::getEntity($id, $domain)->getUuid();
|
||||
}
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new RequestException('Internal Server Error: Unable to register entity', 500, $e);
|
||||
}
|
||||
|
||||
self::successResponse($entityUuid);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue