src/Controller/Security/ResetController.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license Silk Software House Sp. Z O. O.
  4.  */
  5. declare(strict_types=1);
  6. namespace App\Controller\Security;
  7. use App\Entity\User;
  8. use App\Factory\EmailFactory;
  9. use App\Form\Security\ResetPasswordType;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use DateTime;
  18. use Exception;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. /**
  21.  * Class ResetController.
  22.  */
  23. class ResetController extends AbstractController
  24. {
  25.     /**
  26.      * Email factory.
  27.      *
  28.      * @var EmailFactory
  29.      */
  30.     private $emailFactory;
  31.     /**
  32.      * Translator.
  33.      *
  34.      * @var TranslatorInterface
  35.      */
  36.     private $translator;
  37.     /**
  38.      * @var EntityManagerInterface
  39.      */
  40.     private $em;
  41.     private UserPasswordHasherInterface $hasher;
  42.     /**
  43.      * ResetController constructor.
  44.      *
  45.      * @param EmailFactory $emailFactory
  46.      * @param TranslatorInterface $translator
  47.      * @param EntityManagerInterface $em
  48.      * @param UserPasswordHasherInterface $hasher
  49.      */
  50.     public function __construct(
  51.         EmailFactory $emailFactory,
  52.         TranslatorInterface $translator,
  53.         EntityManagerInterface $em,
  54.         UserPasswordHasherInterface $hasher
  55.     ) {
  56.         $this->emailFactory $emailFactory;
  57.         $this->translator $translator;
  58.         $this->em $em;
  59.         $this->hasher $hasher;
  60.     }
  61.     /**
  62.      * Trigger password resetting.
  63.      *
  64.      * @Route("/profile/trigger-password-reset", name="trigger_password_reset", methods={"GET", "POST"})
  65.      *
  66.      * @Template("panel/security/reset/trigger.html.twig")
  67.      *
  68.      * @return array|RedirectResponse
  69.      */
  70.     public function trigger()
  71.     {
  72.         $user $this->getUser();
  73.         if ($user) {
  74.             $user->startPasswordResettingProcess();
  75.             $this->em->persist($user->setHashValidityEndsAt(new DateTime('+7 days')));
  76.             $this->em->flush();
  77.             return $this->redirectToRoute('reset_password', ['id' => $user->getId(), 'hash' => $user->getHash()]);
  78.         }
  79.         return ['error' => $this->translator->trans('To change password please contact administrator')];
  80.     }
  81.     /**
  82.      * Trigger account activation.
  83.      *
  84.      * @Route("/profile/activate/{id}/{hash}", name="activate_account", methods={"GET", "POST"})
  85.      *
  86.      * @Template("panel/security/reset/form.html.twig")
  87.      *
  88.      * @param Request                      $request
  89.      * @param User                         $user
  90.      * @param string                       $hash
  91.      *
  92.      * @return array|RedirectResponse
  93.      *
  94.      * @throws Exception
  95.      */
  96.     public function activate(Request $requestUser $userstring $hash)
  97.     {
  98.         if ($user->getActivatedAt()) {
  99.             $this->addFlash('success''Your account has already been activated. You can sign up here.');
  100.             return $this->redirectToRoute('login');
  101.         }
  102.         return  $this->change($request$user->setActivatedAt(new DateTime()), $hash);
  103.     }
  104.     /**
  105.      * Trigger password resetting.
  106.      *
  107.      * @Route("/profile/reset-password/{id}/{hash}", name="reset_password", methods={"GET", "POST"})
  108.      *
  109.      * @Template("panel/security/reset/form.html.twig")
  110.      *
  111.      * @param Request                      $request
  112.      * @param User                         $user
  113.      * @param string                       $hash
  114.      *
  115.      * @return array|RedirectResponse
  116.      */
  117.     public function change(Request $requestUser $userstring $hash)
  118.     {
  119.         if (!$user->getIsHashCorrect($hash)) {
  120.             $this->addFlash('danger''This link is outdated.');
  121.             return $this->redirectToRoute('trigger_password_reset');
  122.         }
  123.         $form $this->createForm(ResetPasswordType::class);
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             $data $form->getData();
  127.             $this->getDoctrine()->getManager()->getRepository(User::class)->upgradePassword(
  128.                 $user,
  129.                 $this->hasher->encodePassword($user$data['password'])
  130.             );
  131.             $this->addFlash(
  132.                 'success',
  133.                 'Password was changed.'
  134.             );
  135.             return $this->redirectToRoute('panel_devices');
  136.         }
  137.         return  [
  138.             'form' => $form->createView(),
  139.         ];
  140.     }
  141. }