Yii中实现处理前后台登录的新方法


Posted in PHP onDecember 28, 2015

本文实例讲述了Yii中实现处理前后台登录的新方法。分享给大家供大家参考,具体如下:

因为最近在做一个项目涉及到前后台登录问题,我是把后台作为一个模块(Module)来处理的。我看很多人放两个入口文件index.php和admin.php,然后分别指向前台和后台。这种方法固然很好,可以将前后台完全分离,但我总觉得这种方式有点牵强,这和两个应用啥区别?还不如做两个App用一个framework更好。而且Yii官方后台使用方法也是使用Module的方式。但是Moudle的方式有一个很头疼的问题,就是在使用Cwebuser登录时会出现前后台一起登录一起退出的问题,这显然是不合理的。我纠结了很久才找到下文即将介绍的方法,当然,很多也是参考别人的,自己稍作了改动。我一开始的做法是在后台登录时设置一个isadmin的session,然后再前台登录时注销这个session,这样做只能辨别是前台登录还是后台登录,但做不到前后台一起登录,也即前台登录了后台就退出了,后台登录了前台就退出了。出现这种原因的根本原因是我们使用了同一个Cwebuser实例,不能同时设置前后台session,要解决这个问题就要将前后台使用不同的Cwebuser实例登录。下面是我的做法,首先看protected->config->main.php里对前台user(Cwebuser)的配置:

'user'=>array(
  'class'=>'WebUser',//这个WebUser是继承CwebUser,稍后给出它的代码
  'stateKeyPrefix'=>'member',//这个是设置前台session的前缀
  'allowAutoLogin'=>true,//这里设置允许cookie保存登录信息,一边下次自动登录
),

在你用Gii生成一个admin(即后台模块名称)模块时,会在module->admin下生成一个AdminModule.php文件,该类继承了CWebModule类,下面给出这个文件的代码,关键之处就在该文件,望大家仔细研究:

<?php
class AdminModule extends CWebModule
{
  public function init()
  {
    // this method is called when the module is being created
    // you may place code here to customize the module or the application
    parent::init();//这步是调用main.php里的配置文件
    // import the module-level models and componen
    $this->setImport(array(
      'admin.models.*',
      'admin.components.*',
    ));
    //这里重写父类里的组件
    //如有需要还可以参考API添加相应组件
    Yii::app()->setComponents(array(
        'errorHandler'=>array(
            'class'=>'CErrorHandler',
            'errorAction'=>'admin/default/error',
        ),
        'admin'=>array(
            'class'=>'AdminWebUser',//后台登录类实例
            'stateKeyPrefix'=>'admin',//后台session前缀
            'loginUrl'=>Yii::app()->createUrl('admin/default/login'),
        ),
    ), false);
    //下面这两行我一直没搞定啥意思,貌似CWebModule里也没generatorPaths属性和findGenerators()方法
    //$this->generatorPaths[]='admin.generators';
    //$this->controllerMap=$this->findGenerators();
  }
  public function beforeControllerAction($controller, $action)
  {
    if(parent::beforeControllerAction($controller, $action))
    {
      $route=$controller->id.'/'.$action->id;
      if(!$this->allowIp(Yii::app()->request->userHostAddress) && $route!=='default/error')
        throw new CHttpException(403,"You are not allowed to access this page.");
      $publicPages=array(
        'default/login',
        'default/error',
      );
      if(Yii::app()->admin->isGuest && !in_array($route,$publicPages))
        Yii::app()->admin->loginRequired();
      else
        return true;
    }
    return false;
  }
  protected function allowIp($ip)
  {
    if(empty($this->ipFilters))
      return true;
    foreach($this->ipFilters as $filter)
    {
      if($filter==='*' || $filter===$ip || (($pos=strpos($filter,'*'))!==false && !strncmp($ip,$filter,$pos)))
        return true;
    }
    return false;
  }
}
?>

AdminModule 的init()方法就是给后台配置另外的登录实例,让前后台使用不同的CWebUser,并设置后台session前缀,以便与前台session区别开来(他们同事存在$_SESSION这个数组里,你可以打印出来看看)。

