src/Controller/Front/SecurityController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\User;
  4. use App\Form\RegisterType;
  5. use App\Form\RemindPasswordType;
  6. use App\Form\ResetPasswordType;
  7. use App\Security\LoginFormAuthenticator;
  8. use App\Service\EmailSender\ResetPasswordSender;
  9. use App\Service\TokenGenerator;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class SecurityController extends AbstractController
  18. {
  19.   /**
  20.    * @Route("/login/", name="login")
  21.    */
  22.   public function login(AuthenticationUtils $authenticationUtils)
  23.   {
  24.     $error $authenticationUtils->getLastAuthenticationError();
  25.     $lastUsername $authenticationUtils->getLastUsername();
  26.     return $this->render('front/security/login.html.twig', [
  27.       'last_username' => $lastUsername,
  28.       'error' => $error
  29.     ]);
  30.   }
  31.   /**
  32.    * @Route("/rejestracja/", name="register")
  33.    */
  34.   public function register(Request $requestEntityManagerInterface $entityManagerUserPasswordEncoderInterface $encoderLoginFormAuthenticator $loginGuardAuthenticatorHandler $guard)
  35.   {
  36.     $user = new User();
  37.     $form $this->createForm(RegisterType::class, $user);
  38.     $forms $form->handleRequest($request);
  39.     if ($forms->isSubmitted() && $forms->isValid()) {
  40.       $user $forms->getData();
  41.       $plainPassword $forms->get('password')->getData();
  42.       $encodedPassword $encoder->encodePassword($user$plainPassword);
  43.       $user->setPassword($encodedPassword);
  44.       if ($user->getRole() == User::ROLE_DOCTOR) {
  45.         $user->setActive(false);
  46.       }
  47.       $entityManager->persist($user);
  48.       $entityManager->flush();
  49.       $this->addFlash('success''Konto zostało utworzone.');
  50.       return $guard->authenticateUserAndHandleSuccess($user$request$login'main');
  51.     }
  52.     return $this->render('front/security/register.html.twig', [
  53.       'form' => $form->createView()
  54.     ]);
  55.   }
  56.   /**
  57.    * @Route("/logout/", name="logout")
  58.    */
  59.   public function logout(AuthenticationUtils $authenticationUtils)
  60.   {
  61.   }
  62.   /**
  63.    * @Route("/remind-password/", name="remind_password")
  64.    */
  65.   public function remindPasswordAction(Request $requestResetPasswordSender $mailer)
  66.   {
  67.     $form $this->createForm(RemindPasswordType::class);
  68.     $form->handleRequest($request);
  69.     if ($form->isSubmitted() && $form->isValid()) {
  70.       $email $form->get('email')->getData();
  71.       if (!filter_var($emailFILTER_VALIDATE_EMAIL)) {
  72.         $this->addFlash('danger''Niepoprawny format email.');
  73.         return $this->redirectToRoute('remind_password');
  74.       }
  75.       $user $this->getDoctrine()->getRepository(User::class)->findOneByEmail($email);
  76.       if (!$user) {
  77.         $this->addFlash('danger''Podany e-mail nie istnieje w bazie klientów.');
  78.         return $this->redirectToRoute('remind_password');
  79.       }
  80.       $emailSend $this->sendResetPassEmailAndUpdateUser($user$mailer);
  81.       if (!$emailSend) {
  82.         $this->addFlash('danger''Coś poszło nie tak.');
  83.         return $this->redirectToRoute('remind_password');
  84.       }
  85.       $this->addFlash('success''Wiadomość została wysłana na twój adres e-mail.<br/>Zawiera ona odnośnik do strony umożliwiającej dokończenie procedury.');
  86.       return $this->redirectToRoute('remind_password');
  87.     }
  88.     return $this->render('front/security/remind-password.html.twig', [
  89.       'form' => $form->createView(),
  90.     ]);
  91.   }
  92.   private function sendResetPassEmailAndUpdateUser(User $userResetPasswordSender $resetPasswordSender)
  93.   {
  94.     $user->setPasswordToken(TokenGenerator::generate(12));
  95.     $user->setPasswordTokenCreatedAt(new \DateTime());
  96.     $this->getDoctrine()->getManager()->persist($user);
  97.     $this->getDoctrine()->getManager()->flush();
  98.     return $resetPasswordSender->send($userUSER::REMIND_PASSWORD_FRONT);
  99.   }
  100.   /**
  101.    * @Route("/reset-password/", name="reset_password")
  102.    */
  103.   public function resetPasswordAction(Request $requestUserPasswordEncoderInterface $encoder)
  104.   {
  105.     if (!$request->query->has('token')) {
  106.       return $this->redirectToRoute('profile');
  107.     }
  108.     $token $request->query->get('token');
  109.     $user $this->getDoctrine()->getRepository(User::class)->findOneByPasswordToken($token);
  110.     if (!$user) {
  111.       $this->addFlash('danger''Niepoprawny link');
  112.       return $this->redirectToRoute('main');
  113.     }
  114.     $tokenCreatedAt $user->getPasswordTokenCreatedAt();
  115.     $interval $tokenCreatedAt->diff(new \DateTime());
  116.     if ($interval->format('%h') > 24 || $interval->format('%d') > 0) {
  117.       $this->addFlash('danger''Twój token do zmiany hasła wygasł');
  118.       return $this->redirectToRoute('main');
  119.     }
  120.     $form $this->createForm(ResetPasswordType::class, $user);
  121.     $form->handleRequest($request);
  122.     if ($form->isSubmitted() && $form->isValid()) {
  123.       $plainPassword $form->get('password')->getData();
  124.       $encodedPassword $encoder->encodePassword($user$plainPassword);
  125.       $user->setPassword($encodedPassword);
  126.       $user->setPasswordToken(null);
  127.       $em $this->getDoctrine()->getManager();
  128.       $em->persist($user);
  129.       $em->flush();
  130.       $this->addFlash('success''Hasło zostało zmienione');
  131.       return $this->redirectToRoute('profile');
  132.     }
  133.     return $this->render('front/security/reset-password.html.twig', [
  134.       'form' => $form->createView()
  135.     ]);
  136.   }
  137. }