src/Controller/Security/SecurityController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Repository\UserRepository;
  4. use App\Service\Generale;
  5. use App\Service\WSAuth;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class SecurityController extends AbstractController
  12. {
  13.     public function __construct(Generale $service)
  14.     {
  15.         $this->service = $service;
  16.     }
  17.     /**
  18.      * @Route("/logout", name="app_logout", methods={"GET"})
  19.      */
  20.     public function logout(): void
  21.     {
  22.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  23.     }
  24.     /**
  25.      * @Route("/change-password", name="app_change_password")
  26.      */
  27.     public function changePassword(EntityManagerInterface $em, UserPasswordHasherInterface $passwordHasher, UserRepository $userRepository, WSAuth $auth, Request $request)
  28.     {
  29.         if ($request->isMethod('POST')) {
  30.             $changePassword = $auth->changePassword($_POST, $this->getUser()->getUserId(), $this->getUser()->getUserIdentifier());
  31.             if (isset($changePassword['Error']))
  32.                 $this->addFlash('danger', $changePassword['Error']);
  33.             else {
  34.                 $this->addFlash('success', current($changePassword));
  35.                 $user = $userRepository->findOneBy(['username' => $this->getUser()->getUserIdentifier()]);
  36.                 $user->setPassword($passwordHasher->hashPassword(
  37.                     $user,
  38.                     $_POST['plainPassword']['first']
  39.                 ));
  40.                 $em->flush();
  41.                 return $this->redirect('/');
  42.             }
  43.         }
  44.         return $this->render($this->service->checkCustomTemplate('login/change-password.html.twig'));
  45.     }
  46.     /**
  47.      * @Route("/request-reset-password", name="app_request_reset_password")
  48.      */
  49.     public function requestResetPassword()
  50.     {
  51.         return $this->render($this->service->checkCustomTemplate('login/request-reset-password.html.twig'));
  52.     }
  53.     /**
  54.      * @Route("/reset-password", name="app_reset_password")
  55.      */
  56.     public function resetPassword(WSAuth $auth)
  57.     {
  58.         $this->addFlash('info', $auth->resetPassword($_POST));
  59.         return $this->redirectToRoute('app_login');
  60.     }
  61.     /**
  62.      * @Route("/resetting/reset/{token}", name="app_confirm_reset_password")
  63.      */
  64.     public function confirmResetPassword($token, WSAuth $auth)
  65.     {
  66.         $confirmResetPassword = $auth->confirmResetPassword(array_merge($_POST, ['token' => $token]));
  67.         if (isset($confirmResetPassword['Error']))
  68.             $this->addFlash('danger', $confirmResetPassword['Error']);
  69.         if ($confirmResetPassword == "success check token" || isset($confirmResetPassword['Error']))
  70.             return $this->render($this->service->checkCustomTemplate('login/reset-password.html.twig'));
  71.         if (is_null($confirmResetPassword))
  72.             $this->addFlash('danger', "Une erreur est survenue lors de la rĂ©initialisation du mot de passe");
  73.         else
  74.             $this->addFlash('info', $confirmResetPassword);
  75.         return $this->redirectToRoute('app_login');
  76.     }
  77. }