我一開始的作法是在後臺登陸時設置一個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;
- }
- }