Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a7514fc

Browse filesBrowse files
committed
Give info about called security listeners in profiler
1 parent 7c1febd commit a7514fc
Copy full SHA for a7514fc

File tree

11 files changed

+311
-13
lines changed
Filter options

11 files changed

+311
-13
lines changed

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* [BC BREAK] `FirewallContext::getListeners()` now returns `\Traversable|array`
8+
* added info about called security listeners in profiler
89

910
3.3.0
1011
-----

‎src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
2020
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
2121
use Symfony\Component\Security\Core\Role\RoleInterface;
22+
use Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener;
2223
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2324
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2425
use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
@@ -39,6 +40,7 @@ class SecurityDataCollector extends DataCollector implements LateDataCollectorIn
3940
private $logoutUrlGenerator;
4041
private $accessDecisionManager;
4142
private $firewallMap;
43+
private $firewall;
4244
private $hasVarDumper;
4345

4446
/**
@@ -49,14 +51,16 @@ class SecurityDataCollector extends DataCollector implements LateDataCollectorIn
4951
* @param LogoutUrlGenerator|null $logoutUrlGenerator
5052
* @param AccessDecisionManagerInterface|null $accessDecisionManager
5153
* @param FirewallMapInterface|null $firewallMap
54+
* @param TraceableFirewallListener|null $firewall
5255
*/
53-
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null, FirewallMapInterface $firewallMap = null)
56+
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null, FirewallMapInterface $firewallMap = null, TraceableFirewallListener $firewall = null)
5457
{
5558
$this->tokenStorage = $tokenStorage;
5659
$this->roleHierarchy = $roleHierarchy;
5760
$this->logoutUrlGenerator = $logoutUrlGenerator;
5861
$this->accessDecisionManager = $accessDecisionManager;
5962
$this->firewallMap = $firewallMap;
63+
$this->firewall = $firewall;
6064
$this->hasVarDumper = class_exists(ClassStub::class);
6165
}
6266

@@ -167,6 +171,12 @@ public function collect(Request $request, Response $response, \Exception $except
167171
);
168172
}
169173
}
174+
175+
// collect firewall listeners information
176+
$this->data['listeners'] = array();
177+
if ($this->firewall) {
178+
$this->data['listeners'] = $this->firewall->getWrappedListeners();
179+
}
170180
}
171181

172182
public function lateCollect()
@@ -305,6 +315,11 @@ public function getFirewall()
305315
return $this->data['firewall'];
306316
}
307317

