|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* File:Authentication.php |
|
6
|
|
|
* |
|
7
|
|
|
* @author Maciej Sławik <[email protected]> |
|
8
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LizardMedia\PasswordMigrator\Plugin\Model; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use LizardMedia\PasswordMigrator\Api\LegacyAuthenticationInterface; |
|
15
|
|
|
use LizardMedia\PasswordMigrator\Api\PasswordManagementInterface; |
|
16
|
|
|
use Magento\Customer\Model\Authentication as AuthenticationModel; |
|
17
|
|
|
use Magento\Customer\Model\CustomerRegistry; |
|
18
|
|
|
use Magento\Customer\Model\Session; |
|
19
|
|
|
use Magento\Framework\Exception\InputException; |
|
20
|
|
|
use Magento\Framework\Url; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* TODO: Unit test |
|
24
|
|
|
* Class Authentication |
|
25
|
|
|
* @package LizardMedia\PasswordMigrator\Plugin\Model |
|
26
|
|
|
*/ |
|
27
|
|
|
class Authentication |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var LegacyAuthenticationInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $legacyAuthentication; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var PasswordManagementInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $passwordManagement; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var CustomerRegistry |
|
41
|
|
|
*/ |
|
42
|
|
|
private $customerRegistry; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Session |
|
46
|
|
|
*/ |
|
47
|
|
|
private $session; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Url |
|
51
|
|
|
*/ |
|
52
|
|
|
private $url; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Authentication constructor. |
|
56
|
|
|
* @param LegacyAuthenticationInterface $legacyAuthentication |
|
57
|
|
|
* @param PasswordManagementInterface $passwordManagement |
|
58
|
|
|
* @param CustomerRegistry $customerRegistry |
|
59
|
|
|
* @param Session $session |
|
60
|
|
|
* @param Url $url |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
LegacyAuthenticationInterface $legacyAuthentication, |
|
64
|
|
|
PasswordManagementInterface $passwordManagement, |
|
65
|
|
|
CustomerRegistry $customerRegistry, |
|
66
|
|
|
Session $session, |
|
67
|
|
|
Url $url |
|
68
|
|
|
) { |
|
69
|
|
|
$this->legacyAuthentication = $legacyAuthentication; |
|
70
|
|
|
$this->passwordManagement = $passwordManagement; |
|
71
|
|
|
$this->customerRegistry = $customerRegistry; |
|
72
|
|
|
$this->session = $session; |
|
73
|
|
|
$this->url = $url; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param AuthenticationModel $subject |
|
78
|
|
|
* @param callable $proceed |
|
79
|
|
|
* @param $customerId |
|
80
|
|
|
* @param $password |
|
81
|
|
|
* @return bool |
|
82
|
|
|
* @throws Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
public function aroundAuthenticate(AuthenticationModel $subject, callable $proceed, $customerId, $password) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
try { |
|
87
|
|
|
return $proceed($customerId, $password); |
|
88
|
|
|
} catch (Exception $e) { |
|
89
|
|
|
if ($this->legacyAuthentication->canLegacyAuthenticate((int)$customerId, $password)) { |
|
90
|
|
|
$this->updateCustomerPassword((int) $customerId, $password); |
|
91
|
|
|
return $proceed($customerId, $password); |
|
92
|
|
|
} else { |
|
93
|
|
|
throw $e; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param int $customerId |
|
101
|
|
|
* @param string $password |
|
102
|
|
|
* @return void |
|
103
|
|
|
* @throws InputException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function updateCustomerPassword(int $customerId, string $password) : void |
|
106
|
|
|
{ |
|
107
|
|
|
try { |
|
108
|
|
|
$this->passwordManagement->updateCustomerPassword($customerId, $password); |
|
109
|
|
|
} catch (InputException $exception) { |
|
|
|
|
|
|
110
|
|
|
$this->session->setBeforeAuthUrl($this->getResetPasswordUrl($customerId)); |
|
111
|
|
|
throw $exception; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param int $customerId |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
private function getResetPasswordUrl(int $customerId) : string |
|
121
|
|
|
{ |
|
122
|
|
|
$secureData = $this->customerRegistry->retrieveSecureData($customerId); |
|
123
|
|
|
return $this->url->getUrl( |
|
124
|
|
|
'customer/account/createPassword', |
|
125
|
|
|
[ |
|
126
|
|
|
'id' => $customerId, |
|
127
|
|
|
'token' => $secureData->getRpToken() |
|
128
|
|
|
] |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.