Skip to content

Commit 6d52d3e

Browse files
author
Timon de Groot
authored
Merge pull request #1 from sinisa-colic/master
Refactor code to use plugin instead of preference
2 parents 607f446 + b065920 commit 6d52d3e

File tree

3 files changed

+103
-117
lines changed

3 files changed

+103
-117
lines changed

Plugin/AccountManagementPlugin.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Timpack\PwnedValidator\Plugin;
4+
5+
use Magento\Customer\Api\AccountManagementInterface;
6+
use Magento\Customer\Api\Data\CustomerInterface;
7+
use Magento\Framework\Event\ManagerInterface;
8+
9+
class AccountManagementPlugin
10+
{
11+
/**
12+
* @var ManagerInterface
13+
*/
14+
protected $eventManager;
15+
16+
/**
17+
* AccountManagementPlugin constructor.
18+
* @param ManagerInterface $eventManager
19+
*/
20+
public function __construct(
21+
ManagerInterface $eventManager
22+
) {
23+
$this->eventManager = $eventManager;
24+
}
25+
26+
/**
27+
* @param string|null $password
28+
*/
29+
private function dispatchPasswordCheckEvent($password)
30+
{
31+
if (!is_null($password)) {
32+
$this->eventManager->dispatch(
33+
'timpack_pwnedvalidator_check_password_strength',
34+
[
35+
'password' => $password,
36+
]
37+
);
38+
}
39+
}
40+
41+
/**
42+
* @param AccountManagementInterface $subject
43+
* @param string $email
44+
* @param string $resetToken
45+
* @param string $newPassword
46+
*/
47+
public function beforeResetPassword(
48+
AccountManagementInterface $subject,
49+
$email,
50+
$resetToken,
51+
$newPassword
52+
) {
53+
$this->dispatchPasswordCheckEvent($newPassword);
54+
}
55+
56+
/**
57+
* @param AccountManagementInterface $subject
58+
* @param string $email
59+
* @param string $currentPassword
60+
* @param string $newPassword
61+
*/
62+
public function beforeChangePassword(
63+
AccountManagementInterface $subject,
64+
$email,
65+
$currentPassword,
66+
$newPassword
67+
) {
68+
$this->dispatchPasswordCheckEvent($newPassword);
69+
}
70+
71+
/**
72+
* @param AccountManagementInterface $subject
73+
* @param string $customerId
74+
* @param string $currentPassword
75+
* @param string $newPassword
76+
*/
77+
public function beforeChangePasswordById(
78+
AccountManagementInterface $subject,
79+
$customerId,
80+
$currentPassword,
81+
$newPassword
82+
) {
83+
$this->dispatchPasswordCheckEvent($newPassword);
84+
}
85+
86+
/**
87+
* @param AccountManagementInterface $subject
88+
* @param CustomerInterface $customer
89+
* @param null $password
90+
* @param string $redirectUrl
91+
*/
92+
public function beforeCreateAccount(
93+
AccountManagementInterface $subject,
94+
CustomerInterface $customer,
95+
$password = null,
96+
$redirectUrl = ''
97+
) {
98+
$this->dispatchPasswordCheckEvent($password);
99+
}
100+
}

Rewrite/Model/AccountManagement.php

-115
This file was deleted.

etc/di.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<preference for="Timpack\PwnedValidator\Api\ValidatorInterface" type="Timpack\PwnedValidator\Model\Validator"/>
5-
<preference for="Magento\Customer\Model\AccountManagement"
6-
type="Timpack\PwnedValidator\Rewrite\Model\AccountManagement"/>
5+
<type name="Magento\Customer\Api\AccountManagementInterface">
6+
<plugin name="timpack_pwnedvalidator_magento_customer_api_accountmanagementinterface" type="Timpack\PwnedValidator\Plugin\AccountManagementPlugin"/>
7+
</type>
78
<type name="Timpack\PwnedValidator\Api\ValidatorInterface">
89
<arguments>
910
<argument name="httpClient" xsi:type="object">Magento\Framework\HTTP\Client\Curl</argument>

0 commit comments

Comments
 (0)