checkAccess failure

Hi,

I’ve honestly exhausted a search on a possible explanation for this before posting, so please your help will be appreciated.

I’m trying something as simple as using checkAccess to see if user is admin however I’m not getting any results…I’m stumped. I’m accessing it in a view file, where the objective is to show a specific link for admins (and in the future, I will have respective links of other users/user groups).

My question is why isn’t " Yii::app()->user->checkAccess(‘admin’) " working when the user is logged in as admin?

am I lacking the required braincells to figure this out lol please help?




<?php if (Yii::app()->user->checkAccess('admin')) {

    $this->widget('zii.widgets.CMenu', array(

        'items' => $this->menu,

        'htmlOptions' => array('class' => 'operations'),

    ));

} else { echo "<small>Please login</small>";}

    ?>



When using Yii::app()->user->checkAccess(‘admin’) like this, you have to implement all the Role-Based Access Control. See Topics Auth

Otherwise you have to use:




  if (Yii::app()->user->id == 'admin') 

    ...




check this wiki I wrote a couple days ago

Thanks for the quick replies.

Joblo: your suggestion worked perfectly, and in the mean time I will learn more about RBAC

Gustave : thanks, very useful functions :)

Cheers…

Hello Joblo,

In my AuthItem table i have 3 enteries

1: name->admin, type->2, description->admin, bizrull->null, data->N;

2: name->authotized, type->2, description->logedin, bizrull->return !Yii::app()->user->isGuest;, data->N;

3: name->guset, type->2, description->guest, bizrull->return Yii::app()->user->isGuest;, data->N;

In AuthAssignment i have one entry:

1: useritem:admin,userid=22,bizrull=null,data=N;

Below is the UserIdentity code:

public function authenticate()

{


	&#036;record=User::model()-&gt;findByAttributes(array('username'=&gt;&#036;this-&gt;username));


	if(&#036;record===null)


		&#036;this-&gt;errorCode=self::ERROR_USERNAME_INVALID;


	else if(md5(&#036;record-&gt;password)&#33;==md5(&#036;this-&gt;password))


		&#036;this-&gt;errorCode=self::ERROR_PASSWORD_INVALID;


	else


	{


		&#036;this-&gt;_id=&#036;record-&gt;id;


		&#036;this-&gt;errorCode=self::ERROR_NONE;


	}


	return &#33;&#036;this-&gt;errorCode;


}

Below is the code in userController:

public function accessRules()

{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('update'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('create','admin','delete'),


			'users'=&gt;array('admin'),


		),


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}

Login works perfectly… access works correctly

I wants to hide create and admin link from all other users. I am trying below code and it is not working:

//if(Yii::app()->user->id==‘admin’){ //works well

if(Yii::app()->user->checkAccess(‘admin’)){//dont work

&#036;this-&gt;menu=array(


	array('label'=&gt;'Create User', 'url'=&gt;array('create')),


	array('label'=&gt;'Manage User', 'url'=&gt;array('admin')),


);

}

Pl help what i an doing wrong