Skip to content

Commit

Permalink
Symfony 3.x Compatibility: (#6)
Browse files Browse the repository at this point in the history
* Symfony 3.x Compatibility:
 - Updated usage of table helper (deprecated in 3.x) in commands to Table class.
 - Updated references to security context (deprecated in 3.x) to token storage.
 - Updated readme, should only register bundle in dev and test environments where it is intended to be used.

* Symfony 3:
 - Fixes issue with webdev toolbar when on anonymous access pages.
  • Loading branch information
cjgordon authored and egulias committed Apr 22, 2017
1 parent 20767c1 commit 0cbb3bd
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Command/SecurityDebugAclObjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;

/**
* ACL Objects debug command
Expand Down Expand Up @@ -91,7 +92,7 @@ function (&$v, $k) {
$access = $acl->isGranted($masks, array($securityIdentity), false) ? 'Allow' : 'Deny';
}

$table = $this->getHelperSet()->get('table');
$table = new Table($output);
$table->setHeaders(array('Mask', 'Grant', 'Deny'));
$table->setRows($results);
$table->render($output);
Expand Down
3 changes: 2 additions & 1 deletion Command/SecurityDebugAclVotersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Egulias\SecurityDebugCommandBundle\Security\Voter\VotersDebug;
use Egulias\SecurityDebugCommandBundle\Security\Authorization\DecisionManagerDebug;
use Symfony\Component\Console\Helper\Table;

/**
* Voters debug command
Expand Down Expand Up @@ -87,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

$output->writeln($formattedLine);
$table = $this->getHelperSet()->get('table');
$table = new Table($output);
$table->setHeaders(array('Class', 'Abstain', 'Grant', 'Deny'));
$votes = $votersDebug->getVotersVote($token);

Expand Down
5 changes: 3 additions & 2 deletions Command/SecurityDebugFirewallsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Egulias\SecurityDebugCommandBundle\HttpKernel\SimpleHttpKernel;
use Symfony\Component\Console\Helper\Table;

/**
* @author Eduardo Gulias <[email protected]>
Expand Down Expand Up @@ -51,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$session = $this->getContainer()->get('session');
$session->setName('security.debug.console');
$session->set('_security_' . $firewallProvider, serialize($token));
$this->getContainer()->get('security.context')->setToken($token);
$this->getContainer()->get('security.token_storage')->setToken($token);

$kernel = new SimpleHttpKernel();
$request = Request::create($uri, 'GET', array(), array('security.debug.console' => true));
Expand All @@ -77,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
sprintf('Firewall <comment>%s</comment> listeners', $firewallProvider)
);
$output->writeln($formattedLine);
$table = $this->getHelperSet()->get('table');
$table = new Table($output);
$table->setHeaders(array('Class', 'Stopped propagation'));
$firewallContext = $map->getContext();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
Expand Down
3 changes: 2 additions & 1 deletion Command/SecurityDebugVotersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Egulias\SecurityDebugCommandBundle\Security\Voter\VotersDebug;
use Egulias\SecurityDebugCommandBundle\Security\Authorization\DecisionManagerDebug;
use Symfony\Component\Console\Helper\Table;

/**
* Voters debug command
Expand Down Expand Up @@ -71,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

$output->writeln($formattedLine);
$table = $this->getHelperSet()->get('table');
$table = new Table($output);
$table->setHeaders(array('Class', 'Abstain', 'Grant', 'Deny'));
$votes = $votersDebug->getVotersVote($token);

Expand Down
10 changes: 5 additions & 5 deletions DataCollector/FirewallCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

/**
* Class AccessDeniedListener
Expand All @@ -18,22 +18,22 @@ class FirewallCollector
{
const HAS_RESPONSE = SecurityDebugDataCollector::DENIED;

private $securityContext;
private $tokenStorage;
private $container;

public function __construct(
SecurityContextInterface $securityContext,
TokenStorage $tokenStorage,
Container $container
) {
$this->securityContext = $securityContext;
$this->tokenStorage = $tokenStorage;
//Container dependency is a bad thing. This is to be refactored to a compiler pass
//where all the firewall providers will be fetched
$this->container = $container;
}

public function collect(Request $request, \Exception $exception)
{
$token = $this->securityContext->getToken();
$token = $this->tokenStorage->getToken();
if (!method_exists($token, 'getProviderKey')) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions DataCollector/VotersCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Egulias\SecurityDebugCommandBundle\DataCollector;

use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Egulias\SecurityDebugCommandBundle\Security\Voter\VotersDebug;

Expand All @@ -14,20 +14,20 @@
class VotersCollector
{
private $accessDecisionManager;
private $securityContext;
private $tokenStorage;

public function __construct(
AccessDecisionManagerInterface $decisionManager,
SecurityContextInterface $securityContext
TokenStorage $tokenStorage
) {
$this->accessDecisionManager = $decisionManager;
$this->securityContext = $securityContext;
$this->tokenStorage = $tokenStorage;
}

public function collect()
{
$votersDebug = new VotersDebug($this->accessDecisionManager);
$token = $this->securityContext->getToken();
$token = $this->tokenStorage->getToken();

if (!$token || !$token->isAuthenticated()) {
return;
Expand Down
5 changes: 5 additions & 0 deletions EventListener/SecurityListenersDebugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
if (!$event->getException() instanceof AccessDeniedException) {
return;
}

if (gettype($event->getRequest()->get('_controller')) == 'string') {
return;
}

$controller = $event->getRequest()->get('_controller');
$controllerEvent = new FilterControllerEvent(
$event->getKernel(),
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ $ php composer.phar update egulias/security-debug-command-bundle
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Egulias\SecurityDebugCommandBundle\EguliasSecurityDebugCommandBundle(),
// ...
);
// ...
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundle[] = Egulias\SecurityDebugCommandBundle\EguliasSecurityDebugCommandBundle();
}
// ...
}
```
## Configure the user class
Expand Down
8 changes: 4 additions & 4 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
services:
egulias.voters_collector:
class: Egulias\SecurityDebugCommandBundle\DataCollector\VotersCollector
arguments: [@security.access.decision_manager, @security.context]
arguments: ["@security.access.decision_manager", "@security.token_storage"]
egulias.firewall_collector:
class: Egulias\SecurityDebugCommandBundle\DataCollector\FirewallCollector
arguments: [@security.context, @service_container]
arguments: ["@security.token_storage", "@service_container"]
egulias.security_listeners_debug:
class: Egulias\SecurityDebugCommandBundle\EventListener\SecurityListenersDebugListener
arguments: [@sensio_framework_extra.security.listener, @data_collector.egulias_security_debug]
arguments: ["@sensio_framework_extra.security.listener", "@data_collector.egulias_security_debug"]
tags:
- {name: kernel.event_listener, event: kernel.exception, priority: 128}
data_collector.egulias_security_debug:
class: Egulias\SecurityDebugCommandBundle\DataCollector\SecurityDebugDataCollector
arguments: [@egulias.voters_collector, @egulias.firewall_collector]
arguments: ["@egulias.voters_collector", "@egulias.firewall_collector"]
tags:
- { name: data_collector, template: "EguliasSecurityDebugCommandBundle:Collector:security_debug", id: "egulias_security_debug"}

0 comments on commit 0cbb3bd

Please sign in to comment.