8000 Avoid errors when generating the logout URL when there is no firewall key by javiereguiluz · Pull Request #15861 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Avoid errors when generating the logout URL when there is no firewall key #15861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc missing

{
$this->tokenStorage = $tokenStorage;
$this->roleHierarchy = $roleHierarchy;
$this->logoutUrlGenerator = $logoutUrlGenerator;
}

/**
Expand All @@ -50,7 +54,7 @@ public function collect(Request $request, Response $response, \Exception $except
'enabled' => false,
'authenticated' => false,
'token_class' => null,
'provider_key' => null,
'logout_url' => null,
'user' => '',
'roles' => array(),
'inherited_roles' => array(),
Expand All @@ -61,7 +65,7 @@ public function collect(Request $request, Response $response, \Exception $except
'enabled' => true,
'authenticated' => false,
'token_class' => null,
'provider_key' => null,
'logout_url' => null,
'user' => '',
'roles' => array(),
'inherited_roles' => array(),
Expand All @@ -70,6 +74,7 @@ public function collect(Request $request, Response $response, \Exception $except
} else {
$inheritedRoles = array();
$assignedRoles = $token->getRoles();

if (null !== $this->roleHierarchy) {
$allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
foreach ($allRoles as $role) {
Expand All @@ -78,11 +83,21 @@ public function collect(Request $request, Response $response, \Exception $except
}
}
}

$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),
Expand Down Expand Up @@ -167,9 +182,9 @@ public function getTokenClass()
*
* @return string The provider key
*/
public function getProviderKey()
public function getLogoutUrl()
{
return $this->data['provider_key'];
return $this->data['logout_url'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
0