Skip to content

Commit

Permalink
Send absolute URL for mudule actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sowbiba committed Dec 12, 2023
1 parent 057f8ca commit bd42f59
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/Helpers/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ class Config
* @var string|null
*/
private static $SHOP_MBO_UUID;

/**
* @var string|null
*/
private static $SHOP_MBO_ADMIN_MAIL;

/**
* @var string|null
*/
private static $SHOP_URL_WITHOUT_PHYSICAL_URI;

/**
* @var string|null
*/
Expand Down Expand Up @@ -92,8 +99,12 @@ public static function getShopMboAdminMail(): ?string
*
* @return string|null
*/
public static function getShopUrl(): ?string
public static function getShopUrl(bool $withPhysicalUri = true): ?string
{
if (!$withPhysicalUri && null !== self::$SHOP_URL_WITHOUT_PHYSICAL_URI) {
return self::$SHOP_URL_WITHOUT_PHYSICAL_URI;
}

if (null === self::$SHOP_URL) {
$singleShop = self::getSingleShop();
$domains = \Tools::getDomains();
Expand All @@ -107,7 +118,6 @@ function($domain) use($singleShop) {
}
);


$useSecureProtocol = self::isUsingSecureProtocol();
if (empty($shopDomain)) { // If somehow we failed getting the shop_url from ps_shop_url, do it the old way, with configuration values
$domainConfigKey = $useSecureProtocol ? 'PS_SHOP_DOMAIN_SSL' : 'PS_SHOP_DOMAIN';
Expand All @@ -121,12 +131,14 @@ function($domain) use($singleShop) {

if ($domain) {
$domain = preg_replace('#(https?://)#', '', $domain);
self::$SHOP_URL = ($useSecureProtocol ? 'https://' : 'http://') . $domain;
self::$SHOP_URL = self::$SHOP_URL_WITHOUT_PHYSICAL_URI = ($useSecureProtocol ? 'https://' : 'http://') . $domain;
}
} else {
$domain = array_keys($shopDomain)[0];
$domain = preg_replace('#(https?://)#', '', $domain);

self::$SHOP_URL_WITHOUT_PHYSICAL_URI = ($useSecureProtocol ? 'https://' : 'http://') . $domain;

// concatenate the physical_uri
$domainDef = reset($shopDomain[$domain]);
if (isset($domainDef['physical']) && '/' !== $domainDef['physical']) {
Expand All @@ -137,7 +149,7 @@ function($domain) use($singleShop) {
}
}

return self::$SHOP_URL;
return $withPhysicalUri ? self::$SHOP_URL : self::$SHOP_URL_WITHOUT_PHYSICAL_URI;
}

/**
Expand Down
37 changes: 28 additions & 9 deletions src/Service/View/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
use PrestaShop\PrestaShop\Adapter\LegacyContext as ContextAdapter;
use PrestaShop\PrestaShop\Adapter\Module\Module as CoreModule;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use Tools;
use Validate;

class ContextBuilder
{
Expand Down Expand Up @@ -166,10 +168,12 @@ private function getCommonContextContent(): array
'shop_uuid' => Config::getShopMboUuid(),
'mbo_token' => $this->adminAuthenticationProvider->getMboJWT(),
'mbo_version' => \ps_mbo::VERSION,
'mbo_reset_url' => $this->router->generate('admin_module_manage_action', [
'action' => 'reset',
'module_name' => 'ps_mbo',
]),
'mbo_reset_url' => $this->transformToAbsoluteUrl(
$this->router->generate('admin_module_manage_action', [
'action' => 'reset',
'module_name' => 'ps_mbo',
])
),
'user_id' => $context->cookie->id_employee,
'admin_token' => $token,
'refresh_url' => $refreshUrl,
Expand All @@ -178,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->router->generate('admin_mbo_catalog_module'),
'theme_catalog_url' => $this->router->generate('admin_mbo_catalog_theme'),
'module_catalog_url' => $this->transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_module')),
'theme_catalog_url' => $this->transformToAbsoluteUrl($this->router->generate('admin_mbo_catalog_theme')),
'php_version' => phpversion(),
];
}
Expand Down Expand Up @@ -273,9 +277,15 @@ private function getInstalledModules(): array
}

if ($installedModule->isConfigurable()) {
$moduleConfigUrl = $this->router->generate('admin_module_configure_action', [
'module_name' => $moduleName,
]);
$moduleConfigUrl = $this->transformToAbsoluteUrl(
$this->router->generate(
'admin_module_configure_action',
[
'module_name' => $moduleName,
],
UrlGeneratorInterface::ABSOLUTE_URL
)
);
}
$installedModules[] = (new InstalledModule($moduleId, $moduleName, $moduleStatus, (string) $moduleVersion, $moduleConfigUrl))->toArray();
}
Expand All @@ -289,4 +299,13 @@ 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 bd42f59

Please sign in to comment.