Refactor AddressBookUpdateRelationship and ContactManager to improve variable naming and add UUID validation

https://github.com/nosial/Socialbox-PHP/issues/37
This commit is contained in:
netkas 2025-03-11 15:20:56 -04:00
parent f01df7b4c4
commit 50c638b534
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
2 changed files with 28 additions and 7 deletions

View file

@ -30,7 +30,7 @@
try try
{ {
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); $receivingPeerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
} }
catch(InvalidArgumentException $e) catch(InvalidArgumentException $e)
{ {
@ -41,8 +41,8 @@
{ {
throw new MissingRpcArgumentException('relationship'); throw new MissingRpcArgumentException('relationship');
} }
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); $newRelationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null) if($newRelationship === null)
{ {
throw new InvalidRpcArgumentException('relationship'); throw new InvalidRpcArgumentException('relationship');
} }
@ -50,14 +50,14 @@
try try
{ {
// Check if the contact already exists // Check if the contact already exists
$peer = $request->getPeer(); $requestingPeer = $request->getPeer();
if(!ContactManager::isContact($peer, $address)) if(!ContactManager::isContact($requestingPeer->getUuid(), $receivingPeerAddress))
{ {
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist'); return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist');
} }
// Create the contact // Create the contact
ContactManager::updateContactRelationship($peer, $address, $relationship); ContactManager::updateContactRelationship($requestingPeer->getUuid(), $receivingPeerAddress, $newRelationship);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {

View file

@ -2,10 +2,13 @@
namespace Socialbox\Managers; namespace Socialbox\Managers;
use DateTime;
use InvalidArgumentException;
use ncc\ThirdParty\Symfony\Uid\UuidV4; use ncc\ThirdParty\Symfony\Uid\UuidV4;
use PDO; use PDO;
use PDOException; use PDOException;
use Socialbox\Classes\Database; use Socialbox\Classes\Database;
use Socialbox\Classes\Validator;
use Socialbox\Enums\Types\ContactRelationshipType; use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Objects\Database\ContactDatabaseRecord; use Socialbox\Objects\Database\ContactDatabaseRecord;
@ -30,6 +33,15 @@
{ {
$contactAddress = $contactAddress->getAddress(); $contactAddress = $contactAddress->getAddress();
} }
elseif(!Validator::validateUuid($contactAddress))
{
throw new InvalidArgumentException('The given contact address is invalid');
}
if(!Validator::validateUuid($peerUuid))
{
throw new InvalidArgumentException('The given peer internal UUID is not a valid UUID V4');
}
try try
{ {
@ -188,6 +200,15 @@
{ {
$contactAddress = $contactAddress->getAddress(); $contactAddress = $contactAddress->getAddress();
} }
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException('The given contact address is not a valid peer address');
}
if(!Validator::validateUuid($peerUuid))
{
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
try try
{ {
@ -360,7 +381,7 @@
$statement->bindParam(':expires', $expires); $statement->bindParam(':expires', $expires);
$created = $signingKey->getCreated(); $created = $signingKey->getCreated();
$statement->bindParam(':created', $created); $statement->bindParam(':created', $created);
$trustedOn = (new \DateTime())->format('Y-m-d H:i:s'); $trustedOn = (new DateTime())->format('Y-m-d H:i:s');
$statement->bindParam(':trusted_on', $trustedOn); $statement->bindParam(':trusted_on', $trustedOn);
} }
catch(PDOException $e) catch(PDOException $e)