我一开始的作法是在后台登陆时设置一个isadmin的session,而后再前台登陆时注销这个session,这样作只能辨别是前台登陆仍是后台登陆,但作不到先后台一块儿登陆,也即前台登陆了后台就退出了,后台登陆了前台就退出了。出现这种缘由的根本缘由是咱们使用了同一个Cwebuser实例,不能同时设置先后台session,要解决这个问题就要将先后台使用不一样的Cwebuser实例登陆。下面是个人作法,首先看protected->config->main.php里对前台user(Cwebuser)的配置:php
- 'user'=>array(
- 'class'=>'WebUser',//这个WebUser是继承CwebUser,稍后给出它的代码
- 'stateKeyPrefix'=>'member',//这个是设置前台session的前缀
- 'allowAutoLogin'=>true,//这里设置容许cookie保存登陆信息,一边下次自动登陆
- ),
在你用Gii生成一个admin(即后台模块名称)模块时,会在module->admin下生成一个AdminModule.php文件,该类继承了CWebModule类,下面给出这个文件的代码,关键之处就在该文件,望你们仔细研究:html
- <?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()->user->isGuest && !in_array($route,$publicPages))
- Yii::app()->user->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这个数组里,你能够打印出来看看)。web
这样就已经作到了先后台登陆分离开了,可是此时你退出的话你就会发现先后台一块儿退出了。因而我找到了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]);
- }
- }
看到没,就是利用匹配前缀的去注销的。cookie
到此,咱们就能够作到先后台登陆分离,退出分离了。这样才更像一个应用,是吧?嘿嘿…session
差点忘了说明一下:app
- Yii::app()->user//前台访问用户信息方法
- Yii::app()->admin//后台访问用户信息方法
不懂的仔细看一下刚才先后台CWebUser的配置。ide
WebUser.php代码:ui
- <?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);
- }
- }
-
- ?>
AdminWebUser.php代码this
- <?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);
- }
- }
-
- ?>
前台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;
- }
- }
后台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;
- }
- }