<?php
namespace App\EventSubscriber;
use App\Util\SecurityUtil;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TokenSubscriber implements EventSubscriberInterface
{
/**
* @var Environment
*/
protected $twig;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var SecurityUtil
*/
protected $securityUtil;
/**
* @var EngineInterface
*/
protected $templating;
private TranslatorInterface $translator;
/**
* TokenSubscriber constructor.
* @param Environment $twig
* @param EntityManagerInterface $entityManager
* @param SecurityUtil $securityUtil
* @param TranslatorInterface $translator
*/
public function __construct(Environment $twig,
EntityManagerInterface $entityManager,
SecurityUtil $securityUtil,
TranslatorInterface $translator
)
{
$this->twig = $twig;
$this->entityManager = $entityManager;
$this->securityUtil = $securityUtil;
//$this->templating = $templating;
$this->translator = $translator;
}
/**
* @param RequestEvent $event
* @return void|bool
* @throws Exception
*/
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$controller = $event->getRequest()->attributes->get("_controller");
$systemPart = explode('\\', $controller);
$methodPart = explode('::', $controller);
$contentType = $event->getRequest()->headers->get('Content-Type');
if (isset($systemPart[2])) {
$this->twig->addGlobal('_ENV', $_ENV);
if ($systemPart[2] == 'Pages' ) {
if($methodPart[1] != 'forgotPasswordPage' &&
$methodPart[1] != 'passwordResetPage' &&
$methodPart[1] != 'passwordResetSuccessPage' &&
$methodPart[1] != 'passwordCreationPage' &&
$methodPart[1] != 'passwordCreationSuccessPage' &&
$methodPart[1] != 'webManifest' &&
$methodPart[1] != 'serviceWorkerJs' &&
$methodPart[1] != 'translateJs' &&
$methodPart[1] != 'registerPage' &&
$methodPart[1] != 'registerSuccessPage'
){
try {
$accessToken = $event->getRequest()->cookies->get('_token');
$event->getRequest()->headers->set('Authorization', 'Bearer ' . $accessToken);
$tokenData = $this->securityUtil->validateToken($event->getRequest());
$this->twig->addGlobal('tokenData', $tokenData);
$event->getRequest()->attributes->set('tokenData', $tokenData);
$request->setLocale("tr");
//$this->translator->setLocale($request->getLocale());
} catch (Exception $e) {
if ($methodPart[1] == 'logout') {
$event->setResponse($response = new RedirectResponse("/"));
} else {
$viewResponse = $this->twig->render('login.html.twig', []);
$event->setResponse(new Response($viewResponse));
}
}
}
} else if ($systemPart[2] == 'Api') {
if ($methodPart[1] != 'loginAction' &&
$methodPart[1] != 'sendPasswordResetLinkAction' &&
$methodPart[1] != 'resetPasswordAction' &&
$methodPart[1] != 'registerCompanyAction'
) {
try {
$tokenData = $this->securityUtil->validateToken($event->getRequest());
$this->twig->addGlobal('tokenData', $tokenData);
$event->getRequest()->attributes->set('tokenData', $tokenData);
} catch (Exception $e) {
$event->setResponse(
new JsonResponse(
json_decode($e->getMessage(), true),
$e->getCode()
)
);
}
}
}
}
}
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'onKernelRequest',
];
}
}