UpgradePasswordHashAndAddress.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Setup\Patch\Data;
  7. use Magento\Customer\Setup\CustomerSetupFactory;
  8. use Magento\Framework\Encryption\Encryptor;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. /**
  14. * Class UpgradePasswordHashAndAddress
  15. * @package Magento\Customer\Setup\Patch
  16. */
  17. class UpgradePasswordHashAndAddress implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var CustomerSetupFactory
  25. */
  26. private $customerSetupFactory;
  27. /**
  28. * UpgradePasswordHashAndAddress constructor.
  29. * @param ModuleDataSetupInterface $moduleDataSetup
  30. * @param CustomerSetupFactory $customerSetupFactory
  31. */
  32. public function __construct(
  33. ModuleDataSetupInterface $moduleDataSetup,
  34. CustomerSetupFactory $customerSetupFactory
  35. ) {
  36. $this->moduleDataSetup = $moduleDataSetup;
  37. $this->customerSetupFactory = $customerSetupFactory;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function apply()
  43. {
  44. $this->upgradeHash();
  45. $entityAttributes = [
  46. 'customer_address' => [
  47. 'fax' => [
  48. 'is_visible' => false,
  49. 'is_system' => false,
  50. ],
  51. ],
  52. ];
  53. $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
  54. $customerSetup->upgradeAttributes($entityAttributes);
  55. }
  56. /**
  57. * @return void
  58. */
  59. private function upgradeHash()
  60. {
  61. $customerEntityTable = $this->moduleDataSetup->getTable('customer_entity');
  62. $select = $this->moduleDataSetup->getConnection()->select()->from(
  63. $customerEntityTable,
  64. ['entity_id', 'password_hash']
  65. );
  66. $customers = $this->moduleDataSetup->getConnection()->fetchAll($select);
  67. foreach ($customers as $customer) {
  68. if ($customer['password_hash'] === null) {
  69. continue;
  70. }
  71. list($hash, $salt) = explode(Encryptor::DELIMITER, $customer['password_hash']);
  72. $newHash = $customer['password_hash'];
  73. if (strlen($hash) === 32) {
  74. $newHash = implode(Encryptor::DELIMITER, [$hash, $salt, Encryptor::HASH_VERSION_MD5]);
  75. } elseif (strlen($hash) === 64) {
  76. $newHash = implode(Encryptor::DELIMITER, [$hash, $salt, Encryptor::HASH_VERSION_SHA256]);
  77. }
  78. $bind = ['password_hash' => $newHash];
  79. $where = ['entity_id = ?' => (int)$customer['entity_id']];
  80. $this->moduleDataSetup->getConnection()->update($customerEntityTable, $bind, $where);
  81. }
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public static function getDependencies()
  87. {
  88. return [
  89. AddCustomerUpdatedAtAttribute::class,
  90. ];
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public static function getVersion()
  96. {
  97. return '2.0.5';
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getAliases()
  103. {
  104. return [];
  105. }
  106. }