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

<?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; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3941e8c374e44c8c3deaa88bd6995c62.html