Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpgradeHashAlgorithmCommand.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 
16 class UpgradeHashAlgorithmCommand extends Command
17 {
21  private $customerCollectionFactory;
22 
26  private $collection;
27 
31  private $encryptor;
32 
37  public function __construct(
38  CollectionFactory $customerCollectionFactory,
39  Encryptor $encryptor
40  ) {
41  parent::__construct();
42  $this->customerCollectionFactory = $customerCollectionFactory;
43  $this->encryptor = $encryptor;
44  }
45 
49  protected function configure()
50  {
51  $this->setName('customer:hash:upgrade')
52  ->setDescription('Upgrade customer\'s hash according to the latest algorithm');
53  }
54 
58  protected function execute(InputInterface $input, OutputInterface $output)
59  {
60  $this->collection = $this->customerCollectionFactory->create();
61  $this->collection->addAttributeToSelect('*');
62  $customerCollection = $this->collection->getItems();
64  foreach ($customerCollection as $customer) {
65  $customer->load($customer->getId());
66  if (!$this->encryptor->validateHashVersion($customer->getPasswordHash())) {
67  list($hash, $salt, $version) = explode(Encryptor::DELIMITER, $customer->getPasswordHash(), 3);
69  $customer->setPasswordHash($this->encryptor->getHash($hash, $salt, $version));
70  $customer->save();
71  $output->write(".");
72  }
73  }
74  $output->writeln(".");
75  $output->writeln("<info>Finished</info>");
76  }
77 }
$customer
Definition: customers.php:11
__construct(CollectionFactory $customerCollectionFactory, Encryptor $encryptor)