1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys UserBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\UserBundle\Handler
; 
  14 use Doctrine\Persistence\ManagerRegistry
; 
  15 use Psr\Log\LoggerInterface
; 
  16 use Symfony\Bridge\Twig\Mime\TemplatedEmail
; 
  17 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  18 use Symfony\Component\HttpFoundation\RedirectResponse
; 
  19 use Symfony\Component\HttpFoundation\Request
; 
  20 use Symfony\Component\HttpFoundation\RequestStack
; 
  21 use Symfony\Component\HttpFoundation\Response
; 
  22 use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface
; 
  23 use Symfony\Component\HttpKernel\HttpKernelInterface
; 
  24 use Symfony\Component\Mailer\MailerInterface
; 
  25 use Symfony\Component\Mime\Address
; 
  26 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
  27 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  28 use Symfony\Component\Routing\RequestContext
; 
  29 use Symfony\Component\Routing\RouterInterface
; 
  30 use Symfony\Component\Security\Core\Exception\AuthenticationException
; 
  31 use Symfony\Component\Security\Core\Exception\BadCredentialsException
; 
  32 use Symfony\Component\Security\Core\Exception\DisabledException
; 
  33 use Symfony\Component\Security\Core\Exception\UserNotFoundException
; 
  34 use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler
; 
  35 use Symfony\Component\Security\Http\HttpUtils
; 
  36 use Symfony\Contracts\Translation\TranslatorInterface
; 
  38 use Rapsys\PackBundle\Util\SluggerUtil
; 
  39 use Rapsys\UserBundle\Exception\UnactivatedException
; 
  40 use Rapsys\UserBundle\RapsysUserBundle
; 
  45 class AuthenticationFailureHandler 
