Skip to content

Commit

Permalink
Avoid using deprecated APIs in Symfony 2.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Aug 12, 2015
1 parent b2d6fe1 commit cd0f80e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
32 changes: 32 additions & 0 deletions DependencyInjection/Compiler/SecurityContextPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class SecurityContextPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->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);
}
}
}
}
19 changes: 17 additions & 2 deletions EventListener/BlameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
34 changes: 28 additions & 6 deletions EventListener/LoggerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
Expand Down
2 changes: 0 additions & 2 deletions Resources/config/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@

<service id="stof_doctrine_extensions.event_listener.logger" class="%stof_doctrine_extensions.event_listener.logger.class%" public="false">
<argument type="service" id="stof_doctrine_extensions.listener.loggable" />
<argument type="service" id="security.context" on-invalid="null" />
</service>

<service id="stof_doctrine_extensions.event_listener.blame" class="%stof_doctrine_extensions.event_listener.blame.class%" public="false">
<argument type="service" id="stof_doctrine_extensions.listener.blameable" />
<argument type="service" id="security.context" on-invalid="null" />
</service>

<!-- Uploadable -->
Expand Down
6 changes: 3 additions & 3 deletions StofDoctrineExtensionsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit cd0f80e

Please sign in to comment.