<?php
namespace App\Controller\Security;
use App\Core\Exception\ExceptionManager;
use App\Entity\User;
use App\Service\SecurityService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name:'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name:'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route(path: '/sign-in')]
public function signIn(Request $request, MailerInterface $mailer, \Twig\Environment $twig, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
{
if ($request->isMethod('POST')) {
// Recupera i dati dell'utente
$nome = $request->get('nome');
$cognome = $request->get('cognome');
$emailAddress = $request->get('email');
$termini = boolval($request->get('term'));
$password = $request->get('password');
$password2 = $request->get('password');
if(!$termini) ExceptionManager::create('È necessario accettare i termini');
if($password!=$password2) ExceptionManager::create('Le password non corrispondono');
if(empty($nome)||empty($cognome)||empty($emailAddress)) ExceptionManager::create('Non sono stati compilati tutti i campi');
try {
// Crea un nuovo utente
$em->beginTransaction();
$user = new User();
$user->setNome($nome);
$user->setCognome($cognome);
$user->setEmail($emailAddress);
$user->setConfToken(SecurityService::generateToken());
// Codifica la password
$encodedPassword = $passwordHasher->hashPassword($user, $password);
$user->setPassword($encodedPassword);
// Salvataggio dell'utente nel database
$em->persist($user);
$em->flush();
// Invia l'email di conferma
$email=(new Email())
->from(new Address($_ENV['OWNER_MAIL'], 'EventMinder'))
->to(new Address($emailAddress, $nome))
->subject('Conferma indirizzo email')
->html($twig->render('emails/communications/registration.html.twig', [
'nome'=>$nome,
'email'=>$emailAddress,
'token'=>$user->getConfToken()
]));
$mailer->send($email);
$em->commit();
// Reindirizza l'utente dopo la registrazione
return $this->redirectToRoute('thanks');
} catch (\Exception $ex){
$em->rollback();
ExceptionManager::fire($ex);
}
}
return $this->render('security/register.html.twig');
}
#[Route(path: '/thanks', name:'thanks')]
public function thanks(): Response
{
return $this->render('security/thanks.html.twig', []);
}
}