extends DefaultAuthenticationFailureHandler 
{ 
  49         protected array $config; 
  50         protected array $defaultOptions = [ 
  51                 'failure_path' => null, 
  52                 'failure_forward' => false, 
  53                 'login_path' => '/login', 
  54                 'failure_path_parameter' => '_failure_path', 
  58          * @xxx Second argument will be replaced by security.firewalls.main.logout.target 
  59          * @see vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php +360 
  61          * @param HttpKernelInterface $httpKernel The http kernel 
  62          * @param HttpUtils $httpUtils The http utils 
  63          * @param array $options The options 
  64          * @param LoggerInterface $logger The logger instance 
  65          * @param ContainerInterface $container The container instance 
  66          * @param ManagerRegistry $doctrine The doctrine instance 
  67          * @param MailerInterface $mailer The mailer instance 
  68          * @param RouterInterface $router The router instance 
  69          * @param SluggerUtil $slugger The slugger instance 
  70          * @param RequestStack $stack The stack instance 
  71          * @param TranslatorInterface $translator The translator instance 
  75         public function __construct(protected HttpKernelInterface 
$httpKernel, protected HttpUtils 
$httpUtils, protected array $options, protected LoggerInterface 
$logger, protected ContainerInterface 
$container, protected ManagerRegistry 
$doctrine, protected MailerInterface 
$mailer, protected RouterInterface 
$router, protected SluggerUtil 
$slugger, protected RequestStack 
$stack, protected TranslatorInterface 
$translator) { 
  76                 //Call parent constructor 
  77                 parent
::__construct($httpKernel, $httpUtils, $options, $logger); 
  80                 $this->config 
= $container->getParameter(self
::getAlias()); 
  84          * Adds a flash message to the current session for type. 
  86          * @throws \LogicException 
  88         protected function addFlash(string $type, mixed $message): void { 
  90                         $session = $this->stack
->getSession(); 
  91                 } catch (SessionNotFoundException 
$e) { 
  92                         throw new \
LogicException('You cannot use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e); 
  95                 if (!$session instanceof FlashBagAwareSessionInterface
) { 
  96                         throw new \
LogicException(sprintf('You cannot use the addFlash method because class "%s" doesn\'t implement "%s".', get_debug_type($session), FlashBagAwareSessionInterface
::class)); 
  99                 $session->getFlashBag()->add($type, $message); 
 103          * This is called when an interactive authentication attempt fails 
 107         public function onAuthenticationFailure(Request 
$request, AuthenticationException 
$exception): Response 
{ 
 108                 //With bad credential exception 
 109                 if ($exception instanceof BadCredentialsException
) { 
 110                         //With parent exception 
 111                         if (($parent = $exception->getPrevious()) instanceof UserNotFoundException
) { 
 112                                 /** Disabled to prevent user mail + hash retrieval for each unactivated/locked accounts 
 114                                 //Get user identifier 
 115                                 $mail = $parent->getUserIdentifier(); 
 117                                 //Set extra parameters 
 118                                 $extra = ['mail' => $smail = $this->slugger->short($mail), 'hash' => $this->slugger->hash($smail)];*/ 
 120                                 //With failure target path option 
 121                                 if (!empty($failurePath = $this->options
['failure_path'])) { 
 123                                         if ($failurePath[0] == '/') { 
 124                                                 //Create login path request instance 
 125                                                 $req = Request
::create($failurePath); 
 127                                                 //Get login path pathinfo 
 128                                                 $path = $req->getPathInfo(); 
 131                                                 $path = str_replace($request->getScriptName(), '', $path); 
 133                                                 //Try with login path path 
 136                                                         $oldContext = $this->router
->getContext(); 
 138                                                         //Force clean context 
 139                                                         //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST 
 140                                                         //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42 
 141                                                         $this->router
->setContext(new RequestContext()); 
 143                                                         //Retrieve route matching path 
 144                                                         $route = $this->router
->match($path); 
 147                                                         $this->router
->setContext($oldContext); 
 153                                                         if ($name = $route['_route']) { 
 154                                                                 //Remove route and controller from route defaults 
 155                                                                 unset($route['_route'], $route['_controller'], $route['_canonical_route']); 
 158                                                                 $url = $this->router
->generate($name, /*$extra+*/$route); 
 160                                                                 //Return redirect to url response 
 161                                                                 return new RedirectResponse($url, 302); 
 164                                                 } catch (ResourceNotFoundException 
$e) { 
 165                                                         //Unset default path, name and route 
 166                                                         unset($failurePath, $name, $route); 
 170                                                 //Try with login path route 
 172                                                         //Retrieve route matching path 
 173                                                         $url = $this->router
->generate($failurePath/*, $extra*/); 
 175                                                         //Return redirect to url response 
 176                                                         return new RedirectResponse($url, 302); 
 177                                                 //Route not found, missing parameter or invalid parameter 
 178                                                 } catch (RouteNotFoundException
|MissingMandatoryParametersException
|InvalidParameterException 
$e) { 
 179                                                         //Unset default path and url 
 180                                                         unset($failurePath, $url); 
 184                         //With not enabled user 
 185                         } elseif ($parent instanceof DisabledException
) { 
 186                                 //Add error message account is not enabled 
 187                                 $this->addFlash('error', $this->translator
->trans('Account not enabled')); 
 189                                 //Redirect on the same route with sent=1 to cleanup form 
 190                                 return new RedirectResponse($this->router
->generate($request->get('_route'), $request->get('_route_params')), 302); 
 191                         //With not activated user 
 192                         } elseif ($parent instanceof UnactivatedException
) { 
 194                                 $user = $parent->getUser(); 
 198                                         'recipient_mail' => $user->getMail(), 
 199                                         'recipient_name' => $user->getRecipientName() 
 200                                 ] + 
array_replace_recursive( 
 201                                         $this->config
['context'], 
 202                                         $this->config
['register']['view']['context'], 
 203                                         $this->config
['register']['mail']['context'] 
 206                                 //Generate each route route 
 207                                 foreach($this->config
['register']['route'] as $route => $tag) { 
 208                                         //Only process defined routes 
 209                                         if (!empty($this->config
['route'][$route])) { 
 210                                                 //Process for confirm url 
 211                                                 if ($route == 'confirm') { 
 212                                                         //Set the url in context 
 213                                                         $context[$tag] = $this->router
->generate( 
 214                                                                 $this->config
['route'][$route]['name'], 
 215                                                                 //Prepend confirm context with tag 
 217                                                                         'mail' => $smail = $this->slugger
->short($context['recipient_mail']), 
 218                                                                         'hash' => $this->slugger
->hash($smail) 
 219                                                                 ]+
$this->config
['route'][$route]['context'], 
 220                                                                 UrlGeneratorInterface
::ABSOLUTE_URL
 
 226                                 //Iterate on keys to translate 
 227                                 foreach($this->config
['translate'] as $translate) { 
 229                                         $keys = explode('.', $translate); 
 232                                         $current =& $context; 
 234                                         //Iterate on each subkey 
 236                                                 //Skip unset translation keys 
 237                                                 if (!isset($current[current($keys)])) { 
 241                                                 //Set current to subkey 
 242                                                 $current =& $current[current($keys)]; 
 243                                         } while(next($keys)); 
 246                                         $current = $this->translator
->trans($current); 
 253                                 $context['subject'] = $subject = ucfirst( 
 254                                         $this->translator
->trans( 
 255                                                 $this->config
['register']['mail']['subject'], 
 256                                                 $this->slugger
->flatten($context, null, '.', '%', '%') 
 261                                 $message = (new TemplatedEmail()) 
 263                                         ->from(new Address($this->config
['contact']['address'], $this->config
['contact']['name'])) 
 265                                         //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 266                                         ->to(new Address($context['recipient_mail'], $context['recipient_name'])) 
 268                                         ->subject($context['subject']) 
 270                                         //Set path to twig templates 
 271                                         ->htmlTemplate($this->config
['register']['mail']['html']) 
 272                                         ->textTemplate($this->config
['register']['mail']['text']) 
 277                                 //Try sending message 
 278                                 //XXX: mail delivery may silently fail 
 281                                         $this->mailer
->send($message); 
 282                                 //Catch obvious transport exception 
 283                                 } catch(TransportExceptionInterface 
$e) { 
 284                                         //Add error message mail unreachable 
 285                                         $this->addFlash('error', $this->translator
->trans('Unable to reach account')); 
 289                                 $this->addFlash('notice', $this->translator
->trans('Your verification mail has been sent, to activate your account follow the confirmation link inside')); 
 292                                 $this->addFlash('warning', $this->translator
->trans('If you did not receive a verification mail, check your Spam or Junk mail folder')); 
 294                                 //Redirect on the same route with sent=1 to cleanup form 
 295                                 return new RedirectResponse($this->router
->generate($request->get('_route'), $request->get('_route_params')), 302); 
 299                 //Call parent function 
 300                 return parent
::onAuthenticationFailure($request, $exception); 
 306         public function getAlias(): string { 
 307                 return RapsysUserBundle
::getAlias();