src/WebBundle/Controller/ResettingController.php line 52

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Exception;
  5. use FlexApp\Form\ResettingFormType;
  6. use FlexApp\Security\LoginFormAuthenticator;
  7. use FlexApp\Service\Canonicalizer;
  8. use FlexApp\Service\ManagerContactsProvider;
  9. use Symfony\Component\Form\FormFactoryInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. use WebBundle\Entity\User;
  19. use WebBundle\Helper\App;
  20. use WebBundle\Repository\ListCountryRepository;
  21. use WebBundle\Repository\UserRepository;
  22. use WebBundle\Service\ListEmailService;
  23. class ResettingController extends ExtendedController
  24. {
  25.     /** @required  */
  26.     public UserRepository $userRepository;
  27.     /** @required  */
  28.     public ListCountryRepository $listCountryRepository;
  29.     /** @required */
  30.     public Canonicalizer $canonicalizer;
  31.     /** @required */
  32.     public FormFactoryInterface $formFactory;
  33.     /** @required */
  34.     public UserPasswordHasherInterface $userPasswordHasher;
  35.     /** @required */
  36.     public EntityManagerInterface $entityManager;
  37.     /** @required */
  38.     public UserAuthenticatorInterface $userAuthenticator;
  39.     /** @required */
  40.     public LoginFormAuthenticator $loginFormAuthenticator;
  41.     /** @required */
  42.     public RequestStack $requestStack;
  43.     /** @required */
  44.     public ManagerContactsProvider $managerContactsProvider;
  45.     /**
  46.      * Request reset user password: show form
  47.      */
  48.     public function requestAction()
  49.     {
  50.         return $this->render('@Web/Resetting/request.html.twig');
  51.     }
  52.     /**
  53.      * @param Request $request
  54.      * @return RedirectResponse|Response
  55.      * @throws Exception
  56.      */
  57.     public function sendEmailAction(Request $request)
  58.     {
  59.         $username trim($request->request->get('username'));
  60.         $user $this->userRepository->findOneByEmail($this->canonicalizer->canonicalize($username));
  61.         if (null === $user) {
  62.             return $this->render('@Web/Resetting/request.html.twig', [
  63.                 'invalid_username' => $username,
  64.             ]);
  65.         }
  66.         if (null === $user->getConfirmationToken()) {
  67.             $generatedConfirmationToken base64_encode(md5(uniqid(true)));
  68.             $user->setConfirmationToken($generatedConfirmationToken);
  69.             $this->userRepository->save($user);
  70.         }
  71.         $contactsObject $this->managerContactsProvider->getContacts();
  72.         //отправка письма
  73.         /** @var ListEmailService $listEmailService */
  74.         $listEmailService $this->get('app.service.list_email');
  75.         $link $this->container->get('router')->generate('user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
  76.         $listEmailService->sendEmailByCriteria(
  77.             [
  78.                 'keyEmail' => 'password_reset',
  79.             ],
  80.             App::getCurLocale(),
  81.             [
  82.                 $user->getEmail()
  83.             ],
  84.             [
  85.                 '%fio%' => $this->getFio($user),
  86.                 '%designResetPasswordLinkLeft%' => '<center><table border="0" cellpadding="0" cellspacing="0" bgcolor="#6890C5"><tr><td width="10" height="40">&nbsp;</td><td valign="middle" align="center"><a href="' $link '" style="color: white; text-decoration: none;">',
  87.                 '%designResetPasswordLinkRight%' => '</a></td><td width="10" height="40">&nbsp;</td></tr></table></center>',
  88.                 '%resetPasswordLink%' => '<a href="' $link '">' $link '</a>',
  89.                 '%managerName%' => $contactsObject->managerName,
  90.                 '%managerEmail%' => $contactsObject->managerEmail,
  91.                 '%managerPhone%' => $contactsObject->managerPhone,
  92.                 '%designFooterStart%' => '<hr/><table bgcolor="#ededed"><tr><td colspan = 3 height="25px">&nbsp;</td></tr><tr><td width = "25px">&nbsp;</td><td style="font:normal 14px \'Arial\';line-height:18px;color:#333333;">',
  93.                 '%designFooterEnd%' => '</td><td width = "25px">&nbsp;</td></tr><tr><td colspan = 3 height="25px">&nbsp;</td></tr></table>',
  94.             ],
  95.             $this->container->getParameter('mailer_email_from')
  96.         );
  97.         return new RedirectResponse($this->generateUrl('user_resetting_check_email',
  98.             array('email' => $this->getObfuscatedEmail($user))
  99.         ));
  100.     }
  101.     /**
  102.      * @param User $user
  103.      * @return string
  104.      * @throws Exception
  105.      */
  106.     private function getFio($user): string
  107.     {
  108.         $locale App::getCurLocale();
  109.         $mainDeliveryAddress $user->getMainRecipientAddress();
  110.         if (!$mainDeliveryAddress->getId()) {
  111.             $this->entityManager->persist($mainDeliveryAddress);
  112.             $this->entityManager->flush();
  113.         }
  114.         $name $mainDeliveryAddress->getName();
  115.         $sex $user->getSex();
  116.         $t App::trans((($sex == || $sex == null) ? 'title.man' 'title.woman'), $locale);
  117.         if ($locale != 'fr') {
  118.             $name $t ' ' $name;
  119.         } else {
  120.             $name $t;
  121.         }
  122.         $name '<strong style="font-size: 18px;">' $name '</strong>';
  123.         $name .= ',';
  124.         return $name;
  125.     }
  126.     /**
  127.      * Tell the user to check his email provider
  128.      */
  129.     public function checkEmailAction(Request $request)
  130.     {
  131.         $email $request->query->get('email');
  132.         if (empty($email)) {
  133.             // the user does not come from the sendEmail action
  134.             return new RedirectResponse($this->generateUrl('user_resetting_request'));
  135.         }
  136.         return $this->render('@Web/Resetting/checkEmail.html.twig', array(
  137.             'email' => $email,
  138.         ));
  139.     }
  140.     /**
  141.      * Reset user password
  142.      */
  143.     public function resetAction(Request $request$token): Response
  144.     {
  145.         $user $this->userRepository->findOneBy(['confirmationToken' => $token]);
  146.         if (null === $user) {
  147.             throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"'$token));
  148.         }
  149.         $form $this->formFactory->create(ResettingFormType::class);
  150.         $form->setData($user);
  151.         $form->handleRequest($request);
  152.         if ($form->isSubmitted() && $form->isValid()) {
  153.             $plainPassword $form->getData()->getPlainPassword();
  154.             $user->setPassword($this->userPasswordHasher->hashPassword($user$plainPassword));
  155.             $user->setConfirmationToken(null);
  156.             $this->userRepository->save($user);
  157.             $url $this->generateUrl('user_resetting_complete');
  158.             $this->userAuthenticator->authenticateUser($user$this->loginFormAuthenticator$this->requestStack->getCurrentRequest());
  159.             return new RedirectResponse($url);
  160.         }
  161.         return $this->render('@Web/Resetting/reset.html.twig', array(
  162.             'token' => $token,
  163.             'form' => $form->createView(),
  164.         ));
  165.     }
  166.     /**
  167.      * @return Response
  168.      */
  169.     public function completeAction(): Response
  170.     {
  171.         return $this->render('@Web/Resetting/complete_resetting.html.twig');
  172.     }
  173.     /**
  174.      * Get the truncated email displayed when requesting the resetting.
  175.      *
  176.      * The default implementation only keeps the part following @ in the address.
  177.      *
  178.      * @param User $user
  179.      *
  180.      * @return string
  181.      */
  182.     protected function getObfuscatedEmail(User $user)
  183.     {
  184.         $email $user->getEmail();
  185.         if (false !== $pos strpos($email'@')) {
  186.             $email '...' substr($email$pos);
  187.         }
  188.         return $email;
  189.     }
  190. }