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
; 
  16 use Psr\Log\LoggerInterface
; 
  18 use Rapsys\PackBundle\Util\SluggerUtil
; 
  20 use Rapsys\UserBundle\Exception\UnactivatedException
; 
  21 use Rapsys\UserBundle\RapsysUserBundle
; 
  23 use Symfony\Bridge\Twig\Mime\TemplatedEmail
; 
  24 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  25 use Symfony\Component\HttpFoundation\RedirectResponse
; 
  26 use Symfony\Component\HttpFoundation\Request
; 
  27 use Symfony\Component\HttpFoundation\RequestStack
; 
  28 use Symfony\Component\HttpFoundation\Response
; 
  29 use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface
; 
  30 use Symfony\Component\HttpKernel\HttpKernelInterface
; 
  31 use Symfony\Component\Mailer\MailerInterface
; 
  32 use Symfony\Component\Mime\Address
; 
  33 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
  34 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  35 use Symfony\Component\Routing\RequestContext
; 
  36 use Symfony\Component\Routing\RouterInterface
; 
  37 use Symfony\Component\Security\Core\Exception\AuthenticationException
; 
  38 use Symfony\Component\Security\Core\Exception\BadCredentialsException
; 
  39 use Symfony\Component\Security\Core\Exception\DisabledException
; 
  40 use Symfony\Component\Security\Core\Exception\UserNotFoundException
; 
  41 use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler
; 
  42 use Symfony\Component\Security\Http\HttpUtils
; 
  43 use Symfony\Contracts\Translation\TranslatorInterface
; 
  48 class AuthenticationFailureHandler 
