Add information to Yii: app ()-> user by extending CWebUser

Source: Internet
Author: User
(Your login verification may be different from my version, so you will use the appropriate script to meet your needs. I will only tell you what I mean) step: 1. add the $ user attribute to the UserIdentity class. Add the property above the getUser () method-getter. Add the setUser ($ user) method-the property above setter, which can be assigned to the user information through the $ user

I found a better solution.

(Your login verification may be different from my version, so you will use the appropriate script to meet your needs. I only tell you what I mean)

Step: 1. add the $ user attribute to the UserIdentity class. Add the property above the getUser () method-getter. Add the setUser ($ user) method-the property above setter, which can be assigned to the user information through the $ user attribute.

Example of my UserIdentity class:

 errorCode=self::ERROR_PASSWORD_INVALID; $user=User::model()->findByAttributes(array('email'=>CHtml::encode($this->username))); if ($user) { if ($user->password === md5($user->salt.$this->password)) { $this->errorCode=self::ERROR_NONE; $this->setUser($user); } } unset($user); return !$this->errorCode; } public function getUser() { return $this->user; } public function setUser(CActiveRecord $user) { $this->user=$user->attributes; } } ?>

Now that the user's attributes have been set, create a WebUser class and put it in/protected/components

 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); } } ?>

Remember to set this Yii: app ()-> user

 array( 'user'=>array( 'class'=>'WebUser', ) ) ?>

Now you can use the user attribute to access columns in the user database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.