auth-component

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

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.

Requirements

Yii 1.1 or above.

Installation

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

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

'components'=>array(
    ...
    'auth'=>array(
        'class'=>'application.components.AuthComponent',
    ),
    ...
),

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

'auth'=>array(
        '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
    ),

Usage

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
Yii::app()->auth->setUserId(2);
Yii::app()->auth->run();
 
//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
Yii::app()->auth->setUserId(null);
Yii::app()->auth->run();

TODO

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=Yii::app()->authManager;
$auth->assign('post', 1);

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

Yii::app()->user->checkAccess('post');

And just use this:

Yii::app()->auth->checkAccess('post');
#16660 report it
Clem at 2014/03/17 03:37am
Cool...

...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