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

  1. Requirements
  2. Installation
  3. Usage
  4. TODO

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.

6 0
6 followers
375 downloads
Yii Version: Unknown
License: GPL-3.0
Category: Auth
Developed by: Nabi
Created on: Feb 22, 2014
Last updated: 4 years ago

Downloads

show all

Related Extensions