extends DefaultAuthenticationFailureHandler 
{ 
  52         protected string $alias; 
  57         protected array $config; 
  58         protected array $defaultOptions = [ 
  59                 'failure_path' => null, 
  60                 'failure_forward' => false, 
  61                 'login_path' => '/login', 
  62                 'failure_path_parameter' => '_failure_path', 
  68          * @xxx Second argument will be replaced by security.firewalls.main.logout.target 
  69          * @see vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php +360 
  71          * @param HttpKernelInterface $httpKernel The http kernel 
  72          * @param HttpUtils $httpUtils The http utils 
  73          * @param array $options The options 
  74          * @param LoggerInterface $logger The logger instance 
  75          * @param ContainerInterface $container The container instance 
  76          * @param ManagerRegistry $doctrine The doctrine instance 
  77          * @param MailerInterface $mailer The mailer instance 
  78          * @param RouterInterface $router The router instance 
  79          * @param SluggerUtil $slugger The slugger instance 
  80          * @param RequestStack $stack The stack instance 
  81          * @param TranslatorInterface $translator The translator instance 
  83         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) { 
  84                 //Call parent constructor 
  85                 parent
::__construct($httpKernel, $httpUtils, $options, $logger); 
  88                 $this->config 
= $container->getParameter($this->alias 
= RapsysUserBundle
::getAlias()); 
  92          * Adds a flash message to the current session for type. 
  94          * @throws \LogicException 
  96         protected function addFlash(string $type, mixed $message): void { 
  98                         $session = $this->stack
->getSession(); 
  99                 } catch (SessionNotFoundException 
$e) { 
 100                         throw new \
LogicException('You cannot use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e); 
 103                 if (!$session instanceof FlashBagAwareSessionInterface
) { 
 104                         throw new \
LogicException(sprintf('You cannot use the addFlash method because class "%s" doesn\'t implement "%s".', get_debug_type($session), FlashBagAwareSessionInterface
::class)); 
 107                 $session->getFlashBag()->add($type, $message); 
 113          * This is called when an interactive authentication attempt fails 
 115         public function onAuthenticationFailure(Request 
$request, AuthenticationException 
$exception): Response 
{ 
 116                 //With bad credential exception 
 117                 if ($exception instanceof BadCredentialsException
) { 
 118                         //With parent exception 
 119                         if (($parent = $exception->getPrevious()) instanceof UserNotFoundException
) { 
 120                                 /** Disabled to prevent user mail + hash retrieval for each unactivated/locked accounts 
 122                                 //Get user identifier 
 123                                 $mail = $parent->getUserIdentifier(); 
 125                                 //Set extra parameters 
 126                                 $extra = ['mail' => $smail = $this->slugger->short($mail), 'hash' => $this->slugger->hash($smail)];*/ 
 128                                 //With failure target path option 
 129                                 if (!empty($failurePath = $this->options
['failure_path'])) { 
 131                                         if ($failurePath[0] == '/') { 
 132                                                 //Create login path request instance 
 133                                                 $req = Request
::create($failurePath); 
 135                                                 //Get login path pathinfo 
 136                                                 $path = $req->getPathInfo(); 
 139                                                 $path = str_replace($request->getScriptName(), '', $path); 
 141                                                 //Try with login path path 
 144                                                         $oldContext = $this->router
->getContext(); 
 146                                                         //Force clean context 
 147                                                         //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST 
 148                                                         //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42 
 149                                                         $this->router
->setContext(new RequestContext()); 
 151                                                         //Retrieve route matching path 
 152                                                         $route = $this->router
->match($path); 
 155                                                         $this->router
->setContext($oldContext); 
 161                                                         if ($name = $route['_route']) { 
 162                                                                 //Remove route and controller from route defaults 
 163                                                                 unset($route['_route'], $route['_controller'], $route['_canonical_route']); 
 166                                                                 $url = $this->router
->generate($name, /*$extra+*/$route); 
 168                                                                 //Return redirect to url response 
 169                                                                 return new RedirectResponse($url, 302); 
 172                                                 } catch (ResourceNotFoundException 
$e) { 
 173                                                         //Unset default path, name and route 
 174                                                         unset($failurePath, $name, $route); 
 178                                                 //Try with login path route 
 180                                                         //Retrieve route matching path 
 181                                                         $url = $this->router
->generate($failurePath/*, $extra*/); 
 183                                                         //Return redirect to url response 
 184                                                         return new RedirectResponse($url, 302); 
 185                                                 //Route not found, missing parameter or invalid parameter 
 186                                                 } catch (RouteNotFoundException
|MissingMandatoryParametersException
|InvalidParameterException 
$e) { 
 187                                                         //Unset default path and url 
 188                                                         unset($failurePath, $url); 
 192                         //With not enabled user 
 193                         } elseif ($parent instanceof DisabledException
) { 
 194                                 //Add error message account is not enabled 
 195                                 $this->addFlash('error', $this->translator
->trans('Account not enabled', [], $this->alias
)); 
 197                                 //Redirect on the same route with sent=1 to cleanup form 
 198                                 return new RedirectResponse($this->router
->generate($request->get('_route'), $request->get('_route_params')), 302); 
 199                         //With not activated user 
 200                         } elseif ($parent instanceof UnactivatedException
) { 
 202                                 $user = $parent->getUser(); 
 206                                         'recipient_mail' => $user->getMail(), 
 207                                         'recipient_name' => $user->getRecipientName() 
 208                                 ] + 
array_replace_recursive( 
 209                                         $this->config
['context'], 
 210                                         $this->config
['register']['view']['context'], 
 211                                         $this->config
['register']['mail']['context'] 
 214                                 //Generate each route route 
 215                                 foreach($this->config
['register']['route'] as $route => $tag) { 
 216                                         //Only process defined routes 
 217                                         if (!empty($this->config
['route'][$route])) { 
 218                                                 //Process for confirm url 
 219                                                 if ($route == 'confirm') { 
 220                                                         //Set the url in context 
 221                                                         $context[$tag] = $this->router
->generate( 
 222                                                                 $this->config
['route'][$route]['name'], 
 223                                                                 //Prepend confirm context with tag 
 225                                                                         'mail' => $smail = $this->slugger
->short($context['recipient_mail']), 
 226                                                                         'hash' => $this->slugger
->hash($smail) 
 227                                                                 ]+
$this->config
['route'][$route]['context'], 
 228                                                                 UrlGeneratorInterface
::ABSOLUTE_URL
 
 234                                 //Iterate on keys to translate 
 235                                 foreach($this->config
['translate'] as $translate) { 
 237                                         $keys = explode('.', $translate); 
 240                                         $current =& $context; 
 242                                         //Iterate on each subkey 
 244                                                 //Skip unset translation keys 
 245                                                 if (!isset($current[current($keys)])) { 
 249                                                 //Set current to subkey 
 250                                                 $current =& $current[current($keys)]; 
 251                                         } while(next($keys)); 
 254                                         $current = $this->translator
->trans($current, [], $this->alias
); 
 261                                 $context['subject'] = $subject = ucfirst( 
 262                                         $this->translator
->trans( 
 263                                                 $this->config
['register']['mail']['subject'], 
 264                                                 $this->slugger
->flatten($context, null, '.', '%', '%'), 
 270                                 $message = (new TemplatedEmail()) 
 272                                         ->from(new Address($this->config
['contact']['address'], $this->config
['contact']['name'])) 
 274                                         //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 275                                         ->to(new Address($context['recipient_mail'], $context['recipient_name'])) 
 277                                         ->subject($context['subject']) 
 279                                         //Set path to twig templates 
 280                                         ->htmlTemplate($this->config
['register']['mail']['html']) 
 281                                         ->textTemplate($this->config
['register']['mail']['text']) 
 286                                 //Try sending message 
 287                                 //XXX: mail delivery may silently fail 
 290                                         $this->mailer
->send($message); 
 291                                 //Catch obvious transport exception 
 292                                 } catch(TransportExceptionInterface 
$e) { 
 293                                         //Add error message mail unreachable 
 294                                         $this->addFlash('error', $this->translator
->trans('Unable to reach account', [], $this->alias
)); 
 298                                 $this->addFlash('notice', $this->translator
->trans('Your verification mail has been sent, to activate your account follow the confirmation link inside', [], $this->alias
)); 
 301                                 $this->addFlash('warning', $this->translator
->trans('If you did not receive a verification mail, check your Spam or Junk mail folder', [], $this->alias
)); 
 303                                 //Redirect on the same route with sent=1 to cleanup form 
 304                                 return new RedirectResponse($this->router
->generate($request->get('_route'), $request->get('_route_params')), 302); 
 308                 //Call parent function 
 309                 return parent
::onAuthenticationFailure($request, $exception);