Skip to content

Commit

Permalink
Add configuration for the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
p-stoyanov committed Jan 2, 2020
1 parent 11b2252 commit 469964a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ password_policy:
expiry_listener:
# You can change the expiry listener priority
priority: 0
error_msg:
text: 'Your password expired. You need to change it'
type: 'error'
listener_priority: 0
# The route that needs to be shown to the user when password is expired
Expand Down
15 changes: 15 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Configuration implements ConfigurationInterface
private const DEFAULT_PASSWORDS_TO_REMEMBER = 3;
private const DEFAULT_EXPIRY_LISTENER_PRIORITY = 0;
private const DEFAULT_EXPIRY_DAYS = 90;
private const DEFAULT_ERROR_MSG = 'Your password expired. You need to change it';
private const DEFAULT_ERROR_TYPE = 'error';

/**
* Generates the configuration tree builder.
Expand Down Expand Up @@ -74,6 +76,19 @@ public function getConfigTreeBuilder()
->defaultValue(self::DEFAULT_EXPIRY_LISTENER_PRIORITY)
->treatNullLike(self::DEFAULT_EXPIRY_LISTENER_PRIORITY)
->end()
->arrayNode('error_msg')
->addDefaultsIfNotSet()
->children()
->scalarNode('text')
->defaultValue(self::DEFAULT_ERROR_MSG)
->treatNullLike(self::DEFAULT_ERROR_MSG)
->end()
->scalarNode('type')
->defaultValue(self::DEFAULT_ERROR_TYPE)
->treatNullLike(self::DEFAULT_ERROR_TYPE)
->end()
->end()
->end()
->end()
->end()

Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/PasswordPolicyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ private function addExpiryListener(ContainerBuilder $container, array $config):
->addTag('kernel.event_listener', [
'event' => 'kernel.request',
'priority' => $config['expiry_listener']['priority'],
]);
])
->setArgument('$errorMessage', $config['expiry_listener']['error_msg']['text'])
->setArgument('$errorMessageType', $config['expiry_listener']['error_msg']['type']);
}

/**
Expand Down
25 changes: 22 additions & 3 deletions src/EventListener/PasswordExpiryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,33 @@ class PasswordExpiryListener
*/
private $session;

/**
* @var string
*/
private $errorMessageType;

/**
* @var string
*/
private $errorMessage;

/**
* PasswordExpiryListener constructor.
* @param \Despark\PasswordPolicyBundle\Service\PasswordExpiryServiceInterface $passwordExpiryService
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param string $errorMessageType
* @param string $errorMessage
*/
public function __construct(PasswordExpiryServiceInterface $passwordExpiryService, SessionInterface $session)
{
public function __construct(
PasswordExpiryServiceInterface $passwordExpiryService,
SessionInterface $session,
string $errorMessageType,
string $errorMessage
) {
$this->passwordExpiryService = $passwordExpiryService;
$this->session = $session;
$this->errorMessageType = $errorMessageType;
$this->errorMessage = $errorMessage;
}

public function onKernelRequest(GetResponseEvent $event)
Expand All @@ -51,7 +70,7 @@ public function onKernelRequest(GetResponseEvent $event)
if (!in_array($route, $this->passwordExpiryService->getExcludedRoutes())
&& $this->passwordExpiryService->isPasswordExpired()) {
if ($this->session instanceof Session) {
$this->session->getFlashBag()->add('error', 'Your password expired. You need to change it');
$this->session->getFlashBag()->add($this->errorMessageType, $this->errorMessage);
}
$event->setResponse(new RedirectResponse($lockedUrl));
}
Expand Down

0 comments on commit 469964a

Please sign in to comment.