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