318+
public function getListeners()
319+
{
320+
return $this->data['listeners'];
321+
}
322+
308323
/**
309324
* {@inheritdoc}
310325
*/
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Bundle\SecurityBundle\Debug;
13+
14+
use Symfony\Bundle\SecurityBundle\EventListener\FirewallListener;
15+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16+
17+
/**
18+
* Firewall collecting called listeners.
19+
*
20+
* @author Robin Chalas <robin.chalas@gmail.com>
21+
*/
22+
final class TraceableFirewallListener extends FirewallListener
23+
{
24+
private $wrappedListeners;
25+
26+
public function getWrappedListeners()
27+
{
28+
return $this->wrappedListeners;
29+
}
30+
31+
protected function handleRequest(GetResponseEvent $event, $listeners)
32+
{
33+
foreach ($listeners as $listener) {
34+
$wrappedListener = new WrappedListener($listener);
35+
$wrappedListener->handle($event);
36+
$this->wrappedListeners[] = $wrappedListener->getInfo();
37+
38+
if ($event->hasResponse()) {
39+
break;
40+
}
41+
}
42+
}
43+
}
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Bundle\SecurityBundle\Debug;
13+
14+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15+
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
16+
use Symfony\Component\VarDumper\Caster\ClassStub;
17+
18+
/**
19+
* Wraps a security listener for calls record.
20+
*
21+
* @author Robin Chalas <robin.chalas@gmail.com>
22+
*/
23+
final class WrappedListener implements ListenerInterface
24+
{
25+
private $response;
26+
private $listener;
27+
private $time;
28+
private $stub;
29+
private static $hasVarDumper;
30+
31+
public function __construct(ListenerInterface $listener)
32+
{
33+
$this->listener = $listener;
34+
35+
if (null === self::$hasVarDumper) {
36+
self::$hasVarDumper = class_exists(ClassStub::class);
37+
}
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function handle(GetResponseEvent $event)
44+
{
45+
$startTime = microtime(true);
46+
$this->listener->handle($event);
47+
$this->time = microtime(true) - $startTime;
48+
$this->response = $event->getResponse();
49+
}
50+
51+
/**
52+
* Proxies all method calls to the original listener.
53+
*/
54+
public function __call($method, $arguments)
55+
{
56+
return call_user_func_array(array($this->listener, $method), $arguments);
57+
}
58+
59+
public function getWrappedListener()
60+
{
61+
return $this->listener;
62+
}
63+
64+
public function getInfo()
65+
{
66+
if (null === $this->stub) {
67+
$this->stub = self::$hasVarDumper ? new ClassStub(get_class($this->listener)) : get_class($this->listener);
68+
}
69+
70+
return array(
71+
'response' => $this->response,
72+
'time' => $this->time,
73+
'stub' => $this->stub,
74+
);
75+
}
76+
}

‎src/Symfony/Bundle/SecurityBundle/Resources/config/collectors.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/collectors.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<argument type="service" id="security.logout_url_generator" />
1515
<argument type="service" id="security.access.decision_manager" />
1616
<argument type="service" id="security.firewall.map" />
17+
<argument type="service" id="debug.security.firewall" on-invalid="null" />
1718
</service>
1819
</services>
1920
</container>

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security_debug.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/security_debug.xml
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@
1010
<service id="debug.security.access.decision_manager" class="Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager" decorates="security.access.decision_manager">
1111
<argument type="service" id="debug.security.access.decision_manager.inner" />
1212
</service>
13+
14+
<service id="debug.security.firewall" class="Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener" decorates="security.firewall" public="true">
15+
<argument type="service" id="security.firewall.map" />
16+
<argument type="service" id="event_dispatcher" />
17+
<argument type="service" id="security.logout_url_generator" />
18+
</service>
1319
</services>
1420
</container>

‎src/Symfony/Bundle/SecurityBundle/Resources/views/Collector/security.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/views/Collector/security.html.twig
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@
150150
</div>
151151

152152
{% if collector.firewall.security_enabled %}
153+
<h4>Configuration</h4>
154+
153155
<table>
154156
<thead>
155157
<tr>
@@ -188,6 +190,46 @@
188190
</tr>
189191
</tbody>
190192
</table>
193+
194+
<h4>Listeners</h4>
195+
196+
{% if collector.listeners|default([]) is empty %}
197+
<div class="empty">
198+
<p>No security listeners have been recorded. Check that debugging is enabled in the kernel.</p>
199+
</div>
200+
{% else %}
201+
<table>
202+
<thead>
203+
<tr>
204+
<th>Listener</th>
205+
<th>Duration</th>
206+
<th>Response</th>
207+
</tr>
208+
</thead>
209+
210+
{% set previous_event = (collector.listeners|first) %}
211+
{% for listener in collector.listeners %}
212+
{% if loop.first or listener != previous_event %}
213+
{% if not loop.first %}
214+
</tbody>
215+
{% endif %}
216+
217+
<tbody>
218+
{% set previous_event = listener %}
219+
{% endif %}
220+
221+
<tr>
222+
<td class="font-normal">{{ profiler_dump(listener.stub) }}</td>
223+
<td class="no-wrap">{{ '%0.2f'|format(listener.time * 1000) }} ms</td>
224+
<td class="font-normal">{{ listener.response ? profiler_dump(listener.response) : '(none)' }}</td>
225+
</tr>
226+
227+
{% if loop.last %}
228+
</tbody>
229+
{% endif %}
230+
{% endfor %}
231+
</table>
232+
{% endif %}
191233
{% endif %}
192234
{% elseif collector.enabled %}
193235
<div class="empty">

‎src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php
+48-3Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
16+
use Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener;
1617
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
1718
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
21+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1822
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1923
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2024
use Symfony\Component\Security\Core\Role\Role;
2125
use Symfony\Component\Security\Core\Role\RoleHierarchy;
26+
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
2227
use Symfony\Component\Security\Http\FirewallMapInterface;
28+
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2329

2430
class SecurityDataCollectorTest extends TestCase
2531
{
@@ -89,7 +95,7 @@ public function testGetFirewall()
8995
->with($request)
9096
->willReturn($firewallConfig);
9197

92-
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap);
98+
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap, new TraceableFirewallListener($firewallMap, new EventDispatcher(), new LogoutUrlGenerator()));
9399
$collector->collect($request, $this->getResponse());
94100
$collector->lateCollect();
95101
$collected = $collector->getFirewall();
@@ -124,7 +130,7 @@ public function testGetFirewallReturnsNull()
124130
->disableOriginalConstructor()
125131
->getMock();
126132

127-
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap);
133+
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap, new TraceableFirewallListener($firewallMap, new EventDispatcher(), new LogoutUrlGenerator()));
128134
$collector->collect($request, $response);
129135
$this->assertNull($collector->getFirewall());
130136

@@ -134,11 +140,50 @@ public function testGetFirewallReturnsNull()
134140
->disableOriginalConstructor()
135141
->getMock();
136142

137-
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap);
143+
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap, new TraceableFirewallListener($firewallMap, new EventDispatcher(), new LogoutUrlGenerator()));
138144
$collector->collect($request, $response);
139145
$this->assertNull($collector->getFirewall());
140146
}
141147

148+
/**
149+
* @group time-sensitive
150+
*/
151+
public function testGetListeners()
152+
{
153+
$request = $this->getRequest();
154+
$event = new GetResponseEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(), $request, HttpKernelInterface::MASTER_REQUEST);
155+
$event->setResponse($response = $this->getResponse());
156+
$listener = $this->getMockBuilder(ListenerInterface::class)->getMock();
157+
$listener
158+
->expects($this->once())
159+
->method('handle')
160+
->with($event);
161+
$firewallMap = $this
162+
->getMockBuilder(FirewallMap::class)
163+
->disableOriginalConstructor()
164+
->getMock();
165+
$firewallMap
166+
->expects($this->any())
167+
->method('getFirewallConfig')
168+
->with($request)
169+
->willReturn(null);
170+
$firewallMap
171+
->expects($this->once())
172+
->method('getListeners')
173+
->with($request)
174+
->willReturn(array(array($listener), null));
175+
176+
$firewall = new TraceableFirewallListener($firewallMap, new EventDispatcher(), new LogoutUrlGenerator());
177+
$firewall->onKernelRequest($event);
178+
179+
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap, $firewall);
180+
$collector->collect($request, $response);
181+
182+
$this->assertNotEmpty($collected = $collector->getListeners()[0]);
183+
$collector->lateCollect();
184+
$this->addToAssertionCount(1);
185+
}
186+
142187
public function provideRoles()
143188
{
144189
return array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.