diff --git a/DependencyInjection/Compiler/SecurityContextPass.php b/DependencyInjection/Compiler/SecurityContextPass.php new file mode 100644 index 00000000..f7232761 --- /dev/null +++ b/DependencyInjection/Compiler/SecurityContextPass.php @@ -0,0 +1,32 @@ +has('security.token_storage')) { + $args = array(new Reference('security.token_storage'), new Reference('security.authorization_checker')); + } elseif ($container->has('security.context')) { + $args = array(new Reference('security.context')); + } else { + return; // SecurityBundle is not configured + } + + $defs = array( + $container->findDefinition('stof_doctrine_extensions.event_listener.blame'), + $container->findDefinition('stof_doctrine_extensions.event_listener.logger'), + ); + + foreach ($defs as $def) { + foreach ($args as $argument) { + $def->addArgument($argument); + } + } + } +} diff --git a/EventListener/BlameListener.php b/EventListener/BlameListener.php index b6050108..6e219613 100644 --- a/EventListener/BlameListener.php +++ b/EventListener/BlameListener.php @@ -5,6 +5,8 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\SecurityContextInterface; use Gedmo\Blameable\BlameableListener; @@ -26,10 +28,23 @@ class BlameListener implements EventSubscriberInterface */ private $blameableListener; - public function __construct(BlameableListener $blameableListener, SecurityContextInterface $securityContext = null) + public function __construct(BlameableListener $blameableListener, $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null) { $this->blameableListener = $blameableListener; - $this->securityContext = $securityContext; + + // BC layer for Symfony 2.5 and older + if ($tokenStorage instanceof SecurityContextInterface) { + $this->tokenStorage = $this->authorizationChecker = $tokenStorage; + + return; + } + + if (null !== $tokenStorage && !$tokenStorage instanceof TokenStorageInterface) { + throw new \InvalidArgumentException(sprintf('The second argument of the %s constructor should be a Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or a Symfony\Component\Security\Core\SecurityContextInterface or null.', __CLASS__)); + } + + $this->tokenStorage = $tokenStorage; + $this->authorizationChecker = $authorizationChecker; } /** diff --git a/EventListener/LoggerListener.php b/EventListener/LoggerListener.php index 38d0dca6..ad3b2b32 100644 --- a/EventListener/LoggerListener.php +++ b/EventListener/LoggerListener.php @@ -5,7 +5,10 @@ use Gedmo\Loggable\LoggableListener; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\SecurityContextInterface; /** @@ -15,14 +18,28 @@ */ class LoggerListener implements EventSubscriberInterface { - private $securityContext; + private $authorizationChecker; + private $tokenStorage; private $loggableListener; - public function __construct(LoggableListener $loggableListener, SecurityContextInterface $securityContext = null) + public function __construct(LoggableListener $loggableListener, $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null) { $this->loggableListener = $loggableListener; - $this->securityContext = $securityContext; + + // BC layer for Symfony 2.5 and older + if ($tokenStorage instanceof SecurityContextInterface) { + $this->tokenStorage = $this->authorizationChecker = $tokenStorage; + + return; + } + + if (null !== $tokenStorage && !$tokenStorage instanceof TokenStorageInterface) { + throw new \InvalidArgumentException(sprintf('The second argument of the %s constructor should be a Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or a Symfony\Component\Security\Core\SecurityContextInterface or null.', __CLASS__)); + } + + $this->tokenStorage = $tokenStorage; + $this->authorizationChecker = $authorizationChecker; } /** @@ -32,12 +49,17 @@ public function __construct(LoggableListener $loggableListener, SecurityContextI */ public function onKernelRequest(GetResponseEvent $event) { - if (null === $this->securityContext) { + if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } - $token = $this->securityContext->getToken(); - if (null !== $token && $this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + if (null === $this->tokenStorage || null === $this->authorizationChecker) { + return; + } + + $token = $this->tokenStorage->getToken(); + + if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $this->loggableListener->setUsername($token); } } diff --git a/Resources/config/listeners.xml b/Resources/config/listeners.xml index f704a52b..6442448a 100644 --- a/Resources/config/listeners.xml +++ b/Resources/config/listeners.xml @@ -93,12 +93,10 @@ - - diff --git a/StofDoctrineExtensionsBundle.php b/StofDoctrineExtensionsBundle.php index d5ac35c1..561b9cf8 100644 --- a/StofDoctrineExtensionsBundle.php +++ b/StofDoctrineExtensionsBundle.php @@ -2,9 +2,10 @@ namespace Stof\DoctrineExtensionsBundle; +use Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler\SecurityContextPass; +use Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler\ValidateExtensionConfigurationPass; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler\ValidateExtensionConfigurationPass; use Gedmo\Uploadable\Mapping\Validator; class StofDoctrineExtensionsBundle extends Bundle @@ -14,9 +15,8 @@ class StofDoctrineExtensionsBundle extends Bundle */ public function build(ContainerBuilder $container) { - parent::build($container); $container->addCompilerPass(new ValidateExtensionConfigurationPass()); - + $container->addCompilerPass(new SecurityContextPass()); } public function boot()