diff --git a/README.md b/README.md index 7391a03..bda8876 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8d95114..f677ea6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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. @@ -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() diff --git a/src/DependencyInjection/PasswordPolicyExtension.php b/src/DependencyInjection/PasswordPolicyExtension.php index 0818df3..73ec81c 100644 --- a/src/DependencyInjection/PasswordPolicyExtension.php +++ b/src/DependencyInjection/PasswordPolicyExtension.php @@ -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']); } /** diff --git a/src/EventListener/PasswordExpiryListener.php b/src/EventListener/PasswordExpiryListener.php index d3a6e63..d72b9c1 100644 --- a/src/EventListener/PasswordExpiryListener.php +++ b/src/EventListener/PasswordExpiryListener.php @@ -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) @@ -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)); }