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\Session; |
18
|
|
|
use Magento\Framework\Exception\InputException; |
19
|
|
|
use Magento\Framework\Url; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TODO: Unit test |
23
|
|
|
* Class Authentication |
24
|
|
|
* @package LizardMedia\PasswordMigrator\Plugin\Model |
25
|
|
|
*/ |
26
|
|
|
class Authentication |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var LegacyAuthenticationInterface |
30
|
|
|
*/ |
31
|
|
|
private $legacyAuthentication; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PasswordManagementInterface |
35
|
|
|
*/ |
36
|
|
|
private $passwordManagement; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Session |
40
|
|
|
*/ |
41
|
|
|
private $session; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Url |
45
|
|
|
*/ |
46
|
|
|
private $url; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Authentication constructor. |
50
|
|
|
* @param LegacyAuthenticationInterface $legacyAuthentication |
51
|
|
|
* @param PasswordManagementInterface $passwordManagement |
52
|
|
|
* @param Session $session |
53
|
|
|
* @param Url $url |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
LegacyAuthenticationInterface $legacyAuthentication, |
57
|
|
|
PasswordManagementInterface $passwordManagement, |
58
|
|
|
Session $session, |
59
|
|
|
Url $url |
60
|
|
|
) { |
61
|
|
|
$this->legacyAuthentication = $legacyAuthentication; |
62
|
|
|
$this->passwordManagement = $passwordManagement; |
63
|
|
|
$this->session = $session; |
64
|
|
|
$this->url = $url; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param AuthenticationModel $subject |
69
|
|
|
* @param callable $proceed |
70
|
|
|
* @param $customerId |
71
|
|
|
* @param $password |
72
|
|
|
* @return bool |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
public function aroundAuthenticate(AuthenticationModel $subject, callable $proceed, $customerId, $password) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
return $proceed($customerId, $password); |
79
|
|
|
} catch (Exception $e) { |
80
|
|
|
if ($this->legacyAuthentication->canLegacyAuthenticate((int)$customerId, $password)) { |
81
|
|
|
$this->updateCustomerPassword((int) $customerId, $password); |
82
|
|
|
return $proceed($customerId, $password); |
83
|
|
|
} else { |
84
|
|
|
throw $e; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $customerId |
92
|
|
|
* @param string $password |
93
|
|
|
* @return void |
94
|
|
|
* @throws InputException |
95
|
|
|
*/ |
96
|
|
|
private function updateCustomerPassword(int $customerId, string $password) : void |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$this->passwordManagement->updateCustomerPassword($customerId, $password); |
100
|
|
|
} catch (InputException $exception) { |
|
|
|
|
101
|
|
|
$this->session->setBeforeAuthUrl($this->getResetPasswordUrl()); |
102
|
|
|
throw $exception; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private function getResetPasswordUrl() : string |
111
|
|
|
{ |
112
|
|
|
return $this->url->getUrl('customer/account/forgotpassword'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.