UpgradeData.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Setup;
  21. use Magento\Framework\App\Config\ScopeConfigInterface;
  22. use Magento\Framework\Filesystem\Io\File;
  23. use Magento\Framework\Json\EncoderInterface;
  24. use Magento\Framework\Module\Dir\Reader;
  25. use Magento\Framework\Setup\ModuleContextInterface;
  26. use Magento\Framework\Setup\ModuleDataSetupInterface;
  27. use Magento\Framework\Setup\UpgradeDataInterface;
  28. use Magento\Framework\Json\DecoderInterface;
  29. use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
  30. use MSP\TwoFactorAuth\Model\Provider\Engine\DuoSecurity;
  31. /**
  32. * @codeCoverageIgnore
  33. */
  34. class UpgradeData implements UpgradeDataInterface
  35. {
  36. /**
  37. * @var EncoderInterface
  38. */
  39. private $encoder;
  40. /**
  41. * @var ConfigInterface
  42. */
  43. private $config;
  44. /**
  45. * @var DecoderInterface
  46. */
  47. private $decoder;
  48. /**
  49. * @var Reader
  50. */
  51. private $moduleReader;
  52. /**
  53. * @var File
  54. */
  55. private $file;
  56. /**
  57. * @var ScopeConfigInterface
  58. */
  59. private $scopeConfig;
  60. public function __construct(
  61. EncoderInterface $encoder,
  62. DecoderInterface $decoder,
  63. ConfigInterface $config,
  64. ScopeConfigInterface $scopeConfig,
  65. File $file,
  66. Reader $moduleReader
  67. ) {
  68. $this->encoder = $encoder;
  69. $this->config = $config;
  70. $this->decoder = $decoder;
  71. $this->moduleReader = $moduleReader;
  72. $this->file = $file;
  73. $this->scopeConfig = $scopeConfig;
  74. }
  75. /**
  76. * Move config from srcPath to dstPath
  77. * @param ModuleDataSetupInterface $setup
  78. * @param string $srcPath
  79. * @param string $dstPath
  80. */
  81. private function moveConfig(ModuleDataSetupInterface $setup, $srcPath, $dstPath)
  82. {
  83. $value = $this->scopeConfig->getValue($srcPath);
  84. if (is_array($value)) {
  85. foreach (array_keys($value) as $k) {
  86. $this->moveConfig($setup, $srcPath . '/' . $k, $dstPath . '/' . $k);
  87. }
  88. } else {
  89. $connection = $setup->getConnection();
  90. $configData = $setup->getTable('core_config_data');
  91. $connection->update($configData, ['path' => $dstPath], 'path='.$connection->quote($srcPath));
  92. }
  93. }
  94. private function upgradeTo010200(ModuleDataSetupInterface $setup)
  95. {
  96. $this->moveConfig(
  97. $setup,
  98. 'msp_securitysuite/twofactorauth/allow_trusted_devices',
  99. 'msp_securitysuite_twofactorauth/google/allow_trusted_devices'
  100. );
  101. $this->moveConfig(
  102. $setup,
  103. 'msp_securitysuite/twofactorauth',
  104. 'msp_securitysuite_twofactorauth/general'
  105. );
  106. // Generate random duo security key
  107. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  108. $charactersLength = strlen($characters);
  109. $randomString = '';
  110. for ($i = 0; $i < 64; $i++) {
  111. $randomString .= $characters[rand(0, $charactersLength - 1)];
  112. }
  113. $this->config->saveConfig(DuoSecurity::XML_PATH_APPLICATION_KEY, $randomString, 'default', 0);
  114. }
  115. private function upgradeTo020000(ModuleDataSetupInterface $setup)
  116. {
  117. $this->moveConfig(
  118. $setup,
  119. 'msp_securitysuite_twofactorauth/general/force_provider',
  120. 'msp_securitysuite_twofactorauth/general/force_provider_0'
  121. );
  122. }
  123. private function upgradeTo020001(ModuleDataSetupInterface $setup)
  124. {
  125. $connection = $setup->getConnection();
  126. $tableName = $setup->getTable('msp_tfa_country_codes');
  127. $countryCodesJsonFile =
  128. $this->moduleReader->getModuleDir(false, 'MSP_TwoFactorAuth') . DIRECTORY_SEPARATOR . 'Setup' .
  129. DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'country_codes.json';
  130. $countryCodesJson = $this->file->read($countryCodesJsonFile);
  131. $countryCodes = $this->decoder->decode(trim($countryCodesJson));
  132. // @codingStandardsIgnoreStart
  133. foreach ($countryCodes as $countryCode) {
  134. $connection->insert($tableName, $countryCode);
  135. }
  136. // @codingStandardsIgnoreEnd
  137. }
  138. /**
  139. * Upgrades data for a module
  140. *
  141. * @param ModuleDataSetupInterface $setup
  142. * @param ModuleContextInterface $context
  143. * @return void
  144. */
  145. public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  146. {
  147. $setup->startSetup();
  148. if (version_compare($context->getVersion(), '1.2.0') < 0) {
  149. $this->upgradeTo010200($setup);
  150. }
  151. if (version_compare($context->getVersion(), '2.0.0') < 0) {
  152. $this->upgradeTo020000($setup);
  153. }
  154. if (version_compare($context->getVersion(), '2.0.1') < 0) {
  155. $this->upgradeTo020001($setup);
  156. }
  157. $setup->endSetup();
  158. }
  159. }