UpgradeHashAlgorithmCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Console\Command;
  7. use Magento\Customer\Model\Customer;
  8. use Magento\Framework\Encryption\Encryptor;
  9. use Magento\Customer\Model\ResourceModel\Customer\Collection;
  10. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class UpgradeHashAlgorithmCommand extends Command
  15. {
  16. /**
  17. * @var CollectionFactory
  18. */
  19. private $customerCollectionFactory;
  20. /**
  21. * @var Collection
  22. */
  23. private $collection;
  24. /**
  25. * @var Encryptor
  26. */
  27. private $encryptor;
  28. /**
  29. * @param CollectionFactory $customerCollectionFactory
  30. * @param Encryptor $encryptor
  31. */
  32. public function __construct(
  33. CollectionFactory $customerCollectionFactory,
  34. Encryptor $encryptor
  35. ) {
  36. parent::__construct();
  37. $this->customerCollectionFactory = $customerCollectionFactory;
  38. $this->encryptor = $encryptor;
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. protected function configure()
  44. {
  45. $this->setName('customer:hash:upgrade')
  46. ->setDescription('Upgrade customer\'s hash according to the latest algorithm');
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. protected function execute(InputInterface $input, OutputInterface $output)
  52. {
  53. $this->collection = $this->customerCollectionFactory->create();
  54. $this->collection->addAttributeToSelect('*');
  55. $customerCollection = $this->collection->getItems();
  56. /** @var $customer Customer */
  57. foreach ($customerCollection as $customer) {
  58. $customer->load($customer->getId());
  59. if (!$this->encryptor->validateHashVersion($customer->getPasswordHash())) {
  60. list($hash, $salt, $version) = explode(Encryptor::DELIMITER, $customer->getPasswordHash(), 3);
  61. $version .= Encryptor::DELIMITER . Encryptor::HASH_VERSION_LATEST;
  62. $customer->setPasswordHash($this->encryptor->getHash($hash, $salt, $version));
  63. $customer->save();
  64. $output->write(".");
  65. }
  66. }
  67. $output->writeln(".");
  68. $output->writeln("<info>Finished</info>");
  69. }
  70. }