<?php
namespace App\Controller\Security;
use App\Repository\UserRepository;
use App\Service\Generale;
use App\Service\WSAuth;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
class SecurityController extends AbstractController
{
public function __construct(Generale $service)
{
$this->service = $service;
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout(): void
{
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/change-password", name="app_change_password")
*/
public function changePassword(EntityManagerInterface $em, UserPasswordHasherInterface $passwordHasher, UserRepository $userRepository, WSAuth $auth, Request $request)
{
if ($request->isMethod('POST')) {
$changePassword = $auth->changePassword($_POST, $this->getUser()->getUserId(), $this->getUser()->getUserIdentifier());
if (isset($changePassword['Error']))
$this->addFlash('danger', $changePassword['Error']);
else {
$this->addFlash('success', current($changePassword));
$user = $userRepository->findOneBy(['username' => $this->getUser()->getUserIdentifier()]);
$user->setPassword($passwordHasher->hashPassword(
$user,
$_POST['plainPassword']['first']
));
$em->flush();
return $this->redirect('/');
}
}
return $this->render($this->service->checkCustomTemplate('login/change-password.html.twig'));
}
/**
* @Route("/request-reset-password", name="app_request_reset_password")
*/
public function requestResetPassword()
{
return $this->render($this->service->checkCustomTemplate('login/request-reset-password.html.twig'));
}
/**
* @Route("/reset-password", name="app_reset_password")
*/
public function resetPassword(WSAuth $auth)
{
$this->addFlash('info', $auth->resetPassword($_POST));
return $this->redirectToRoute('app_login');
}
/**
* @Route("/resetting/reset/{token}", name="app_confirm_reset_password")
*/
public function confirmResetPassword($token, WSAuth $auth)
{
$confirmResetPassword = $auth->confirmResetPassword(array_merge($_POST, ['token' => $token]));
if (isset($confirmResetPassword['Error']))
$this->addFlash('danger', $confirmResetPassword['Error']);
if ($confirmResetPassword == "success check token" || isset($confirmResetPassword['Error']))
return $this->render($this->service->checkCustomTemplate('login/reset-password.html.twig'));
if (is_null($confirmResetPassword))
$this->addFlash('danger', "Une erreur est survenue lors de la réinitialisation du mot de passe");
else
$this->addFlash('info', $confirmResetPassword);
return $this->redirectToRoute('app_login');
}
}