UserIdentity object

Hi,

I have created a User object as follows: -




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

How do I get around this problem?

Wrap it in an isset:


if(isset(Yii::app()->user->username)) dostuff

Cheers for your help.

Oops, I meant:


if(isset(Yii::app()->user->record->first_name)) echo Yii::app()->user->record->first_name;

I had to bash my head against the wall a couple of hours minutes myself. ;)

Thanks for your help, and yeah I understood the gist of it when you first wrote it, do not worry.

Ohhh just another quick questions, do you know how this could be applied inside the CMenu option.




<?php

				

					$this->widget("zii.widgets.CMenu", array(

						

						"items" => array(

							

							array("label" => "Log in", "url" => array("account/login"), "visible" => Yii::app()->user->isGuest),

							array("label" => "My info", "url" => array("site/index"), "visible" => !Yii::app()->user->isGuest),

							array("label" => "Sign up", "url" => array("account/signup"), "visible" => Yii::app()->user->isGuest),

							array("label" => "Sign out", "url" => array("account/logout"), "visible" => !Yii::app()->user->isGuest)

							

						)

						

					));

				

				?>



So if it needs to be in the visible part how would I apply that?

That one is even simpler:


'visible' => isset(Yii::app()->user->record->first_name)

:P

Thank you very much.

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.