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
This commit is contained in:
Fabien Potencier 2015-09-23 10:16:07 +02:00
commit d1ae400cb1
3 changed files with 24 additions and 8 deletions

View File

@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
/**
* SecurityDataCollector.
@ -27,17 +28,20 @@ class SecurityDataCollector extends DataCollector
{
private $tokenStorage;
private $roleHierarchy;
private $logoutUrlGenerator;
/**
* Constructor.
*
* @param TokenStorageInterface|null $tokenStorage
* @param RoleHierarchyInterface|null $roleHierarchy
* @param LogoutUrlGenerator|null $logoutUrlGenerator
*/
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null)
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null)
{
$this->tokenStorage = $tokenStorage;
$this->roleHierarchy = $roleHierarchy;
$this->logoutUrlGenerator = $logoutUrlGenerator;
}
/**
@ -50,7 +54,7 @@ class SecurityDataCollector extends DataCollector
'enabled' => false,
'authenticated' => false,
'token_class' => null,
'provider_key' => null,
'logout_url' => null,
'user' => '',
'roles' => array(),
'inherited_roles' => array(),
@ -61,7 +65,7 @@ class SecurityDataCollector extends DataCollector
'enabled' => true,
'authenticated' => false,
'token_class' => null,
'provider_key' => null,
'logout_url' => null,
'user' => '',
'roles' => array(),
'inherited_roles' => array(),
@ -70,6 +74,7 @@ class SecurityDataCollector extends DataCollector
} else {
$inheritedRoles = array();
$assignedRoles = $token->getRoles();
if (null !== $this->roleHierarchy) {
$allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
foreach ($allRoles as $role) {
@ -78,11 +83,21 @@ class SecurityDataCollector extends DataCollector
}
}
}
$logoutUrl = null;
try {
if (null !== $this->logoutUrlGenerator) {
$logoutUrl = $this->logoutUrlGenerator->getLogoutPath();
}
} catch(\Exception $e) {
// fail silently when the logout URL cannot be generated
}
$this->data = array(
'enabled' => true,
'authenticated' => $token->isAuthenticated(),
'token_class' => get_class($token),
'provider_key' => method_exists($token, 'getProviderKey') ? $token->getProviderKey() : null,
'logout_url' => $logoutUrl,
'user' => $token->getUsername(),
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
@ -167,9 +182,9 @@ class SecurityDataCollector extends DataCollector
*
* @return string The provider key
*/
public function getProviderKey()
public function getLogoutUrl()
{
return $this->data['provider_key'];
return $this->data['logout_url'];
}
/**

View File

@ -13,6 +13,7 @@
<tag name="data_collector" template="@Security/Collector/security.html.twig" id="security" />
<argument type="service" id="security.token_storage" on-invalid="ignore" />
<argument type="service" id="security.role_hierarchy" />
<argument type="service" id="security.logout_url_generator" />
</service>
</services>
</container>

View File

@ -33,10 +33,10 @@
<span>{{ collector.tokenClass|abbr_class }}</span>
</div>
{% endif %}
{% if collector.providerKey %}
{% if collector.logoutUrl %}
<div class="sf-toolbar-info-piece">
<b>Actions</b>
<span><a href="{{ logout_path(collector.providerKey) }}">Logout</a></span>
<span><a href="{{ collector.logoutUrl }}">Logout</a></span>
</div>
{% endif %}
{% elseif collector.enabled %}