<?php
namespace App\Controller;
use LogicException;
use Oz\ApiNvl\Form\User\UserForgotPasswordEmailType;
use Oz\ApiNvl\Form\User\UserForgotPasswordType;
use Oz\ApiNvl\Model\User\UserForgotPassword;
use Oz\ApiNvl\Model\User\UserForgotPasswordEmail;
use Oz\ApiNvl\Service\User\UserForgotPasswordHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="APP_LOGIN")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// 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("/reset-password", name="RESET_PASSWORD")
*/
public function resetPassword(Request $request, UserForgotPasswordHelper $userForgotPasswordHelper): Response
{
$userForgotPasswordEmail = new UserForgotPasswordEmail();
$form = $this->createForm(UserForgotPasswordEmailType::class, $userForgotPasswordEmail);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userForgotPasswordHelper->sendEmail($userForgotPasswordEmail);
$this->addFlash('success', 'Un email vous a été envoyé pour modifier votre mot de passe.');
return $this->redirectToRoute('APP_LOGIN');
}
return $this->render('security/reset-password.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/reset-password/submit/", name="RESET_PASSWORD_SUBMIT")
*/
public function resetPasswordSubmit(Request $request, UserForgotPasswordHelper $userForgotPasswordHelper): Response
{
$userForgotPassword = (new UserForgotPassword())
->setEmail($request->get('email'))
->setToken($request->get('token'));
$form = $this->createForm(UserForgotPasswordType::class, $userForgotPassword);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($userForgotPasswordHelper->changePassword($userForgotPassword)) {
$this->addFlash('success', 'Votre mot de passe a bien été modifié.');
} else {
$this->addFlash('danger', 'Votre mot de passe n\'a pas pu étre modifié.');
}
return $this->redirectToRoute('APP_LOGIN');
}
return $this->render('security/reset-password-submit.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/admin/login", name="LOGIN_ADMIN")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function loginAdmin(AuthenticationUtils $authenticationUtils)
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/admin-login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/admin/check-login-admin", name="CHECK_LOGIN_ADMIN")
*/
public function checkLoginAdmin()
{
throw new LogicException('This method can be blank - it will be intercepted by the check login.');
}
/**
* @Route("/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("/admin/logout", name="APP_ADMIN_LOGOUT")
*/
public function logoutAdmin()
{
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}