Firewall.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  14. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Security\Http\Firewall\AccessListener;
  17. /**
  18. * Firewall uses a FirewallMap to register security listeners for the given
  19. * request.
  20. *
  21. * It allows for different security strategies within the same application
  22. * (a Basic authentication for the /api, and a web based authentication for
  23. * everything else for instance).
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class Firewall implements EventSubscriberInterface
  28. {
  29. private $map;
  30. private $dispatcher;
  31. private $exceptionListeners;
  32. public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher)
  33. {
  34. $this->map = $map;
  35. $this->dispatcher = $dispatcher;
  36. $this->exceptionListeners = new \SplObjectStorage();
  37. }
  38. public function onKernelRequest(GetResponseEvent $event)
  39. {
  40. if (!$event->isMasterRequest()) {
  41. return;
  42. }
  43. // register listeners for this firewall
  44. $listeners = $this->map->getListeners($event->getRequest());
  45. $authenticationListeners = $listeners[0];
  46. $exceptionListener = $listeners[1];
  47. $logoutListener = isset($listeners[2]) ? $listeners[2] : null;
  48. if (null !== $exceptionListener) {
  49. $this->exceptionListeners[$event->getRequest()] = $exceptionListener;
  50. $exceptionListener->register($this->dispatcher);
  51. }
  52. $accessListener = null;
  53. // initiate the listener chain
  54. foreach ($authenticationListeners as $listener) {
  55. if ($listener instanceof AccessListener) {
  56. $accessListener = $listener;
  57. continue;
  58. }
  59. $listener->handle($event);
  60. if ($event->hasResponse()) {
  61. break;
  62. }
  63. }
  64. if (null !== $logoutListener) {
  65. $logoutListener->handle($event);
  66. }
  67. if (!$event->hasResponse() && null !== $accessListener) {
  68. $accessListener->handle($event);
  69. }
  70. }
  71. public function onKernelFinishRequest(FinishRequestEvent $event)
  72. {
  73. $request = $event->getRequest();
  74. if (isset($this->exceptionListeners[$request])) {
  75. $this->exceptionListeners[$request]->unregister($this->dispatcher);
  76. unset($this->exceptionListeners[$request]);
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public static function getSubscribedEvents()
  83. {
  84. return array(
  85. KernelEvents::REQUEST => array('onKernelRequest', 8),
  86. KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
  87. );
  88. }
  89. }