src/Controller/Security/SecurityController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Core\Exception\ExceptionManager;
  4. use App\Entity\User;
  5. use App\Service\SecurityService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. class SecurityController extends AbstractController
  17. {
  18.     #[Route(path: '/login', name:'app_login')]
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         // get the login error if there is one
  22.         $error = $authenticationUtils->getLastAuthenticationError();
  23.         // last username entered by the user
  24.         $lastUsername = $authenticationUtils->getLastUsername();
  25.         return $this->render('security/login.html.twig', [
  26.             'last_username' => $lastUsername,
  27.             'error' => $error,
  28.         ]);
  29.     }
  30.     #[Route(path: '/logout', name:'app_logout')]
  31.     public function logout(): void
  32.     {
  33.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  34.     }
  35.     #[Route(path: '/sign-in')]
  36.     public function signIn(Request $request, MailerInterface $mailer, \Twig\Environment $twig, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
  37.     {
  38.         if ($request->isMethod('POST')) {
  39.             // Recupera i dati dell'utente
  40.             $nome = $request->get('nome');
  41.             $cognome = $request->get('cognome');
  42.             $emailAddress = $request->get('email');
  43.             $termini = boolval($request->get('term'));
  44.             $password = $request->get('password');
  45.             $password2 = $request->get('password');
  46.             if(!$termini) ExceptionManager::create('È necessario accettare i termini');
  47.             if($password!=$password2) ExceptionManager::create('Le password non corrispondono');
  48.             if(empty($nome)||empty($cognome)||empty($emailAddress)) ExceptionManager::create('Non sono stati compilati tutti i campi');
  49.             try {
  50.                 // Crea un nuovo utente
  51.                 $em->beginTransaction();
  52.                 $user = new User();
  53.                 $user->setNome($nome);
  54.                 $user->setCognome($cognome);
  55.                 $user->setEmail($emailAddress);
  56.                 $user->setConfToken(SecurityService::generateToken());
  57.                 // Codifica la password
  58.                 $encodedPassword = $passwordHasher->hashPassword($user, $password);
  59.                 $user->setPassword($encodedPassword);
  60.                 // Salvataggio dell'utente nel database
  61.                 $em->persist($user);
  62.                 $em->flush();
  63.                 // Invia l'email di conferma
  64.                 $email=(new Email())
  65.                     ->from(new Address($_ENV['OWNER_MAIL'], 'EventMinder'))
  66.                     ->to(new Address($emailAddress, $nome))
  67.                     ->subject('Conferma indirizzo email')
  68.                     ->html($twig->render('emails/communications/registration.html.twig', [
  69.                         'nome'=>$nome,
  70.                         'email'=>$emailAddress,
  71.                         'token'=>$user->getConfToken()
  72.                     ]));
  73.                 $mailer->send($email);
  74.                 $em->commit();
  75.                 // Reindirizza l'utente dopo la registrazione
  76.                 return $this->redirectToRoute('thanks');
  77.             } catch (\Exception $ex){
  78.                 $em->rollback();
  79.                 ExceptionManager::fire($ex);
  80.             }
  81.         }
  82.         return $this->render('security/register.html.twig');
  83.     }
  84.     #[Route(path: '/thanks', name:'thanks')]
  85.     public function thanks(): Response
  86.     {
  87.         return $this->render('security/thanks.html.twig', []);
  88.     }
  89. }