Add SettingsSetDisplayName method and refactor unused imports

This commit is contained in:
netkas 2024-12-24 00:51:13 -05:00
parent 9da2ac2db2
commit 85bdff7d3c
15 changed files with 98 additions and 16 deletions

View file

@ -2,8 +2,6 @@
namespace Socialbox\Classes;
use mysqli;
use mysqli_sql_exception;
use PDO;
use PDOException;
use Socialbox\Exceptions\DatabaseOperationException;

View file

@ -2,7 +2,6 @@
namespace Socialbox\Classes;
use InvalidArgumentException;
use Socialbox\Enums\DatabaseObjects;
class Resources

View file

@ -5,7 +5,6 @@
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Resources;
use Socialbox\Enums\StandardError;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;

View file

@ -5,7 +5,6 @@
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Resources;
use Socialbox\Enums\StandardError;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;

View file

@ -5,7 +5,6 @@
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Resources;
use Socialbox\Enums\StandardError;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;

View file

@ -0,0 +1,46 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsSetDisplayName extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('name'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'name' parameter");
}
try
{
// Set the password
RegisteredPeerManager::updateDisplayName($request->getPeer(), $rpcRequest->getParameter('name'));
// Remove the SET_PASSWORD flag
SessionManager::removeFlags($request->getSessionUuid(), [SessionFlags::SET_DISPLAY_NAME]);
// Check & update the session flow
SessionManager::updateFlow($request->getSession());
}
catch(Exception $e)
{
throw new StandardException('Failed to set password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -2,12 +2,10 @@
namespace Socialbox\Classes;
use DateTime;
use InvalidArgumentException;
use JsonException;
use RuntimeException;
use Socialbox\Enums\StandardHeaders;
use Socialbox\Objects\PeerAddress;
use Throwable;
class Utilities

View file

@ -2,8 +2,6 @@
namespace Socialbox\Enums\Flags;
use Socialbox\Classes\Logger;
enum PeerFlags : string
{
// Administrative Flags

View file

@ -2,6 +2,7 @@
namespace Socialbox\Managers;
use InvalidArgumentException;
use PDO;
use PDOException;
use Socialbox\Classes\Configuration;
@ -317,6 +318,50 @@
}
}
/**
* Updates the display name of a registered peer based on the given unique identifier or RegisteredPeerRecord object.
*
* @param string|RegisteredPeerRecord $peer The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
* @param string $name The new
*/
public static function updateDisplayName(string|RegisteredPeerRecord $peer, string $name): void
{
if(empty($name))
{
throw new InvalidArgumentException('The display name cannot be empty');
}
if(strlen($name) > 256)
{
throw new InvalidArgumentException('The display name cannot exceed 256 characters');
}
if(is_string($peer))
{
$peer = self::getPeer($peer);
}
if($peer->isExternal())
{
throw new InvalidArgumentException('Cannot update the display name of an external peer');
}
Logger::getLogger()->verbose(sprintf("Updating display name of peer %s to %s", $peer->getUuid(), $name));
try
{
$statement = Database::getConnection()->prepare('UPDATE `registered_peers` SET display_name=? WHERE uuid=?');
$statement->bindParam(1, $name);
$uuid = $peer->getUuid();
$statement->bindParam(2, $uuid);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException('Failed to update the display name of the peer in the database', $e);
}
}
/**
* Retrieves the password authentication record associated with the given unique peer identifier or a RegisteredPeerRecord object.
*

View file

@ -460,6 +460,13 @@
*/
public static function updateFlow(SessionRecord $session): void
{
// Don't do anything if the session is already authenticated
if(!in_array(SessionFlags::REGISTRATION_REQUIRED, $session->getFlags()) || !in_array(SessionFlags::AUTHENTICATION_REQUIRED, $session->getFlags()))
{
return;
}
// Check if all registration/authentication requirements are met
if(SessionFlags::isComplete($session->getFlags()))
{
SessionManager::setAuthenticated($session->getUuid(), true);

View file

@ -4,7 +4,6 @@
use InvalidArgumentException;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\SessionState;
use Socialbox\Enums\StandardHeaders;

View file

@ -4,7 +4,6 @@ namespace Socialbox\Objects\Database;
use DateTime;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Logger;
use Socialbox\Enums\Status\CaptchaStatus;
use Socialbox\Interfaces\SerializableInterface;

View file

@ -5,7 +5,6 @@
use Socialbox\Classes\Configuration;
use Socialbox\Classes\SecuredPassword;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Managers\EncryptionRecordsManager;
class EncryptionRecord
{

View file

@ -3,10 +3,8 @@
namespace Socialbox\Objects;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Classes\Logger;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;

View file

@ -3,7 +3,6 @@
namespace Socialbox\Objects\Standard;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\CaptchaRecord;
class ImageCaptcha implements SerializableInterface
{