Skip to content

Commit

Permalink
Add helper to transform to absolute URL + Modify configUrl after modu…
Browse files Browse the repository at this point in the history
…le action
  • Loading branch information
sowbiba committed Dec 12, 2023
1 parent bd42f59 commit f8f04ed
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
35 changes: 31 additions & 4 deletions src/Api/Service/ModuleTransitionExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ private function generateTokenizedModuleActionUrl($url): string
{
$components = parse_url($url);
$baseUrl = ($components['path'] ?? '');

$composedUrl = '';
if (!empty($components['scheme'])) {
$scheme = $components['scheme'];
$composedUrl .= $scheme . ':';
}

if (!empty($components['host'])) {
$composedUrl .= '//';
if (isset($components['user'])) {
$composedUrl .= $components['user'];
if (isset($components['pass'])) {
$composedUrl .= ':' . $components['pass'];
}
$composedUrl .= '@';
}

$composedUrl .= $components['host'];

// Only include the port if it is not the default port of the scheme
if (isset($components['port'])) {
$composedUrl .= ':' . $components['port'];
}
}

$composedUrl .= $baseUrl;

$queryParams = [];
if (isset($components['query'])) {
$query = $components['query'];
Expand All @@ -118,19 +145,19 @@ private function generateTokenizedModuleActionUrl($url): string
}

if (!isset($queryParams['_token'])) {
return $url;
return $composedUrl;
}

$adminToken = Tools::getValue('admin_token');
$queryParams['_token'] = $adminToken;

$url = $baseUrl . '?' . http_build_query($queryParams, '', '&');
$composedUrl .= '?' . http_build_query($queryParams, '', '&');
if (isset($components['fragment']) && $components['fragment'] !== '') {
/* This copy-paste from Symfony's UrlGenerator */
$url .= '#' . strtr(rawurlencode($components['fragment']), ['%2F' => '/', '%3F' => '?']);
$composedUrl .= '#' . strtr(rawurlencode($components['fragment']), ['%2F' => '/', '%3F' => '?']);
}

return $url;
return $composedUrl;
}

private function authenticateAddonsUser(Session $session): void
Expand Down
36 changes: 36 additions & 0 deletions src/Helpers/UrlHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
declare(strict_types=1);

namespace PrestaShop\Module\Mbo\Helpers;

use Validate;

class UrlHelper
{
public static function transformToAbsoluteUrl(string $url): string
{
if (Validate::isAbsoluteUrl($url)) {
return $url;
}

return rtrim(Config::getShopUrl(false), '/') . DIRECTORY_SEPARATOR . ltrim($url, '/');
}
}
12 changes: 9 additions & 3 deletions src/Module/ModuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
use PaymentModule;
use PhpParser;
use PrestaShop\Module\Mbo\Helpers\ErrorHelper;
use PrestaShop\Module\Mbo\Helpers\UrlHelper;
use PrestaShopBundle\Service\Routing\Router;
use Psr\Log\LoggerInterface;
use stdClass;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Validate;

/**
Expand Down Expand Up @@ -136,9 +138,13 @@ protected function generateAddonsUrls(Module $module): void
&& $module->attributes->getBoolean('is_configurable')
) {
$module->attributes->set('urls', [
'configure' => $this->router->generate('admin_module_configure_action', [
'module_name' => $moduleName,
]),
'configure' => UrlHelper::transformToAbsoluteUrl($this->router->generate(
'admin_module_configure_action',
[
'module_name' => $moduleName,
],
UrlGeneratorInterface::ABSOLUTE_URL
)),
]);
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/Service/View/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PrestaShop\Module\Mbo\Accounts\Provider\AccountsDataProvider;
use PrestaShop\Module\Mbo\Api\Security\AdminAuthenticationProvider;
use PrestaShop\Module\Mbo\Helpers\Config;
use PrestaShop\Module\Mbo\Helpers\UrlHelper;
use PrestaShop\Module\Mbo\Module\Module;
use PrestaShop\Module\Mbo\Module\Workflow\ModuleStateMachine;
use PrestaShop\Module\Mbo\Tab\Tab;
Expand All @@ -40,7 +41,6 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use Tools;
use Validate;

class ContextBuilder
{
Expand Down Expand Up @@ -168,7 +168,7 @@ private function getCommonContextContent(): array
'shop_uuid' => Config::getShopMboUuid(),
'mbo_token' => $this->adminAuthenticationProvider->getMboJWT(),
'mbo_version' => \ps_mbo::VERSION,
'mbo_reset_url' => $this->transformToAbsoluteUrl(
'mbo_reset_url' => UrlHelper::transformToAbsoluteUrl(
$this->router->generate('admin_module_manage_action', [
'action' => 'reset',
'module_name' => 'ps_mbo',
Expand All @@ -182,8 +182,8 @@ private function getCommonContextContent(): array
'accounts_shop_id' => $this->accountsDataProvider->getAccountsShopId(),
'accounts_token' => $this->accountsDataProvider->getAccountsToken(),
'accounts_component_loaded' => false,
'module_catalog_url' => $this->transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_module')),
'theme_catalog_url' => $this->transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_theme')),
'module_catalog_url' => UrlHelper::transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_module')),
'theme_catalog_url' => UrlHelper::transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_theme')),
'php_version' => phpversion(),
];
}
Expand Down Expand Up @@ -277,7 +277,7 @@ private function getInstalledModules(): array
}

if ($installedModule->isConfigurable()) {
$moduleConfigUrl = $this->transformToAbsoluteUrl(
$moduleConfigUrl = UrlHelper::transformToAbsoluteUrl(
$this->router->generate(
'admin_module_configure_action',
[
Expand All @@ -299,13 +299,4 @@ private function getCacheKey(): string
{
return sprintf('mbo_installed_modules_list_%s', Config::getShopMboUuid());
}

private function transformToAbsoluteUrl(string $url): string
{
if (Validate::isAbsoluteUrl($url)) {
return $url;
}

return rtrim(Config::getShopUrl(false), '/') . DIRECTORY_SEPARATOR . ltrim($url, '/');
}
}

0 comments on commit f8f04ed

Please sign in to comment.