Category: Yii 2

A few days ago I had to use Yii user permissions for a client website. I spent days looking here and there on google and trying several alternatives but no one worked for me easily. I tried several Yii extensions but I was not lucky to get any one working.
Finally I found Yii Rights extension but I could not any working instructions or a complete example. I decided to write this simple tutorial.

I used the following steps in order to use Yii Rights in my project.

1. Download the extension from its official site here. (http://www.yiiframework.com/extension/rights/). and extract the archive. Place the folder rights in protected/modules/ folder. If modules folder does not exist create it. The Yii application structure should look like the following:

yii_project
|
|----protected
|----models
|----modules
|----rights

2. Add the following lines to your application’s configuration. You would need to make the changes in three different places as shown below.

3. You must have/create a table User with the columns id and username. Now copy the following SQL and run it.
in your database manually (may be one by one table). It will create four tables in your DB.

4. Now you can run your Yii application with Yii Rights extension installed by typing the URL:
http://localhost/yiiapp/rights/ or http://localhost/yiiapp/index.php?r=rights/
where yiiapp is the name of your yii application. Here you may receive an error like:

Error 403. There must be at least one superuser!

Now to address this issue simply edit file /modules/rights/components/RAuthorizer.php and comment out the lines near line no 304 and 305.

//if( $superusers===array() )
// throw new CHttpException(403, Rights::t('core', 'There must be at least one superuser!'));

Now if you open the above link you will be on rights page. Now you can create roles, tasks and operations using the rights web interface.

5. The controllers must be extended from BController now instead of CController and the filters function inside each controller using Rights should look like the following.

public function filters()
{
return array(
'rights'
);
}

If you want any action to be ignored by Rigths simply add a minus in from of it in the filters function for example:

public function filters()
{
return array(
'rights -index'
);
}

Furthermore you can remove the accessRules function for all controllers using Rights as it is no more needed.

Some times you get squares on the web page although you have added the css file font-awesome.css successfully. The solution is very easy you and need to place the font directory (containing four font files) as well inside the CSS folder where you have placed font-awesome.css.