src/Controller/SecurityController.php line 49
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ChangePasswordFormType;
use App\Form\RegistrationFormType;
use App\Repository\UserRepository;
use App\Security\AppEmailAuthenticator;
use App\Service\BrevoService;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
class SecurityController extends AbstractController
{
private $resetPasswordHelper;
use ResetPasswordControllerTrait;
public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, EntityManagerInterface $entityManager)
{
$this->resetPasswordHelper = $resetPasswordHelper;
$this->entityManager = $entityManager;
}
#[Route('/connexion', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_station_index');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route('/inscription', name: 'app_register', methods: ['GET', 'POST'])]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppEmailAuthenticator $authenticator, EntityManagerInterface $entityManager, BrevoService $sibService): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_station_index');
}
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
//TODO: Send Mail
if($request->request->get('newsletter')) {
$sibService->saveContact($user->getEmail(), $user->getUsername(), true);
}
return $userAuthenticator->authenticateUser(
$user,
$authenticator,
$request
);
}
return $this->render('security/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
#[Route('/deconnexion', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('');
}
#[Route('/configuration', name: 'app_parameters')]
public function parameters(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserRepository $userRepository): Response
{
$form = $this->createForm(ChangePasswordFormType::class);
$form->handleRequest($request);
$user = $userRepository->find($this->getUser());
if ($form->isSubmitted() && $form->isValid()) {
// Encode(hash) the plain password, and set it.
$encodedPassword = $userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
);
$user->setPassword($encodedPassword);
$this->entityManager->flush();
// The session is cleaned up after the password has been changed.
$this->cleanSessionAfterReset();
return $this->redirectToRoute('app_parameters');
}
return $this->render('security/parameters.html.twig', [
'resetForm' => $form->createView(),
]);
}
}