Ver código fonte

Merge pull request #4062 from fbourigault/fix-empty-list-navbar

Fix empty list navbar
Grégoire Paris 9 anos atrás
pai
commit
1bf689b786

+ 25 - 6
Admin/AbstractAdmin.php

@@ -2797,37 +2797,43 @@ EOT;
     {
         $list = array();
 
-        if (in_array($action, array('tree', 'show', 'edit', 'delete', 'list', 'batch'))) {
+        if (in_array($action, array('tree', 'show', 'edit', 'delete', 'list', 'batch')) && $this->hasAccess('create')) {
             $list['create'] = array(
                 'template' => 'SonataAdminBundle:Button:create_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'delete', 'acl', 'history')) && $object) {
+        if (in_array($action, array('show', 'delete', 'acl', 'history')) && $this->canAccessObject('edit', $object)) {
             $list['edit'] = array(
                 'template' => 'SonataAdminBundle:Button:edit_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'edit', 'acl')) && $object) {
+        if (in_array($action, array('show', 'edit', 'acl')) && $this->canAccessObject('history', $object)) {
             $list['history'] = array(
                 'template' => 'SonataAdminBundle:Button:history_button.html.twig',
             );
         }
 
-        if (in_array($action, array('edit', 'history')) && $object) {
+        if (in_array($action, array('edit', 'history'))
+            && $this->isAclEnabled()
+            && $this->canAccessObject('acl', $object)
+        ) {
             $list['acl'] = array(
                 'template' => 'SonataAdminBundle:Button:acl_button.html.twig',
             );
         }
 
-        if (in_array($action, array('edit', 'history', 'acl')) && $object) {
+        if (in_array($action, array('edit', 'history', 'acl'))
+            && $this->canAccessObject('show', $object)
+            && count($this->getShow()) > 0
+        ) {
             $list['show'] = array(
                 'template' => 'SonataAdminBundle:Button:show_button.html.twig',
             );
         }
 
-        if (in_array($action, array('show', 'edit', 'delete', 'acl', 'batch'))) {
+        if (in_array($action, array('show', 'edit', 'delete', 'acl', 'batch')) && $this->hasAccess('list')) {
             $list['list'] = array(
                 'template' => 'SonataAdminBundle:Button:list_button.html.twig',
             );
@@ -3212,4 +3218,17 @@ EOT;
             $extension->configureRoutes($this, $this->routes);
         }
     }
+
+    /**
+     * Check object existence and access, without throw Exception.
+     *
+     * @param string $action
+     * @param object $object
+     *
+     * @return bool
+     */
+    private function canAccessObject($action, $object)
+    {
+        return $object && $this->id($object) && $this->hasAccess($action, $object);
+    }
 }

+ 3 - 5
Resources/views/Button/acl_button.html.twig

@@ -9,8 +9,6 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.isAclEnabled() and admin.hasRoute('acl') and admin.id(object) and admin.isGranted('MASTER', object) %}
-    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('acl', object) }}">
-        <i class="fa fa-users"></i>
-        {{ 'link_action_acl'|trans({}, 'SonataAdminBundle') }}</a>
-{% endif %}
+<a class="sonata-action-element" href="{{ admin.generateObjectUrl('acl', object) }}">
+    <i class="fa fa-users"></i>
+    {{ 'link_action_acl'|trans({}, 'SonataAdminBundle') }}</a>

+ 15 - 17
Resources/views/Button/create_button.html.twig

@@ -9,21 +9,19 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.hasRoute('create') and admin.isGranted('CREATE') %}
-    {% if admin.subClasses is empty %}
-        <a class="sonata-action-element" href="{{ admin.generateUrl('create') }}">
-            <i class="fa fa-plus-circle"></i>
-            {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }}</a>
-    {% else %}
-        <li class="divider" role="presentation"></li>
-        {% for subclass in admin.subclasses|keys %}
-            <li>
-                <a href="{{ admin.generateUrl('create', {'subclass': subclass}) }}">
-                    <i class="fa fa-plus-circle"></i>
-                    {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }} {{ subclass|trans({}, admin.translationdomain) }}
-                </a>
-            </li>
-        {% endfor %}
-        <li class="divider" role="presentation"></li>
-    {% endif %}
+{% if admin.subClasses is empty %}
+    <a class="sonata-action-element" href="{{ admin.generateUrl('create') }}">
+        <i class="fa fa-plus-circle"></i>
+        {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }}</a>
+{% else %}
+    <li class="divider" role="presentation"></li>
+    {% for subclass in admin.subclasses|keys %}
+        <li>
+            <a href="{{ admin.generateUrl('create', {'subclass': subclass}) }}">
+                <i class="fa fa-plus-circle"></i>
+                {{ 'link_action_create'|trans({}, 'SonataAdminBundle') }} {{ subclass|trans({}, admin.translationdomain) }}
+            </a>
+        </li>
+    {% endfor %}
+    <li class="divider" role="presentation"></li>
 {% endif %}

+ 3 - 5
Resources/views/Button/edit_button.html.twig

