Getting An Error When Yii Tries To Read The User Role

Hello everybody!

I’am a new in Yii and I’m trying to do a RBAC control in my web application. I write an authorization and registration pages and everything working perfect. But when I trying to read current user’s role using Yii::app()->user->role I’m getting an error with the following content:


PHP warning

include(User.php): failed to open stream: No such file or directory

I did everything like in official cookbook. Honestly, I don’t know why this happens that’s why if someone could help me to solve this problem I will be very appreciated.

Here I wrote the steps which I did when writing the role based user control.

As mentioned in official Yii cookbook I created a simple database table named users where I defined the role attribute. Then I write a WebUser class:




class WebUser extends CWebUser {

    private $_model = null;

 

    function getRole() {

        if($user = $this->getModel()){

            return $user->role;

        }

    }

 

    private function getModel(){

        if (!$this->isGuest && $this->_model === null){

            $this->_model = User::model()->findByPk($this->id, array('select' => 'role'));

        }

        return $this->_model;

    }

}



Next step I changed the default realization of the UserIdentity::authentificate() method, where I trying to assign role to the current user:


public function authenticate()

	{

		$users = Users::model()->find('LOWER(name)=?', array(strtolower($this->name)));

		if($users === null)

			$this->errorCode = self::ERROR_USERNAME_INVALID;

		else if ($users->validatePassword(md5($this->password))) 

			$this->errorCode = self::ERROR_PASSWORD_INVALID;


		else

		{

			$this->_id = $users->id;

			$this->username = $users->name;


			$auth=Yii::app()->authManager;


			echo "user role = ".$users->role.", user id = ".$this->_id;

        	if(!$auth->isAssigned($users->role,$this->_id))

        	{

            	if($auth->assign($users->role,$this->_id))

            	{

                	Yii::app()->authManager->save();

            	}

        	}

        	

        	$this->errorCode=self::ERROR_NONE;

		}

		return $this->errorCode == self::ERROR_NONE;

	}

Finally, I declare all this components in the main web config file:




...

'import'=>array(

		'application.models.*',

		'application.components.*',

	),

...


'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'class'=>'WebUser',

			'allowAutoLogin'=>true,

		),


		'authManager' => array(

			'class' => 'PhpAuthManager',

			'defaultRoles' => array('guest'),

		),

...



Looks closely at the error message, it says it can’t find a User model. Maybe you call your model Users - in plural? You’d need to adjust the getModel() method of your WebUser class.

Also, make sure that your configuration import array designates the path to this User.php file.

This is particularly true if you started without code generation or have customized things.

Thank you! You was right this error was occurred because I made a mistake in WebUser::getModel() method. I should call my model Users (in pluralize form).

Thanks all for reply))