Yii login 扩展, 存放信息

来源:互联网 发布:网络视频会议系统 编辑:程序博客网 时间:2024/06/04 00:50


如果在用户登录后想额外调用除 user,id之外的数据库变量,可以这样设置:

在登陆验证时候增加额外项:Yii::app()->user->last_login_time

在UserIdentity.php中

代码  收藏代码
  1. class UserIdentity extends CUserIdentity  
  2. {  
  3.     $this->setState('last_login_time',$user->last_login_time);  
  4. }  

 如此,在应用程序的任何地方,这个属性可以通过如下获取:Yii::app()->user->last_login_time

再重新登录看看,

代码  收藏代码
  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. }  

其实他将信息放到session中了

其中的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类例子:

代码  收藏代码
  1. <?php  
  2. class UserIdentity extends CUserIdentity {  
  3.     /** 
  4.      * User's attributes 
  5.      * @var array 
  6.      */  
  7.     public $user;  
  8.   
  9.     public function authenticate() {  
  10.         $this->errorCode = self::ERROR_PASSWORD_INVALID;  
  11.         $user = User::model()->findByAttributes(array('email' => CHtml::encode($this->username)));  
  12.         if ($user) {  
  13.             if ($user->password === md5($user->salt . $this->password)) {  
  14.                 $this->errorCode = self::ERROR_NONE;  
  15.                 $this->setUser($user);  
  16.             }  
  17.         }  
  18.         unset($user);  
  19.         return !$this->errorCode;  
  20.     }  
  21.   
  22.     public function getUser() {  
  23.         return $this->user;  
  24.     }  
  25.   
  26.     public function setUser(CActiveRecord $user) {  
  27.         $this->user = $user->attributes;  
  28.     }  
  29. }  
  30.   
  31. ?>   

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

代码  收藏代码
  1. <?php  
  2. class WebUser extends CWebUser {  
  3.     public function __get($name) {  
  4.         if ($this->hasState('__userInfo')) {  
  5.             $user = $this->getState('__userInfo', array());  
  6.             if (isset($user[$name])) {  
  7.                 return $user[$name];  
  8.             }  
  9.         }  
  10.   
  11.         return parent::__get($name);  
  12.     }  
  13.   
  14.     public function login($identity, $duration) {  
  15.         $this->setState('__userInfo', $identity->getUser());  
  16.         parent::login($identity, $duration);  
  17.     }  
  18.   
  19.     public function getIsGuest() {  
  20.         $customer = Yii::app()->session->get('customer');  
  21.         return $customer === null || $customer['id'] === null;  
  22.     }  
  23. }  
  24. ?>  

 记得设置一下这个类Yii::app()->user

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

调用方法

Yii::app()->user->getIsGuest()

 

2用户信息存到单独的文件

代码  收藏代码
  1. <?php  
  2. class WebUser extends CWebUser  
  3. {  
  4.     public function getReturnUrl($defaultUrl=null)  
  5.     {     
  6.         $userInfo = $this->getUserInfo();  
  7.         if(isset($userInfo['url'])){  
  8.             return $userInfo['url'];  
  9.         }  
  10.         return parent::getReturnUrl($defaultUrl);  
  11.     }  
  12.   
  13.     protected function afterLogin($fromCookie)  
  14.     {  
  15.         parent::afterLogin($fromCookie);  
  16.         $users = require(dirname(__FILE__) . '/../config/password.php');  
  17.   
  18.         $this->setState('userInfo',$users[$this->getName()]);  
  19.     }  
  20.   
  21.     public function getUserInfo()  
  22.     {  
  23.         return $this->getState('userInfo',array());  
  24.     }  
  25. //accessRules  roles  
  26.     public function checkAccess($operation,$params=array(),$allowCaching=true)  
  27.     {  
  28.         $userInfo = $this->getUserInfo();  
  29.         if($userInfo['group'] == $operation){  
  30.             return true;  
  31.         }  
  32.         return parent::checkAccess($operation,$params,$allowCaching);  
  33.     }  
  34. }  

 password.php

代码  收藏代码
  1. <?php  
  2.   
  3. return array(  
  4.     'dianyin'           => array(  
  5.         'pwd'           => 'dianyinXX',  
  6.         'url'           => array('dianyin/order/index'),  
  7.         'merchant_id'   => 1,  
  8.         'group'         => 'dianyin',  
  9.      ),  
  10.     'boer' => array(  
  11.         'pwd'           => 'boerXX',  
  12.         'url'           =>  array('third_jifen/default/index'),  
  13.         'merchant_id'   => 1,  
  14.         'group'         => 'jifen',  
  15.     ),  
  16. );  

 权限checkAccess结合roles

代码  收藏代码
  1. public function accessRules()  
  2. {  
  3.     return array(  
  4.         array('allow'// allow authenticated users to access all actions  
  5.             'roles'=>array('jifen'),  
  6.         ),  
  7.         array('allow',  // deny all users  
  8.             'actions'=>array('login','logout'),  
  9.             'users'=>array('*'),  
  10.         ),    
  11.         array('deny',  // deny all users  
  12.             'users'=>array('*'),  
  13.         ),  
  14.       
  15.     );  
原创粉丝点击