浏览代码

add support for roles with dashboard's blocks

Thomas Rabaix 10 年之前
父节点
当前提交
b8cc89f3ed

+ 10 - 1
DependencyInjection/Configuration.php

@@ -115,11 +115,20 @@ class Configuration implements ConfigurationInterface
                             ->end()
                             ->end()
                         ->end()
                         ->end()
                         ->arrayNode('blocks')
                         ->arrayNode('blocks')
-                            ->defaultValue(array(array('position' => 'left', 'settings' => array(), 'type' => 'sonata.admin.block.admin_list')))
+                            ->defaultValue(array(array(
+                                'position' => 'left',
+                                'settings' => array(),
+                                'type'     => 'sonata.admin.block.admin_list',
+                                'roles'    => array()
+                            )))
                             ->prototype('array')
                             ->prototype('array')
                                 ->fixXmlConfig('setting')
                                 ->fixXmlConfig('setting')
                                 ->children()
                                 ->children()
                                     ->scalarNode('type')->cannotBeEmpty()->end()
                                     ->scalarNode('type')->cannotBeEmpty()->end()
+                                    ->arrayNode('roles')
+                                        ->defaultValue(array())
+                                        ->prototype('scalar')->end()
+                                    ->end()
                                     ->arrayNode('settings')
                                     ->arrayNode('settings')
                                         ->useAttributeAsKey('id')
                                         ->useAttributeAsKey('id')
                                         ->prototype('variable')->defaultValue(array())->end()
                                         ->prototype('variable')->defaultValue(array())->end()

+ 6 - 2
Resources/doc/reference/dashboard.rst

@@ -33,8 +33,8 @@ The Dashboard is actually built using ``Blocks`` from ``SonataBlockBundle``. You
 can learn more about this bundle and how to build your own Blocks on the
 can learn more about this bundle and how to build your own Blocks on the
 `SonataBlock documentation page`_.
 `SonataBlock documentation page`_.
 
 
-The ``Admin`` list
-------------------
+The ``Admin`` list block
+------------------------
 
 
 The ``Admin`` list is a ``Block`` that fetches information from the ``Admin`` service's
 The ``Admin`` list is a ``Block`` that fetches information from the ``Admin`` service's
 ``Pool`` and prints it in the nicely formated list you have on your default Dashboard.
 ``Pool`` and prints it in the nicely formated list you have on your default Dashboard.
@@ -219,6 +219,7 @@ a text block and RSS feed block on the right. The configuration for this scenari
                     -
                     -
                         position: right
                         position: right
                         type: sonata.block.service.rss
                         type: sonata.block.service.rss
+                        roles: [POST_READER]
                         settings:
                         settings:
                             title: Sonata Project's Feeds
                             title: Sonata Project's Feeds
                             url: http://sonata-project.org/blog/archive.rss
                             url: http://sonata-project.org/blog/archive.rss
@@ -230,6 +231,9 @@ a text block and RSS feed block on the right. The configuration for this scenari
     work properly. Refer to the associated documentation/implementation to
     work properly. Refer to the associated documentation/implementation to
     get more information on each block's options and requirements.
     get more information on each block's options and requirements.
 
 
+    You can also configure the ``roles`` section to configure users that can
+    view the block.
+
 Display two ``Admin`` list blocks with different dashboard groups
 Display two ``Admin`` list blocks with different dashboard groups
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 

+ 15 - 7
Resources/views/Core/dashboard.html.twig

@@ -21,7 +21,9 @@ file that was distributed with this source code.
         <div class="row">
         <div class="row">
             {% for block in blocks.top %}
             {% for block in blocks.top %}
                 <div class="{{ block.class }}">
                 <div class="{{ block.class }}">
-                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% if block.roles|length == 0 or is_granted(block.roles) %}
+                        {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% endif %}
                 </div>
                 </div>
             {% endfor %}
             {% endfor %}
         </div>
         </div>
@@ -32,21 +34,27 @@ file that was distributed with this source code.
 
 
         <div class="{% if blocks.center|length > 0 %}col-md-4{% else %}col-md-6{% endif %}">
         <div class="{% if blocks.center|length > 0 %}col-md-4{% else %}col-md-6{% endif %}">
             {% for block in blocks.left %}
             {% for block in blocks.left %}
-                {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% if block.roles|length == 0 or is_granted(block.roles) %}
+                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% endif %}
             {% endfor %}
             {% endfor %}
         </div>
         </div>
 
 
         {% if blocks.center|length > 0 %}
         {% if blocks.center|length > 0 %}
             <div class="col-md-4">
             <div class="col-md-4">
                 {% for block in blocks.center %}
                 {% for block in blocks.center %}
-                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% if block.roles|length == 0 or is_granted(block.roles) %}
+                        {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% endif %}
                 {% endfor %}
                 {% endfor %}
             </div>
             </div>
         {% endif %}
         {% endif %}
 
 
         <div class="{% if blocks.center|length > 0 %}col-md-4{% else %}col-md-6{% endif %}">
         <div class="{% if blocks.center|length > 0 %}col-md-4{% else %}col-md-6{% endif %}">
             {% for block in blocks.right %}
             {% for block in blocks.right %}
-                {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% if block.roles|length == 0 or is_granted(block.roles) %}
+                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% endif %}
             {% endfor %}
             {% endfor %}
         </div>
         </div>
     </div>
     </div>
@@ -55,14 +63,14 @@ file that was distributed with this source code.
         <div class="row">
         <div class="row">
             {% for block in blocks.bottom %}
             {% for block in blocks.bottom %}
                 <div class="{{ block.class }}">
                 <div class="{{ block.class }}">
-                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% if block.roles|length == 0 or is_granted(block.roles) %}
+                        {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                    {% endif %}
                 </div>
                 </div>
             {% endfor %}
             {% endfor %}
         </div>
         </div>
     {% endif %}
     {% endif %}
 
 
-
-
     {{ sonata_block_render_event('sonata.admin.dashboard.bottom', { 'admin_pool': admin_pool }) }}
     {{ sonata_block_render_event('sonata.admin.dashboard.bottom', { 'admin_pool': admin_pool }) }}
 
 
 {% endblock %}
 {% endblock %}

+ 23 - 0
Tests/DependencyInjection/ConfigurationTest.php

@@ -91,4 +91,27 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
             )
             )
         ), $config['admin_services']['my_admin_id']);
         ), $config['admin_services']['my_admin_id']);
     }
     }
+
+    public function testDashboardWithoutRoles()
+    {
+        $processor = new Processor();
+        $config = $processor->processConfiguration(new Configuration(), array());
+
+        $this->assertEmpty($config['dashboard']["blocks"][0]["roles"]);
+    }
+
+    public function testDashboardWithRoles()
+    {
+        $processor = new Processor();
+        $config = $processor->processConfiguration(new Configuration(), array(array(
+            "dashboard" => array(
+                "blocks" => array(array(
+                    "roles" => array("ROLE_ADMIN"),
+                    "type"  => "my.type"
+                ))
+            )
+        )));
+
+        $this->assertEquals($config['dashboard']["blocks"][0]["roles"], array("ROLE_ADMIN"));
+    }
 }
 }