2024-09-13 13:50:50 -04:00
|
|
|
<?php
|
|
|
|
|
2024-12-12 14:55:44 -05:00
|
|
|
namespace Socialbox\Enums;
|
2024-09-13 13:50:50 -04:00
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\AcceptCommunityGuidelines;
|
2024-12-14 00:43:19 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\AcceptPrivacyPolicy;
|
|
|
|
use Socialbox\Classes\StandardMethods\AcceptTermsOfService;
|
2024-12-23 19:02:37 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\GetCommunityGuidelines;
|
2024-12-14 00:43:19 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\GetPrivacyPolicy;
|
2024-12-12 14:55:44 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\GetSessionState;
|
2024-12-14 00:43:19 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\GetTermsOfService;
|
2024-12-12 14:55:44 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\Ping;
|
2025-01-03 18:30:50 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\SettingsAddSigningKey;
|
2025-01-04 15:32:42 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\SettingsDeleteDisplayName;
|
2025-01-03 18:30:50 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\SettingsGetSigningKeys;
|
2024-12-24 15:05:35 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\SettingsSetDisplayName;
|
2024-12-14 00:43:19 -05:00
|
|
|
use Socialbox\Classes\StandardMethods\SettingsSetPassword;
|
|
|
|
use Socialbox\Classes\StandardMethods\VerificationAnswerImageCaptcha;
|
|
|
|
use Socialbox\Classes\StandardMethods\VerificationGetImageCaptcha;
|
|
|
|
use Socialbox\Enums\Flags\SessionFlags;
|
2024-12-12 14:55:44 -05:00
|
|
|
use Socialbox\Exceptions\StandardException;
|
|
|
|
use Socialbox\Interfaces\SerializableInterface;
|
|
|
|
use Socialbox\Objects\ClientRequest;
|
|
|
|
use Socialbox\Objects\RpcRequest;
|
2024-09-13 13:50:50 -04:00
|
|
|
|
2024-12-12 14:55:44 -05:00
|
|
|
enum StandardMethods : string
|
2024-09-13 13:50:50 -04:00
|
|
|
{
|
2024-12-12 14:55:44 -05:00
|
|
|
case PING = 'ping';
|
|
|
|
case GET_SESSION_STATE = 'getSessionState';
|
2024-12-14 00:43:19 -05:00
|
|
|
|
|
|
|
case GET_PRIVACY_POLICY = 'getPrivacyPolicy';
|
|
|
|
case ACCEPT_PRIVACY_POLICY = 'acceptPrivacyPolicy';
|
|
|
|
case GET_TERMS_OF_SERVICE = 'getTermsOfService';
|
|
|
|
case ACCEPT_TERMS_OF_SERVICE = 'acceptTermsOfService';
|
2024-12-20 15:02:57 -05:00
|
|
|
case GET_COMMUNITY_GUIDELINES = 'getCommunityGuidelines';
|
|
|
|
case ACCEPT_COMMUNITY_GUIDELINES = 'acceptCommunityGuidelines';
|
|
|
|
|
|
|
|
case VERIFICATION_EMAIL = 'verificationEmail';
|
|
|
|
case VERIFICATION_ANSWER_EMAIL = 'verificationAnswerEmail';
|
|
|
|
|
|
|
|
case VERIFICATION_SMS = 'verificationSms';
|
|
|
|
case VERIFICATION_ANSWER_SMS = 'verificationAnswerSms';
|
|
|
|
|
|
|
|
case VERIFICATION_PHONE_CALL = 'verificationPhoneCall';
|
|
|
|
case VERIFICATION_ANSWER_PHONE_CALL = 'verificationAnswerPhoneCall';
|
2024-12-14 00:43:19 -05:00
|
|
|
|
|
|
|
case VERIFICATION_GET_IMAGE_CAPTCHA = 'verificationGetImageCaptcha';
|
|
|
|
case VERIFICATION_ANSWER_IMAGE_CAPTCHA = 'verificationAnswerImageCaptcha';
|
|
|
|
|
2024-12-20 15:02:57 -05:00
|
|
|
case VERIFICATION_GET_TEXT_CAPTCHA = 'verificationGetTextCaptcha';
|
|
|
|
case VERIFICATION_ANSWER_TEXT_CAPTCHA = 'verificationAnswerTextCaptcha';
|
|
|
|
|
|
|
|
case VERIFICATION_GET_EXTERNAL_URL = 'verificationGetExternalUrl';
|
|
|
|
case VERIFICATION_ANSWER_EXTERNAL_URL = 'verificationAnswerExternalUrl';
|
|
|
|
|
2024-12-14 00:43:19 -05:00
|
|
|
case SETTINGS_SET_PASSWORD = 'settingsSetPassword';
|
2024-12-20 15:02:57 -05:00
|
|
|
case SETTINGS_SET_OTP = 'settingsSetOtp';
|
|
|
|
case SETTINGS_SET_DISPLAY_NAME = 'settingsSetDisplayName';
|
2025-01-04 15:32:42 -05:00
|
|
|
case SETTINGS_DELETE_DISPLAY_NAME = 'settingsDeleteDisplayName';
|
2024-12-20 15:02:57 -05:00
|
|
|
case SETTINGS_SET_DISPLAY_PICTURE = 'settingsSetDisplayPicture';
|
|
|
|
case SETTINGS_SET_EMAIL = 'settingsSetEmail';
|
|
|
|
case SETTINGS_SET_PHONE = 'settingsSetPhone';
|
|
|
|
case SETTINGS_SET_BIRTHDAY = 'settingsSetBirthday';
|
2024-12-12 14:55:44 -05:00
|
|
|
|
2025-01-03 18:30:50 -05:00
|
|
|
case SETTINGS_ADD_SIGNING_KEY = 'settingsAddSigningKey';
|
|
|
|
case SETTINGS_GET_SIGNING_KEYS = 'settingsGetSigningKeys';
|
|
|
|
|
2024-12-12 14:55:44 -05:00
|
|
|
/**
|
2024-12-14 00:43:19 -05:00
|
|
|
* Executes the appropriate operation based on the current context and requests provided.
|
|
|
|
*
|
|
|
|
* @param ClientRequest $request The client request object containing necessary data for execution.
|
|
|
|
* @param RpcRequest $rpcRequest The RPC request object providing additional parameters for execution.
|
|
|
|
* @return SerializableInterface|null The result of the operation as a serializable interface or null if no operation matches.
|
|
|
|
* @throws StandardException If an error occurs during execution
|
2024-12-12 14:55:44 -05:00
|
|
|
*/
|
|
|
|
public function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
2024-09-13 13:50:50 -04:00
|
|
|
{
|
2024-12-12 14:55:44 -05:00
|
|
|
return match ($this)
|
|
|
|
{
|
|
|
|
self::PING => Ping::execute($request, $rpcRequest),
|
|
|
|
self::GET_SESSION_STATE => GetSessionState::execute($request, $rpcRequest),
|
2024-12-14 00:43:19 -05:00
|
|
|
|
|
|
|
self::GET_PRIVACY_POLICY => GetPrivacyPolicy::execute($request, $rpcRequest),
|
|
|
|
self::ACCEPT_PRIVACY_POLICY => AcceptPrivacyPolicy::execute($request, $rpcRequest),
|
|
|
|
self::GET_TERMS_OF_SERVICE => GetTermsOfService::execute($request, $rpcRequest),
|
|
|
|
self::ACCEPT_TERMS_OF_SERVICE => AcceptTermsOfService::execute($request, $rpcRequest),
|
2024-12-23 19:02:37 -05:00
|
|
|
self::GET_COMMUNITY_GUIDELINES => GetCommunityGuidelines::execute($request, $rpcRequest),
|
|
|
|
self::ACCEPT_COMMUNITY_GUIDELINES => AcceptCommunityGuidelines::execute($request, $rpcRequest),
|
2024-12-14 00:43:19 -05:00
|
|
|
|
|
|
|
self::VERIFICATION_GET_IMAGE_CAPTCHA => VerificationGetImageCaptcha::execute($request, $rpcRequest),
|
|
|
|
self::VERIFICATION_ANSWER_IMAGE_CAPTCHA => VerificationAnswerImageCaptcha::execute($request, $rpcRequest),
|
|
|
|
|
|
|
|
self::SETTINGS_SET_PASSWORD => SettingsSetPassword::execute($request, $rpcRequest),
|
2024-12-24 15:05:35 -05:00
|
|
|
self::SETTINGS_SET_DISPLAY_NAME => SettingsSetDisplayName::execute($request, $rpcRequest),
|
2025-01-04 15:32:42 -05:00
|
|
|
self::SETTINGS_DELETE_DISPLAY_NAME => SettingsDeleteDisplayName::execute($request, $rpcRequest),
|
2024-12-23 19:02:37 -05:00
|
|
|
|
2025-01-03 18:30:50 -05:00
|
|
|
self::SETTINGS_ADD_SIGNING_KEY => SettingsAddSigningKey::execute($request, $rpcRequest),
|
|
|
|
self::SETTINGS_GET_SIGNING_KEYS => SettingsGetSigningKeys::execute($request, $rpcRequest),
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
default => $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, sprintf("The method %s is not supported by the server", $rpcRequest->getMethod()))
|
2024-12-12 14:55:44 -05:00
|
|
|
};
|
|
|
|
}
|
2024-12-14 00:43:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the access method is allowed for the given client request.
|
|
|
|
*
|
|
|
|
* @param ClientRequest $clientRequest The client request instance to check access against.
|
|
|
|
* @return void
|
|
|
|
* @throws StandardException If the method is not allowed for the given client request.
|
|
|
|
*/
|
|
|
|
public function checkAccess(ClientRequest $clientRequest): void
|
|
|
|
{
|
|
|
|
if(in_array($this, self::getAllowedMethods($clientRequest)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new StandardException(StandardError::METHOD_NOT_ALLOWED->getMessage(), StandardError::METHOD_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the list of allowed methods for a given client request.
|
|
|
|
*
|
|
|
|
* @param ClientRequest $clientRequest The client request for which allowed methods are determined.
|
|
|
|
* @return array Returns an array of allowed methods for the provided client request.
|
|
|
|
*/
|
|
|
|
public static function getAllowedMethods(ClientRequest $clientRequest): array
|
|
|
|
{
|
2024-12-23 19:02:37 -05:00
|
|
|
// These methods should always accessible
|
2024-12-14 00:43:19 -05:00
|
|
|
$methods = [
|
2024-12-23 19:02:37 -05:00
|
|
|
// Important methods
|
|
|
|
self::PING, // Always allow the ping method
|
|
|
|
self::GET_SESSION_STATE, // The session state should always be accessible
|
|
|
|
self::GET_PRIVACY_POLICY, // The user should always be able to get the privacy policy
|
|
|
|
self::GET_TERMS_OF_SERVICE, // The user should always be able to get the terms of service
|
|
|
|
self::GET_COMMUNITY_GUIDELINES, // The user should always be able to get the community guidelines
|
2024-12-14 00:43:19 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$session = $clientRequest->getSession();
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// If the flag `VER_PRIVACY_POLICY` is set, then the user can accept the privacy policy
|
|
|
|
if($session->flagExists(SessionFlags::VER_PRIVACY_POLICY))
|
2024-12-14 00:43:19 -05:00
|
|
|
{
|
|
|
|
$methods[] = self::ACCEPT_PRIVACY_POLICY;
|
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// If the flag `VER_TERMS_OF_SERVICE` is set, then the user can accept the terms of service
|
|
|
|
if($session->flagExists(SessionFlags::VER_TERMS_OF_SERVICE))
|
2024-12-14 00:43:19 -05:00
|
|
|
{
|
|
|
|
$methods[] = self::ACCEPT_TERMS_OF_SERVICE;
|
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// If the flag `VER_COMMUNITY_GUIDELINES` is set, then the user can accept the community guidelines
|
|
|
|
if($session->flagExists(SessionFlags::VER_COMMUNITY_GUIDELINES))
|
|
|
|
{
|
|
|
|
$methods[] = self::ACCEPT_COMMUNITY_GUIDELINES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the flag `VER_IMAGE_CAPTCHA` is set, then the user has to get and answer an image captcha
|
|
|
|
if($session->flagExists(SessionFlags::VER_IMAGE_CAPTCHA))
|
2024-12-14 00:43:19 -05:00
|
|
|
{
|
|
|
|
$methods[] = self::VERIFICATION_GET_IMAGE_CAPTCHA;
|
|
|
|
$methods[] = self::VERIFICATION_ANSWER_IMAGE_CAPTCHA;
|
|
|
|
}
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// If the flag `SET_PASSWORD` is set, then the user has to set a password
|
2024-12-14 00:43:19 -05:00
|
|
|
if(in_array(SessionFlags::SET_PASSWORD, $session->getFlags()))
|
|
|
|
{
|
|
|
|
$methods[] = self::SETTINGS_SET_PASSWORD;
|
|
|
|
}
|
|
|
|
|
2025-01-03 18:30:50 -05:00
|
|
|
// If the user is authenticated, then allow additional method calls
|
2024-12-23 19:02:37 -05:00
|
|
|
if($session->isAuthenticated())
|
|
|
|
{
|
2025-01-03 18:30:50 -05:00
|
|
|
// Authenticated users can always manage their signing keys
|
|
|
|
$methods[] = self::SETTINGS_ADD_SIGNING_KEY;
|
|
|
|
$methods[] = self::SETTINGS_GET_SIGNING_KEYS;
|
|
|
|
|
2024-12-23 19:02:37 -05:00
|
|
|
// Always allow the authenticated user to change their password
|
|
|
|
if(!in_array(SessionFlags::SET_PASSWORD, $session->getFlags()))
|
|
|
|
{
|
|
|
|
$methods[] = self::SETTINGS_SET_PASSWORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-14 00:43:19 -05:00
|
|
|
return $methods;
|
|
|
|
}
|
2024-12-12 14:55:44 -05:00
|
|
|
}
|