src/Controller/SecurityController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\Mailer;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  13. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. class SecurityController extends VicinAppController
  19. {
  20.     /**
  21.      * @Route("/login", name="app_login")
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtilsRequest $request): Response
  24.     {
  25.          if ($this->getUser()) {
  26.              return $this->redirectToRoute('index');
  27.          }
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  33.     }
  34.     /**
  35.      * @Route("/forgotPassword", name="forgotPassword")
  36.      */
  37.     public function forgotPassword(Mailer $mailerRequest $request): Response
  38.     {
  39.         $form $this->createFormBuilder()
  40.                 ->add('email'EmailType::class, [
  41.                     'label' => 'Indirizzo email',
  42.                     'constraints' => new NotBlank(),
  43.                     'required' => true,
  44.                 ])
  45.                 ->add('save',SubmitType::class, [
  46.                         'attr' => ['class' => 'btn btn-primary'],
  47.                         'label' => 'Invia',
  48.                     ])
  49.                     /*->add('back',ButtonType::class, [
  50.                         'attr' => ['class' => 'btn btn-secondary', 'onclick' => "window.location.href='{$back}'"],
  51.                         'label' => 'Indietro',
  52.                     ])*/
  53.                 ->getForm();
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $em $this->doctrine->getManager();
  57.             $repo $em->getRepository(User::class);
  58.             $formData $form->getData();
  59.             $user $repo->findOneByEmail($formData['email']);
  60.             if ($user !== null) {
  61.                 $user->generateUniqueToken($repo);
  62.                     //send email first - WARNING: catch TransportExceptionInterface
  63.                     $link $this->generateUrl('resetPassword',['token'=>$user->getToken()],UrlGeneratorInterface::ABSOLUTE_URL);
  64.                     $text 'Fai click su questo link per reimpostare la tua password su Vicinapp.com: ' $link;
  65.                     $mailer->sendTextMessage($formData['email'], 'Reimpostazione password Vicinapp.com'$text);
  66.                     $this->addFlash('success''L\'email per la reimpostazione della password è stata inviata');
  67.                     $em->flush();
  68.                     return new RedirectResponse($this->generateUrl('index'));
  69.             } else {
  70.                 $this->addFlash('danger','L\'indirizzo digitato non è presente nel sistema');
  71.             }
  72.         }
  73.         return $this->render('security/forgotPassword.html.twig', ['form' => $form->createView()]);
  74.     }
  75.     /**
  76.      * @Route("/resetPassword/{token}", name="resetPassword")
  77.      */
  78.     public function resetPassword(Request $request$tokenUser $userUserPasswordHasherInterface $passwordHasher)
  79.     {
  80.         $form $this->createFormBuilder()
  81.                 ->add('newPassword'RepeatedType::class, [
  82.                     'type' => PasswordType::class,
  83.                     'invalid_message' => 'La password non corrisponde',
  84.                     'options' => ['attr' => ['class' => 'password-field']],
  85.                     'required' => true,
  86.                     'constraints' => new NotBlank(),
  87.                     'first_options'  => ['label' => 'Nuova Password'],
  88.                     'second_options' => ['label' => 'Ripeti Password'],
  89.                 ])        
  90.                 ->add('save',SubmitType::class, [
  91.                     'attr' => ['class' => 'btn btn-primary'],
  92.                     'label' => 'Salva',
  93.                 ])
  94.                 ->getForm();
  95.         $form->handleRequest($request);
  96.         if ($form->isSubmitted() && $form->isValid()) {
  97.             $em $this->doctrine->getManager();
  98.             $hashedPassword $passwordHasher->hashPassword($user$form->getData()['newPassword']);
  99.             $user->setPassword($hashedPassword);
  100.             $em->flush();
  101.             $this->addFlash('success''La password è stata reimpostata con successo');
  102.             return new RedirectResponse($this->generateUrl('user'));
  103.         }
  104.         return $this->render('security/resetPassword.html.twig', array('form' => $form->createView()));
  105.         
  106.     }
  107.     
  108.     /**
  109.      * @Route("/logout", name="app_logout")
  110.      */
  111.     public function logout()
  112.     {
  113.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  114.     }
  115. }