Skip to content

Commit

Permalink
bug #1571 Added a new easyadmin_logout_path() helper (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #1571).

Discussion
----------

Added a new easyadmin_logout_path() helper

When we implemented #1560 my fear was that uncaught security exceptions would break it. In #1568 we received the first report. So, let's reimplement Symfony's helper to avoid any issue.

Commits
-------

7ccf62b Added a new easyadmin_logout_path() helper
  • Loading branch information
javiereguiluz committed Apr 8, 2017
2 parents e5a2751 + 7ccf62b commit 62ba778
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<argument type="service" id="easyadmin.config.manager" />
<argument type="service" id="property_accessor" />
<argument>%kernel.debug%</argument>
<argument type="service" id="security.logout_url_generator" on-invalid="null" />
<tag name="twig.extension" />
</service>

Expand Down
7 changes: 3 additions & 4 deletions Resources/views/default/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@

<div class="navbar-custom-menu">
{% block header_custom_menu %}
{# logout_path() without arguments only works in Symfony >= 2.7 #}
{% set _is_logout_supported = constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION_ID') >= 20700 %}
{% set _logout_path = easyadmin_logout_path() %}
<ul class="nav navbar-nav">
<li class="dropdown user user-menu">
{% block user_menu %}
Expand All @@ -85,7 +84,7 @@
{% if app.user|default(false) == false %}
<i class="hidden-xs fa fa-user-times"></i>
{{ 'user.anonymous'|trans(domain = 'EasyAdminBundle') }}
{% elseif not _is_logout_supported %}
{% elseif not _logout_path %}
<i class="hidden-xs fa fa-user"></i>
{{ app.user.username|default('user.unnamed'|trans(domain = 'EasyAdminBundle')) }}
{% else %}
Expand All @@ -100,7 +99,7 @@
<ul class="dropdown-menu" role="menu">
{% block user_menu_dropdown %}
<li>
<a href="{{ logout_path() }}"><i class="fa fa-sign-out"></i> {{ 'user.signout'|trans(domain = 'EasyAdminBundle') }}</a>
<a href="{{ _logout_path }}"><i class="fa fa-sign-out"></i> {{ 'user.signout'|trans(domain = 'EasyAdminBundle') }}</a>
</li>
{% endblock user_menu_dropdown %}
</ul>
Expand Down
3 changes: 1 addition & 2 deletions Tests/Controller/CustomizedBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace JavierEguiluz\Bundle\EasyAdminBundle\Tests\Controller;

use JavierEguiluz\Bundle\EasyAdminBundle\Tests\Fixtures\AbstractTestCase;
use Symfony\Component\HttpKernel\Kernel;

class CustomizedBackendTest extends AbstractTestCase
{
Expand All @@ -33,7 +32,7 @@ public function testUserMenuForLoggedUsers()

$this->assertContains('admin', $crawler->filter('header .user-menu')->text());

if (Kernel::VERSION_ID >= 20700) {
if (class_exists('Symfony\\Component\\Security\\Http\\Logout\\LogoutUrlGenerator')) {
$this->assertContains('Sign out', $crawler->filter('header .user-menu .dropdown-menu')->text());
} else {
$this->assertCount(0, $crawler->filter('header .user-menu .dropdown-menu'));
Expand Down
23 changes: 22 additions & 1 deletion Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ class EasyAdminTwigExtension extends \Twig_Extension
private $propertyAccessor;
/** @var bool */
private $debug;
private $logoutUrlGenerator;

public function __construct(ConfigManager $configManager, PropertyAccessor $propertyAccessor, $debug = false)
public function __construct(ConfigManager $configManager, PropertyAccessor $propertyAccessor, $debug = false, $logoutUrlGenerator)
{
$this->configManager = $configManager;
$this->propertyAccessor = $propertyAccessor;
$this->debug = $debug;
$this->logoutUrlGenerator = $logoutUrlGenerator;
}

/**
Expand All @@ -52,6 +54,7 @@ public function getFunctions()
new \Twig_SimpleFunction('easyadmin_get_action', array($this, 'getActionConfiguration')),
new \Twig_SimpleFunction('easyadmin_get_action_for_*_view', array($this, 'getActionConfiguration')),
new \Twig_SimpleFunction('easyadmin_get_actions_for_*_item', array($this, 'getActionsForItem')),
new \Twig_SimpleFunction('easyadmin_logout_path', array($this, 'getLogoutPath')),
);
}

Expand Down Expand Up @@ -349,6 +352,24 @@ public function truncateText(\Twig_Environment $env, $value, $length = 64, $pres
return $value;
}

/**
* This reimplementation of Symfony's logout_path() helper is needed because
* when no arguments are passed to the getLogoutPath(), it's common to get
* exceptions and there is no way to recover from them in a Twig template.
*/
public function getLogoutPath()
{
if (null === $this->logoutUrlGenerator) {
return;
}

try {
return $this->logoutUrlGenerator->getLogoutPath();
} catch (\Exception $e) {
return;
}
}

/**
* It returns the last part of the fully qualified class name
* (e.g. 'AppBundle\Entity\User' -> 'User').
Expand Down

0 comments on commit 62ba778

Please sign in to comment.