8000 Integrate current firewall in profile · symfony/symfony@bab3497 · GitHub
[go: up one dir, main page]

Skip to content

Commit bab3497

Browse files
committed
Integrate current firewall in profile
Add firewall name to the toolbar Display bool values as metrics, Dont display request_matcher anymore Use allowsAnonymous()/isSecurityEnabled() in template, update labels accordingly Add testCollectFirewall()
1 parent 8df9a80 commit bab3497

File tree

4 files changed

+143
-5
lines changed

4 files changed

+143
-5
lines changed

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2121
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2222
use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
23+
use Symfony\Component\Security\Http\FirewallMapInterface;
24+
use Symfony\Bundle\SecurityBundle\Security\ContextAwareFirewallMapInterface;
2325

2426
/**
2527
* SecurityDataCollector.
@@ -32,21 +34,24 @@ class SecurityDataCollector extends DataCollector
3234
private $roleHierarchy;
3335
private $logoutUrlGenerator;
3436
private $accessDecisionManager;
37+
private $firewallMap;
3538

3639
/**
3740
* Constructor.
3841
*
39-
* @param TokenStorageInterface|null $tokenStorage
40-
* @param RoleHierarchyInterface|null $roleHierarchy
41-
* @param LogoutUrlGenerator|null $logoutUrlGenerator
42-
* @param AccessDecisionManagerInterface|null $accessDecisionManager
42+
* @param TokenStorageInterface|null $tokenStorage
43+
* @param RoleHierarchyInterface|null $roleHierarchy
44+
* @param LogoutUrlGenerator|null $logoutUrlGenerator
45+
* @param AccessDecisionManagerInterface|null $accessDecisionManager
46+
* @param ContextAwareFirewallMapInterface|null $firewallMap
4347
*/
44-
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null)
48+
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null, FirewallMapInterface $firewallMap = null)
4549
{
4650
$this->tokenStorage = $tokenStorage;
4751
$this->roleHierarchy = $roleHierarchy;
4852
$this->logoutUrlGenerator = $logoutUrlGenerator;
4953
$this->accessDecisionManager = $accessDecisionManager;
54+
$this->firewallMap = $firewallMap;
5055
}
5156

