src/Controller/ManagerSecurityController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\AskRecoveryType;
  5. use App\Form\RecoverType;
  6. use App\Service\UserService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class ManagerSecurityController extends AbstractController
  13. {
  14.     const DEFAULT_ROUTE 'manager_home';
  15.     public function nolocale(): RedirectResponse
  16.     {
  17.         return $this->redirectToRoute('manager_login', ['_locale' => 'en']);
  18.     }
  19.     public function login(AuthenticationUtils $authUtils): Response
  20.     {
  21.         if ($this->getUser() instanceof User) {
  22.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  23.         }
  24.         $error $authUtils->getLastAuthenticationError();
  25.         $lastUsername $authUtils->getLastUsername();
  26.         return $this->render('security/manager/login.html.twig', [
  27.             'last_username' => $lastUsername,
  28.             'error' => $error,
  29.         ]);
  30.     }
  31.     public function askRecovery(Request $requestUserService $util): Response
  32.     {
  33.         if ($this->getUser()) {
  34.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  35.         }
  36.         $form $this->createForm(AskRecoveryType::class);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             $data $form->getData();
  40.             $user $util->getUserByEmail($data['email']);
  41.             if ($user !== null) {
  42.                 $util->sendRecovery($user);
  43.             }
  44.             return $this->render('security/recover_sent.html.twig');
  45.         }
  46.         return $this->render('security/ask_recovery.html.twig', [
  47.             'form' => $form->createView(),
  48.             'error' => false,
  49.         ]);
  50.     }
  51.     public function recover(Request $requeststring $tokenUserService $util): Response
  52.     {
  53.         if ($this->getUser()) {
  54.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  55.         }
  56.         if ($token === null) {
  57.             return $this->render('security/recover_invalid_token.html.twig');
  58.         }
  59.         $user $util->getRecoverableUser($token);
  60.         if ($user === null) {
  61.             return $this->render('security/recover_invalid_token.html.twig');
  62.         }
  63.         $form $this->createForm(RecoverType::class);
  64.         $form->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             $data $form->getData();
  67.             $util->changePassword($user$data['password']);
  68.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  69.         }
  70.         return $this->render('security/recover.html.twig', [
  71.             'form' => $form->createView(),
  72.         ]);
  73.     }
  74.     public function createPassword(Request $requeststring $tokenUserService $util): Response
  75.     {
  76.         if ($this->getUser()) {
  77.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  78.         }
  79.         if ($token === null) {
  80.             return $this->render('security/create_password_invalid_token.html.twig');
  81.         }
  82.         $user $util->getRecoverableUser($token);
  83.         if ($user === null) {
  84.             return $this->render('security/create_password_invalid_token.html.twig');
  85.         }
  86.         $form $this->createForm(RecoverType::class);
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $data $form->getData();
  90.             $util->changePassword($user$data['password']);
  91.             return $this->redirectToRoute(self::DEFAULT_ROUTE);
  92.         }
  93.         return $this->render('security/create_password.html.twig', [
  94.             'form' => $form->createView(),
  95.         ]);
  96.     }
  97. }