From 85bdff7d3cad9b30a54721a481a6f8c629e51c6b Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 24 Dec 2024 00:51:13 -0500 Subject: [PATCH] Add SettingsSetDisplayName method and refactor unused imports --- src/Socialbox/Classes/Database.php | 2 - src/Socialbox/Classes/Resources.php | 1 - .../GetCommunityGuidelines.php | 1 - .../StandardMethods/GetPrivacyPolicy.php | 1 - .../StandardMethods/GetTermsOfService.php | 1 - .../SettingsSetDisplayName.php | 46 +++++++++++++++++++ src/Socialbox/Classes/Utilities.php | 2 - src/Socialbox/Enums/Flags/PeerFlags.php | 2 - .../Managers/RegisteredPeerManager.php | 45 ++++++++++++++++++ src/Socialbox/Managers/SessionManager.php | 7 +++ src/Socialbox/Objects/ClientRequest.php | 1 - .../Objects/Database/CaptchaRecord.php | 1 - .../Objects/Database/EncryptionRecord.php | 1 - src/Socialbox/Objects/RpcRequest.php | 2 - .../Objects/Standard/ImageCaptcha.php | 1 - 15 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/Socialbox/Classes/StandardMethods/SettingsSetDisplayName.php diff --git a/src/Socialbox/Classes/Database.php b/src/Socialbox/Classes/Database.php index 47fdcbc..68b3955 100644 --- a/src/Socialbox/Classes/Database.php +++ b/src/Socialbox/Classes/Database.php @@ -2,8 +2,6 @@ namespace Socialbox\Classes; -use mysqli; -use mysqli_sql_exception; use PDO; use PDOException; use Socialbox\Exceptions\DatabaseOperationException; diff --git a/src/Socialbox/Classes/Resources.php b/src/Socialbox/Classes/Resources.php index ca23c34..c44a114 100644 --- a/src/Socialbox/Classes/Resources.php +++ b/src/Socialbox/Classes/Resources.php @@ -2,7 +2,6 @@ namespace Socialbox\Classes; - use InvalidArgumentException; use Socialbox\Enums\DatabaseObjects; class Resources diff --git a/src/Socialbox/Classes/StandardMethods/GetCommunityGuidelines.php b/src/Socialbox/Classes/StandardMethods/GetCommunityGuidelines.php index 4021f57..9b4abb0 100644 --- a/src/Socialbox/Classes/StandardMethods/GetCommunityGuidelines.php +++ b/src/Socialbox/Classes/StandardMethods/GetCommunityGuidelines.php @@ -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; diff --git a/src/Socialbox/Classes/StandardMethods/GetPrivacyPolicy.php b/src/Socialbox/Classes/StandardMethods/GetPrivacyPolicy.php index db83fd0..53c6136 100644 --- a/src/Socialbox/Classes/StandardMethods/GetPrivacyPolicy.php +++ b/src/Socialbox/Classes/StandardMethods/GetPrivacyPolicy.php @@ -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; diff --git a/src/Socialbox/Classes/StandardMethods/GetTermsOfService.php b/src/Socialbox/Classes/StandardMethods/GetTermsOfService.php index 96c1d51..c0c9224 100644 --- a/src/Socialbox/Classes/StandardMethods/GetTermsOfService.php +++ b/src/Socialbox/Classes/StandardMethods/GetTermsOfService.php @@ -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; diff --git a/src/Socialbox/Classes/StandardMethods/SettingsSetDisplayName.php b/src/Socialbox/Classes/StandardMethods/SettingsSetDisplayName.php new file mode 100644 index 0000000..93b8d90 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/SettingsSetDisplayName.php @@ -0,0 +1,46 @@ +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); + } + } \ No newline at end of file diff --git a/src/Socialbox/Classes/Utilities.php b/src/Socialbox/Classes/Utilities.php index ff38b98..7d1b73b 100644 --- a/src/Socialbox/Classes/Utilities.php +++ b/src/Socialbox/Classes/Utilities.php @@ -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 diff --git a/src/Socialbox/Enums/Flags/PeerFlags.php b/src/Socialbox/Enums/Flags/PeerFlags.php index 1ca08b2..a92537a 100644 --- a/src/Socialbox/Enums/Flags/PeerFlags.php +++ b/src/Socialbox/Enums/Flags/PeerFlags.php @@ -2,8 +2,6 @@ namespace Socialbox\Enums\Flags; -use Socialbox\Classes\Logger; - enum PeerFlags : string { // Administrative Flags diff --git a/src/Socialbox/Managers/RegisteredPeerManager.php b/src/Socialbox/Managers/RegisteredPeerManager.php index e70c4cc..669ec6b 100644 --- a/src/Socialbox/Managers/RegisteredPeerManager.php +++ b/src/Socialbox/Managers/RegisteredPeerManager.php @@ -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. * diff --git a/src/Socialbox/Managers/SessionManager.php b/src/Socialbox/Managers/SessionManager.php index c96bc2a..4111e8d 100644 --- a/src/Socialbox/Managers/SessionManager.php +++ b/src/Socialbox/Managers/SessionManager.php @@ -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); diff --git a/src/Socialbox/Objects/ClientRequest.php b/src/Socialbox/Objects/ClientRequest.php index 5a368fb..00bd0fc 100644 --- a/src/Socialbox/Objects/ClientRequest.php +++ b/src/Socialbox/Objects/ClientRequest.php @@ -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; diff --git a/src/Socialbox/Objects/Database/CaptchaRecord.php b/src/Socialbox/Objects/Database/CaptchaRecord.php index 0e94d68..72b77e2 100644 --- a/src/Socialbox/Objects/Database/CaptchaRecord.php +++ b/src/Socialbox/Objects/Database/CaptchaRecord.php @@ -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; diff --git a/src/Socialbox/Objects/Database/EncryptionRecord.php b/src/Socialbox/Objects/Database/EncryptionRecord.php index e2aad9c..707c39d 100644 --- a/src/Socialbox/Objects/Database/EncryptionRecord.php +++ b/src/Socialbox/Objects/Database/EncryptionRecord.php @@ -5,7 +5,6 @@ use Socialbox\Classes\Configuration; use Socialbox\Classes\SecuredPassword; use Socialbox\Exceptions\CryptographyException; - use Socialbox\Managers\EncryptionRecordsManager; class EncryptionRecord { diff --git a/src/Socialbox/Objects/RpcRequest.php b/src/Socialbox/Objects/RpcRequest.php index b0ffa88..6bc1d94 100644 --- a/src/Socialbox/Objects/RpcRequest.php +++ b/src/Socialbox/Objects/RpcRequest.php @@ -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; diff --git a/src/Socialbox/Objects/Standard/ImageCaptcha.php b/src/Socialbox/Objects/Standard/ImageCaptcha.php index ed66b24..cbcc158 100644 --- a/src/Socialbox/Objects/Standard/ImageCaptcha.php +++ b/src/Socialbox/Objects/Standard/ImageCaptcha.php @@ -3,7 +3,6 @@ namespace Socialbox\Objects\Standard; use Socialbox\Interfaces\SerializableInterface; -use Socialbox\Objects\Database\CaptchaRecord; class ImageCaptcha implements SerializableInterface {