Extending UserIdentity

Because I wanna to access role type. I add some function in UserIdentity


<?php




class UserIdentity extends CUserIdentity

{

	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the username and password

	 * are both 'demo'.

	 * In practical applications, this should be changed to authenticate

	 * against some persistent user identity storage (e.g. database).

	 * @return boolean whether authentication succeeds.

	 */

        private $_id;

        private $_roleId;

        

	public function authenticate()

	{

		

                $user = Users::model()->findByAttributes(array('username' => $this->username) );                

                if($user->username !== $this->username){

                    $this->errorCode = self::ERROR_USERNAME_INVALID;

                }elseif($user->password !== $this->hash($this->password)){

                    $this->errorCode = self::ERROR_PASSWORD_INVALID;

                }else{

                    $this->errorCode = self::ERROR_NONE;

                    $this->_id = $user->user_id;

                    $this->roleId = $user->user_level_id;

                }

		return !$this->errorCode;

	}


        public function hash($string){

            return sha1($string);

        }


        public function getId(){

            return $this->_id;

        }


        public function getRoleType(){

            return $this->_roleId;

        }

        

}

I add getRoleType() function. but when I try to access it using

Yii::app()->user->getRoleType() it was failed

how I can extend into UserIdentity component?

thanks

The user component is a class of CWebUser and not CUserIdentity.

Ooo… yes sir… so how I can access Yii::app()->user->getRoleLevel(); to get role of user (administrator or guest)

I use role to determine the ACL.

I just want to access role user within Yii::app()->user->getRoleLevel();.

can you help me? :huh:

thanks

As I said you have to extend CWebUser then.


class MyWebUser extends CWebUser

{


   public function getRoleType()

   {

      ...

   }


}

In config you have to define it of course:


'components' => array(

   ...

   'user' => array(

      'class' => 'MyWebUser',

   ),

   ...

),

See here for more info about CWebUser. There you can see how authentication process between UserIdentity and CWebUser looks like. Should give you an idea how to implement the things you need.

oh thank you sir, you often help me.

Depending on how you have setup your RBAC. I find very handy the following approach:


Yii::app()->authManager->getRoles(Yii::app()->user->id);

…this returns me all roles assigned to my current logged in user

hope this helps

bettor

hello!

i find this post very interesting, thanks guys… but i have a question. i modified my config/main.php adding:




'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

                        'class'=>'MyWebUser',

		),



then i created components/MyWebUser.php like this:




class MyWebUser extends CWebUser

{

        private $_id;

        private $_level;

        public function isLoser(){

            return $this->_level;

        }


}



then i’d like to use isLoser in a controller in order to filter results in actionIndex. If i put there:




var_dump(Yii::app()->authManager->isLoser(Yii::app()->user->id));



the result is "CPhpAuthManager does not have a method named "isLoser"", and if i use:


var_dump(Yii::app()->user);

i get correctly a MyWebUser object, which is great… but with _id and _level set to null! :( so it looks like MyWebUser->_level is never set, even though i add something like:




public function login()

        {

            $this->_level="MYLEVEL";

        }



in MyWebUser class. How should i set it?

please, help :)

WOW, got it!


class MyWebUser extends CWebUser

{

        public function getLevel($name){

            $user=BoUsers::model()->find('LOWER(emailAddress)=?',array($name));

            return $user->powerUser;

        }


}

is the right way of doing it! :)… i think :D

So

Am i right if i say :

CuserIdentity is to use for user authentication, then pass it to Yii::app()->login(). And that’s all.

Usualy, nothing more than the authenticate() method has to be modified.

CWebUser is used in the whole web application.

It’s usualy that class that has to be overriden.