<?php
class UserIdentity extends CUserIdentity {
public function authenticate() {
$email = $this->username; // Set because "Username" is used in parent class
$password = $this->password;
$record = User::model()->findByAttributes(array("email" => $email, "password" => $password));
if($record === null) {
return false;
} else {
$this->setState("record", $record);
return true;
}
}
}
?>
As you can see it stores the User record in the set stated option which is accessed in the application as follows: -
Yii::app()->user->record->first_name
The problem I am getting is I might want to display the users next to the log in link, so it states Welcome James, for instance.
However it works when the user is logged in, but when the user is logged out it throws an exception stating that the Yii::app()->user->record does not exist.
JamesBarnsley, i don’t think it’s a good idea to save a complete AR object in user state. Remember, everything you save to a UserIdentity state will be stored in a cookie if you enable cookie login. Something you might not do now, but who knows, maybe in a year you wanna try it and completely forgot about that record.
For one thing the data in $record might be very large. But more important: It could contain sensible user information. So maybe only save the 1 or 2 attributes you really need for this user.
Yeah I will look into it, I understand what you are saying. I probably do not think its save to store the password in there either lol. Anyways the thing I am making now is really just a prototype, I will probably just store the user id in the final version.