<?php
declare(strict_types=1);
namespace App\Controller\Auth;
use App\Container\Model\Certificate\CertificateService;
use App\ReadModel\User\UserFetcher;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AuthController extends AbstractController
{
private UserFetcher $userFetcher;
private \App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Handler $handlerRepeatRequestMessage;
public function __construct(
UserFetcher $userFetcher,
\App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Handler $handlerRepeatRequestMessage
) {
$this->userFetcher = $userFetcher;
$this->handlerRepeatRequestMessage = $handlerRepeatRequestMessage;
}
/**
* @Route("/login", name="app_login")
* @param Request $request
* @param AuthenticationUtils $authenticationUtils
* @return RedirectResponse|Response
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils)//: Response
{
if ($request->isXmlHttpRequest()) {
return $this->redirect($this->generateUrl('app_login'), Response::HTTP_FOUND);
}
$error = $authenticationUtils->getLastAuthenticationError();
if ($error instanceof DisabledException) {
$this->checkUserConfirmationStatus($error->getUser()->getId());
}
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('app/auth/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/login_crypt", name="app_login_crypt")
* @param Request $request
* @param AuthenticationUtils $authenticationUtils
* @param CertificateService $env
* @return RedirectResponse|Response
*/
public function loginCrypt(Request $request, AuthenticationUtils $authenticationUtils, CertificateService $env)
{
if ($this->getUser()) {
return $this->redirectToRoute('home');
}
if ($request->isXmlHttpRequest()) {
return $this->redirect($this->generateUrl('app_login'), Response::HTTP_FOUND);
}
$error = $authenticationUtils->getLastAuthenticationError();
if ($error instanceof DisabledException) {
if (!$this->checkUserConfirmationStatus($error->getUser()->getId())) {
return $this->redirect($this->generateUrl('app_login'));
}
}
return $this->render('app/auth/loginCrypt.html.twig', ['error' => $error, 'crypt_login_hash' => $env->getHash()]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(Request $request)
{
$request->getSession()->remove('user_data');
// controller can be blank: it will never be executed!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/logout-user", name="app_logout_user")
*/
private function checkUserConfirmationStatus(string $userId): RedirectResponse
{
$getInfoUser = $this->userFetcher->findDetail($userId);
if ($getInfoUser) {
try {
$now = new \DateTimeImmutable();
$command = new \App\Model\User\UseCase\User\SignUp\RepeatRequestMessage\Command($userId, $now);
$this->handlerRepeatRequestMessage->handle($command);
$confirmTokenExpires = new \DateTimeImmutable($getInfoUser->confirm_token_expires);
$date = $now->diff($confirmTokenExpires);
$this->addFlash('warning', "Ваша учетная запись еще не активирована. <br>
Письмо с инструкцией по активации было повторно отправлено на ваш email. <br>
Повторная отправка письма станет доступна через " .
($date->format('%H') > 0 ? "{$date->format('%H ч. %I мин.')}" : "{$date->format('%I мин.')}"));
} catch (\DomainException $e) {
$this->addFlash('error', $e->getMessage());
}
} else {
$this->addFlash('error', 'User not found');
}
return $this->redirect($this->generateUrl('app_login'));
}
}