1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Customer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Magento\Framework\App\Area; |
7
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class ChangePasswordCommand extends AbstractCustomerCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Magento\Framework\App\State |
16
|
|
|
*/ |
17
|
|
|
private $state; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository |
21
|
|
|
*/ |
22
|
|
|
private $customerRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Magento\Customer\Model\CustomerRegistry $customerRegistry |
26
|
|
|
*/ |
27
|
|
|
private $customerRegistry; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Magento\Framework\Encryption\EncryptorInterface |
31
|
|
|
*/ |
32
|
|
|
private $encryptor; |
33
|
|
|
|
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('customer:change-password') |
38
|
|
|
->addArgument('email', InputArgument::OPTIONAL, 'Email') |
39
|
|
|
->addArgument('password', InputArgument::OPTIONAL, 'Password') |
40
|
|
|
->addArgument('website', InputArgument::OPTIONAL, 'Website of the customer') |
41
|
|
|
->setDescription('Changes the password of a customer.') |
42
|
|
|
; |
43
|
|
|
|
44
|
|
|
$help = <<<HELP |
45
|
|
|
- Website parameter must only be given if more than one websites are available. |
46
|
|
|
HELP; |
47
|
|
|
$this->setHelp($help); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository |
52
|
|
|
* @param \Magento\Customer\Model\CustomerRegistry $customerRegistry |
53
|
|
|
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor |
54
|
|
|
*/ |
55
|
|
|
public function inject( |
56
|
|
|
\Magento\Framework\App\State $state, |
57
|
|
|
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, |
58
|
|
|
\Magento\Customer\Model\CustomerRegistry $customerRegistry, |
59
|
|
|
\Magento\Framework\Encryption\EncryptorInterface $encryptor |
60
|
|
|
) { |
61
|
|
|
$this->state = $state; |
62
|
|
|
$this->customerRepository = $customerRepository; |
63
|
|
|
$this->customerRegistry = $customerRegistry; |
64
|
|
|
$this->encryptor = $encryptor; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param InputInterface $input |
69
|
|
|
* @param OutputInterface $output |
70
|
|
|
* |
71
|
|
|
* @return int|void |
72
|
|
|
*/ |
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$this->detectMagento($output); |
76
|
|
|
if ($this->initMagento()) { |
77
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
78
|
|
|
$email = $this->getHelper('parameter')->askEmail($input, $output); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// Password |
81
|
|
|
if (($password = $input->getArgument('password')) == null) { |
82
|
|
|
$password = $dialog->ask($output, '<question>Password:</question>'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$website = $this->getHelper('parameter')->askWebsite($input, $output); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$changePassword = function () use ($email, $website, $password) { |
88
|
|
|
$customer = $this->customerRepository->get($email, $website->getId()); |
89
|
|
|
$passwordHash = $this->encryptor->getHash($password, true); |
90
|
|
|
$this->customerRepository->save($customer, $passwordHash); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$this->state->emulateAreaCode(Area::AREA_FRONTEND, $changePassword); |
95
|
|
|
|
96
|
|
|
$output->writeln('<info>Password successfully changed</info>'); |
97
|
|
|
} catch (NoSuchEntityException $e) { |
|
|
|
|
98
|
|
|
$output->writeln('<error>Customer could not be found.</error>'); |
99
|
|
|
} catch (Exception $e) { |
100
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: