From b6eabda80b47940acbd2fbb94ffd8f79ae50ed90 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Tue, 22 Apr 2014 09:17:47 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=8E=E5=85=B6=E5=AE=83?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=99=BB=E5=BD=95=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vendor/Sookon/User/Account.php | 49 ++-------- vendor/Sookon/User/Handle/LoginHandle.php | 103 ++++++++++++++++++---- 2 files changed, 93 insertions(+), 59 deletions(-) diff --git a/vendor/Sookon/User/Account.php b/vendor/Sookon/User/Account.php index b908aec7..4dfc2f11 100644 --- a/vendor/Sookon/User/Account.php +++ b/vendor/Sookon/User/Account.php @@ -134,17 +134,12 @@ class Account implements EventManagerAwareInterface $results = $this->getEventManager()->trigger('login.checkParam', $this, compact('data')); $cache_data = $results->last(); - if($cache_data !== true) + if(isset($cache_data['error']) && !empty($cache_data['error'])) { - if(!is_array($cache_data)) - { - return array('error'=>$cache_data); - }else{ - return $cache_data; - } + return array('error'=>$cache_data); } - $state = $this->storeLogin($data); + $state = $this->storeLogin($cache_data); if(isset($state['success'])) { @@ -161,42 +156,12 @@ class Account implements EventManagerAwareInterface $auth = new AuthenticationService(); $auth->setStorage(new SessionStorage($this->config->session_namespace)); - new Zend_Db($dbAdapter); - - $authAdapter = new \Zend\Authentication\Adapter\DbTable( - $dbAdapter, - $this->conf->table->member, - $this->conf->field->uname, - $this->conf->field->pwd - ); - - if($md5 === true) - { - $password = md5($data[$this->conf->field->pwd]); - }else{ - $password = $data[$this->conf->field->pwd]; - } - - $authAdapter - ->setIdentity($data[$this->conf->field->uname]) - ->setCredential($password) - ; - - $result = $authAdapter->authenticate(); - - $user = $authAdapter->getResultRowObject(null,array($this->conf->field->pwd)); - - if(!$result->isValid()) - { - return array("error"=>"用户信息验证失败"); - } - - $email = $user->email; + $email = $data['email']; $results = $this->getEventManager()->trigger('login.success.createAvatar', $this, compact('email')); - $user->avatar = $results->last(); - $auth->getStorage()->write($user); + $data['avatar'] = $results->last(); + $auth->getStorage()->write($data); - $id = $user->id; + $id = $data['id']; $results = $this->getEventManager()->trigger('login.success.updateStatus', $this, compact('id')); return array('success'=>1); diff --git a/vendor/Sookon/User/Handle/LoginHandle.php b/vendor/Sookon/User/Handle/LoginHandle.php index 112f3fa6..0f29892e 100644 --- a/vendor/Sookon/User/Handle/LoginHandle.php +++ b/vendor/Sookon/User/Handle/LoginHandle.php @@ -14,10 +14,19 @@ class LoginHandle implements \Sookon\User\Event\LoginEvent private $config; //全局配置 private $conf; + public $dsn; + + private $last_login_data; + function __construct() { $this->db = new Db(); $this->conf = Config::get(); + + $this->dsn = array( + array('host'=>'localhost','db'=>'','user'=>'','pwd'=>''), + array('host'=>'localhost','db'=>'','user'=>'','pwd'=>''), + ); } public function checkParam(EventInterface $e){ @@ -47,28 +56,88 @@ class LoginHandle implements \Sookon\User\Event\LoginEvent return array('error'=>"请输入密码",'place'=>'password'); } - $sql = "SELECT id,{$this->conf->field->pwd} FROM {$this->conf->table->member} WHERE {$this->conf->field->uname}=?"; - $sth = $this->db->prepare($sql); - $rs = $sth->execute(array($data[$this->conf->field->uname])); - $row = $sth->fetch(); - - if(isset($row['id']) && !empty($row['id'])) + $status = $this->checkLogin($data[$this->conf->field->uname],$data[$this->conf->field->pwd]); + if($status !== true) { - if(strlen($row[$this->conf->field->pwd]) !== 32) - { - return array('error'=>"您的密码或因安全猿因或其他问题已经被重置,请先重置密码再登陆",'place'=>'password'); - } - if($row[$this->conf->field->pwd] !== md5($data[$this->conf->field->pwd])) - { - return array('error'=>"密码错误",'place'=>'password'); - } - return true; - }else{ - return array('error'=>"用户不存在",'place'=>'username'); + return $status; + } + + else{ + return $this->last_login_data; } }//checkParam + //检查登录信息是否正确 + public function checkLogin($username,$password) + { + + $row = $this->checkUser($username); + + $uname_avalible = false; + + if( isset($row['error']) && !empty($row['error']) ) + { + if(count($this->dsn) > 0) + { + foreach($this->dsn as $k=>$v) + { + $db = new Db($v); + + $row = $this->checkUser($username,$db); + + if( !isset($row['error']) && empty($row['error']) ) + { + $uname_avalible = true; + break; + } + } + } + } + + if(!$uname_avalible) + return $row; + else + $this->last_login_data = $row; + + return $this->checkPwd($row,$password); + } + + //获取数据 + public function checkUser($username,$db = NULL,$memberTable = "") + { + if(empty($db)) + $db = $this->db; + + if(empty($memberTable)) + $memberTable = $this->conf->table->member; + $sql = "SELECT id,{$this->conf->field->pwd} FROM $memberTable WHERE {$this->conf->field->uname}=?"; + $sth = $db->prepare($sql); + $rs = $sth->execute(array($username)); + $row = $sth->fetch(); + + if(!isset($row['id']) || empty($row['id'])) + { + return array('error'=>"用户不存在",'place'=>'username'); + } + + return $row; + } + + //检查数据 + public function checkPwd($row,$password) + { + if(strlen($row[$this->conf->field->pwd]) !== 32) + { + return array('error'=>"您的密码或因安全原因和其他问题已经被重置,请先重置密码再登陆",'place'=>'password'); + } + if($row[$this->conf->field->pwd] !== md5($password)) + { + return array('error'=>"密码错误",'place'=>'password'); + } + return true; + } + public function updateStatus(EventInterface $e){ $id = (int)$e->getParam('id');