Using YiiSmartMenu with Rights Extension

Yii Smart can be used out of the box (require only a few steps) with Yii Rights Extension. The purpose of the Yii Smart menu is to show those menu items to the user for which the logged in user has access to. Yii smart menu can work with Rights Extension, RBAC etc. In order to install Yii Rights you can read in my previous post.

Follow the following steps to use YiiSmartMenu widget.

1. Download the widget class from here  http://www.yiiframework.com/extension/yiismartmenu/ and copy it to your components folder i.e yii_app/protected/components.

2. In order to change top menu open yii_app/protected/views/layouts/main.php and replace echo occurrence of  $this->widget('zii.widgets.CMenu', ... TO

$this->widget('application.components.YiiSmartMenu', array(
       'partItemSeparator'=>'.',
       'upperCaseFirstLetter'=>true,

The partItemSeparator concatenates ControllerID to ActionID e.g. Site and Index would be concatenated as Site.Index. Rights use authitem in this format and so ‘.’ is compatible with Rights.
Similarly upperCaseFirstLetter makes ControllerID and ActionID uppercase like in Site.Index, again its compatible with Rights Extension. ( You can verify this in AuthItem table of Rights extension that each operation is formed as ControllerID.ActionID).

if you want to check access to an individual action in your code use:

     Yii::app()->user->checkAccess("Site.Index", $_GET)';

The above code will return true if the current logged in user has access to site/index action.
If you want any menu item should be not be checked by YiiSmartMenu and hence should be visible always e.g. a Home link you can add visible property of the menu item like:

    'items'=>array(
         array('label'=>'Home', 'url'=>array('/site/index'), 'visible' => true),

Please note that any top menu item for which permissions is in the form of ControllerID.* in the Rights extension does not work, it may be a bug in this extension. In order to sort this out simply grant permissions like ControllerID.ActionID to the top menu item in the Rights extension.

🙂

Leave a Comment

Your email address will not be published. Required fields are marked *