Sonata-project Admin/Controller中判断有无权限

1.权限配置

   打开app/config/sonata/sonata_admin.yml文件:

dashboard:
        ......
        groups:
            common.user:
                label: 后台用户管理
                label_catalogue: JyAdminBundle
                roles: [ ROLE_COMMON, ROLE_MOBILE_PMA ]

   注:roles的意思是拥有ROLE_COMMON或者ROLE_MOBILE_PMA权限的用户都可以访问

2.Controller/Admin中判断是否有权限

$this->container->get('security.context')->isGranted('ROLE_MOBILE')

3.模板中判断有无权限


{% if app.security.token and is_granted('ROLE_SONATA_ADMIN') %}

4.本地Layout 模板备份(可跳过)

三级菜单:

{% extends 'SonataAdminBundle::standard_layout.html.twig' %}
{% block side_bar_before_nav %}
{% if app.session.get('old_token') %}
<div style="text-align: center;">
    <a href="">切换</a>
</div>
{% endif %}
{% endblock %}
{% block side_bar_nav %}
    {% if app.security.token and is_granted('ROLE_SONATA_ADMIN') %}
        <ul class="sidebar-menu">
            {% if is_granted('ROLE_COMMON') %}
            <li class="treeview top-treeview">
                <a href="#"><span>公共</span><i class="fa pull-right fa-angle-left"></i></a>
                <ul class="treeview-menu sidebar-menu">
                    {% for group_name, group in admin_pool.dashboardgroups if group_name|split('.')|first == 'common' %}
                        {% set display = (group.roles is empty or is_granted('ROLE_SUPER_ADMIN') ) %}
                        {% for role in group.roles if not display %}
                            {% set display = is_granted(role) %}
                        {% endfor %}
    
                        {# Do not display the group label if no item in group is available #}
                        {% set item_count = 0 %}
                        {% if display %}
                            {% for admin in group.items if item_count == 0 %}
                                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                    {% set item_count = item_count+1 %}
                                {% endif %}
                            {% endfor %}
                        {% endif %}
    
                        {#{% if display and (item_count > 0) %}#}
                            {% set active = false %}
                            {% for admin in group.items %}
                                {% set end_code = admin.code|split('.')|last %}
                                {% if admin.hasroute('list') and admin.isGranted('LIST') and app.request.get('_sonata_admin') == admin.code %}
                                    {% set active = true %}
                                {% elseif admin.hasroute(end_code) and app.request.get('_sonata_admin') == admin.code %}
                                    {% set active = true %}
                                {% endif %}
                            {% endfor %}
                            <li class="treeview{% if active %} active{% endif %}">
                                <a href="#">
                                    {% if group.icon|default() %}{{ group.icon|raw }}{% endif %}
                                    <span>{{ group.label|trans({}, group.label_catalogue) }}</span>
                                    <i class="fa pull-right fa-angle-left"></i>
                                </a>
                                <ul class="treeview-menu{% if active %} active{% endif %}">
                                    {% for admin in group.items %}
                                        {% set end_code = admin.code|split('.')|last %}
                                        {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                        <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl('list')}}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                                        {% elseif admin.hasroute(end_code) %}
                                            <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl(end_code) }}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                                        {% endif %}
                                    {% endfor %}
                                </ul>
                            </li>
                            {#{% endif %}#}
                    {% endfor %}
                </ul>
            </li>
            {% endif %}
             {% if is_granted('ROLE_MOBILE') %}
            <li class="treeview top-treeview">
                <a href="#"><span>MOBILE</span><i class="fa pull-right fa-angle-left"></i></a>
                <ul class="treeview-menu sidebar-menu">
                    {% for group_name, group in admin_pool.dashboardgroups if group_name|split('.')|first == 'mobile' and is_granted(group.roles) %}
                        {% set display = (group.roles is empty or is_granted('ROLE_SUPER_ADMIN') ) %}
                        {% for role in group.roles if not display %}
                            {% set display = is_granted(role) %}
                        {% endfor %}
    
                        {# Do not display the group label if no item in group is available #}
                        {% set item_count = 0 %}
                        {% if display %}
                            {% for admin in group.items if item_count == 0 %}
                                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                    {% set item_count = item_count+1 %}
                                {% endif %}
                            {% endfor %}
                        {% endif %}
    
                        {#{% if display and (item_count > 0) %}#}
                            {% set active = false %}
                            {% for admin in group.items %}
                                {% set end_code = admin.code|split('.')|last %}
                                {% if admin.hasroute('list') and admin.isGranted('LIST') and app.request.get('_sonata_admin') == admin.code %}
                                    {% set active = true %}
                                {% elseif admin.hasroute(end_code) and app.request.get('_sonata_admin') == admin.code %}
                                    {% set active = true %}
                                {% endif %}
                            {% endfor %}
                            <li class="treeview{% if active %} active{% endif %}">
                                <a href="#">
                                    {% if group.icon|default() %}{{ group.icon|raw }}{% endif %}
                                    <span>{{ group.label|trans({}, group.label_catalogue) }}</span>
                                    <i class="fa pull-right fa-angle-left"></i>
                                </a>
                                <ul class="treeview-menu{% if active %} active{% endif %}">
                                    {% for admin in group.items %}
                                        {% set end_code = admin.code|split('.')|last %}
                                        {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                        <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl('list')}}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                                        {% elseif admin.hasroute(end_code) %}
                                            <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl(end_code) }}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                                        {% endif %}
                                    {% endfor %}
                                </ul>
                            </li>
                            {#{% endif %}#}
                    {% endfor %}
                </ul>
            </li>
            {% endif %}
        </ul>
    {% endif %}
{% endblock side_bar_nav %}
    
{% block side_bar_after_nav %}
{% endblock %}
    
{% block js_includes %}
    {{ parent() }}
    <script src="{{ asset('js/bootstrap-datepicker.js') }}"></script>
    <script src="{{ asset('js/sonata-datepicker-filter.js') }}"></script>
    
{% endblock %}
{% block css_includes %}
    {{ parent() }}
    
    <link rel="stylesheet" href="{{ asset('css/sonataadmin/css/layout.css') }}" type="text/css" media="all" />
    <link rel='stylesheet' type="text/css" href="{{ asset('css/datepicker.css')}}">
{% endblock %}

二级菜单:

{% extends 'SonataAdminBundle::standard_layout.html.twig' %}
{% block side_bar_before_nav %}
{% if app.session.get('old_token') %}
<div style="text-align: center;">
    <a href="">切换</a>
</div>
{% endif %}
{% endblock %}
  
{% block sonata_sidebar_search %}
{% endblock sonata_sidebar_search %}
{% block side_bar_nav %}
    <script>
   $(document).ready(function(){
        {% if admin is defined %}
        $(".content").css("width","{{ admin.getListWidth()  }}%");
        {% endif %}
        $(".sidebar-offcanvas").css("z-index","1000");      
   });
</script>
  {#% if app.security.token and is_granted('ROLE_SONATA_ADMIN') %#}
  <ul class="sidebar-menu">
    {% for group in admin_pool.dashboardgroups %}
          
        {% set display = (group.roles is empty or is_granted('ROLE_SUPER_ADMIN')) %}
        {% for role in group.roles if not display %}
            {% set display = is_granted(role) %}
        {% endfor %}
      
        {# Do not display the group label if no item in group is available #}
        {% set item_count = 0 %}
        {% if display %}
            {% for admin in group.items if item_count == 0 %}
                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                    {% set item_count = item_count+1 %}
                {% endif %}
            {% endfor %}
        {% endif %}
  
        {% set code = group.roles[0] | upper %}
        {% if is_granted('ROLE_MOBILE_PMA') or is_granted(code)%}
            {% set active = false %}
            {% for admin in group.items %}
                {% set end_code = admin.code|split('.')|last %}
                {% if admin.hasroute('list') and admin.isGranted('LIST') and app.request.get('_sonata_admin') == admin.code %}
                    {% set active = true %}
                {% elseif admin.hasroute(end_code) and app.request.get('_sonata_admin') == admin.code %}
                    {% set active = true %}
                {% endif %}
            {% endfor %}
          <li class="treeview{% if active %} active{% endif %}">
            <a href="#">
                {% if group.icon|default() %}{{ group.icon|raw }}{% endif %}
                <span>{{ group.label|trans({}, group.label_catalogue) }}</span>
                <i class="fa pull-right fa-angle-left"></i>
            </a>
            <ul class="treeview-menu{% if active %} active{% endif %}">
            {% for admin in group.items %}
                {% set end_code = admin.code|split('.')|last %}
                {% if admin.checkRoles() %}
                    {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                        <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl('list')}}"><i class="fa fa-angle-double-right"></i> {% if admin.role is defined%}{{ admin.role }}{% endif %}{{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                    {% elseif admin.hasroute(end_code) %}
                        <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl(end_code) }}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
                    {% endif %}
                {% endif %}
            {% endfor %}
            </ul>
          </li>
        {% endif %}
      {% endfor %}
      </ul>
    {#% endif %#}
{% endblock side_bar_nav %}
  
{% block sonata_admin_content %}
  
    {% block notice %}
        {% include 'SonataCoreBundle:FlashMessage:render.html.twig' %}
    {% endblock notice %}
  
    {% if _preview is not empty %}
        <div class="sonata-ba-preview">{{ _preview|raw }}</div>
    {% endif %}
  
    {% if _content is not empty %}
        <div class="sonata-ba-content">{{ _content|raw }}</div>
    {% endif %}
  
    {% if _show is not empty %}
        <div class="sonata-ba-show">{{ _show|raw }}</div>
    {% endif %}
  
    {% if _form is not empty %}
        <div class="sonata-ba-form">{{ _form|raw }}</div>
    {% endif %}
  
    {% if _list_table is not empty or _list_filters is not empty %}
        <div class="row">
            <div class="sonata-ba-list col-md-12">
                {{ _list_table|raw }}
            </div>
  
             
        </div>
    {% endif %}
{% endblock sonata_admin_content %}
  
  
{% block sonata_page_content_nav %}
  
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">高级搜索</h4>
          </div>
          <div class="modal-body">
             {% if _list_filters|trim %}
                    <div class="sonata-ba-filter col-md-12">
                        {{ _list_filters|raw }}
                    </div>
            {% endif %}
            <div style="clear:both;"></div>
          </div>
        </div>
      </div>
    </div>
   
    {% if _tab_menu is not empty or _actions is not empty %}
        <nav class="navbar navbar-default" role="navigation">
            {% block tab_menu_navbar_header %}
                {% if _navbar_title is not empty %}
                    <div class="navbar-header">
                        <span class="navbar-brand">{{ _navbar_title|raw }}</span>
                    </div>
                {% endif %}
            {% endblock %}
            <div class="container-fluid">
                <div class="navbar-left">
                    {% if _tab_menu is not empty %}
                        {{ _tab_menu|raw }}
                    {% endif %}
                </div>
                {% if _list_filters|trim %}
                <div class="navbar-left pull-right">
                <ul class="nav navbar-nav">
                    <li>               
                     <span type="button" class="btn btn-sm btn-primary " data-toggle="modal" data-target="#myModal" id="highsearch">高级搜索</span>   
                    </li>
                </ul>
                </div>
                {% endif %}
                {#   link_actions
                {% if _actions|replace({ '<li>': '', '</li>': '' })|trim is not empty %}
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown sonata-actions">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ 'link_actions'|trans({}, 'SonataAdminBundle') }} <b class="caret"></b></a>
                            <ul class="dropdown-menu" role="menu">
                                {{ _actions|raw }}
                            </ul>
                        </li>
                    </ul>
                {% endif %}
                #}
            </div>
        </nav>
    {% endif %}
{% endblock sonata_page_content_nav %}
  
  
  
{% block side_bar_after_nav %}
{% endblock %}
  
{% block js_includes %}
    {{ parent() }}
    <script src="{{ asset('js/bootstrap-datepicker.js') }}"></script>
    <script src="{{ asset('js/sonata-datepicker-filter.js') }}"></script>
  
{% endblock %}
{% block css_includes %}
    {{ parent() }}
  
    <link rel="stylesheet" href="{{ asset('css/sonataadmin/css/layout.css') }}" type="text/css" media="all" />
    <link rel='stylesheet' type="text/css" href="{{ asset('css/datepicker.css')}}">
{% endblock %}
  
{% block sonata_wrapper %}
{{parent()}}
    <style>
html, body {
    overflow-x: visible!important;
}
#highsearch{
    margin-top:10px;
}
</style>
{% endblock sonata_wrapper %}


by 雪洁 2015-01-06 08:51:11 3179 views
我来说几句

相关文章