Yii Framework Forum: [Solved] How to expand the User object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved] How to expand the User object Rate Topic: ****- 2 Votes

#1 User is offline   rktaxali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-July 10

Posted 23 July 2010 - 02:18 PM

I am a newbie, started using Yii two days ago. I am able to set up and use the blog tutorial. However, when I tried to add any new feature to the tutorial, I am not able to figure out which file to modify. For example, there is an email field in tbl_user. And, I have added firstname and lastname fields to tbl_user. After login, I want to display the firstname on a page with something like

Yii::app()->user->getFirstname()     or  
Yii::app()->user->firstname


I shall appreciate if some expert can guide me how should I implement this.

Also on the same lines, if I want to implement a simple scheme to determine whether a logged-in user is a regular user or has admin rights, based on the contents of a field in tbl_user (e.g. tbl_user.level > 1 means 'admin' and tbl_user.level <= 1 means a regular user), how do I implement this?

Thanks in advance.
0

#2 User is offline   Jaz Manister 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 27-October 09
  • Location:Rural Western New York, USA

Posted 23 July 2010 - 02:34 PM

You're going to want to grab the actual instance of your user model for the logged in user.


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

//then you can
echo $user->firstname


0

#3 User is offline   rktaxali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-July 10

Posted 23 July 2010 - 04:12 PM

View PostJaz Manister, on 23 July 2010 - 02:34 PM, said:

You're going to want to grab the actual instance of your user model for the logged in user.


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

//then you can
echo $user->firstname



Jaz,

Thanks for your quick response. Well, your suggestion will work. However, I believe that it will require fetching data from the user table from the database. Is it possible to modify/extend the CWebUser or some other class to store the required details, such as User's firstname, last name, phone number, etc. in the user object on successful login? I also want to determine whether the user is 'admin' based on some data in the user table. Then I would like to use user->isAdmin() to determine if the logged-in user has admin rights.

Any suggestions?
Thanks.
0

#4 User is offline   rktaxali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-July 10

Posted 26 July 2010 - 12:35 PM

I tried to implement what I wanted (e.g. Yii::app()->user->getFirstname() ), but could not achieve, may be due to my limited knowledge about the Yii framework and too many files involved in implementing user and authentication. (UserIdentity.php, User.php, cUserIdentiry.php, CWebUser.php and many more, and I did not want to modify any Framework files.)

Anyway, I achieved what I wanted through Session in the authenticate method of protected/components/UserIdentity.php as shown here.

			$this->_id=$user->id;
			$this->username=$user->username;
			$this->errorCode=self::ERROR_NONE;
			// Added to save details about the logged-in user 
			Yii::app()->session['user_firstname'] = $user->firstname; 
			Yii::app()->session['user_lastname'] = $user->lastname; 
			Yii::app()->session['user_fullname'] = trim($user->firstname). ' ' . trim($user->lastname); 			
			Yii::app()->session['user_email'] = $user->email;	




I would still prefer if I can implement my original scheme, i.e. Yii::app()->user->getFirstname(), however, my workaround works through <?php echo "Email= " . Yii::app()->session['user_email'] . "<BR>" ?>. I don't know whether saving non-sensitive data in session will lead to any security issues. Any comments?
0

#5 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 26 July 2010 - 01:19 PM

Take a look at CWebUser::setState(). It also stores the data in session AND if you use auto-login cookie, it will store those informations in there. Note that in the auto-login case, you shouldn't save sensitive informations like passwords since they appear in clear text in cookie.

After you set a state, you can get it back with getState(). You can also do this of course:

class MyWebUser extends CWebUser
{

   public function getFirstname()
   {
      return $this->getState('firstname');
   }

}


Also note there is an afterLogin() method of CWebUser. In there you can do the setState() for each attribute instead of doing it in UserIdentity.
1

#6 User is offline   rktaxali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-July 10

Posted 27 July 2010 - 02:07 PM

View PostY!!, on 26 July 2010 - 01:19 PM, said:

Take a look at CWebUser::setState().
After you set a state, you can get it back with getState().

You can also do this of course:

class MyWebUser extends CWebUser
{

   public function getFirstname()
   {
      return $this->getState('firstname');
   }

}


Also note there is an afterLogin() method of CWebUser. In there you can do the setState() for each attribute instead of doing it in UserIdentity.



Thanks for providing useful information regarding user->setState() and user->getState(). This looks more logical then directly saving data in session variable.

Re. extending cWebUser: After extending cWebUser, where do I use it? I mean, I could not find CWebUser class being used in my application files.
0

#7 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 27 July 2010 - 02:14 PM

View Postrktaxali, on 27 July 2010 - 02:07 PM, said:

Re. extending cWebUser: After extending cWebUser, where do I use it? I mean, I could not find CWebUser class being used in my application files.


CWebUser, the user application component, is automatically configured by default. If you want to change the class do this in config:

'components' => array(
   ...
   'user' => array(
      'class' => 'application.components.MyWebUser',
   ),
   ...
),


In this case MyWebUser.php must be placed in protected/components
0

#8 User is offline   rktaxali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 21-July 10

Posted 28 July 2010 - 10:08 AM

Y!!,

Thank you so much for your help. I am sure other newbies will also find your tips useful.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users