Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch PasswordStrengthValidateEvent in PasswordStrengthEstimatorModel #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/bundles/UserBundle/Event/PasswordStrengthValidateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Mautic\UserBundle\Event;

use Symfony\Contracts\EventDispatcher\Event;

class PasswordStrengthValidateEvent extends Event
{
public function __construct(
public bool $isValid,
public string $password
) {
}
}
12 changes: 10 additions & 2 deletions app/bundles/UserBundle/Model/PasswordStrengthEstimatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Mautic\UserBundle\Model;

use Mautic\UserBundle\Event\PasswordStrengthValidateEvent;
use Mautic\UserBundle\UserEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use ZxcvbnPhp\Zxcvbn as PasswordStrengthEstimator;

class PasswordStrengthEstimatorModel
Expand All @@ -21,7 +24,7 @@ class PasswordStrengthEstimatorModel

private PasswordStrengthEstimator $passwordStrengthEstimator;

public function __construct()
public function __construct(private EventDispatcherInterface $dispatcher)
{
$this->passwordStrengthEstimator = new PasswordStrengthEstimator();
}
Expand All @@ -31,7 +34,12 @@ public function __construct()
*/
public function validate(?string $password, int $score = self::MINIMUM_PASSWORD_STRENGTH_ALLOWED, array $dictionary = self::DICTIONARY): bool
{
return $score <= $this->passwordStrengthEstimator->passwordStrength($password, $this->sanitizeDictionary($dictionary))['score'];
$isValid = $score <= $this->passwordStrengthEstimator->passwordStrength($password, $this->sanitizeDictionary($dictionary))['score'];

$passwordStrengthValidateEvent = new PasswordStrengthValidateEvent($isValid, $password);
$this->dispatcher->dispatch($passwordStrengthValidateEvent, UserEvents::USER_PASSWORD_STRENGTH_VALIDATION);

return $passwordStrengthValidateEvent->isValid;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class PasswordSubscriberTest extends TestCase
{
Expand All @@ -30,9 +31,15 @@ class PasswordSubscriberTest extends TestCase
*/
private $pluginToken;

/**
* @var MockObject|EventDispatcherInterface
*/
private $dispatcher;

protected function setUp(): void
{
$this->passwordStrengthEstimatorModel = new PasswordStrengthEstimatorModel();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->passwordStrengthEstimatorModel = new PasswordStrengthEstimatorModel($this->dispatcher);
$this->passwordSubscriber = new PasswordSubscriber($this->passwordStrengthEstimatorModel);
$this->authenticationEvent = $this->createMock(AuthenticationEvent::class);
$this->pluginToken = $this->createMock(PluginToken::class);
Expand All @@ -42,7 +49,7 @@ protected function setUp(): void
->willReturn($this->pluginToken);
}

public function testThatItIsSubcribedToEvents(): void
public function testThatItIsSubscribedToEvents(): void
{
Assert::assertArrayHasKey(UserEvents::USER_FORM_POST_LOCAL_PASSWORD_AUTHENTICATION, PasswordSubscriber::getSubscribedEvents());
}
Expand Down
10 changes: 10 additions & 0 deletions app/bundles/UserBundle/UserEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ final class UserEvents
* @var string
*/
public const USER_FORM_POST_LOCAL_PASSWORD_AUTHENTICATION ='mautic.user_form_post_local_password_authentication';

/**
* The mautic.user_password_strength_validation event is dispatched after mautic checks if user's password meets the strength requirements
* This can be used to add custom password requirements.
*
* The event listener receives a Mautic\UserBundle\Event\PasswordStrengthValidateEvent instance.
*
* @var string
*/
public const USER_PASSWORD_STRENGTH_VALIDATION = 'mautic.user_password_strength_validation';
}