这样就已经做到了前后台登录分离开了,但是此时你退出的话你就会发现前后台一起退出了。于是我找到了logout()这个方法,发现他有一个参数$destroySession=true,原来如此,如果你只是logout()的话那就会将session全部注销,加一个false参数的话就只会注销当前登录实例的session了,这也就是为什么要设置前后台session前缀的原因了,下面我们看看设置了false参数的logout方法是如何注销session的:

/**
* Clears all user identity information from persistent storage.
 * This will remove the data stored via {@link setState}.
 */
public function clearStates()
{
  $keys=array_keys($_SESSION);
  $prefix=$this->getStateKeyPrefix();
  $n=strlen($prefix);
  foreach($keys as $key)
  {
    if(!strncmp($key,$prefix,$n))
      unset($_SESSION[$key]);
  }
}

看到没,就是利用匹配前缀的去注销的。

到此,我们就可以做到前后台登录分离,退出分离了。这样才更像一个应用,是吧?嘿嘿…

差点忘了说明一下:

Yii::app()->user //前台访问用户信息方法
Yii::app()->admin //后台访问用户信息方法

不懂的仔细看一下刚才前后台CWebUser的配置。

附件1:WebUser.php代码:

<?php
class WebUser extends CWebUser
{
  public function __get($name)
  {
    if ($this->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);
  }
}
?>

附件2:AdminWebUser.php代码

<?php
class AdminWebUser extends CWebUser
{
  public function __get($name)
  {
    if ($this->hasState('__adminInfo')) {
      $user=$this->getState('__adminInfo',array());
      if (isset($user[$name])) {
        return $user[$name];
      }
    }
    return parent::__get($name);
  }
  public function login($identity, $duration) {
    $this->setState('__adminInfo', $identity->getUser());
    parent::login($identity, $duration);
  }
}
?>

附件3:前台UserIdentity.php代码

<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
  /**
   * Authenticates a user.
   * The example implementation makes sure if the username and password
   * are both 'demo'.
   * In practical applications, this should be changed to authenticate
   * against some persistent user identity storage (e.g. database).
   * @return boolean whether authentication succeeds.
   */
  public $user;
  public $_id;
  public $username;
  public function authenticate()
  {
    $this->errorCode=self::ERROR_PASSWORD_INVALID;
    $user=User::model()->find('username=:username',array(':username'=>$this->username));
     if ($user)
    {
      $encrypted_passwd=trim($user->password);
      $inputpassword = trim(md5($this->password));
      if($inputpassword===$encrypted_passwd)
      {
        $this->errorCode=self::ERROR_NONE;
        $this->setUser($user);
        $this->_id=$user->id;
        $this->username=$user->username;
        //if(isset(Yii::app()->user->thisisadmin))
          // unset (Yii::app()->user->thisisadmin);
      }
      else
      {
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
      }
    }
    else
    {
      $this->errorCode=self::ERROR_USERNAME_INVALID;
    }
    unset($user);
    return !$this->errorCode;
  }
  public function getUser()
  {
    return $this->user;
  }
  public function getId()
  {
    return $this->_id;
  }
  public function getUserName()
  {
    return $this->username;
  }
  public function setUser(CActiveRecord $user)
  {
    $this->user=$user->attributes;
  }
}

附件4:后台UserIdentity.php代码

