UpgradeData.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Dotdigitalgroup\Email\Setup;
  3. use Magento\Framework\Setup\UpgradeDataInterface;
  4. use Magento\Framework\Setup\ModuleContextInterface;
  5. use Magento\Framework\Setup\ModuleDataSetupInterface;
  6. use Magento\Framework\App\Config\ScopeConfigInterface;
  7. use Magento\Store\Model\ScopeInterface;
  8. use Dotdigitalgroup\Email\Helper\Config;
  9. use Dotdigitalgroup\Email\Helper\Transactional;
  10. use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
  11. use Dotdigitalgroup\Email\Helper\Data;
  12. use Magento\Framework\App\Config\ReinitableConfigInterface;
  13. use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;
  14. use Magento\User\Model\ResourceModel\User;
  15. /**
  16. * @codeCoverageIgnore
  17. */
  18. class UpgradeData implements UpgradeDataInterface
  19. {
  20. /**
  21. * @var Data
  22. */
  23. private $helper;
  24. /**
  25. * @var CollectionFactory
  26. */
  27. private $configCollectionFactory;
  28. /**
  29. * @var ReinitableConfigInterface
  30. */
  31. private $config;
  32. /**
  33. * @var UserCollectionFactory
  34. */
  35. private $userCollectionFactory;
  36. /**
  37. * @var User
  38. */
  39. private $userResource;
  40. /**
  41. * UpgradeData constructor.
  42. *
  43. * @param Data $helper
  44. * @param CollectionFactory $configCollectionFactory
  45. * @param ReinitableConfigInterface $config
  46. * @param UserCollectionFactory $userCollectionFactory
  47. * @param User $userResource
  48. */
  49. public function __construct(
  50. Data $helper,
  51. CollectionFactory $configCollectionFactory,
  52. ReinitableConfigInterface $config,
  53. UserCollectionFactory $userCollectionFactory,
  54. User $userResource
  55. ) {
  56. $this->configCollectionFactory = $configCollectionFactory;
  57. $this->helper = $helper;
  58. $this->config = $config;
  59. $this->userCollectionFactory = $userCollectionFactory;
  60. $this->userResource = $userResource;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  66. {
  67. $installer = $setup;
  68. $installer->startSetup();
  69. if (version_compare($context->getVersion(), '2.4.4', '<')) {
  70. //Encrypt api & transactional password for all websites
  71. $this->encryptAllPasswords();
  72. //Encrypt refresh token saved against admin users
  73. $this->encryptAllRefreshTokens();
  74. //Clear config cache
  75. $this->config->reinit();
  76. }
  77. if (version_compare($context->getVersion(), '2.5.0', '<')) {
  78. // Save config for allow non subscriber for features; AC and order review trigger campaign
  79. //For AC
  80. $this->helper->saveConfigData(
  81. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_REVIEW_ALLOW_NON_SUBSCRIBERS,
  82. 1,
  83. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  84. 0
  85. );
  86. //For order review
  87. $this->helper->saveConfigData(
  88. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_ALLOW_NON_SUBSCRIBERS,
  89. 1,
  90. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  91. 0
  92. );
  93. //Clear config cache
  94. $this->config->reinit();
  95. }
  96. if (version_compare($context->getVersion(), '2.5.1', '<')) {
  97. // Save config for allow non subscriber contacts to sync
  98. $this->helper->saveConfigData(
  99. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ALLOW_NON_SUBSCRIBERS,
  100. 1,
  101. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  102. 0
  103. );
  104. //Clear config cache
  105. $this->config->reinit();
  106. }
  107. $installer->endSetup();
  108. }
  109. /**
  110. * Encrypt all tokens
  111. */
  112. private function encryptAllRefreshTokens()
  113. {
  114. $userCollection = $this->userCollectionFactory->create()
  115. ->addFieldToFilter('refresh_token', ['notnull' => true]);
  116. foreach ($userCollection as $user) {
  117. $this->encryptAndSaveRefreshToken($user);
  118. }
  119. }
  120. /**
  121. * Encrypt token and save
  122. *
  123. * @param \Magento\User\Model\User $user
  124. */
  125. private function encryptAndSaveRefreshToken($user)
  126. {
  127. $user->setRefreshToken(
  128. $this->helper->encryptor->encrypt($user->getRefreshToken())
  129. );
  130. $this->userResource->save($user);
  131. }
  132. /**
  133. * Encrypt passwords and save for all websites
  134. */
  135. private function encryptAllPasswords()
  136. {
  137. $websites = $this->helper->getWebsites(true);
  138. $paths = [
  139. Config::XML_PATH_CONNECTOR_API_PASSWORD,
  140. Transactional::XML_PATH_DDG_TRANSACTIONAL_PASSWORD
  141. ];
  142. foreach ($websites as $website) {
  143. if ($website->getId() > 0) {
  144. $scope = ScopeInterface::SCOPE_WEBSITES;
  145. } else {
  146. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  147. }
  148. foreach ($paths as $path) {
  149. $this->encryptAndSavePassword(
  150. $path,
  151. $scope,
  152. $website->getId()
  153. );
  154. }
  155. }
  156. }
  157. /**
  158. * Encrypt already saved passwords
  159. *
  160. * @param string $path
  161. * @param string $scope
  162. * @param int $id
  163. */
  164. private function encryptAndSavePassword($path, $scope, $id)
  165. {
  166. $configCollection = $this->configCollectionFactory->create()
  167. ->addFieldToFilter('scope', $scope)
  168. ->addFieldToFilter('scope_id', $id)
  169. ->addFieldToFilter('path', $path)
  170. ->setPageSize(1);
  171. if ($configCollection->getSize()) {
  172. $value = $configCollection->getFirstItem()->getValue();
  173. if ($value) {
  174. $this->helper->saveConfigData(
  175. $path,
  176. $this->helper->encryptor->encrypt($value),
  177. $scope,
  178. $id
  179. );
  180. }
  181. }
  182. }
  183. }