<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class SecurityController extends VicinAppController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('index');
}
// 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("/forgotPassword", name="forgotPassword")
*/
public function forgotPassword(Mailer $mailer, Request $request): Response
{
$form = $this->createFormBuilder()
->add('email', EmailType::class, [
'label' => 'Indirizzo email',
'constraints' => new NotBlank(),
'required' => true,
])
->add('save',SubmitType::class, [
'attr' => ['class' => 'btn btn-primary'],
'label' => 'Invia',
])
/*->add('back',ButtonType::class, [
'attr' => ['class' => 'btn btn-secondary', 'onclick' => "window.location.href='{$back}'"],
'label' => 'Indietro',
])*/
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->doctrine->getManager();
$repo = $em->getRepository(User::class);
$formData = $form->getData();
$user = $repo->findOneByEmail($formData['email']);
if ($user !== null) {
$user->generateUniqueToken($repo);
//send email first - WARNING: catch TransportExceptionInterface
$link = $this->generateUrl('resetPassword',['token'=>$user->getToken()],UrlGeneratorInterface::ABSOLUTE_URL);
$text = 'Fai click su questo link per reimpostare la tua password su Vicinapp.com: ' . $link;
$mailer->sendTextMessage($formData['email'], 'Reimpostazione password Vicinapp.com', $text);
$this->addFlash('success', 'L\'email per la reimpostazione della password è stata inviata');
$em->flush();
return new RedirectResponse($this->generateUrl('index'));
} else {
$this->addFlash('danger','L\'indirizzo digitato non è presente nel sistema');
}
}
return $this->render('security/forgotPassword.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/resetPassword/{token}", name="resetPassword")
*/
public function resetPassword(Request $request, $token, User $user, UserPasswordHasherInterface $passwordHasher)
{
$form = $this->createFormBuilder()
->add('newPassword', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'La password non corrisponde',
'options' => ['attr' => ['class' => 'password-field']],
'required' => true,
'constraints' => new NotBlank(),
'first_options' => ['label' => 'Nuova Password'],
'second_options' => ['label' => 'Ripeti Password'],
])
->add('save',SubmitType::class, [
'attr' => ['class' => 'btn btn-primary'],
'label' => 'Salva',
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->doctrine->getManager();
$hashedPassword = $passwordHasher->hashPassword($user, $form->getData()['newPassword']);
$user->setPassword($hashedPassword);
$em->flush();
$this->addFlash('success', 'La password è stata reimpostata con successo');
return new RedirectResponse($this->generateUrl('user'));
}
return $this->render('security/resetPassword.html.twig', array('form' => $form->createView()));
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}