src/Controller/Auth/AuthController.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Auth;
  4. use App\Container\Model\Certificate\CertificateService;
  5. use App\ReadModel\User\UserFetcher;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Exception\DisabledException;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class AuthController extends AbstractController
  14. {
  15.     private UserFetcher $userFetcher;
  16.     private \App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Handler $handlerRepeatRequestMessage;
  17.     public function __construct(
  18.         UserFetcher $userFetcher,
  19.         \App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Handler $handlerRepeatRequestMessage
  20.     ) {
  21.         $this->userFetcher $userFetcher;
  22.         $this->handlerRepeatRequestMessage $handlerRepeatRequestMessage;
  23.     }
  24.     /**
  25.      * @Route("/login", name="app_login")
  26.      * @param Request $request
  27.      * @param AuthenticationUtils $authenticationUtils
  28.      * @return RedirectResponse|Response
  29.      */
  30.     public function login(Request $requestAuthenticationUtils $authenticationUtils)//: Response
  31.     {
  32.         if ($request->isXmlHttpRequest()) {
  33.             return $this->redirect($this->generateUrl('app_login'), Response::HTTP_FOUND);
  34.         }
  35.         $error $authenticationUtils->getLastAuthenticationError();
  36.         if ($error instanceof DisabledException) {
  37.             $this->checkUserConfirmationStatus($error->getUser()->getId());
  38.         }
  39.         $lastUsername $authenticationUtils->getLastUsername();
  40.         return $this->render('app/auth/login.html.twig', [
  41.             'last_username' => $lastUsername,
  42.             'error' => $error,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/login_crypt", name="app_login_crypt")
  47.      * @param Request $request
  48.      * @param AuthenticationUtils $authenticationUtils
  49.      * @param CertificateService $env
  50.      * @return RedirectResponse|Response
  51.      */
  52.     public function loginCrypt(Request $requestAuthenticationUtils $authenticationUtilsCertificateService $env)
  53.     {
  54.         if ($this->getUser()) {
  55.             return $this->redirectToRoute('home');
  56.         }
  57.         if ($request->isXmlHttpRequest()) {
  58.             return $this->redirect($this->generateUrl('app_login'), Response::HTTP_FOUND);
  59.         }
  60.         $error $authenticationUtils->getLastAuthenticationError();
  61.         if ($error instanceof DisabledException) {
  62.             if (!$this->checkUserConfirmationStatus($error->getUser()->getId())) {
  63.                 return $this->redirect($this->generateUrl('app_login'));
  64.             }
  65.         }
  66.         return $this->render('app/auth/loginCrypt.html.twig', ['error' => $error'crypt_login_hash' => $env->getHash()]);
  67.     }
  68.     /**
  69.      * @Route("/logout", name="app_logout")
  70.      */
  71.     public function logout(Request $request)
  72.     {
  73.         $request->getSession()->remove('user_data');
  74.         // controller can be blank: it will never be executed!
  75.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  76.     }
  77.     /**
  78.      * @Route("/logout-user", name="app_logout_user")
  79.      */
  80.     private function checkUserConfirmationStatus(string $userId): RedirectResponse
  81.     {
  82.         $getInfoUser $this->userFetcher->findDetail($userId);
  83.         if ($getInfoUser) {
  84.             try {
  85.                 $now = new \DateTimeImmutable();
  86.                 $command = new \App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Command($userId$now);
  87.                 $this->handlerRepeatRequestMessage->handle($command);
  88.                 $confirmTokenExpires = new \DateTimeImmutable($getInfoUser->confirm_token_expires);
  89.                 $date $now->diff($confirmTokenExpires);
  90.                 $this->addFlash('warning'"Ваша учетная запись еще не активирована. <br>
  91.                 Письмо с инструкцией по активации было повторно отправлено на ваш email. <br>
  92.                 Повторная отправка письма станет доступна через " .
  93.                 ($date->format('%H') > "{$date->format('%H ч. %I мин.')}"{$date->format('%I мин.')}"));
  94.             } catch (\DomainException $e) {
  95.                 $this->addFlash('error'$e->getMessage());
  96.             }
  97.         } else {
  98.             $this->addFlash('error''User not found');
  99.         }
  100.         return $this->redirect($this->generateUrl('app_login'));
  101.     }
  102. }