YII 存放登录信息的类

如果在用户登录后想额外调用除 user,id之外的数据库变量,可以这样设置:
在登陆验证时候增加额外项:Yii::app()->user->last_login_time
在UserIdentity.php中

01. class UserIdentity extends CUserIdentity
02. {
03. public function authenticate()
04. {
05. //xxxxxxxxxx
06. $this->setState('last_login_time',$user->last_login_time);
07. //xxxxxxxxxx
08. }
09. }

如此,在应用程序的任何地方,这个属性可以通过如下获取:Yii::app()->user->last_login_time;
将其他的信息存放在session中:
重写父类的方法

1. public function setState($key$value$defaultValue = null) {
2. $key $this->getStateKeyPrefix() . $key;
3. if ($value === $defaultValue)
4. unset($_SESSION[$key]);
5. else
6. $_SESSION[$key] = $value;
7. }

其中的user是yii的一个components.需要在protected/config/main.php中定义

1. 'user'=>array(
2. // enable cookie-based authentication
3. 'allowAutoLogin'=>true,
4. 'loginUrl' => array('site/login'),
5. ),

通过扩展CWebUser添加信息到Yii:app()->user
步骤:1、添加$user属性到UserIdentity类。 添加getUser()方法-getter上面这个属性。加setUser($user)方法-setter上面这个属性,它可以赋值给user的信息通过$user这个属性。
我的UserIdentity类例子:

01. <?php
02. class UserIdentity extends CUserIdentity
03. {
04. /**
05. * User's attributes
06. * @var array
07. */
08. public $user;
09.  
10. public function authenticate()
11. {
12. $this->errorCode=self::ERROR_PASSWORD_INVALID;
13. $user=User::model()->findByAttributes(array('email'=>CHtml::encode($this->username)));
14. if ($user)
15. {
16. if ($user->password === md5($user->salt.$this->password)) {
17. $this->errorCode=self::ERROR_NONE;
18. $this->setUser($user);
19. }
20. }
21. unset($user);
22. return !$this->errorCode;
23. }
24.  
25. public function getUser()
26. {
27. return $this->user;
28. }
29.  
30. public function setUser(CActiveRecord $user)
31. {
32. $this->user=$user->attributes;
33. }
34. }
35. ?>

现在用户的属性已经设置,创建WebUser类并把它放在/protected/components

01. <?php
02. class WebUser extends CWebUser
03. {
04. public function __get($name)
05. {
06. if ($this->hasState('__userInfo')) {
07. $user=$this->getState('__userInfo',array());
08. if (isset($user[$name])) {
09. return $user[$name];
10. }
11. }
12.  
13. return parent::__get($name);
14. }
15.  
16. public function login($identity$duration) {
17. $this->setState('__userInfo'$identity->getUser());
18. parent::login($identity$duration);
19. }
20. public function getIsGuest()
21. {
22. $customer = Yii::app()->session->get('customer');
23. return  $customer===null||$customer['id']===null;
24. }
25.  
26. public function getCustomerId()
27. {
28. if(!$this->getIsGuest()){
29. $customer = Yii::app()->session->get('customer');
30. $this->customerId = isset($customer['id']) ? $customer['id'] : 0;
31. }
32. return $this->customerId;
33. }
34. }
35. ?>

别忘了修改配置文件中的配置

1. <?php
2. 'components'=>array(
3. 'user'=>array(
4. 'class'=>'WebUser',
5. )
6. )
7. ?>

调用方法:

1. 调用方法
2. Yii::app()->user->customerId
3. Yii::app()->user->getIsGuest();

我们要是想在登陆的时候存放一些自己的信息到user组件中 那么我们可以这样子:

01. public function authenticate()
02. {
03. $user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
04. $_tmp array();
05. foreach($user as $key=>$val){
06.  
07. $_tmp[$key] = $val;
08.  
09. }
10. $_tmp['fbbin'] = $_SERVER;
11. //存放自己的信息
12. $this->setPersistentStates($_tmp);
13. $this->setState('yanlin', 225500);
14. return $this->errorCode==self::ERROR_NONE;
15. }

那么这样子以来 我们可以再任何地方通过Yii::app()->user->yanlin; 来获取你设置的值

你可能感兴趣的:(yii)