`

Yii login 扩展, 存放信息

    博客分类:
  • php
 
阅读更多

 

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

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

在UserIdentity.php中

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

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

再重新登录看看,

代码收藏代码
  1. publicfunctionsetState($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. //enablecookie-basedauthentication
  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. classUserIdentityextendsCUserIdentity{
  3. /**
  4. *User'sattributes
  5. *@vararray
  6. */
  7. public$user;
  8. publicfunctionauthenticate(){
  9. $this->errorCode=self::ERROR_PASSWORD_INVALID;
  10. $user=User::model()->findByAttributes(array('email'=>CHtml::encode($this->username)));
  11. if($user){
  12. if($user->password===md5($user->salt.$this->password)){
  13. $this->errorCode=self::ERROR_NONE;
  14. $this->setUser($user);
  15. }
  16. }
  17. unset($user);
  18. return!$this->errorCode;
  19. }
  20. publicfunctiongetUser(){
  21. return$this->user;
  22. }
  23. publicfunctionsetUser(CActiveRecord$user){
  24. $this->user=$user->attributes;
  25. }
  26. }
  27. ?>

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

代码收藏代码
  1. <?php
  2. classWebUserextendsCWebUser{
  3. publicfunction__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. returnparent::__get($name);
  11. }
  12. publicfunctionlogin($identity,$duration){
  13. $this->setState('__userInfo',$identity->getUser());
  14. parent::login($identity,$duration);
  15. }
  16. publicfunctiongetIsGuest(){
  17. $customer=Yii::app()->session->get('customer');
  18. return$customer===null||$customer['id']===null;
  19. }
  20. }
  21. ?>

记得设置一下这个类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. classWebUserextendsCWebUser
  3. {
  4. publicfunctiongetReturnUrl($defaultUrl=null)
  5. {
  6. $userInfo=$this->getUserInfo();
  7. if(isset($userInfo['url'])){
  8. return$userInfo['url'];
  9. }
  10. returnparent::getReturnUrl($defaultUrl);
  11. }
  12. protectedfunctionafterLogin($fromCookie)
  13. {
  14. parent::afterLogin($fromCookie);
  15. $users=require(dirname(__FILE__).'/../config/password.php');
  16. $this->setState('userInfo',$users[$this->getName()]);
  17. }
  18. publicfunctiongetUserInfo()
  19. {
  20. return$this->getState('userInfo',array());
  21. }
  22. //accessRulesroles
  23. publicfunctioncheckAccess($operation,$params=array(),$allowCaching=true)
  24. {
  25. $userInfo=$this->getUserInfo();
  26. if($userInfo['group']==$operation){
  27. returntrue;
  28. }
  29. returnparent::checkAccess($operation,$params,$allowCaching);
  30. }
  31. }

password.php

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

权限checkAccess结合roles

代码收藏代码
  1. publicfunctionaccessRules()
  2. {
  3. returnarray(
  4. array('allow',//allowauthenticateduserstoaccessallactions
  5. 'roles'=>array('jifen'),
  6. ),
  7. array('allow',//denyallusers
  8. 'actions'=>array('login','logout'),
  9. 'users'=>array('*'),
  10. ),
  11. array('deny',//denyallusers
  12. 'users'=>array('*'),
  13. ),
  14. );
  15. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics