Skip to content

Commit d1ae400

Browse files
committed
bug #15861 Avoid errors when generating the logout URL when there is no firewall key (javiereguiluz)
This PR was squashed before being merged into the 2.8 branch (closes #15861). Discussion ---------- Avoid errors when generating the logout URL when there is no firewall key | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15853 | License | MIT | Doc PR | - Commits ------- a811912 Avoid errors when generating the logout URL when there is no firewall key
2 parents 70fcc9c + a811912 commit d1ae400

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1919
use Symfony\Component\Security\Core\Role\RoleInterface;
20+
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2021

2122
/**
2223
* SecurityDataCollector.
@@ -27,17 +28,20 @@ class SecurityDataCollector extends DataCollector
2728
{
2829
private $tokenStorage;
2930
private $roleHierarchy;
31+
private $logoutUrlGenerator;
3032

3133
/**
3234
* Constructor.
3335
*
3436
* @param TokenStorageInterface|null $tokenStorage
3537
* @param RoleHierarchyInterface|null $roleHierarchy
38+
* @param LogoutUrlGenerator|null $logoutUrlGenerator
3639
*/
37-
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null)
40+
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null)
3841
{
3942
$this->tokenStorage = $tokenStorage;
4043
$this->roleHierarchy = $roleHierarchy;
44+
$this->logoutUrlGenerator = $logoutUrlGenerator;
4145
}
4246

4347
/**
@@ -50,7 +54,7 @@ public function collect(Request $request, Response $response, \Exception $except
5054
'enabled' => false,
5155
'authenticated' => false,
5256
'token_class' => null,
53-
'provider_key' => null,
57+
'logout_url' => null,
5458
'user' => '',
5559
'roles' => array(),
5660
'inherited_roles' => array(),
@@ -61,7 +65,7 @@ public function collect(Request $request, Response $response, \Exception $except
6165
'enabled' => true,
6266
'authenticated' => false,
6367
'token_class' => null,
64-
'provider_key' => null,
68+
'logout_url' => null,
6569
'user' => '',
6670
'roles' => array(),
6771
'inherited_roles' => array(),
@@ -70,6 +74,7 @@ public function collect(Request $request, Response $response, \Exception $except
7074
} else {
7175
$inheritedRoles = array();
7276
$assignedRoles = $token->getRoles();
77+
7378
if (null !== $this->roleHierarchy) {
7479
$allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
7580
foreach ($allRoles as $role) {
@@ -78,11 +83,21 @@ public function collect(Request $request, Response $response, \Exception $except
7883
}
7984
}
8085
}
86+
87+
$logoutUrl = null;
88+
try {
89+
if (null !== $this->logoutUrlGenerator) {
90+
$logoutUrl = $this->logoutUrlGenerator->getLogoutPath();
91+
}
92+
} catch(\Exception $e) {
93+
// fail silently when the logout URL cannot be generated
94+
}
95+
8196
$this->data = array(
8297
'enabled' => true,
8398
'authenticated' => $token->isAuthenticated(),
8499
'token_class' => get_class($token),
85-
'provider_key' => method_exists($token, 'getProviderKey') ? $token->getProviderKey() : null,
100+
'logout_url' => $logoutUrl,
86101
'user' => $token->getUsername(),
87102
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
88103
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
@@ -167,9 +182,9 @@ public function getTokenClass()
167182
*
168183
* @return string The provider key
169184
*/
170-
public function getProviderKey()
185+
public function getLogoutUrl()
171186
{
172-
return $this->data['provider_key'];
187+
return $this->data['logout_url'];
173188
}
174189

175190
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<tag name="data_collector" template="@Security/Collector/security.html.twig" id="security" />
1414
<argument type="service" id="security.token_storage" on-invalid="ignore" />
1515
<argument type="service" id="security.role_hierarchy" />
16+
<argument type="service" id="security.logout_url_generator" />
1617
</service>
1718
</services>
1819
</container>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
<span>{{ collector.tokenClass|abbr_class }}</span>
3434
</div>
3535
{% endif %}
36-
{% if collector.providerKey %}
36+
{% if collector.logoutUrl %}
3737
<div class="sf-toolbar-info-piece">
3838
<b>Actions</b>
39-
<span><a href="{{ logout_path(collector.providerKey) }}">Logout</a></span>
39+
<span><a href="{{ collector.logoutUrl }}">Logout</a></span>
4040
</div>
4141
{% endif %}
4242
{% elseif collector.enabled %}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy