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\Controller
; 
  14 use Doctrine\Bundle\DoctrineBundle\Registry
; 
  15 use Doctrine\ORM\EntityManagerInterface
; 
  16 use Psr\Log\LoggerInterface
; 
  17 use Symfony\Bridge\Twig\Mime\TemplatedEmail
; 
  18 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
; 
  19 use Symfony\Component\DependencyInjection\ContainerInterface
; 
  20 use Symfony\Component\Form\FormError
; 
  21 use Symfony\Component\HttpFoundation\Request
; 
  22 use Symfony\Component\HttpFoundation\Response
; 
  23 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException
; 
  24 use Symfony\Component\Mailer\Exception\TransportExceptionInterface
; 
  25 use Symfony\Component\Mailer\MailerInterface
; 
  26 use Symfony\Component\Mime\Address
; 
  27 use Symfony\Component\Routing\Exception\MethodNotAllowedException
; 
  28 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
  29 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  30 use Symfony\Component\Routing\RequestContext
; 
  31 use Symfony\Component\Routing\RouterInterface
; 
  32 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
; 
  33 use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface
; 
  34 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils
; 
  35 use Symfony\Component\Translation\TranslatorInterface
; 
  37 use Rapsys\PackBundle\Util\SluggerUtil
; 
  38 use Rapsys\UserBundle\RapsysUserBundle
; 
  40 class DefaultController 