5257
/**
@@ -123,6 +128,16 @@ public function collect(Request $request, Response $response, \Exception $except
123128
$this->data['voter_strategy'] = 'unknown';
124129
$this->data['voters'] = array();
125130
}
131+
132+
// collect firewall context informations
133+
$this->data['firewall'] = null;
134+
if ($this->firewallMap instanceof ContextAwareFirewallMapInterface) {
135+
$firewallContext = $this->firewallMap->getContext($request);
136+
137+
if (null !== $firewallContext) {
138+
$this->data['firewall'] = $firewallContext->getConfig();
139+
}
140+
}
126141
}
127142

128143
/**
@@ -236,6 +251,16 @@ public function getAccessDecisionLog()
236251
return $this->data['access_decision_log'];
237252
}
238253

254+
/**
255+
* Returns the configuration of the current firewall context.
256+
*
257+
* @return array
258+
*/
259+
public function getFirewall()
260+
{
261+
return $this->data['firewall'];
262+
}
263+
239264
/**
240265
* {@inheritdoc}
241266
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<argument type="service" id="security.role_hierarchy" />
1212
<argument type="service" id="security.logout_url_generator" />
1313
<argument type="service" id="security.access.decision_manager" />
14+
<argument type="service" id="security.firewall.map" />
1415
</service>
1516
</services>
1617
</container>

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<span>{{ collector.tokenClass|abbr_class }}</span>
3434
</div>
3535
{% endif %}
36+
{% if collector.firewall %}
37+
<div class="sf-toolbar-info-piece">
38+
<b>Firewall name</b>
39+
<span>{{ collector.firewall.name }}</span>
40+
</div>
41+
{% endif %}
3642
{% if collector.logoutUrl %}
3743
<div class="sf-toolbar-info-piece">
3844
<b>Actions</b>
@@ -120,6 +126,77 @@
120126
</div>
121127
{% endif %}
122128

129+
<h2>Security Firewall</h2>
130+
131+
{% if collector.firewall %}
132+
<div class="metrics">
133+
<div class="metric">
134+
<span class="value">{{ collector.firewall.name }}</span>
135+
<span class="label">Name</span>
136+
</div>
137+
<div class="metric">
138+
<span class="value">{{ include('@WebProfiler/Icon/' ~ (collector.firewall.securityEnabled ? 'yes' : 'no') ~ '.svg') }}</span>
139+
<span class="label">Security enabled</span>
140+
</div>
141+
<div class="metric">
142+
<span class="value">{{ include('@WebProfiler/Icon/' ~ (collector.firewall.stateless ? 'yes' : 'no') ~ '.svg') }}</span>
143+
<span class="label">Stateless</span>
144+
</div>
145+
<div class="metric">
146+
<span class="value">{{ include('@WebProfiler/Icon/' ~ (collector.firewall.allowsAnonymous ? 'yes' : 'no') ~ '.svg') }}</span>
147+
<span class="label">Allow Anonymous</span>
148+
</div>
149+
</div>
150+
{% if collector.firewall.securityEnabled %}
151+
<table>
152+
<thead>
153+
<tr>
154+
<th scope="col" class="key">Key</th>
155+
<th scope="col">Value</th>
156+
</tr>
157+
</thead>
158+
<tbody>
159+
<tr>
160+
<th>provider</th>
161+
<td>{{ collector.firewall.provider }}</td>
162+
</tr>
163+
<tr>
164+
<th>context</th>
165+
<td>{{ collector.firewall.context }}</td>
166+
</tr>
167+
<tr>
168+
<th>entry_point</th>
169+
<td>{{ collector.firewall.entryPoint }}</td>
170+
</tr>
171+
<tr>
172+
<th>user_checker</th>
173+
<td>{{ collector.firewall.userChecker }}</td>
174+
</tr>
175+
<tr>
176+
<th>access_denied_handler</th>
177+
<td>{{ collector.firewall.accessDeniedHandler }}</td>
178+
</tr>
179+
<tr>
180+
<th>access_denied_url</th>
181+
<td>{{ collector.firewall.accessDeniedUrl }}</td>
182+
</tr>
183+
<tr>
184+
<th>listeners</th>
185+
<td>{{ collector.firewall.listeners|yaml_encode }}</td>
186+
</tr>
187+
</tbody>
188+
</table>
189+
{% endif %}
190+
{% elseif collector.enabled %}
191+
<div class="empty">
192+
<p>There is no security firewall.</p>
193+
</div>
194+
{% else %}
195+
<div class="empty">
196+
<p>The security component is disabled.</p>
197+
</div>
198+
{% endif %}
199+
123200
{% if collector.voters|default([]) is not empty %}
124201
<h2>Security Voters <small>({{ collector.voters|length }})</small></h2>
125202

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector;
1313

1414
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
15+
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
16+
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
17+
use Symfony\Bundle\SecurityBundle\Security\ContextAwareFirewallMapInterface;
1518
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1619
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1720
use Symfony\Component\Security\Core\Role\Role;
@@ -32,6 +35,7 @@ public function testCollectWhenSecurityIsDisabled()
3235
$this->assertCount(0, $collector->getRoles());
3336
$this->assertCount(0, $collector->getInheritedRoles());
3437
$this->assertEmpty($collector->getUser());
38+
$this->assertNull($collector->getFirewall());
3539
}
3640

3741
public function testCollectWhenAuthenticationTokenIsNull()
@@ -47,6 +51,7 @@ public function testCollectWhenAuthenticationTokenIsNull()
4751
$this->assertCount(0, $collector->getRoles());
4852
$this->assertCount(0, $collector->getInheritedRoles());
4953
$this->assertEmpty($collector->getUser());
54+
$this->assertNull($collector->getFirewall());
5055
}
5156

5257
/** @dataProvider provideRoles */
@@ -67,6 +72,36 @@ public function testCollectAuthenticationTokenAndRoles(array $roles, array $norm
6772
$this->assertSame('hhamon', $collector->getUser());
6873
}
6974

75+
public function testCollectFirewall()
76+
{
77+
$firewallConfig = new FirewallConfig('dummy', 'security.request_matcher.dummy');
78+
$request = $this->getRequest();
79+
80+
$firewallContext = $this
81+
->getMockBuilder(FirewallContext::class)
82+
->disableOriginalConstructor()
83+
->getMock();
84+
$firewallContext
85+
->expects($this->once())
86+
->method('getConfig')
87+
->willReturn($firewallConfig);
88+
89+
$firewallMap = $this
90+
->getMockBuilder(ContextAwareFirewallMapInterface::class)
91+
->disableOriginalConstructor()
92+
->getMock();
93+
$firewallMap
94+
->expects($this->once())
95+
->method('getContext')
96+
->with($request)
97+
->willReturn($firewallContext);
98+
99+
$collector = new SecurityDataCollector(null, null, null, null, $firewallMap);
100+
$collector->collect($request, $this->getResponse());
101+
102+
$this->assertSame($firewallConfig, $collector->getFirewall());
103+
}
104+
70105
public function provideRoles()
71106
{
72107
return array(

0 commit comments

Comments
 (0)
0