Merge branch 'development' of http://git.westgis.ac.cn:8000/lijianxuan/westdc-core into development

This commit is contained in:
Li Jianxuan 2015-01-19 16:45:53 +08:00
commit fa4aeaf15b
3 changed files with 79 additions and 64 deletions

View File

@ -7,6 +7,7 @@ use Zend\Authentication\Storage\Session as SessionStorage;
class Auth
{
public $auth;
function __construct($getAuthService = false)
{
$config = Config::get();
@ -25,11 +26,23 @@ class Auth
return $this->auth->clearIdentity();
}
public function getIdentity($field)
public function getIdentity($field = "")
{
if(empty($field))
return $this->auth->getIdentity();
if(isset($this->auth->getIdentity()->$field))
return $this->auth->getIdentity()->$field;
else
return null;
}
public function write($user)
{
if(is_array($user))
$user = (object)$user;
$this->auth->getStorage()->write($user);
}
}

View File

@ -10,12 +10,10 @@ use Zend\Authentication\Adapter\DbTable;
use Westdc\Helpers\Assist as view;
use Westdc\Helpers\Config;
use Westdc\Helpers\Dbh as dbh;
use Westdc\Db\Pdo as Db;
use Westdc\Db\Db as Zend_Db;
use Westdc\Mail\Mail;
use Westdc\User\Member;
class Account extends AbstractEventManager implements ServiceManagerAwareInterface
{
public $memberTable = "tbl_member";
@ -64,11 +62,15 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
return $rs->fetch();
}
//注册
/**
* 用户注册
* @param $data
* @return array
*/
public function register($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)
@ -80,43 +82,49 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
return $cache_data;
}
}
$results = $this->getEventManager()->trigger('register.checkUser', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
if(!is_array($cache_data))
{
return array('error'=>$cache_data);
}else{
return $cache_data;
}
}
/**
* 将数据分为两组,一组用于做注册成功后的登陆,一组用于写入数据库
* 写入数据库的数据需要完全重建一个新的,防止因为前端传入时有人恶意增加字段造成破坏
*/
$loginData = array(
'username'=>$data['username'],
'password'=>$data['password']
);
$data['password'] = md5($data['password']);
$data['usertype'] = "guest";
unset($data['confirm_password']);
$registerData = [
'username' => $data['username'],
'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();
$id = $dbh->insert($this->memberTable,$data,true);
$id = $dbh->insert($this->memberTable,$registerData,true);
if(!empty($id) && is_numeric($id))
{
$this->storeLogin($loginData);
if(isset($state['success']))
{
//$mb = new Member();
//$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]);
}
$data = $registerData;
$params = compact('data','id');
$results = $this->getEventManager()->trigger('register.success', $this, $params);
$this->getEventManager()->trigger('register.success', $this, $params);
return array("success" => 1);
}else{
if($id === false)
@ -128,11 +136,15 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
}
}//register
//登陆
/**
* 用户登陆
* @param $data
* @return array
*/
public function login($data)
{
$results = $this->getEventManager()->trigger('login.checkParam', $this, compact('data'));
$results = $this->getEventManager()->trigger('login.pre', $this, compact('data'));
$cache_data = $results->last();
if($cache_data !== true)
@ -149,14 +161,25 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
if(isset($state['success']))
{
//$mb = new Member();
//$mb->putcookie($data[$this->FieldUsername],md5($data[$this->FieldPasword]));
}
//$mb = new Member();
//$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]);
$user = (array)$state['user'];
$this->getEventManager()->trigger('login.success', $this, compact('user'));
}else{
$this->getEventManager()->trigger('login.failed', $this, compact('data'));
}
return $state;
return $state;
}//login
//storeLogin
/**
* 存储用户登陆信息
* 为了防止login中的用户信息检查不规范再加入一层内置的数据库权限检查以防通过漏洞登入系统
* @param $data
* @param bool $md5 是否对密码进行md5加密再校验
* @return array
*/
private function storeLogin($data,$md5 = true)
{
$auth = new AuthenticationService();
@ -184,24 +207,20 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa
;
$result = $authAdapter->authenticate();
$user = $authAdapter->getResultRowObject(null,array('password'));
if(!$result->isValid())
{
return array("error"=>"用户信息验证失败");
}
$email = $user->email;
$results = $this->getEventManager()->trigger('login.success.createAvatar', $this, compact('email'));
$user->avatar = $results->last();
$auth->getStorage()->write($user);
$id = $user->id;
$results = $this->getEventManager()->trigger('login.success.updateStatus', $this, compact('id'));
$user = $authAdapter->getResultRowObject(null,array('password'));
$auth->getStorage()->write($user);
return array('success'=>1);
}
return array(
'success'=>1,
'user' => $user
);
}//storeLogin
public function cookieLogin($data)
{

View File

@ -1,17 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2014/11/4
* Time: 11:23
*/
namespace Westdc\Service\ServiceAgent;
use Westdc\Member\Account as Westdc_Account;
class Account extends Westdc_Account{
}