Add email address validation and RPC method to set email
This commit is contained in:
parent
3e3bcfd143
commit
2656becd25
2 changed files with 61 additions and 0 deletions
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods;
|
||||
|
||||
use Exception;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Validator;
|
||||
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
|
||||
SettingsSetEmailAddress extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
if(!$rpcRequest->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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue