From 508d4565d96f84566b9aa8227c2794f33dba3cd7 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 31 Jan 2025 18:13:45 -0500 Subject: [PATCH] Added existence check --- .../Settings/SettingsDeleteSignature.php | 16 +++++++++--- src/Socialbox/Managers/SigningKeysManager.php | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteSignature.php b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteSignature.php index 0211037..176163c 100644 --- a/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteSignature.php +++ b/src/Socialbox/Classes/StandardMethods/Settings/SettingsDeleteSignature.php @@ -7,6 +7,9 @@ use ncc\ThirdParty\Symfony\Uid\Uuid; use Socialbox\Abstracts\Method; use Socialbox\Enums\StandardError; + use Socialbox\Exceptions\DatabaseOperationException; + use Socialbox\Exceptions\Standard\InvalidRpcArgumentException; + use Socialbox\Exceptions\Standard\MissingRpcArgumentException; use Socialbox\Exceptions\Standard\StandardRpcException; use Socialbox\Interfaces\SerializableInterface; use Socialbox\Managers\SigningKeysManager; @@ -22,23 +25,28 @@ { if(!$rpcRequest->containsParameter('uuid')) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'uuid' parameter"); + throw new MissingRpcArgumentException('uuid'); } try { $uuid = Uuid::fromString($rpcRequest->getParameter('uuid')); } - catch(InvalidArgumentException $e) + catch(InvalidArgumentException) { - return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid UUID', $e); + throw new InvalidRpcArgumentException('uuid'); } try { + if(!SigningKeysManager::signingKeyExists($request->getPeer()->getUuid(), $uuid)) + { + return $rpcRequest->produceResponse(false); + } + SigningKeysManager::deleteSigningKey($request->getPeer()->getUuid(), $uuid); } - catch(Exception $e) + catch(DatabaseOperationException $e) { throw new StandardRpcException('Failed to delete the signing key', StandardError::INTERNAL_SERVER_ERROR, $e); } diff --git a/src/Socialbox/Managers/SigningKeysManager.php b/src/Socialbox/Managers/SigningKeysManager.php index 2b78fa7..3dde942 100644 --- a/src/Socialbox/Managers/SigningKeysManager.php +++ b/src/Socialbox/Managers/SigningKeysManager.php @@ -195,6 +195,31 @@ } } + /** + * Checks if a signing key exists in the database using the provided UUID. + * + * @param string $peerUuid The UUID of the peer associated with the signing key. + * @param string $uuid The UUID of the signing key to check. + * @return bool True if the signing key exists, false otherwise. + * @throws DatabaseOperationException If a database error occurs during the operation. + */ + public static function signingKeyExists(string $peerUuid, string $uuid): bool + { + try + { + $statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM signing_keys WHERE uuid=:uuid AND peer_uuid=:peer_uuid"); + $statement->bindParam(':uuid', $uuid); + $statement->bindParam(':peer_uuid', $peerUuid); + $statement->execute(); + + return $statement->fetchColumn() > 0; + } + catch (PDOException $e) + { + throw new DatabaseOperationException('Failed to check if the signing key exists in the database', $e); + } + } + /** * Deletes a signing key from the database using the provided UUID. *