diff --git a/src/Socialbox/Classes/StandardMethods/SettingsSetEmailAddress.php b/src/Socialbox/Classes/StandardMethods/SettingsSetEmailAddress.php new file mode 100644 index 0000000..c8fda50 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/SettingsSetEmailAddress.php @@ -0,0 +1,50 @@ +containsParameter('email_address')) + { + return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'email_address' parameter"); + } + + if(!Validator::validateEmailAddress($rpcRequest->getParameter('email_address'))) + { + return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'email_address' parameter, must be a valid email address"); + } + + try + { + // Set the password + RegisteredPeerManager::updateEmailAddress($request->getPeer(), $rpcRequest->getParameter('email_address')); + + // Check & update the session flow + SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_EMAIL]); + } + catch(Exception $e) + { + throw new StandardException('Failed to set email address 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/Validator.php b/src/Socialbox/Classes/Validator.php index 4d0bda5..628dd30 100644 --- a/src/Socialbox/Classes/Validator.php +++ b/src/Socialbox/Classes/Validator.php @@ -18,6 +18,17 @@ class Validator return preg_match(self::PEER_ADDRESS_PATTERN, $address) === 1; } + /** + * Checks if the provided email address is in a valid email format. + * + * @param string $emailAddress The email address to be validated. + * @return bool Returns true if the email address is valid, otherwise false. + */ + public static function validateEmailAddress(string $emailAddress): bool + { + return filter_var($emailAddress, FILTER_VALIDATE_EMAIL) !== false; + } + /** * Validates a username *