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?
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.
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?
<?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: