<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\NewPassFormType;
use App\Form\ResetPassFormType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Mime\Address;
use Symfony\Bridge\Twig\Mime\TemplatedEmail as Email;
class SecurityController extends AbstractController
{
/**
* @Route("/login" , name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_homepage');
}
// 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("/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.');
}
/**
* @Route("/pass-reset/request", name="app_pass_reset_request")
*/
public function passReset(Request $request, TranslatorInterface $translator, MailerInterface $mailer): Response
{
$form = $this->createForm(ResetPassFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get('email')->getData();
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)
->findOneBy(['email' => $email]);
if ($user instanceof User && $user->isEnabled()) {
$email = (new Email())
->from(new Address($this->get('parameter_bag')->get('mailer_from'), $this->get('parameter_bag')->get('mailer_from_name')))
->to($user->getEmail())
->subject('Resetowanie hasła')
// path of the Twig template to render
->htmlTemplate('emails/pass-reset.html.twig')
// pass variables (name => value) to the template
->context([
'link' => $this->get('router')->generate('app_pass_reset_new_pass', ['token' => urlencode($user->getPassword())], UrlGeneratorInterface::ABSOLUTE_URL)
]);
$mailer->send($email);
$this->addFlash(
'success',
'Wiadomość resetująca została wysłana.'
);
return new RedirectResponse($this->get('router')->generate('app_login'));
} else {
$this->addFlash(
'error',
'Nie ma takiego użytkownika.'
);
}
}
return $this->render('security/pass-reset.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/pass-reset/new/{token}", name="app_pass_reset_new_pass", requirements={"token"=".+"})
*/
public function newPass(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder, $token)
{
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)
->findOneBy(['password' => urldecode($token)]);
if ($user instanceof User && $user->isEnabled()) {
$form = $this->createForm(NewPassFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('newPassword')->getData()
)
);
$entityManager->flush();
$this->addFlash(
'success',
'Hasło zostało zmienione. Teraz możesz się zalogować.'
);
return new RedirectResponse($this->get('router')->generate('app_login'));
}
return $this->render('security/new-pass.html.twig', [
'form' => $form->createView(),
]);
} else {
$this->addFlash(
'error',
$translator->trans('passreset.new-pass.wrong-token', [], 'security')
);
return new RedirectResponse($this->get('router')->generate('app_login'));
}
}
}