Search code examples
phpyiiyii-extensionsrights

Yii Rights set sub menu visible false based on Role


I am new to Yii Framework. In my application iam used Rights using this link : http://www.yiiframework.com/wiki/423/installing-yii-users-and-rights-to-newly-created-yii-app/

I want to make navigation bar submenu item visible based on user's role in yii rights.

my navigation sub menu item looks like this :

array(
    'label'=>'Low Inventory Level <span class="caret"></span>',
    'url'=>'#',
    'itemOptions'=>array('class'=>'dropdown','tabindex'=>"-1"),
    'linkOptions'=>array('class'=>'dropdown-oggle','data-toggle'=>"dropdown"),
    'items'=>array(
        array('label'=>'RM Warehouse', 'url'=>array('/rmlInventoryMwh/load')),
        array('label'=>'Prod Warehouse', 'url'=>array('/rmlInventoryPwh/load ')),
    ),
    'visible' => Yii::app()->user->checkAccess('master'),
)

Solution

  • Just add checkAccess to your sub menu:

        array(
        'label'=>'Low Inventory Level <span class="caret"></span>',
        'url'=>'#',
        'itemOptions'=>array('class'=>'dropdown','tabindex'=>"-1"),
        'linkOptions'=>array('class'=>'dropdown-oggle','data-toggle'=>"dropdown"),
        'items'=>array(
            array('label'=>'RM Warehouse', 'url'=>array('/rmlInventoryMwh/load'),'visible' => Yii::app()->user->checkAccess('sub_menu')),
            array('label'=>'Prod Warehouse', 'url'=>array('/rmlInventoryPwh/load '),'visible' => Yii::app()->user->checkAccess('sub_menu')),
        ),
        'visible' => Yii::app()->user->checkAccess('master'),
    )
    

    And then just add permissions to your operation for user roles.