Firewall.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\KernelEvents;
  13. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. /**
  17. * Firewall uses a FirewallMap to register security listeners for the given
  18. * request.
  19. *
  20. * It allows for different security strategies within the same application
  21. * (a Basic authentication for the /api, and a web based authentication for
  22. * everything else for instance).
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class Firewall implements EventSubscriberInterface
  27. {
  28. private $map;
  29. private $dispatcher;
  30. /**
  31. * Constructor.
  32. *
  33. * @param FirewallMapInterface $map A FirewallMapInterface instance
  34. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  35. */
  36. public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher)
  37. {
  38. $this->map = $map;
  39. $this->dispatcher = $dispatcher;
  40. }
  41. /**
  42. * Handles security.
  43. *
  44. * @param GetResponseEvent $event An GetResponseEvent instance
  45. */
  46. public function onKernelRequest(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. $listener->handle($event);
  59. if ($event->hasResponse()) {
  60. break;
  61. }
  62. }
  63. }
  64. public static function getSubscribedEvents()
  65. {
  66. return array(KernelEvents::REQUEST => array('onKernelRequest', 8));
  67. }
  68. }