src/EventSubscriber/RequestSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\RouteConfig;
  4. use App\Entity\User;
  5. use App\Entity\UserActivity;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\FormFactoryInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\Routing\Router;
  13. use Symfony\Component\Security\Core\Security;
  14. class RequestSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var EntityManagerInterface
  18.      */
  19.     private $em;
  20.     
  21.     /**
  22.      * @var Security
  23.      */
  24.     private $security;
  25.     public function __construct(EntityManagerInterface $emSecurity $security)
  26.     {
  27.         $this->em $em;
  28.         $this->security $security;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             RequestEvent::class => 'onKernelRequest',
  34. //            ResponseEvent::class => 'onKernelResponse',
  35.         ];
  36.     }
  37.     public function onKernelRequest(RequestEvent $event): bool
  38.     {
  39.         $req $event->getRequest();
  40.         
  41.         $controller $req->attributes->get('_controller');
  42.         $ctrArr explode('::'$controller);
  43.         
  44.         $controllerPath $ctrArr[0] ?? '';
  45.         $actionName $ctrArr[1] ?? '';
  46.         
  47.         if (!$controllerPath || !$actionName) {
  48.             return false;
  49.         }
  50.         // check first time login
  51.         if ($this->security->getUser()) {
  52.             /** @var User $user */
  53.             $user $this->security->getUser();
  54.             if ('user_change_password' != $req->attributes->get('_route')) {
  55.                 if ($user->isIsNew()) {
  56.                     $event->setResponse(new RedirectResponse('/users/change-password'));
  57.                     return false;
  58.                 }
  59.             }
  60.         }
  61.         
  62.         if (strpos(strtolower($actionName), 'json')) {
  63.             return false;
  64.         }
  65.         
  66.         /** @var User $user */
  67.         $user $this->security->getUser();
  68.         if ($user) {
  69.             $rRepo $this->em->getRepository(RouteConfig::class);
  70.             /** @var RouteConfig $route */
  71.             $route $rRepo->findOneBy([
  72.                 'name' => $req->attributes->get('_route'),
  73.                 'isTrack' => true,
  74.             ]);
  75.             
  76.             if ($route) {
  77.                 $activity = new UserActivity();
  78.                 $activity->setType('ACCESS');
  79.                 $activity->setUser($user);
  80.                 $activity->setName($route->getLabel() ?: $route->getName());
  81.                 $activity->setActiveAt(new \DateTime('now'));
  82.                 $activity->setRouteName($req->attributes->get('_route'));
  83.                 $activity->setRouteParams($req->attributes->get('_route_params'));
  84.                 $activity->setPathInfo($req->getPathInfo());
  85.                 $activity->setRequestUri($req->getRequestUri());
  86.                 $activity->setRequestMethod($req->getRealMethod());
  87.                 $activity->setCtrName($controllerPath);
  88.                 $activity->setActName($actionName);
  89.                 
  90.                 $options = [
  91.                     'platform' => $req->headers->get('sec-ch-ua-platform'),
  92.                     'user-agent' => $req->headers->get('user-agent'),
  93.                     'referer' => $req->headers->get('referer'),
  94.                 ];
  95.                 $activity->setOptions($options);
  96.                 $this->em->persist($activity);
  97.                 $this->em->flush();
  98.             }
  99.         }
  100.         
  101.         return true;
  102.     }
  103.     public function onKernelResponse(ResponseEvent $event)
  104.     {
  105.         
  106.     }
  107. }