* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ require_once __DIR__ . '/../../src/Polyfill/Traits/Controller/AjaxRender.php'; use PrestaShop\Module\PsAccounts\Account\Exception\UnknownStatusException; use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession; use PrestaShop\Module\PsAccounts\Account\ShopUrl; use PrestaShop\Module\PsAccounts\Account\StatusManager; use PrestaShop\Module\PsAccounts\AccountLogin\OAuth2Session; use PrestaShop\Module\PsAccounts\Adapter\Link as AccountsLink; use PrestaShop\Module\PsAccounts\Installer\Installer; use PrestaShop\Module\PsAccounts\Log\Logger; use PrestaShop\Module\PsAccounts\Polyfill\Traits\Controller\AjaxRender; use PrestaShop\Module\PsAccounts\Provider\ShopProvider; use PrestaShop\Module\PsAccounts\Service\SentryService; use PrestaShop\Module\PsAccounts\Service\UpgradeService; /** * Controller for all ajax calls. */ class AdminAjaxPsAccountsController extends \ModuleAdminController { use AjaxRender; /** * @var Ps_accounts */ public $module; /** * @var string */ private $alertCss = ' '; /** * @var string */ private $translationClass; /** * AdminAjaxPsAccountsController constructor. * * @throws Exception */ public function __construct() { parent::__construct(); $this->ajax = true; $this->content_only = true; $this->translationClass = self::class; } /** * @return void * * @throws Exception */ public function ajaxProcessGetOrRefreshToken() { try { /** @var ShopSession $shopSession */ $shopSession = $this->module->getService(ShopSession::class); header('Content-Type: text/json'); $token = $shopSession->getValidToken(); $this->ajaxRender( (string) json_encode([ 'token' => (string) $token->getJwt(), 'refreshToken' => $token->getRefreshToken(), ]) ); } catch (Exception $e) { SentryService::captureAndRethrow($e); } } /** * @return void * * @throws Exception */ public function ajaxProcessGetOrRefreshAccessToken() { try { /** @var OAuth2Session $oauth2Session */ $oauth2Session = $this->module->getService(OAuth2Session::class); header('Content-Type: text/json'); $this->ajaxRender( (string) json_encode([ 'token' => (string) $oauth2Session->getOrRefreshAccessToken(), ]) ); } catch (Exception $e) { SentryService::captureAndRethrow($e); } } /** * @return void */ public function ajaxProcessGetNotifications() { $notifications = []; try { $notifications = array_merge( $this->getNotificationsUpgradeFailed(), $this->getNotificationsUrlMismatch() ); } catch (\Exception $e) { Logger::getInstance()->error($e->getMessage()); } catch (\Throwable $e) { Logger::getInstance()->error($e->getMessage()); } $this->ajaxRender( (string) json_encode($notifications ?: []) ); } /** * @return array|array[] * * @throws UnknownStatusException */ protected function getNotificationsUrlMismatch() { /** @var StatusManager $statusManager */ $statusManager = $this->module->getService(StatusManager::class); if (!$statusManager->identityCreated()) { return []; } $status = $statusManager->getStatus(); $shopId = $this->context->shop->id; $cloudShopUrl = ShopUrl::createFromStatus($status, $shopId)->trimmed(); /** @var ShopProvider $shopProvider */ $shopProvider = $this->module->getService(ShopProvider::class); $localShopUrl = $shopProvider->getUrl($shopId)->trimmed(); try { if ($cloudShopUrl->frontendUrlEquals($localShopUrl)) { return []; } } catch (\InvalidArgumentException $e) { Logger::getInstance()->error($e->getMessage()); return []; } /** @var AccountsLink $link */ $link = $this->module->getService(AccountsLink::class); $moduleLink = $link->getAdminLink('AdminModules', true, [], [ 'configure' => 'ps_accounts', ]); return [[ 'html' => $this->alertCss . '
' . $this->module->l('Action required: confirm your store URL', $this->translationClass) . '

' . $this->module->l('We\'ve noticed that your store\'s URL no longer matches the one registered in your PrestaShop Account.', $this->translationClass) . '
' . $this->module->l('For your services to function properly, you must either confirm this change or create a new identity for your store.', $this->translationClass) . '

', ]]; } /** * @return array|array[] */ protected function getNotificationsUpgradeFailed() { /** @var StatusManager $statusManager */ $statusManager = $this->module->getService(StatusManager::class); /** @var UpgradeService $upgradeService */ $upgradeService = $this->module->getService(UpgradeService::class); if ($upgradeService->getCoreRegisteredVersion() === \Ps_accounts::VERSION && (!$statusManager->identityCreated() || $upgradeService->getRegisteredVersion() === \Ps_accounts::VERSION)) { return []; } /** @var AccountsLink $link */ $link = $this->module->getService(AccountsLink::class); $resetLink = $link->getAdminLink('AdminAjaxPsAccounts', true, [], ['ajax' => 1, 'action' => 'resetModule']); return [[ 'html' => $this->alertCss . '
' . $this->module->l('Action required: reset your PS Account module', $this->translationClass) . '

' . $this->module->l('A simple reset is needed to finish the update and ensure all your modules are working correctly.', $this->translationClass) . '

', ]]; } /** * @return void */ public function ajaxProcessResetModule() { $status = false; try { /** @var Installer $installer */ $installer = $this->module->getService(Installer::class); $status = $installer->resetModule('ps_accounts'); } catch (\Exception $e) { Logger::getInstance()->error($e->getMessage()); } catch (\Throwable $e) { Logger::getInstance()->error($e->getMessage()); } $this->ajaxRender( (string) json_encode([ 'status' => $status, ]) ); } }