Yii Framework Forum: Extending UserIdentity - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Extending UserIdentity Rate Topic: -----

#1 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 09-February 10

Posted 13 February 2010 - 07:07 PM

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
Thanks and Kind Regards
------
DanDinDun
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 13 February 2010 - 07:22 PM

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

#3 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 09-February 10

Posted 13 February 2010 - 08:09 PM

View PostY!!, on 13 February 2010 - 07:22 PM, said:

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
Thanks and Kind Regards
------
DanDinDun
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 13 February 2010 - 08:39 PM

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

#5 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 09-February 10

Posted 14 February 2010 - 12:58 AM

View PostY!!, on 13 February 2010 - 08:39 PM, said:

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.
Thanks and Kind Regards
------
DanDinDun
0

#6 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 747
  • Joined: 02-February 09

Posted 14 February 2010 - 04:20 AM

View PostDan Din Dun, on 13 February 2010 - 08:09 PM, said:

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



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
0

#7 User is offline   joeysantiago 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 01-April 10

Posted 08 April 2010 - 03:27 AM

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
joey santiago

an Italian with Suomi inside.
0

#8 User is offline   seb7 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 03-April 12

Posted 12 April 2012 - 04:46 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users