From 44143cb8cdfec44ea4634c5cb7a9617e299ae62d Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 30 Oct 2024 15:29:00 -0400 Subject: [PATCH] Add user registration method --- .../Classes/StandardMethods/Register.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/Socialbox/Classes/StandardMethods/Register.php diff --git a/src/Socialbox/Classes/StandardMethods/Register.php b/src/Socialbox/Classes/StandardMethods/Register.php new file mode 100644 index 0000000..40c1904 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/Register.php @@ -0,0 +1,80 @@ +isRegistrationEnabled()) + { + return $rpcRequest->produceError(StandardError::REGISTRATION_DISABLED, StandardError::REGISTRATION_DISABLED->getMessage()); + } + + // Check if the username parameter exists + if(!$rpcRequest->containsParameter('username')) + { + return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing parameter \'username\''); + } + + // Check if the username is valid + if(!Validator::validateUsername($rpcRequest->getParameter('username'))) + { + return $rpcRequest->produceError(StandardError::INVALID_USERNAME, StandardError::INVALID_USERNAME->getMessage()); + } + + // Check if the username exists already + try + { + if (RegisteredPeerManager::usernameExists($rpcRequest->getParameter('username'))) + { + return $rpcRequest->produceError(StandardError::USERNAME_ALREADY_EXISTS, StandardError::USERNAME_ALREADY_EXISTS->getMessage()); + } + } + catch (DatabaseOperationException $e) + { + throw new StandardException("There was an unexpected error while trying to check the username existence", StandardError::INTERNAL_SERVER_ERROR, $e); + } + + // Check if the request has a Session UUID + if($request->getSessionUuid() === null) + { + return $rpcRequest->produceError(StandardError::SESSION_REQUIRED); + } + + try + { + // Get the session and check if it's already authenticated + $session = SessionManager::getSession($request->getSessionUuid()); + if($session->getAuthenticatedPeerUuid() !== null) + { + return $rpcRequest->produceError(StandardError::ALREADY_AUTHENTICATED); + } + + // Create the peer & set the current's session authenticated peer as the newly created peer + SessionManager::updateAuthenticatedPeer($session->getUuid(), RegisteredPeerManager::createPeer($rpcRequest->getParameter('username'))); + } + catch(DatabaseOperationException $e) + { + throw new StandardException("There was an unexpected error while trying to register", StandardError::INTERNAL_SERVER_ERROR, $e); + } + + // Return true to indicate the operation was a success + return $rpcRequest->produceResponse(true); + } +} \ No newline at end of file