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 1 commit
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
Next Next commit
Avoid errors when generating the logout URL when there is no firewall…
… key
  • Loading branch information
javiereguiluz committed Sep 21, 2015
commit 5cf459704ffc55bdebd01f872985843005cbe2b3
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,19 @@ class SecurityDataCollector extends DataCollector
{
private $tokenStorage;
private $roleHierarchy;
private $logoutUrlGenerator;

/**
* Constructor.
*
* @param TokenStorageInterface|null $tokenStorage
* @param RoleHierarchyInterface|null $roleHierarchy
*/
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 +53,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 +64,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 +73,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 +82,19 @@ public function collect(Request $request, Response $response, \Exception $except
}
}
}

try {
$logoutUrl = $this->logoutUrlGenerator->getLogoutPath();
Copy link
Contributor

Choose a reason for hiding this comment

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

this fails if logoutUrlGenerator is null

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. Fixed. Thanks.

} catch(\Exception $e) {
// fail silently when the logout URL cannot be generated
$logoutUrl = null;
}

$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 +179,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