2010-10-19 12:06:43 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-10-19 12:06:43 +01:00
|
|
|
namespace Symfony\Component\HttpKernel\Security;
|
|
|
|
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\EventDispatcher\Event;
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
/**
|
2010-10-23 09:42:49 +01:00
|
|
|
* Firewall uses a FirewallMap to register security listeners for the given
|
|
|
|
* request.
|
2010-10-19 12:06:43 +01:00
|
|
|
*
|
|
|
|
* It allows for different security strategies within the same application
|
2010-10-23 09:42:49 +01:00
|
|
|
* (a Basic authentication for the /api, and a web based authentication for
|
|
|
|
* everything else for instance).
|
2010-10-19 12:06:43 +01:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Firewall
|
|
|
|
{
|
|
|
|
protected $map;
|
|
|
|
protected $dispatcher;
|
2010-12-03 17:47:54 +00:00
|
|
|
protected $currentListeners;
|
2010-10-19 12:06:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param FirewallMap $map A FirewallMap instance
|
|
|
|
*/
|
2011-01-21 10:00:52 +00:00
|
|
|
public function __construct(FirewallMapInterface $map)
|
2010-10-19 12:06:43 +01:00
|
|
|
{
|
|
|
|
$this->map = $map;
|
2010-12-03 17:47:54 +00:00
|
|
|
$this->currentListeners = array();
|
2010-10-19 12:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a core.request listener to enforce security.
|
|
|
|
*
|
|
|
|
* @param EventDispatcher $dispatcher An EventDispatcher instance
|
|
|
|
* @param integer $priority The priority
|
|
|
|
*/
|
|
|
|
public function register(EventDispatcher $dispatcher, $priority = 0)
|
|
|
|
{
|
|
|
|
$dispatcher->connect('core.request', array($this, 'handle'), $priority);
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles security.
|
|
|
|
*
|
|
|
|
* @param Event $event An Event instance
|
|
|
|
*/
|
|
|
|
public function handle(Event $event)
|
|
|
|
{
|
2010-11-23 08:42:19 +00:00
|
|
|
if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
|
2010-10-19 12:06:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-23 08:42:19 +00:00
|
|
|
$request = $event->get('request');
|
2010-10-19 12:06:43 +01:00
|
|
|
|
2010-12-03 17:47:54 +00:00
|
|
|
// disconnect all listeners from core.security to avoid the overhead
|
|
|
|
// of most listeners having to do this manually
|
2010-10-19 12:06:43 +01:00
|
|
|
$this->dispatcher->disconnect('core.security');
|
2011-01-21 10:00:52 +00:00
|
|
|
|
2010-12-03 17:47:54 +00:00
|
|
|
// ensure that listeners disconnect from wherever they have connected to
|
|
|
|
foreach ($this->currentListeners as $listener) {
|
|
|
|
$listener->unregister($this->dispatcher);
|
|
|
|
}
|
2011-01-21 10:00:52 +00:00
|
|
|
|
2010-12-03 17:47:54 +00:00
|
|
|
// register listeners for this firewall
|
2010-10-19 12:06:43 +01:00
|
|
|
list($listeners, $exception) = $this->map->getListeners($request);
|
|
|
|
if (null !== $exception) {
|
|
|
|
$exception->register($this->dispatcher);
|
|
|
|
}
|
|
|
|
foreach ($listeners as $listener) {
|
|
|
|
$listener->register($this->dispatcher);
|
|
|
|
}
|
2011-01-21 10:00:52 +00:00
|
|
|
|
2010-12-03 17:47:54 +00:00
|
|
|
// save current listener instances
|
|
|
|
$this->currentListeners = $listeners;
|
2010-12-08 12:51:26 +00:00
|
|
|
if (null !== $exception) {
|
|
|
|
$this->currentListeners[] = $exception;
|
|
|
|
}
|
2010-10-19 12:06:43 +01:00
|
|
|
|
2010-12-03 17:47:54 +00:00
|
|
|
// initiate the listener chain
|
2010-10-19 12:06:43 +01:00
|
|
|
$e = $this->dispatcher->notifyUntil(new Event($request, 'core.security', array('request' => $request)));
|
|
|
|
if ($e->isProcessed()) {
|
|
|
|
$event->setReturnValue($e->getReturnValue());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|