Yii Auth Component is for check multi access with single query.

If you are using Yii::app()->user->checkAccess('post'); for check access. You must know it execute one query db be like this SELECT * FROM auth_item WHERE name='post', Now If you want to check another access use other same command and so that execute another query and more... Now, for example if you have many link in the page that you need show anyone with specific access, you must use many if(Yii::app()->user->checkAccess... so in your page a lot of many query executed that you know it's not proper.
And so I research for check multi access one time so that execute just single query db. But I don't found any way! (+, +).
So, I write this components for resolve this issue.

Yii Auth Component is for check multi access with single query.

Note: This component didn't checked bizRule now.


Yii 1.1 or above.


Copy the AuthComponent.php file to your protected/components folder.

And put the following code in protected/config/main.php


And also you can set another parameters in auth array if you need:

        'cacheDuration'=>60*60*24*30, //default: false
        'connectionID'=>'db', //default: db
        'itemTable' => 'auth_item', //default: AuthItem
        'itemChildTable' => 'auth_item_child', //default: AuthItemChild
        'assignmentTable' => 'auth_assignment', //default: AuthAssignment


You can check access for logged in user simply be like this:

//this code just execute single query (not two query)
if (Yii::app()->auth->checkAccess('post') || Yii::app()->auth->checkAccess('comment')) {
    //is allowed
} else {
    //is not allowed.

If you want to see all access for user as an array, use this code:

var_dump( Yii::app()->auth->getAllAccess() );

For check specific user you can use this simple code:

//set `2` for userId
//check `post` access for `2` userId
if (Yii::app()->auth->checkAccess('post')) {
    //is allowed
} else {
    //is not allowed.
//set back default value of logged in user


Must modify code for check bizRule.

Total 3 comments

#16690 report it
Nabi at 2014/03/19 05:09pm
RE: clem

@Clem , this extension is independent, that's mean don't extended from base authorizations yii system and authManager component. This extension just use of your auth tables for execute single query for check access. So, for doing anything (assign, revoke, createRole, addItemChild,...) you can use Yii::app()->authManager , for example for assign authorizations to an user you can use be like this:

$auth->assign('post', 1);

And just when you can need to check access user, don't use this:


And just use this:

#16660 report it
Clem at 2014/03/17 03:37am

...but now, how can we assign authorizations to an user?

#16437 report it
javad at 2014/02/22 03:35pm
best extension

this is best extension. thanks Mr Nabi.

Leave a comment

Please to leave your comment.

Create extension