浏览代码

move actions buttons display logic to AbstractAdmin

Fabien Bourigault 8 年之前
父节点
当前提交
ee9b434879

+ 25 - 6
Admin/AbstractAdmin.php

@@ -2782,37 +2782,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',
             );
@@ -3197,4 +3203,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>

+ 8 - 0
Tests/Admin/AdminTest.php

@@ -1643,6 +1643,14 @@ 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));
     }