Refactor SettingsUpdateInformationPrivacy and PeerInformationManager to ensure proper UUID validation and improve error handling for privacy state updates

https://github.com/nosial/Socialbox-PHP/issues/69
This commit is contained in:
netkas 2025-03-12 14:16:34 -04:00
parent 9c8945141c
commit 2967c87990
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
2 changed files with 16 additions and 8 deletions

View file

@ -27,7 +27,7 @@
{
throw new MissingRpcArgumentException('field');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
$fieldName = InformationFieldName::tryFrom(strtoupper((string)$rpcRequest->getParameter('field')));
if($fieldName === null)
{
throw new InvalidRpcArgumentException('field');
@ -39,7 +39,7 @@
throw new MissingRpcArgumentException('privacy');
}
$privacy = PrivacyState::tryFrom(strtoupper($rpcRequest->getParameter('privacy')));
$privacy = PrivacyState::tryFrom(strtoupper((string)$rpcRequest->getParameter('privacy')));
if($privacy === null)
{
throw new InvalidRpcArgumentException('privacy');
@ -47,20 +47,19 @@
try
{
$peer = $request->getPeer();
if(!PeerInformationManager::fieldExists($peer, $fieldName))
$requestingPeer = $request->getPeer();
if(!PeerInformationManager::fieldExists($requestingPeer, $fieldName))
{
return $rpcRequest->produceResponse(false);
}
PeerInformationManager::updatePrivacyState($peer, $fieldName, $privacy);
PeerInformationManager::updatePrivacyState($requestingPeer, $fieldName, $privacy);
}
catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to update the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -2,9 +2,11 @@
namespace Socialbox\Managers;
use InvalidArgumentException;
use PDOException;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database;
use Socialbox\Classes\Validator;
use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
@ -113,10 +115,14 @@
{
$peerUuid = $peerUuid->getUuid();
}
elseif(!Validator::validateUuid($peerUuid))
{
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
if(!self::fieldExists($peerUuid, $property))
{
throw new \InvalidArgumentException(sprintf('Cannot update privacy state, the requested property %s does not exist with %s', $property->value, $peerUuid));
throw new InvalidArgumentException(sprintf('Cannot update privacy state, the requested property %s does not exist with %s', $property->value, $peerUuid));
}
try
@ -149,6 +155,10 @@
{
$peerUuid = $peerUuid->getUuid();
}
elseif(!Validator::validateUuid($peerUuid))
{
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
try
{
@ -253,7 +263,6 @@
}
$results = [];
/** @var PrivacyState $privacyState */
foreach($privacyFilters as $privacyState)
{
try