How to get a hold of logged in user model

Hi,

Just starting out on Yii so having some issues in getting my head around things. I want to be able to get a hold of the User model that is currently logged in. What is the best way to do this? There is a user component in the main.php but this does not seem to be what I am after. What exactly is the user component? What class is it? I’m finding the main.php somewhat confusing. Is there something that explains this well? The guide wasn’t really very helpful for me.

I am looking at the blog demo and there is a log component which is of class CLogRouter but there is no mention of what class the user component is.

I want to get a hold of the user model when a user logs in and then be able to access this model easily from anywhere. What would be the best way to do this?

Thanks

Ziad

forgot to turn on email notification

You should look at WebRoot/protected/components/UserIdentity, it has a method getId which you can override to return the id for your model (e.g. an ActiveRecord). IUserIdentity.

To get the user component (configured in main.php):




$user = Yii::app()->user



The user component is an instance of CWebUser, take a look at CWebApplication.

I found the guide very useful. Did you already see this?

Thanks for your reply, gives me a better understanding of whats going on but I still can’t achieve what I wanted to achieve through the UserIdentity as that can only return an id or name. I’d have to extend the WebUser class to get what I want. For now I’ve just put a method in the base controller that allows me to get a hold of the current logged in user.

Thanks

Ziad

Hi, Ziad.

If I understand you correctly you want to load a User model for currently logged user?




$user = User::model()->findByPk(Yii::app()->user->id);



Is this what are you looking for?

Hi Galymzhan,

Yeah that’s exactly what I’ve done. I’ve put that code in a function in the base controller (getCurrentUser) so that I can get hold of the logged in user easily from any controller but I would have preferred to have it in the user component so that I could easily acces it from anywhere e.g views.

If I want to extend the base WebUser class where do I need to put it and how do I tell Yii to use my extended version and not the base one, so when I do Yii:app()->user it gives me my extended version?

Thanks

Ziad

That’ easy. Suppose you have a class in ‘protected/components’ folder:




class ExtendedUserComponent extends CWebUser {

 ... your code

}



I guess your config looks like this:




'user' => array(

        // enable cookie-based authentication

        'allowAutoLogin' => true,

),



Change it like this:




'user' => array(

      'class' => 'application.components.ExtendedUserComponent',

      // enable cookie-based authentication

      'allowAutoLogin' => true,

),



I’m not 100% sure as I didn’t try it, but it should work. Don’t forget to post here whether it’s worked or not.

use your UserIdentity.php class like




<?php


/**

 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.

 */

class UserIdentity extends CUserIdentity

{

	private $_id;

	/**

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

	 */

	

	public function authenticate()

	{

		if(!($user=User::model()->findByAttributes(array('email'=>$this->username))))

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		elseif($user->password!==md5($this->password))

		$this->errorCode=self::ERROR_PASSWORD_INVALID;

		elseif($user->status==User::INACTIVE)

		$this->errorCode=self::ERROR_USER_INACTIVE;

		else

		{   

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

            $this->setState('roles', $user->role);     

            $this->setState('lastLoginTime', $user->lastLoginTime);            

            $this->errorCode=self::ERROR_NONE;

			

		}

		return !$this->errorCode;

	}

	

	public function getId()

	{

		return $this->_id;

	}

}

access user details like this anywhere in your site like below:




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

Yii::app()->$user->lastLoginTime

you can assign any values here for user…that you can access in your app

Thanks :)