vendor/stof/doctrine-extensions-bundle/src/EventListener/BlameListener.php line 35

Open in your IDE?
  1. <?php
  2. namespace Stof\DoctrineExtensionsBundle\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\HttpKernelInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Gedmo\Blameable\BlameableListener;
  10. /**
  11.  * Sets the username from the security context by listening on kernel.request
  12.  *
  13.  * @author David Buchmann <mail@davidbu.ch>
  14.  */
  15. class BlameListener implements EventSubscriberInterface
  16. {
  17.     private $authorizationChecker;
  18.     private $tokenStorage;
  19.     private $blameableListener;
  20.     public function __construct(BlameableListener $blameableListenerTokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authorizationChecker null)
  21.     {
  22.         $this->blameableListener $blameableListener;
  23.         $this->tokenStorage $tokenStorage;
  24.         $this->authorizationChecker $authorizationChecker;
  25.     }
  26.     /**
  27.      * @internal
  28.      */
  29.     public function onKernelRequest(RequestEvent $event)
  30.     {
  31.         if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  32.             return;
  33.         }
  34.         if (null === $this->tokenStorage || null === $this->authorizationChecker) {
  35.             return;
  36.         }
  37.         $token $this->tokenStorage->getToken();
  38.         if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  39.             $this->blameableListener->setUserValue($token->getUser());
  40.         }
  41.     }
  42.     /**
  43.      * @return string[]
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return array(
  48.             KernelEvents::REQUEST => 'onKernelRequest',
  49.         );
  50.     }
  51. }