Firewall.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http;
  11. use Symfony\Component\HttpKernel\HttpKernelInterface;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. /**
  16. * Firewall uses a FirewallMap to register security listeners for the given
  17. * request.
  18. *
  19. * It allows for different security strategies within the same application
  20. * (a Basic authentication for the /api, and a web based authentication for
  21. * everything else for instance).
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class Firewall
  26. {
  27. private $map;
  28. private $dispatcher;
  29. private $currentListeners;
  30. /**
  31. * Constructor.
  32. *
  33. * @param FirewallMap $map A FirewallMap instance
  34. */
  35. public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher)
  36. {
  37. $this->map = $map;
  38. $this->dispatcher = $dispatcher;
  39. $this->currentListeners = array();
  40. }
  41. /**
  42. * Handles security.
  43. *
  44. * @param GetResponseEvent $event An GetResponseEvent instance
  45. */
  46. public function onCoreRequest(GetResponseEvent $event)
  47. {
  48. if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  49. return;
  50. }
  51. // register listeners for this firewall
  52. list($listeners, $exception) = $this->map->getListeners($event->getRequest());
  53. if (null !== $exception) {
  54. $exception->register($this->dispatcher);
  55. }
  56. // initiate the listener chain
  57. foreach ($listeners as $listener) {
  58. $response = $listener->handle($event);
  59. if ($event->hasResponse()) {
  60. break;
  61. }
  62. }
  63. }
  64. }