Skip to content

Commit 97f868e

Browse files
security-package/issues/101: Migration update.
1 parent 07dd8e7 commit 97f868e

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

ReCaptchaMigration/Setup/Patch/Data/MigrateConfigToRecaptchaModules.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
use Magento\Framework\App\Config\ScopeConfigInterface;
1111
use Magento\Framework\App\Config\Storage\WriterInterface;
12+
use Magento\Framework\Encryption\EncryptorInterface;
1213
use Magento\Framework\Setup\ModuleDataSetupInterface;
1314
use Magento\Framework\Setup\Patch\DataPatchInterface;
1415
use Magento\Framework\Setup\Patch\PatchVersionInterface;
1516

1617
/**
17-
* Migrate config from frontend and backend scopes to recaptcha modules.
18+
* Migrate config from frontend and backend scopes to reCAPTCHA modules.
1819
*/
1920
class MigrateConfigToRecaptchaModules implements DataPatchInterface, PatchVersionInterface
2021
{
@@ -33,19 +34,27 @@ class MigrateConfigToRecaptchaModules implements DataPatchInterface, PatchVersio
3334
*/
3435
private $writer;
3536

37+
/**
38+
* @var EncryptorInterface
39+
*/
40+
private $encryptor;
41+
3642
/**
3743
* @param ModuleDataSetupInterface $moduleDataSetup
3844
* @param ScopeConfigInterface $scopeConfig
3945
* @param WriterInterface $writer
46+
* @param EncryptorInterface $encryptor
4047
*/
4148
public function __construct(
4249
ModuleDataSetupInterface $moduleDataSetup,
4350
ScopeConfigInterface $scopeConfig,
44-
WriterInterface $writer
51+
WriterInterface $writer,
52+
EncryptorInterface $encryptor
4553
) {
4654
$this->moduleDataSetup = $moduleDataSetup;
4755
$this->scopeConfig = $scopeConfig;
4856
$this->writer = $writer;
57+
$this->encryptor = $encryptor;
4958
}
5059

5160
/**
@@ -58,6 +67,7 @@ public function apply()
5867
$this->copyRecaptchaKeys($scope);
5968
$this->copyModuleSpecificRecords($scope);
6069
$this->copyEnabledRecaptcha($scope);
70+
$this->disableLegacyRecaptcha($scope);
6171
}
6272
}
6373

@@ -86,21 +96,38 @@ private function copyEnabledRecaptcha(string $scope): void
8696
}
8797
}
8898

99+
/**
100+
* Disable legacy reCAPTCHA module to prevent multiple widget rendering.
101+
*
102+
* @param string $scope
103+
*/
104+
private function disableLegacyRecaptcha(string $scope): void
105+
{
106+
$this->writer->save("msp_securitysuite_recaptcha/$scope/enabled", 0);
107+
}
108+
89109
/**
90110
* Copy reCAPTCHA keys.
91111
*
92112
* @param string $scope
93113
*/
94114
private function copyRecaptchaKeys(string $scope): void
95115
{
96-
$keys = ['public_key', 'private_key'];
97116
$type = $this->getActiveRecaptchaType();
98117
if ($type) {
99-
foreach ($keys as $key) {
100-
$this->copyRecord(
101-
"msp_securitysuite_recaptcha/general/$key",
102-
"recaptcha_$scope/type_$type/$key"
103-
);
118+
$this->copyRecord(
119+
"msp_securitysuite_recaptcha/general/public_key",
120+
"recaptcha_$scope/type_$type/public_key"
121+
);
122+
$privateKey = $this->scopeConfig->getValue(
123+
"recaptcha_$scope/type_$type/private_key"
124+
);
125+
$privateKeyLegacy = $this->scopeConfig->getValue(
126+
'msp_securitysuite_recaptcha/general/private_key'
127+
);
128+
if (!$privateKey && $privateKeyLegacy) {
129+
$privateKeyEncrypted = $this->encryptor->encrypt($privateKeyLegacy);
130+
$this->writer->save("recaptcha_$scope/type_$type/private_key", $privateKeyEncrypted);
104131
}
105132
}
106133
}

0 commit comments

Comments
 (0)