src/Event/AdminEventSubscriber.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\ClientApiKey;
  4. use App\Entity\User;
  5. use App\Repository\UserRepository;
  6. use App\Security\NotClientContextException;
  7. use Sentry\State\Scope;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\AuthenticationEvents;
  15. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  16. use Symfony\Component\Security\Core\Security;
  17. use function Sentry\configureScope;
  18. class AdminEventSubscriber implements EventSubscriberInterface {
  19.   protected $requestStack;
  20.   protected $userRepository;
  21.   protected $security;
  22.   public function __construct(Security $securityRequestStack $requestStackUserRepository $userRepository)
  23.   {
  24.       $this->requestStack $requestStack;
  25.       $this->userRepository $userRepository;
  26.       $this->security $security;
  27.   }
  28.     
  29.   public static function getSubscribedEvents() {
  30.     return [
  31.       KernelEvents::CONTROLLER => 'onKernelController',
  32.       KernelEvents::EXCEPTION => 'onKernelException',
  33.       AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
  34.     ];
  35.   }
  36.   
  37.   public function onKernelController(ControllerEvent $event) {
  38.     $user $this->security->getUser();
  39.     if($user) {
  40.       configureScope(function(Scope $scope) use($user) {
  41.         if($user instanceof User) {
  42.           $scope->setUser([
  43.               'email' => $user->getUserIdentifier(),
  44.               'user_id' => $user->getId(),
  45.               'client' => ($user->getClient() ? [
  46.                   'id' => $user->getClient()->getId(),
  47.                   'name' => $user->getClient()->getName(),
  48.                 ] : null)
  49.           ]);
  50.         } elseif($user instanceof ClientApiKey) {
  51.           $scope->setUser([
  52.               'name' => $user->getUserIdentifier(),
  53.               'id' => $user->getId(),
  54.               'client' => [
  55.                   'id' => $user->getClient()->getId(),
  56.                   'name' => $user->getClient()->getName(),
  57.               ]
  58.           ]);
  59.         }
  60.       });
  61.     }
  62.   }
  63.   
  64.   // AUTO MANAGE CLIENT AND RELOAD IF ADMIN ON NotClientContextException
  65.   public function onKernelException(ExceptionEvent $event) {
  66.     $ex $event->getThrowable();
  67.     if($ex instanceof NotClientContextException && $this->security->isGranted('ROLE_ADMIN') && $ex->getClient()) {
  68.       $this->requestStack->getSession()->set("MANAGE_CLIENT"$ex->getClient()->getId());
  69.       $event->setResponse(new RedirectResponse($event->getRequest()->getUri()));
  70.     }
  71.   }
  72.   public function onAuthenticationSuccess(AuthenticationSuccessEvent $event) {
  73.     $user $event->getAuthenticationToken()->getUser();
  74.     
  75.     if(!($user instanceof User)) return;
  76.     $this->userRepository->registerLogin($event->getAuthenticationToken()->getUser(), $this->requestStack->getMainRequest());
  77.   }
  78. }