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;
|
|
|
|
|
2011-01-25 13:23:23 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\EventInterface;
|
2010-10-19 12:06:43 +01:00
|
|
|
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
|
|
|
*
|
2011-01-23 17:02:16 +00:00
|
|
|
* The handle method must be connected to the core.request event.
|
|
|
|
*
|
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-25 13:23:23 +00:00
|
|
|
public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher)
|
2010-10-19 12:06:43 +01:00
|
|
|
{
|
|
|
|
$this->map = $map;
|
|
|
|
$this->dispatcher = $dispatcher;
|
2011-01-23 17:02:16 +00:00
|
|
|
$this->currentListeners = array();
|
2010-10-19 12:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles security.
|
|
|
|
*
|
2011-01-25 13:23:23 +00:00
|
|
|
* @param EventInterface $event An EventInterface instance
|
2010-10-19 12:06:43 +01:00
|
|
|
*/
|
2011-01-25 13:23:23 +00:00
|
|
|
public function handle(EventInterface $event)
|
2010-10-19 12:06:43 +01:00
|
|
|
{
|
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
|
2011-01-26 06:50:06 +00:00
|
|
|
$ret = $this->dispatcher->notifyUntil($event = new Event($request, 'core.security', array('request' => $request)));
|
|
|
|
if ($event->isProcessed()) {
|
|
|
|
$event->setProcessed();
|
2010-10-19 12:06:43 +01:00
|
|
|
|
2011-01-26 06:50:06 +00:00
|
|
|
return $ret;
|
2010-10-19 12:06:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|