extends AbstractController 
{ 
  45         protected $translator; 
  50          * @TODO: move all canonical and other view related stuff in an user AbstractController like in RapsysAir render feature !!!! 
  52          * @param ContainerInterface $container The containter instance 
  53          * @param RouterInterface $router The router instance 
  54          * @param TranslatorInterface $translator The translator instance 
  56         public function __construct(ContainerInterface 
$container, RouterInterface 
$router, TranslatorInterface 
$translator) { 
  58                 $this->config 
= $container->getParameter(self
::getAlias()); 
  61                 $this->translator 
= $translator; 
  64                 $stack = $container->get('request_stack'); 
  67                 $request = $stack->getCurrentRequest(); 
  70                 $currentLocale = $request->getLocale(); 
  73                 $this->config
['context']['locale'] = str_replace('_', '-', $currentLocale); 
  78                 //Look for keys to translate 
  79                 if (!empty($this->config
['translate'])) { 
  80                         //Iterate on keys to translate 
  81                         foreach($this->config
['translate'] as $translate) { 
  85                                 foreach(array_reverse(explode('.', $translate)) as $curkey) { 
  86                                         $tmp = array_combine([$curkey], [$tmp]); 
  89                                 $translates = array_replace_recursive($translates, $tmp); 
  93                 //Inject every requested route in view and mail context 
  94                 foreach($this->config 
as $tag => $current) { 
  95                         //Look for entry with title subkey 
  96                         if (!empty($current['title'])) { 
  97                                 //Translate title value 
  98                                 $this->config
[$tag]['title'] = $translator->trans($current['title']); 
 101                         //Look for entry with route subkey 
 102                         if (!empty($current['route'])) { 
 103                                 //Generate url for both view and mail 
 104                                 foreach(['view', 'mail'] as $view) { 
 105                                         //Check that context key is usable 
 106                                         if (isset($current[$view]['context']) && is_array($current[$view]['context'])) { 
 107                                                 //Merge with global context 
 108                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
['context'], $this->config
[$tag][$view]['context']); 
 110                                                 //Process every routes 
 111                                                 foreach($current['route'] as $route => $key) { 
 113                                                         if ($route == 'confirm') { 
 114                                                                 //Skip route as it requires some parameters 
 119                                                         $value = $router->generate( 
 120                                                                 $this->config
['route'][$route]['name'], 
 121                                                                 $this->config
['route'][$route]['context'], 
 122                                                                 //Generate absolute url for mails 
 123                                                                 $view=='mail'?UrlGeneratorInterface
::ABSOLUTE_URL
:UrlGeneratorInterface
::ABSOLUTE_PATH
 
 127                                                         if (strpos($key, '.') !== false) { 
 132                                                                 foreach(array_reverse(explode('.', $key)) as $curkey) { 
 133                                                                         $tmp = array_combine([$curkey], [$tmp]); 
 137                                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp); 
 141                                                                 $this->config
[$tag][$view]['context'][$key] = $value; 
 145                                                 //Look for successful intersections 
 146                                                 if (!empty(array_intersect_key($translates, $this->config
[$tag][$view]['context']))) { 
 147                                                         //Iterate on keys to translate 
 148                                                         foreach($this->config
['translate'] as $translate) { 
 150                                                                 $keys = explode('.', $translate); 
 153                                                                 $tmp = $this->config
[$tag][$view]['context']; 
 156                                                                 foreach($keys as $curkey) { 
 158                                                                         if (!isset($tmp[$curkey])) { 
 164                                                                         $tmp = $tmp[$curkey]; 
 167                                                                 //Translate tmp value 
 168                                                                 $tmp = $translator->trans($tmp); 
 171                                                                 foreach(array_reverse($keys) as $curkey) { 
 173                                                                         $tmp = array_combine([$curkey], [$tmp]); 
 177                                                                 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp); 
 182                                                 if ($view == 'view') { 
 184                                                         $pathInfo = $router->getContext()->getPathInfo(); 
 186                                                         //Iterate on locales excluding current one 
 187                                                         foreach($this->config
['locales'] as $locale) { 
 191                                                                 //Iterate on other locales 
 192                                                                 foreach(array_diff($this->config
['locales'], [$locale]) as $other) { 
 193                                                                         $titles[$other] = $translator->trans($this->config
['languages'][$locale], [], null, $other); 
 196                                                                 //Retrieve route matching path 
 197                                                                 $route = $router->match($pathInfo); 
 200                                                                 $name = $route['_route']; 
 203                                                                 unset($route['_route']); 
 205                                                                 //With current locale 
 206                                                                 if ($locale == $currentLocale) { 
 207                                                                         //Set locale locales context 
 208                                                                         $this->config
[$tag][$view]['context']['canonical'] = $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
); 
 210                                                                         //Set locale locales context 
 211                                                                         $this->config
[$tag][$view]['context']['alternates'][$locale] = [ 
 212                                                                                 'absolute' => $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
), 
 213                                                                                 'relative' => $router->generate($name, ['_locale' => $locale]+
$route), 
 214                                                                                 'title' => implode('/', $titles), 
 215                                                                                 'translated' => $translator->trans($this->config
['languages'][$locale], [], null, $locale) 
 220                                                                 if (empty($this->config
[$tag][$view]['context']['alternates'][$slocale = substr($locale, 0, 2)])) { 
 222                                                                         $this->config
[$tag][$view]['context']['alternates'][$slocale] = [ 
 223                                                                                 'absolute' => $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
), 
 224                                                                                 'relative' => $router->generate($name, ['_locale' => $locale]+
$route), 
 225                                                                                 'title' => implode('/', $titles), 
 226                                                                                 'translated' => $translator->trans($this->config
['languages'][$locale], [], null, $locale) 
 238          * Confirm account from mail link 
 240          * @param Request $request The request 
 241          * @param Registry $manager The doctrine registry 
 242          * @param UserPasswordEncoderInterface $encoder The password encoder 
 243          * @param EntityManagerInterface $manager The doctrine entity manager 
 244          * @param SluggerUtil $slugger The slugger 
 245          * @param MailerInterface $mailer The mailer 
 246          * @param string $mail The shorted mail address 
 247          * @param string $hash The hashed password 
 248          * @return Response The response 
 250         public function confirm(Request 
$request, Registry 
$doctrine, UserPasswordEncoderInterface 
$encoder, EntityManagerInterface 
$manager, SluggerUtil 
$slugger, MailerInterface 
$mailer, $mail, $hash): Response 
{ 
 252                 if ($hash != $slugger->hash($mail)) { 
 254                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash])); 
 258                 $mail = $slugger->unshort($smail = $mail); 
 261                 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) { 
 263                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 264                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail])); 
 267                 //Without existing registrant 
 268                 if (!($user = $doctrine->getRepository($this->config
['class']['user'])->findOneByMail($mail))) { 
 269                         //Add error message mail already exists 
 270                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 271                         $this->addFlash('error', $this->translator
->trans('Account %mail% do not exists', ['%mail%' => $smail])); 
 273                         //Redirect to register view 
 274                         return $this->redirectToRoute($this->config
['route']['register']['name'], ['mail' => $smail, 'field' => $sfield = $slugger->serialize([]), 'hash' => $slugger->hash($smail.$sfield)]+
$this->config
['route']['register']['context']); 
 278                 $user->setActive(true); 
 281                 $user->setUpdated(new \
DateTime('now')); 
 284                 $manager->persist($user); 
 289                 //Add error message mail already exists 
 290                 $this->addFlash('notice', $this->translator
->trans('Your account has been activated')); 
 292                 //Redirect to user view 
 293                 return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']); 
 297          * Edit account by shorted mail 
 299          * @param Request $request The request 
 300          * @param Registry $manager The doctrine registry 
 301          * @param UserPasswordEncoderInterface $encoder The password encoder 
 302          * @param EntityManagerInterface $manager The doctrine entity manager 
 303          * @param SluggerUtil $slugger The slugger 
 304          * @param string $mail The shorted mail address 
 305          * @param string $hash The hashed password 
 306          * @return Response The response 
 308         public function edit(Request 
$request, Registry 
$doctrine, UserPasswordEncoderInterface 
$encoder, EntityManagerInterface 
$manager, SluggerUtil 
$slugger, $mail, $hash): Response 
{ 
 310                 if ($hash != $slugger->hash($mail)) { 
 312                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash])); 
 316                 $mail = $slugger->unshort($smail = $mail); 
 318                 //With existing subscriber 
 319                 if (empty($user = $doctrine->getRepository($this->config
['class']['user'])->findOneByMail($mail))) { 
 321                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 322                         throw $this->createNotFoundException($this->translator
->trans('Unable to find account %mail%', ['%mail%' => $smail])); 
 325                 //Prevent access when not admin, user is not guest and not currently logged user 
 326                 if (!$this->isGranted('ROLE_ADMIN') && $user != $this->getUser() || !$this->isGranted('IS_AUTHENTICATED_FULLY')) { 
 327                         //Throw access denied 
 328                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 329                         throw $this->createAccessDeniedException($this->translator
->trans('Unable to access user: %mail%', ['%mail%' => $smail])); 
 332                 //Create the RegisterType form and give the proper parameters 
 333                 $editForm = $this->createForm($this->config
['register']['view']['form'], $user, [ 
 334                         //Set action to register route name and context 
 335                         'action' => $this->generateUrl($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']), 
 337                         'civility_class' => $this->config
['class']['civility'], 
 338                         //Set civility default 
 339                         'civility_default' => $doctrine->getRepository($this->config
['class']['civility'])->findOneByTitle($this->config
['default']['civility']), 
 341                         'mail' => $this->isGranted('ROLE_ADMIN'), 
 348                 //Create the RegisterType form and give the proper parameters 
 349                 $edit = $this->createForm($this->config
['edit']['view']['edit'], $user, [ 
 350                         //Set action to register route name and context 
 351                         'action' => $this->generateUrl($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']), 
 353                         'civility_class' => $this->config
['class']['civility'], 
 354                         //Set civility default 
 355                         'civility_default' => $doctrine->getRepository($this->config
['class']['civility'])->findOneByTitle($this->config
['default']['civility']), 
 357                         'mail' => $this->isGranted('ROLE_ADMIN'), 
 365                 if ($this->isGranted('ROLE_ADMIN')) { 
 366                         //Create the LoginType form and give the proper parameters 
 367                         $reset = $this->createForm($this->config
['edit']['view']['reset'], $user, [ 
 368                                 //Set action to register route name and context 
 369                                 'action' => $this->generateUrl($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']), 
 377                         if ($request->isMethod('POST')) { 
 378                                 //Refill the fields in case the form is not valid. 
 379                                 $reset->handleRequest($request); 
 381                                 //With reset submitted and valid 
 382                                 if ($reset->isSubmitted() && $reset->isValid()) { 
 384                                         $data = $reset->getData(); 
 387                                         $data->setPassword($encoder->encodePassword($data, $data->getPassword())); 
 390                                         $data->setUpdated(new \
DateTime('now')); 
 393                                         $manager->persist($data); 
 395                                         //Flush to get the ids 
 399                                         $this->addFlash('notice', $this->translator
->trans('Account %mail% password updated', ['%mail%' => $mail = $data->getMail()])); 
 401                                         //Redirect to cleanup the form 
 402                                         return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail = $slugger->short($mail), 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']); 
 407                         $this->config
['edit']['view']['context']['reset'] = $reset->createView(); 
 409                 //XXX: prefer a reset on login to force user unspam action 
 412                         $this->addFlash('notice', $this->translator
->trans('To change your password login with your mail and any password then follow the procedure')); 
 416                 if ($request->isMethod('POST')) { 
 417                         //Refill the fields in case the form is not valid. 
 418                         $edit->handleRequest($request); 
 420                         //With edit submitted and valid 
 421                         if ($edit->isSubmitted() && $edit->isValid()) { 
 423                                 $data = $edit->getData(); 
 426                                 $data->setUpdated(new \
DateTime('now')); 
 429                                 $manager->persist($data); 
 431                                 //Flush to get the ids 
 435                                 $this->addFlash('notice', $this->translator
->trans('Account %mail% updated', ['%mail%' => $mail = $data->getMail()])); 
 437                                 //Redirect to cleanup the form 
 438                                 return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail = $slugger->short($mail), 'hash' => $slugger->hash($smail)]+
$this->config
['route']['edit']['context']); 
 443                 return $this->render( 
 445                         $this->config
['edit']['view']['name'], 
 447                         ['edit' => $edit->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->config
['edit']['view']['context'] 
 454          * @param Request $request The request 
 455          * @param AuthenticationUtils $authenticationUtils The authentication utils 
 456          * @param RouterInterface $router The router instance 
 457          * @param SluggerUtil $slugger The slugger 
 458          * @param string $mail The shorted mail address 
 459          * @param string $hash The hashed password 
 460          * @return Response The response 
 462         public function login(Request 
$request, AuthenticationUtils 
$authenticationUtils, RouterInterface 
$router, SluggerUtil 
$slugger, $mail, $hash): Response 
{ 
 463                 //Create the LoginType form and give the proper parameters 
 464                 $login = $this->createForm($this->config
['login']['view']['form'], null, [ 
 465                         //Set action to login route name and context 
 466                         'action' => $this->generateUrl($this->config
['route']['login']['name'], $this->config
['route']['login']['context']), 
 467                         //Disable repeated password 
 468                         'password_repeated' => false, 
 477                 if (!empty($mail) && !empty($hash)) { 
 479                         if ($hash != $slugger->hash($mail)) { 
 481                                 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash])); 
 485                         $mail = $slugger->unshort($smail = $mail); 
 488                         if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) { 
 490                                 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail])); 
 494                         $login->get('mail')->setData($mail); 
 495                 //Last username entered by the user 
 496                 } elseif ($lastUsername = $authenticationUtils->getLastUsername()) { 
 497                         $login->get('mail')->setData($lastUsername); 
 500                 //Get the login error if there is one 
 501                 if ($error = $authenticationUtils->getLastAuthenticationError()) { 
 502                         //Get translated error 
 503                         $error = $this->translator
->trans($error->getMessageKey()); 
 505                         //Add error message to mail field 
 506                         $login->get('mail')->addError(new FormError($error)); 
 508                         //Create the LoginType form and give the proper parameters 
 509                         $recover = $this->createForm($this->config
['recover']['view']['form'], null, [ 
 510                                 //Set action to recover route name and context 
 511                                 'action' => $this->generateUrl($this->config
['route']['recover']['name'], $this->config
['route']['recover']['context']), 
 518                         //Get recover mail entity 
 519                         $recover->get('mail') 
 520                                 //Set mail from login form 
 521                                 ->setData($login->get('mail')->getData()) 
 523                                 ->addError(new FormError($this->translator
->trans('Use this form to recover your account'))); 
 525                         //Add recover form to context 
 526                         $context['recover'] = $recover->createView(); 
 529                         $this->addFlash('notice', $this->translator
->trans('To change your password login with your mail and any password then follow the procedure')); 
 533                 return $this->render( 
 535                         $this->config
['login']['view']['name'], 
 537                         ['login' => $login->createView()]+
$context+
$this->config
['login']['view']['context'] 
 544          * @param Request $request The request 
 545          * @param Registry $manager The doctrine registry 
 546          * @param UserPasswordEncoderInterface $encoder The password encoder 
 547          * @param EntityManagerInterface $manager The doctrine entity manager 
 548          * @param SluggerUtil $slugger The slugger 
 549          * @param MailerInterface $mailer The mailer 
 550          * @param string $mail The shorted mail address 
 551          * @param string $pass The shorted password 
 552          * @param string $hash The hashed password 
 553          * @return Response The response 
 555         public function recover(Request 
$request, Registry 
$doctrine, UserPasswordEncoderInterface 
$encoder, EntityManagerInterface 
$manager, SluggerUtil 
$slugger, MailerInterface 
$mailer, $mail, $pass, $hash): Response 
{ 
 556                 //Without mail, pass and hash 
 557                 if (empty($mail) && empty($pass) && empty($hash)) { 
 558                         //Create the LoginType form and give the proper parameters 
 559                         $form = $this->createForm($this->config
['recover']['view']['form'], null, [ 
 560                                 //Set action to recover route name and context 
 561                                 'action' => $this->generateUrl($this->config
['route']['recover']['name'], $this->config
['route']['recover']['context']), 
 568                         if ($request->isMethod('POST')) { 
 569                                 //Refill the fields in case the form is not valid. 
 570                                 $form->handleRequest($request); 
 572                                 if ($form->isValid()) { 
 574                                         $data = $form->getData(); 
 576                                         //Find user by data mail 
 577                                         if ($user = $doctrine->getRepository($this->config
['class']['user'])->findOneByMail($data['mail'])) { 
 579                                                 $recoverMail =& $this->config
['recover']['mail']; 
 582                                                 $mail = $slugger->short($user->getMail()); 
 585                                                 $pass = $slugger->hash($user->getPassword()); 
 587                                                 //Generate each route route 
 588                                                 foreach($this->config
['recover']['route'] as $route => $tag) { 
 589                                                         //Only process defined routes 
 590                                                         if (!empty($this->config
['route'][$route])) { 
 591                                                                 //Process for recover mail url 
 592                                                                 if ($route == 'recover') { 
 593                                                                         //Set the url in context 
 594                                                                         $recoverMail['context'][$tag] = $this->get('router')->generate( 
 595                                                                                 $this->config
['route'][$route]['name'], 
 596                                                                                 //Prepend recover context with tag 
 600                                                                                         'hash' => $slugger->hash($mail.$pass) 
 601                                                                                 ]+
$this->config
['route'][$route]['context'], 
 602                                                                                 UrlGeneratorInterface
::ABSOLUTE_URL
 
 609                                                 $recoverMail['context']['recipient_mail'] = $user->getMail(); 
 612                                                 $recoverMail['context']['recipient_name'] = trim($user->getForename().' '.$user->getSurname().($user->getPseudonym()?' ('.$user->getPseudonym().')':'')); 
 614                                                 //Init subject context 
 615                                                 $subjectContext = $slugger->flatten(array_replace_recursive($this->config
['recover']['view']['context'], $recoverMail['context']), null, '.', '%', '%'); 
 618                                                 $recoverMail['subject'] = ucfirst($this->translator
->trans($recoverMail['subject'], $subjectContext)); 
 621                                                 $message = (new TemplatedEmail()) 
 623                                                         ->from(new Address($this->config
['contact']['mail'], $this->config
['contact']['title'])) 
 625                                                         //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 626                                                         ->to(new Address($recoverMail['context']['recipient_mail'], $recoverMail['context']['recipient_name'])) 
 628                                                         ->subject($recoverMail['subject']) 
 630                                                         //Set path to twig templates 
 631                                                         ->htmlTemplate($recoverMail['html']) 
 632                                                         ->textTemplate($recoverMail['text']) 
 635                                                         //XXX: require recursive merge to avoid loosing subkeys 
 636                                                         //['subject' => $recoverMail['subject']]+$recoverMail['context']+$this->config['recover']['view']['context'] 
 637                                                         ->context(array_replace_recursive($this->config
['recover']['view']['context'], $recoverMail['context'], ['subject' => $recoverMail['subject']])); 
 639                                                 //Try sending message 
 640                                                 //XXX: mail delivery may silently fail 
 643                                                         $mailer->send($message); 
 645                                                         //Redirect on the same route with sent=1 to cleanup form 
 646                                                         return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params')); 
 647                                                 //Catch obvious transport exception 
 648                                                 } catch(TransportExceptionInterface 
$e) { 
 649                                                         //Add error message mail unreachable 
 650                                                         $form->get('mail')->addError(new FormError($this->translator
->trans('Account found but unable to contact: %mail%', array('%mail%' => $data['mail'])))); 
 654                                                 //Add error message to mail field 
 655                                                 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to find account %mail%', ['%mail%' => $data['mail']]))); 
 661                         return $this->render( 
 663                                 $this->config
['recover']['view']['name'], 
 665                                 ['form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->config
['recover']['view']['context'] 
 670                 if ($hash != $slugger->hash($mail.$pass)) { 
 672                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash])); 
 676                 $mail = $slugger->unshort($smail = $mail); 
 679                 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) { 
 681                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 682                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail])); 
 685                 //With existing subscriber 
 686                 if (empty($user = $doctrine->getRepository($this->config
['class']['user'])->findOneByMail($mail))) { 
 688                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 689                         throw $this->createNotFoundException($this->translator
->trans('Unable to find account %mail%', ['%mail%' => $smail])); 
 692                 //With unmatched pass 
 693                 if ($pass != $slugger->hash($user->getPassword())) { 
 695                         //XXX: prevent use of outdated recover link 
 696                         throw $this->createNotFoundException($this->translator
->trans('Outdated recover link')); 
 699                 //Create the LoginType form and give the proper parameters 
 700                 $form = $this->createForm($this->config
['recover']['view']['form'], $user, [ 
 701                         //Set action to recover route name and context 
 702                         'action' => $this->generateUrl($this->config
['route']['recover']['name'], ['mail' => $smail, 'pass' => $pass, 'hash' => $hash]+
$this->config
['route']['recover']['context']), 
 709                 if ($request->isMethod('POST')) { 
 710                         //Refill the fields in case the form is not valid. 
 711                         $form->handleRequest($request); 
 713                         if ($form->isValid()) { 
 715                                 $data = $form->getData(); 
 717                                 //Set encoded password 
 718                                 $encoded = $encoder->encodePassword($user, $user->getPassword()); 
 721                                 $pass = $slugger->hash($encoded); 
 724                                 $user->setPassword($encoded); 
 727                                 $user->setUpdated(new \
DateTime('now')); 
 730                                 $manager->persist($user); 
 736                                 $this->addFlash('notice', $this->translator
->trans('Account %mail% password updated', ['%mail%' => $mail])); 
 738                                 //Redirect to user login 
 739                                 return $this->redirectToRoute($this->config
['route']['login']['name'], ['mail' => $smail, 'hash' => $slugger->hash($smail)]+
$this->config
['route']['login']['context']); 
 744                 return $this->render( 
 746                         $this->config
['recover']['view']['name'], 
 748                         ['form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->config
['recover']['view']['context'] 
 753          * Register an account 
 755          * @param Request $request The request 
 756          * @param Registry $manager The doctrine registry 
 757          * @param UserPasswordEncoderInterface $encoder The password encoder 
 758          * @param EntityManagerInterface $manager The doctrine entity manager 
 759          * @param SluggerUtil $slugger The slugger 
 760          * @param MailerInterface $mailer The mailer 
 761          * @param LoggerInterface $logger The logger 
 762          * @param string $mail The shorted mail address 
 763          * @param string $field The serialized then shorted form field array 
 764          * @param string $hash The hashed serialized field array 
 765          * @return Response The response 
 767         public function register(Request 
$request, Registry 
$doctrine, UserPasswordEncoderInterface 
$encoder, EntityManagerInterface 
$manager, SluggerUtil 
$slugger, MailerInterface 
$mailer, LoggerInterface 
$logger, $mail, $field, $hash): Response 
{ 
 769                 $reflection = new \
ReflectionClass($this->config
['class']['user']); 
 772                 $user = $reflection->newInstance(); 
 774                 //With mail and field 
 775                 if (!empty($field) && !empty($hash)) { 
 777                         if ($hash != $slugger->hash($mail.$field)) { 
 779                                 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash])); 
 785                                 $mail = $slugger->unshort($smail = $mail); 
 788                                 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) { 
 790                                         //XXX: prevent slugger reverse engineering by not displaying decoded mail 
 791                                         throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail])); 
 795                                 $user->setMail($mail); 
 804                                 //Unshort then unserialize field 
 805                                 $field = $slugger->unserialize($sfield = $field); 
 807                         } catch (\Error
|\Exception 
$e) { 
 809                                 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'field', '%value%' => $field]), $e); 
 812                         //With non array field 
 813                         if (!is_array($field)) { 
 815                                 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'field', '%value%' => $field])); 
 817                 //Without field and hash 
 829                 //Create the RegisterType form and give the proper parameters 
 830                 $form = $this->createForm($this->config
['register']['view']['form'], $user, $field+
[ 
 831                         //Set action to register route name and context 
 832                         'action' => $this->generateUrl($this->config
['route']['register']['name'], ['mail' => $smail, 'field' => $sfield, 'hash' => $hash]+
$this->config
['route']['register']['context']), 
 834                         'civility_class' => $this->config
['class']['civility'], 
 835                         //Set civility default 
 836                         'civility_default' => $doctrine->getRepository($this->config
['class']['civility'])->findOneByTitle($this->config
['default']['civility']), 
 843                 if ($request->isMethod('POST')) { 
 844                         //Refill the fields in case the form is not valid. 
 845                         $form->handleRequest($request); 
 847                         if ($form->isValid()) { 
 849                                 $data = $form->getData(); 
 851                                 //With existing registrant 
 852                                 if ($doctrine->getRepository($this->config
['class']['user'])->findOneByMail($mail = $data->getMail())) { 
 853                                         //Add error message mail already exists 
 854                                         $this->addFlash('warning', $this->translator
->trans('Account %mail% already exists', ['%mail%' => $mail])); 
 856                                         //Redirect to user view 
 857                                         return $this->redirectToRoute( 
 858                                                 $this->config
['route']['edit']['name'], 
 860                                                         'mail' => $smail = $slugger->short($mail), 
 861                                                         'hash' => $slugger->hash($smail) 
 862                                                 ]+
$this->config
['route']['edit']['context'] 
 867                                 $registerMail =& $this->config
['register']['mail']; 
 869                                 //Extract names and pseudonym from mail 
 870                                 $names = explode(' ', $pseudonym = ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $data->getMail())))))); 
 873                                 $user->setPseudonym($user->getPseudonym()??$pseudonym); 
 876                                 $user->setForename($user->getForename()??$names[0]); 
 879                                 $user->setSurname($user->getSurname()??$names[1]??$names[0]); 
 882                                 $user->setPassword($encoder->encodePassword($user, $user->getPassword()??$data->getMail())); 
 885                                 $user->setCreated(new \
DateTime('now')); 
 888                                 $user->setUpdated(new \
DateTime('now')); 
 891                                 $manager->persist($user); 
 893                                 //Iterate on default group 
 894                                 foreach($this->config
['default']['group'] as $i => $groupTitle) { 
 896                                         if (($group = $doctrine->getRepository($this->config
['class']['group'])->findOneByTitle($groupTitle))) { 
 898                                                 //XXX: see vendor/symfony/security-core/Role/Role.php 
 899                                                 $user->addGroup($group); 
 903                                                 //XXX: consider missing group as fatal 
 904                                                 throw new \
Exception(sprintf('Group from rapsys_user.default.group[%d] not found by title: %s', $i, $groupTitle)); 
 908                                 //Generate each route route 
 909                                 foreach($this->config
['register']['route'] as $route => $tag) { 
 910                                         //Only process defined routes 
 911                                         if (!empty($this->config
['route'][$route])) { 
 912                                                 //Process for confirm url 
 913                                                 if ($route == 'confirm') { 
 914                                                         //Set the url in context 
 915                                                         $registerMail['context'][$tag] = $this->get('router')->generate( 
 916                                                                 $this->config
['route'][$route]['name'], 
 917                                                                 //Prepend subscribe context with tag 
 919                                                                         'mail' => $smail = $slugger->short($data->getMail()), 
 920                                                                         'hash' => $slugger->hash($smail) 
 921                                                                 ]+
$this->config
['route'][$route]['context'], 
 922                                                                 UrlGeneratorInterface
::ABSOLUTE_URL
 
 928                                 //XXX: DEBUG: remove me 
 929                                 //die($registerMail['context']['confirm_url']); 
 934                                         $this->translator
->trans( 
 935                                                 'newuser:mail=%mail%|locale=%locale%|confirm=%confirm%', 
 937                                                         '%mail%' => $data->getMail(), 
 938                                                         '%locale%' => $request->getLocale(), 
 939                                                         '%confirm%' => $registerMail['context'][$this->config
['register']['route']['confirm']] 
 945                                 $registerMail['context']['recipient_mail'] = $data->getMail(); 
 948                                 $registerMail['context']['recipient_name'] = ''; 
 951                                 $registerMail['context']['recipient_name'] = implode(' ', [$data->getForename(), $data->getSurname(), $data->getPseudonym()?'('.$data->getPseudonym().')':'']); 
 953                                 //Init subject context 
 954                                 $subjectContext = $slugger->flatten(array_replace_recursive($this->config
['register']['view']['context'], $registerMail['context']), null, '.', '%', '%'); 
 957                                 $registerMail['subject'] = ucfirst($this->translator
->trans($registerMail['subject'], $subjectContext)); 
 960                                 $message = (new TemplatedEmail()) 
 962                                         ->from(new Address($this->config
['contact']['mail'], $this->config
['contact']['title'])) 
 964                                         //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 965                                         ->to(new Address($registerMail['context']['recipient_mail'], $registerMail['context']['recipient_name'])) 
 967                                         ->subject($registerMail['subject']) 
 969                                         //Set path to twig templates 
 970                                         ->htmlTemplate($registerMail['html']) 
 971                                         ->textTemplate($registerMail['text']) 
 974                                         ->context(['subject' => $registerMail['subject']]+
$registerMail['context']); 
 976                                 //Try saving in database 
 981                                         //Add error message mail already exists 
 982                                         $this->addFlash('notice', $this->translator
->trans('Your account has been created')); 
 984                                         //Try sending message 
 985                                         //XXX: mail delivery may silently fail 
 988                                                 $mailer->send($message); 
 990                                                 //Redirect on the same route with sent=1 to cleanup form 
 991                                                 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params')); 
 992                                         //Catch obvious transport exception 
 993                                         } catch(TransportExceptionInterface 
$e) { 
 994                                                 //Add error message mail unreachable 
 995                                                 $form->get('mail')->addError(new FormError($this->translator
->trans('Account %mail% tried subscribe but unable to contact', ['%mail%' => $data->getMail()]))); 
 997                                 //Catch double subscription 
 998                                 } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException 
$e) { 
 999                                         //Add error message mail already exists 
1000                                         $this->addFlash('error', $this->translator
->trans('Account %mail% already exists', ['%mail%' => $mail])); 
1006                 return $this->render( 
1008                         $this->config
['register']['view']['name'], 
1010                         ['form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->config
['register']['view']['context'] 
1017         public function getAlias(): string { 
1018                 return RapsysUserBundle
::getAlias();