<?php
namespace App\EventListener;
use App\Entity\CommonData\Application;
use App\Entity\CommonData\Subscription;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class AccessListener
*
* @package App\EventListener
*/
class AccessListener
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var TokenStorageInterface
*/
private $security;
/**
* @var SessionInterface
*/
private $session;
/**
* @var RouterInterface
*/
private $router;
/**
* AccessListener constructor.
*
* @param EntityManagerInterface $em
* @param TokenStorageInterface $security
* @param SessionInterface $session
* @param RouterInterface $router
*/
public function __construct(
EntityManagerInterface $em,
TokenStorageInterface $security,
SessionInterface $session,
RouterInterface $router
) {
$this->em = $em;
$this->security = $security;
$this->session = $session;
$this->router = $router;
}
/**
* @param FilterControllerEvent $event
*
* @return null
* @throws \Exception
*/
public function onKernelController(FilterControllerEvent $event)
{
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return null;
}
if ($this->security->getToken()) {
$request = $event->getRequest();
$user = $this
->security
->getToken()
->getUser();
if (strpos($request->getRequestUri(), '/app/') !== false &&
strpos($request->getRequestUri(), '/admin/') === false &&
strpos($request->getRequestUri(), '/api/') === false
) {
list($controller, $methodName) = $event->getController();
if (!($controller instanceof RedirectController)) {
$annotationParams = (new AnnotationReader())
->getClassAnnotations(new \ReflectionClass($controller));
$application = $this
->em
->getRepository(Application::class)
->findOneBy([
'routePattern' => $annotationParams[0]->getName()
]);
if ($application->getType() !== Application::APPLICATION_TYPES['free']) {
$subscription = $this
->em
->getRepository(Subscription::class)
->findOneBy([
'user' => $user,
'application' => $application
]);
if (!$subscription) {
$this
->session
->getFlashBag()->add(
'error',
"You don't have subscription for " . $application->getName() . "!"
);
$redirectUrl = $this
->router
->generate('mpsh_application_detail', [ 'id' => $application->getId() ]);
$event->setController(function () use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
} elseif ($subscription->getStatus() !== Subscription::SUBSCRIPTION_STATUSES['active']) {
$this
->session
->getFlashBag()->add(
'error',
"Your subscription has expired for " . $application->getName() . "!"
);
$redirectUrl = $this
->router
->generate('mpsh_profile_subscription');
$event->setController(function () use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
}
}
// ToDo: this part is wrong, rewrite it fully
if (!$user->getEnabled()) {
$redirectUrl = $this
->router
->generate('app_logout');
$event->setController(function () use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
}
}
}
}
}
}