Skip to content

Commit

Permalink
Merge pull request #1 from sinisa-colic/master
Browse files Browse the repository at this point in the history
Refactor code to use plugin instead of preference
  • Loading branch information
Timon de Groot authored May 8, 2020
2 parents 607f446 + b065920 commit 6d52d3e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 117 deletions.
100 changes: 100 additions & 0 deletions Plugin/AccountManagementPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Timpack\PwnedValidator\Plugin;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;

class AccountManagementPlugin
{
/**
* @var ManagerInterface
*/
protected $eventManager;

/**
* AccountManagementPlugin constructor.
* @param ManagerInterface $eventManager
*/
public function __construct(
ManagerInterface $eventManager
) {
$this->eventManager = $eventManager;
}

/**
* @param string|null $password
*/
private function dispatchPasswordCheckEvent($password)
{
if (!is_null($password)) {
$this->eventManager->dispatch(
'timpack_pwnedvalidator_check_password_strength',
[
'password' => $password,
]
);
}
}

/**
* @param AccountManagementInterface $subject
* @param string $email
* @param string $resetToken
* @param string $newPassword
*/
public function beforeResetPassword(
AccountManagementInterface $subject,
$email,
$resetToken,
$newPassword
) {
$this->dispatchPasswordCheckEvent($newPassword);
}

/**
* @param AccountManagementInterface $subject
* @param string $email
* @param string $currentPassword
* @param string $newPassword
*/
public function beforeChangePassword(
AccountManagementInterface $subject,
$email,
$currentPassword,
$newPassword
) {
$this->dispatchPasswordCheckEvent($newPassword);
}

/**
* @param AccountManagementInterface $subject
* @param string $customerId
* @param string $currentPassword
* @param string $newPassword
*/
public function beforeChangePasswordById(
AccountManagementInterface $subject,
$customerId,
$currentPassword,
$newPassword
) {
$this->dispatchPasswordCheckEvent($newPassword);
}

/**
* @param AccountManagementInterface $subject
* @param CustomerInterface $customer
* @param null $password
* @param string $redirectUrl
*/
public function beforeCreateAccount(
AccountManagementInterface $subject,
CustomerInterface $customer,
$password = null,
$redirectUrl = ''
) {
$this->dispatchPasswordCheckEvent($password);
}
}
115 changes: 0 additions & 115 deletions Rewrite/Model/AccountManagement.php

This file was deleted.

5 changes: 3 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Timpack\PwnedValidator\Api\ValidatorInterface" type="Timpack\PwnedValidator\Model\Validator"/>
<preference for="Magento\Customer\Model\AccountManagement"
type="Timpack\PwnedValidator\Rewrite\Model\AccountManagement"/>
<type name="Magento\Customer\Api\AccountManagementInterface">
<plugin name="timpack_pwnedvalidator_magento_customer_api_accountmanagementinterface" type="Timpack\PwnedValidator\Plugin\AccountManagementPlugin"/>
</type>
<type name="Timpack\PwnedValidator\Api\ValidatorInterface">
<arguments>
<argument name="httpClient" xsi:type="object">Magento\Framework\HTTP\Client\Curl</argument>
Expand Down

0 comments on commit 6d52d3e

Please sign in to comment.