change register function

This commit is contained in:
Jianxuan Li 2015-01-19 00:00:34 +08:00
parent 68f53c9d9a
commit fbe7ef3bfe
1 changed files with 51 additions and 28 deletions

View File

@ -62,24 +62,15 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
return $rs->fetch(); return $rs->fetch();
} }
//注册 /**
* 用户注册
* @param $data
* @return array
*/
public function register($data) public function register($data)
{ {
$params = compact('data'); $params = compact('data');
$results = $this->getEventManager()->trigger('register.checkParam', $this, $params); $results = $this->getEventManager()->trigger('register.pre', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
if(!is_array($cache_data))
{
return array('error'=>$cache_data);
}else{
return $cache_data;
}
}
$results = $this->getEventManager()->trigger('register.checkUser', $this, $params);
$cache_data = $results->last(); $cache_data = $results->last();
if($cache_data !== true) if($cache_data !== true)
@ -92,29 +83,48 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
} }
} }
/**
* 将数据分为两组,一组用于做注册成功后的登陆,一组用于写入数据库
* 写入数据库的数据需要完全重建一个新的,防止因为前端传入时有人恶意增加字段造成破坏
*/
$loginData = array( $loginData = array(
'username'=>$data['username'], 'username'=>$data['username'],
'password'=>$data['password'] 'password'=>$data['password']
); );
$data['password'] = md5($data['password']); $registerData = [
$data['usertype'] = "guest"; 'username' => $data['username'],
unset($data['confirm_password']); 'password' => md5($data['password']),
'usertype' => 'guest',
'email' => $data['email'],
'realname' => $data['realname'],
'unit' => $data['unit'],
'address' => $data['address'],
'project' => $data['project'],
'phone' => $data['phone'],
'project_id' => $data['project_id'],
'project_type' => $data['project_type'],
'project_title' => $data['project_title'],
'project_leader' => $data['project_leader'],
];
$dbh = new dbh(); $dbh = new dbh();
$id = $dbh->insert($this->memberTable,$data,true); $id = $dbh->insert($this->memberTable,$registerData,true);
if(!empty($id) && is_numeric($id)) if(!empty($id) && is_numeric($id))
{ {
$this->storeLogin($loginData); $this->storeLogin($loginData);
if(isset($state['success'])) if(isset($state['success']))
{ {
//$mb = new Member(); //$mb = new Member();
//$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); //$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]);
} }
$data = $registerData;
$params = compact('data','id'); $params = compact('data','id');
$results = $this->getEventManager()->trigger('register.success', $this, $params); $this->getEventManager()->trigger('register.success', $this, $params);
return array("success" => 1); return array("success" => 1);
}else{ }else{
if($id === false) if($id === false)
@ -127,7 +137,11 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
}//register }//register
//登陆 /**
* 用户登陆
* @param $data
* @return array
*/
public function login($data) public function login($data)
{ {
$results = $this->getEventManager()->trigger('login.pre', $this, compact('data')); $results = $this->getEventManager()->trigger('login.pre', $this, compact('data'));
@ -147,6 +161,9 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
if(isset($state['success'])) if(isset($state['success']))
{ {
//$mb = new Member();
//$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]);
$user = (array)$state['user']; $user = (array)$state['user'];
$this->getEventManager()->trigger('login.success', $this, compact('user')); $this->getEventManager()->trigger('login.success', $this, compact('user'));
}else{ }else{
@ -156,7 +173,13 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
return $state; return $state;
}//login }//login
//storeLogin /**
* 存储用户登陆信息
* 为了防止login中的用户信息检查不规范再加入一层内置的数据库权限检查以防通过漏洞登入系统
* @param $data
* @param bool $md5 是否对密码进行md5加密再校验
* @return array
*/
private function storeLogin($data,$md5 = true) private function storeLogin($data,$md5 = true)
{ {
$auth = new AuthenticationService(); $auth = new AuthenticationService();
@ -197,7 +220,7 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
'success'=>1, 'success'=>1,
'user' => $user 'user' => $user
); );
} }//storeLogin
public function cookieLogin($data) public function cookieLogin($data)
{ {