@@ -9,8 +9,6 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.hasRoute('edit') and admin.id(object) and admin.isGranted('EDIT', object) %}
-    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('edit', object) }}">
-        <i class="fa fa-edit"></i>
-        {{ 'link_action_edit'|trans({}, 'SonataAdminBundle') }}</a>
-{% endif %}
+<a class="sonata-action-element" href="{{ admin.generateObjectUrl('edit', object) }}">
+    <i class="fa fa-edit"></i>
+    {{ 'link_action_edit'|trans({}, 'SonataAdminBundle') }}</a>

+ 3 - 5
Resources/views/Button/history_button.html.twig

@@ -9,8 +9,6 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.hasroute('history') and admin.id(object) and admin.isGranted('EDIT', object) %}
-    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('history', object) }}">
-        <i class="fa fa-archive"></i>
-        {{ 'link_action_history'|trans({}, 'SonataAdminBundle') }}</a>
-{% endif %}
+<a class="sonata-action-element" href="{{ admin.generateObjectUrl('history', object) }}">
+    <i class="fa fa-archive"></i>
+    {{ 'link_action_history'|trans({}, 'SonataAdminBundle') }}</a>

+ 3 - 5
Resources/views/Button/list_button.html.twig

@@ -9,8 +9,6 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.hasroute('list') and admin.isGranted('LIST') %}
-    <a class="sonata-action-element" href="{{ admin.generateUrl('list') }}">
-        <i class="fa fa-list"></i>
-        {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>
-{% endif %}
+<a class="sonata-action-element" href="{{ admin.generateUrl('list') }}">
+    <i class="fa fa-list"></i>
+    {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>

+ 3 - 5
Resources/views/Button/show_button.html.twig

@@ -9,8 +9,6 @@ file that was distributed with this source code.
 
 #}
 
-{% if admin.hasroute('show') and admin.id(object) and admin.isGranted('VIEW', object) and admin.show|length > 0 %}
-    <a class="sonata-action-element" href="{{ admin.generateObjectUrl('show', object) }}">
-        <i class="fa fa-eye"></i>
-        {{ 'link_action_show'|trans({}, 'SonataAdminBundle') }}</a>
-{% endif %}
+<a class="sonata-action-element" href="{{ admin.generateObjectUrl('show', object) }}">
+    <i class="fa fa-eye"></i>
+    {{ 'link_action_show'|trans({}, 'SonataAdminBundle') }}</a>

+ 2 - 2
Resources/views/CRUD/action.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}
     {% if action is defined %}

+ 2 - 2
Resources/views/CRUD/base_acl.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% import 'SonataAdminBundle:CRUD:base_acl_macro.html.twig' as acl %}
 

+ 2 - 2
Resources/views/CRUD/base_edit.html.twig

@@ -23,9 +23,9 @@ file that was distributed with this source code.
     {{ block('title') }}
 {% endblock %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': sonata_admin.adminPool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
 

+ 2 - 2
Resources/views/CRUD/base_history.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block content %}
 

+ 3 - 3
Resources/views/CRUD/base_list.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': sonata_admin.adminPool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
 
@@ -206,7 +206,7 @@ file that was distributed with this source code.
 {% endblock %}
 
 {% block list_filters_actions %}
-    {%- if admin.datagrid.filters|length %}
+    {%- if admin.datagrid.hasDisplayableFilters %}
         <ul class="nav navbar-nav navbar-right">
 
             <li class="dropdown sonata-actions">

+ 2 - 2
Resources/views/CRUD/base_show.html.twig

@@ -13,9 +13,9 @@ file that was distributed with this source code.
 
 {% import 'SonataAdminBundle:CRUD:base_show_macro.html.twig' as show_helper %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}
     {{ knp_menu_render(admin.sidemenu(action), {

+ 2 - 2
Resources/views/CRUD/batch_confirmation.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': sonata_admin.adminPool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
 

+ 2 - 2
Resources/views/CRUD/delete.html.twig

@@ -11,9 +11,9 @@ file that was distributed with this source code.
 
 {% extends base_template %}
 
-{% block actions %}
+{%- block actions -%}
     {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
-{% endblock %}
+{%- endblock -%}
 
 {% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': sonata_admin.adminPool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
 

+ 26 - 0
Tests/Admin/AdminTest.php

@@ -1643,9 +1643,35 @@ class AdminTest extends \PHPUnit_Framework_TestCase
 
         $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
 
+        $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
+        $securityHandler
+            ->expects($this->once())
+            ->method('isGranted')
+            ->with($admin, 'CREATE', $admin)
+            ->will($this->returnValue(true));
+        $admin->setSecurityHandler($securityHandler);
+
         $this->assertSame($expected, $admin->getActionButtons('list', null));
     }
 
+    /**
+     * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
+     */
+    public function testGetActionButtonsListCreateDisabled()
+    {
+        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
+
+        $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
+        $securityHandler
+            ->expects($this->once())
+            ->method('isGranted')
+            ->with($admin, 'CREATE', $admin)
+            ->will($this->returnValue(false));
+        $admin->setSecurityHandler($securityHandler);
+
+        $this->assertSame(array(), $admin->getActionButtons('list', null));
+    }
+
     /**
      * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
      */