UpgradePasswordHashes.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Setup\Patch\Data;
  7. use Magento\Framework\Encryption\Encryptor;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\Setup\Patch\DataPatchInterface;
  10. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  11. /**
  12. * Class UpgradePasswordHashes
  13. * @package Magento\User\Setup\Patch
  14. */
  15. class UpgradePasswordHashes implements DataPatchInterface, PatchVersionInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  19. */
  20. private $moduleDataSetup;
  21. /**
  22. * PatchInitial constructor.
  23. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  24. */
  25. public function __construct(
  26. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  27. ) {
  28. $this->moduleDataSetup = $moduleDataSetup;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function apply()
  34. {
  35. $this->moduleDataSetup->getConnection()->startSetup();
  36. $this->upgradeHash();
  37. $this->moduleDataSetup->getConnection()->endSetup();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function getDependencies()
  43. {
  44. return [];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public static function getVersion()
  50. {
  51. return '2.0.1';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getAliases()
  57. {
  58. return [];
  59. }
  60. /**
  61. * Upgrade password hashes.
  62. */
  63. private function upgradeHash()
  64. {
  65. $connection = $this->moduleDataSetup->getConnection();
  66. $customerEntityTable = $this->moduleDataSetup->getTable('admin_user');
  67. $select = $connection->select()->from(
  68. $customerEntityTable,
  69. ['user_id', 'password']
  70. );
  71. $customers = $connection->fetchAll($select);
  72. foreach ($customers as $customer) {
  73. list($hash, $salt) = explode(Encryptor::DELIMITER, $customer['password']);
  74. $newHash = $customer['password'];
  75. if (strlen($hash) === 32) {
  76. $newHash = implode(Encryptor::DELIMITER, [$hash, $salt, Encryptor::HASH_VERSION_MD5]);
  77. } elseif (strlen($hash) === 64) {
  78. $newHash = implode(Encryptor::DELIMITER, [$hash, $salt, Encryptor::HASH_VERSION_SHA256]);
  79. }
  80. $bind = ['password' => $newHash];
  81. $where = ['user_id = ?' => (int)$customer['user_id']];
  82. $connection->update($customerEntityTable, $bind, $where);
  83. }
  84. }
  85. }