Using Useridentity And Webuser Class

I have 3 tables

users( id, default_profile_id, username, password)

profiles( id, name_first, name_last, email)

staff( id, profile_id, organisation_id,is_admin)

so to get information like profile_id, organisation_id, username, user_id i am using UserIdentity class and WebUser class. Here is the WebUser class:

Code:


class WebUser extends CWebUser

{

	private $_model;

	public function __get($name)

	{

		if ($this->hasState('__userInfo')){

			$user=$this->getState('__userInfo',array());

			if (isset($user[$name])) {

				return $user[$name];

			}

		}

		return parent::__get($name);

	}


	public function login($identity, $duration){

		$this->setState('__userInfo', $identity->getUser());

		parent::login($identity, $duration);

	}


	// Return user's name.

	// access it by Yii::app()->user->getName()

	public function getName(){

		if(Yii::app()->user->isGuest)

		{

			return "";

		}else{

			$user=$this->loadUser(Yii::app()->user->id);

			if($user===null)

				return 0;

			else{

				return $user['name_first'].' '.$user['name_last'];

			}

		}

	}


	// This is a function that checks the field 'is_admin'

	// in the Staff model to be equal to 1, that means it's admin

	// access it by Yii::app()->user->isAdmin()

	public function isAdmin(){

		$user = $this->loadUser(Yii::app()->user->id);

		if($user===null)

			return 0;

		else

			return $user['is_admin'];

	}


	// Return user's organisation id.

	// from the Staff model,

	// access it by Yii::app()->user->getOrganisationId()

	public function getOrganisationId(){

		$user = $this->loadUser(Yii::app()->user->id);

		if($user===null)

			return 0;

		else

			return $user['organisation_id'];

	}


	// Load user model

	protected function loadUser($id=null){

		if($this->_model===null)

		{

			if($id!==null)

			{

				$this->_model=Yii::app()->db->createCommand()

					->select('name_first,name_last,profile_id,organisation_id,is_admin')

					->from('staff hks')

					->join('users hku','hku.default_profile_id=hks.profile_id')

					->join('profiles hkp','hkp.id=hku.default_profile_id')

					->where('hku.id=:id',array(':id'=>$id))

					->queryRow();

			}

			return $this->_model;

		}

	}



Now when i am trying to access aboce mentioned information using:

Yii::app()->user->getName() for name of user,

Yii::app()->user->getOrganisationId() for organisation id and

Yii::app()->user->isAdmin() to check whether he is admin or not.

Then Only one of the above three is working and others are returning zero value depending which one i have written first in a particular page.

Output:

NAME: Chirag Chetan

Organisation ID: 0

isAdmin: 0

Please suggest a method to how to retrieve above information.

Staffhks.php model already has attributes like organisation_id and is_admin. So what is the use of declaring it gain.

sorry…i don’t understand your question can u detail in brief. :unsure:

Basically i have 3 tables namely users(id,default_profile_id,username,password),profiles(id,name_first,name_last, email) and staff(id,profile_id,organisation_id,is_admin).

Now when a particular user logins then i have to write different queries to check whether he is admin or not and also for organisation id to which he belongs and his name too. So i tried to retrieve all these information during his login session by using functions (getName(),isAdmin() and getOrganisationId()) as discussed above in WebUser class.

But I am unable to retrieve organisation-id and is_admin value and his name,if i use all three functions simultaneously. Is it clear now please let me know…

can you please post the Staffhks.php model code ?

Basically after trying some methods i did found the solution.

i created states using setstate like $this->setState(‘organisationID’,$model[‘organisation_id’]) while checking authentication of user and using this Yii::app()->user->organisationID in other controllers where ever is needed.