<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
  /**
   * Authenticates a user.
   * The example implementation makes sure if the username and password
   * are both 'demo'.
   * In practical applications, this should be changed to authenticate
   * against some persistent user identity storage (e.g. database).
   * @return boolean whether authentication succeeds.
   */
  public $admin;
  public $_id;
  public $username;
  public function authenticate()
  {
    $this->errorCode=self::ERROR_PASSWORD_INVALID;
    $user=Staff::model()->find('username=:username',array(':username'=>$this->username));
     if ($user)
    {
      $encrypted_passwd=trim($user->password);
      $inputpassword = trim(md5($this->password));
      if($inputpassword===$encrypted_passwd)
      {
        $this->errorCode=self::ERROR_NONE;
        $this->setUser($user);
        $this->_id=$user->id;
        $this->username=$user->username;
        // Yii::app()->user->setState("thisisadmin", "true");
      }
      else
      {
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
      }
    }
    else
    {
      $this->errorCode=self::ERROR_USERNAME_INVALID;
    }
    unset($user);
    return !$this->errorCode;
  }
  public function getUser()
  {
    return $this->admin;
  }
  public function getId()
  {
    return $this->_id;
  }
  public function getUserName()
  {
    return $this->username;
  }
  public function setUser(CActiveRecord $user)
  {
    $this->admin=$user->attributes;
  }
}

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP 相关文章推荐
开发大型 PHP 项目的方法
Jan 02 PHP
PhpMyAdmin中无法导入sql文件的解决办法
Jan 08 PHP
《PHP编程最快明白》第六讲:Mysql数据库操作
Nov 01 PHP
php class中self,parent,this的区别以及实例介绍
Apr 24 PHP
分享8个最佳的代码片段在线测试网站
Jun 29 PHP
php生成图片缩略图的方法
Apr 07 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
Dec 31 PHP
php实现的操作excel类详解
Jan 15 PHP
PHP去除字符串最后一个字符的三种方法实例
Mar 01 PHP
PHP中phar包的使用教程
Jun 14 PHP
PHP闭包定义与使用简单示例
Apr 13 PHP
php实现微信发红包功能
Jul 13 PHP
Yii中CGridView实现批量删除的方法
Dec 28 #PHP
Yii基于数组和对象的Model查询技巧实例详解
Dec 28 #PHP
yii权限控制的方法(三种方法)
Dec 28 #PHP
Yii使用Captcha验证码的方法
Dec 28 #PHP
yii使用activeFileField控件实现上传文件与图片的方法
Dec 28 #PHP
yii实现使用CUploadedFile上传文件的方法
Dec 28 #PHP
Yii中Model(模型)的创建及使用方法
Dec 28 #PHP
You might like
在PHP中利用XML技术构造远程服务(上)
2006/10/09 PHP
PHP投票系统防刷票判断流程分析
2012/02/04 PHP
PHP添加图片水印、压缩、剪切的封装类
2015/08/17 PHP
php微信支付之公众号支付功能
2018/05/30 PHP
php面向对象程序设计中self与static的区别分析
2019/05/21 PHP
jquery ready()的几种实现方法小结
2010/06/18 Javascript
javascript中interval与setTimeOut的区别示例介绍
2014/03/14 Javascript
在JavaScript中处理时间之getHours()方法的使用
2015/06/10 Javascript
javascript页面倒计时实例
2015/07/25 Javascript
Vue.js仿Metronic高级表格(二)数据渲染
2017/04/19 Javascript
Bootstrap treeview实现动态加载数据并添加快捷搜索功能
2018/01/07 Javascript
微信小程序全局变量改变监听的实现方法
2019/07/15 Javascript
微信小程序wxs实现吸顶效果
2020/01/08 Javascript
Python中的生成器和yield详细介绍
2015/01/09 Python
python实现字符串连接的三种方法及其效率、适用场景详解
2017/01/13 Python
win7上python2.7连接mysql数据库的方法
2017/01/14 Python
Python Selenium Cookie 绕过验证码实现登录示例代码
2018/04/10 Python
Python利用公共键如何对字典列表进行排序详解
2018/05/19 Python
python3使用matplotlib绘制条形图
2020/03/25 Python
Django 导出项目依赖库到 requirements.txt过程解析
2019/08/23 Python
django 做 migrate 时 表已存在的处理方法
2019/08/31 Python
使用Pandas将inf, nan转化成特定的值
2019/12/19 Python
torch 中各种图像格式转换的实现方法
2019/12/26 Python
Python字符串格式化f-string多种功能实现
2020/05/07 Python
如何Tkinter模块编写Python图形界面
2020/10/14 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
2021/03/03 Python
canvas探照灯效果的示例代码
2018/11/30 HTML / CSS
荷兰包包购物网站:The Little Green Bag
2018/03/17 全球购物
加拿大领先家居家具网上购物:Aosom.ca
2020/05/27 全球购物
学校地质灾害防治方案
2014/06/10 职场文书
酒店七夕情人节活动策划方案
2014/08/24 职场文书
中学团支部工作总结
2015/08/13 职场文书
2016年师德先进个人事迹材料
2016/02/29 职场文书
Python基础之hashlib模块详解
2021/05/06 Python
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis
SQL中去除重复数据的几种方法汇总(窗口函数对数据去重)
2023/05/08 MySQL