Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable non native modules on PS8.0 and onward #498

Merged
merged 8 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
use PrestaShop\Module\AutoUpgrade\UpgradeException;
use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Domain\MailTemplate\Command\GenerateThemeMailTemplatesCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\BulkToggleModuleStatusCommand;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
use PrestaShop\PrestaShop\Adapter\Module\Repository\ModuleRepository;

class CoreUpgrader80 extends CoreUpgrader
{
Expand Down Expand Up @@ -100,4 +102,10 @@ protected function upgradeLanguage($lang)

// TODO: Update AdminTranslationsController::addNewTabs to install tabs translated
}

protected function disableCustomModules()
{
$moduleRepository = new ModuleRepository(_PS_ROOT_DIR_, _PS_MODULE_DIR_);
$this->container->getModuleAdapter()->disableNonNativeModules80($this->pathToUpgradeScripts, $moduleRepository);
}
}
23 changes: 23 additions & 0 deletions classes/UpgradeTools/ModuleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ModuleAdapter

private $commandBus;

private $moduleRepository;

matthieu-rolland marked this conversation as resolved.
Show resolved Hide resolved
public function __construct($db, $translator, $modulesPath, $tempPath, $upgradeVersion, ZipAction $zipAction, SymfonyAdapter $symfonyAdapter)
{
$this->db = $db;
Expand Down Expand Up @@ -99,6 +101,21 @@ public function getCommandBus()
return $this->commandBus;
}

/**
* Available since PrestaShop 8.0
*/
public function getModuleRepository()
{
if (null === $this->moduleRepository) {
$this->moduleRepository = $this->symfonyAdapter
->initAppKernel()
->getContainer()
->get('prestashop.adapter.module.repository.module_repository');
}

return $this->moduleRepository;
}

/**
* Upgrade action, disabling all modules not made by PrestaShop.
*
Expand All @@ -112,6 +129,12 @@ public function disableNonNativeModules($pathToUpgradeScripts)
deactivate_custom_modules();
}

public function disableNonNativeModules80($pathToUpgradeScripts, $moduleRepository)
{
require_once $pathToUpgradeScripts . 'php/deactivate_custom_modules.php';
deactivate_custom_modules80($moduleRepository);
}

/**
* list modules to upgrade and save them in a serialized array in $this->toUpgradeModuleList.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/phpstan/phpstan-latest.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ includes:
- %currentWorkingDirectory%/tests/phpstan/phpstan.neon

parameters:
excludes_analyse:
- ./../../classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
matthieu-rolland marked this conversation as resolved.
Show resolved Hide resolved
ignoreErrors:
- '#Access to an undefined property Module::\$installed.#'
- '#Call to method fetchLocale\(\) on an unknown class PrestaShop\\PrestaShop\\Core\\Cldr\\Update.#'
Expand Down
36 changes: 36 additions & 0 deletions upgrade/php/deactivate_custom_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,39 @@ function deactivate_custom_modules()

return $return;
}

function deactivate_custom_modules80($moduleRepository)
{
$nonNativeModulesList = $moduleRepository->getNonNativeModules();

$return = Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'module` SET `active` = 0 WHERE `name` IN (' . implode(',', array_map('add_quotes', $nonNativeModulesList)) . ')'
);

$nonNativeModules = \Db::getInstance()->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'module` m
WHERE name IN (' . implode(',', array_map('add_quotes', $nonNativeModulesList)) . ') ');

if (!is_array($nonNativeModules) || empty($nonNativeModules)) {
return $return;
}

$toBeUninstalled = [];
foreach ($nonNativeModules as $k => $aModule) {
$toBeUninstalled[] = (int) $aModule['id_module'];
}

if (empty($toBeUninstalled)) {
return $return;
}

$sql = 'DELETE FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` IN (' . implode(',', array_map('add_quotes', $toBeUninstalled)) . ') ';
$return &= Db::getInstance()->execute($sql);

return $return;
}

function add_quotes($str) {
return sprintf("'%s'", $str);
}