From bd7e404a5f4593c69e0a6ef77e7e747834147767 Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Mon, 19 Jan 2015 20:18:46 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=A7=A3=E5=86=B3cookie=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Authentication/AuthenticationService.php | 39 ++++++++++++++-- Westdc/Member/Account.php | 21 +++------ Westdc/Member/Cookie.php | 45 +++++++++++-------- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/Westdc/Authentication/AuthenticationService.php b/Westdc/Authentication/AuthenticationService.php index 80216aa..00a80b8 100644 --- a/Westdc/Authentication/AuthenticationService.php +++ b/Westdc/Authentication/AuthenticationService.php @@ -15,6 +15,7 @@ class AuthenticationService protected $role; public $loginRouterName = "login"; + public $logoutRouterName = "logout"; function __construct() { @@ -47,7 +48,8 @@ class AuthenticationService // view::Dump($e->getRouteMatch()->getMatchedRouteName() . ":" . $controller."-".$action,false); - $this->preCookieCheck(); + if($rsp = $this->preCookieCheck($e) !== false) + return $rsp; try{ if(!$this->acl->hasResource($controller)) @@ -68,14 +70,13 @@ class AuthenticationService } } }catch (Exception $e) { - //echo 'Caught exception: ', $e->getMessage(), "\n"; $this->badRequest($e); return; } } - public function preCookieCheck() + public function preCookieCheck($e) { if(!view::User()) { @@ -85,8 +86,40 @@ class AuthenticationService { $account = new Account(); $account->cookieLogin(array('username'=>$mb->user)); + + $response = $e->getResponse(); + $response->setStatusCode(200); + $response->sendHeaders(); + + $layout = $e->getViewModel(); + + $viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager'); + $partial = $viewHelperManager->get('partial'); + + $page_content = $partial( + 'layout/layout/message', + array( + 'message' => '您的账号已自动登陆', + 'url'=> [ + ['title' => '立即跳转', 'url' => $_SERVER['REQUEST_URI']], + ['title'=>'退出登陆','url'=>$e->getRouter()->assemble(array(), array('name' => $this->logoutRouterName))] + ], + ) + ); + + $layout->setVariable('content',$page_content); + $layout->setTemplate('layout/layout'); + + $e->stopPropagation(); + + return $response; + } } + else + { + return false; + } } public function response($e) diff --git a/Westdc/Member/Account.php b/Westdc/Member/Account.php index 7150fa1..ce6c771 100644 --- a/Westdc/Member/Account.php +++ b/Westdc/Member/Account.php @@ -11,8 +11,6 @@ use Westdc\Helpers\Assist as view; use Westdc\Helpers\Config; use Westdc\Helpers\Dbh as dbh; use Westdc\Db\Db as Zend_Db; -use Westdc\Mail\Mail; -use Westdc\User\Member; class Account extends AbstractEventManager implements ServiceManagerAwareInterface { @@ -95,17 +93,8 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa $registerData = [ 'username' => $data['username'], 'password' => md5($data['password']), - 'usertype' => 'guest', + 'usertype' => $this->RoleMember, '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(); @@ -118,8 +107,8 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa if(isset($state['success'])) { - //$mb = new Member(); - //$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); + $mb = new Member(); + $mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); } $data = $registerData; @@ -161,8 +150,8 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa if(isset($state['success'])) { - //$mb = new Member(); - //$mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); + $mb = new Cookie(); + $mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); $user = (array)$state['user']; $this->getEventManager()->trigger('login.success', $this, compact('user')); diff --git a/Westdc/Member/Cookie.php b/Westdc/Member/Cookie.php index 406d6c1..b859efc 100644 --- a/Westdc/Member/Cookie.php +++ b/Westdc/Member/Cookie.php @@ -3,10 +3,11 @@ namespace Westdc\Member; use Westdc\Helpers\Config; use Westdc\Db\Pdo as Db; +use Zend\Http\Cookies as ZendCookie; class Cookie { - var $ck='Dxe8SqIcmyUf'; + var $ck='ff08XearZpUkjl3H'; var $db; //传入PDO对象 var $mid; //会员ID @@ -29,12 +30,12 @@ class Cookie { $this->db = new Db(); $this->config = Config::get(); - - if(!empty($_COOKIE['scr'])) + + if(isset($_COOKIE['scr']) && !empty($_COOKIE['scr'])) { $this->scr = $_COOKIE['scr']; } - if(!empty($_COOKIE['user'])) + if(isset($_COOKIE['user']) && !empty($_COOKIE['user'])) { $this->user= $_COOKIE['user']; } @@ -48,7 +49,7 @@ class Cookie { $uname = $this->user; $hash = $this->scr; - + if(!empty($uname) && !empty($hash)) { if (preg_match("/[<|>|#|$|%|^|*|(|)|{|}|'|\"|;|:]/i",$uname) || preg_match("/[<|>|#|$|%|^|*|(|)|{|}|'|\"|;|:]/i",$hash)) @@ -61,7 +62,7 @@ class Cookie $rs = $this->db->query($sql); $row = $rs->fetch(); $scr = $this->makescr($row['userid'],$row['pwd']); - + if($hash == $scr) { $this->srpwd=$row['pwd']; @@ -75,26 +76,31 @@ class Cookie return false; }//exit }//function checkcookie - - /** - * putcookie - * - * 登陆成功后放置cookie,包含安全码 - * - * @param String $uname - * @param String $pwd - * @param Int $time - */ + + /** + * putcookie + * + * 登陆成功后放置cookie,包含安全码 + * + * @param $uname + * @param $pwd + * @param int $time + * @return bool + */ public function putcookie($uname,$pwd,$time = 604800) { try { - $scrString = $this->makescr($uname,$pwd);//加密验证串:防止用户密码被盗;防止伪造cookie。 - + $scrString = $this->makescr($uname,md5($pwd));//加密验证串:防止用户密码被盗;防止伪造cookie。 + if(!is_numeric($time)) { $time = 604800; } - + + //$client = new Client(); + //$client->addCookie('user',$uname,time()+$time,"/"); + //$client->addCookie('scr',$scrString,time()+$time,"/"); + setcookie('user',$uname,time()+$time,'/'); setcookie('scr',$scrString,time()+$time,'/'); @@ -110,6 +116,7 @@ class Cookie * * @param String $u * @param String $p + * @return string */ public function makescr($u,$p) { From 911e7d4043f34d28ee34622b06d4d1a8e2637ccc Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Mon, 19 Jan 2015 21:12:40 +0800 Subject: [PATCH 2/8] fixed bug in authenticationService --- Westdc/Authentication/AuthenticationService.php | 10 +++++----- Westdc/Helpers/Auth.php | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Westdc/Authentication/AuthenticationService.php b/Westdc/Authentication/AuthenticationService.php index 00a80b8..5058470 100644 --- a/Westdc/Authentication/AuthenticationService.php +++ b/Westdc/Authentication/AuthenticationService.php @@ -46,10 +46,12 @@ class AuthenticationService return true; } -// view::Dump($e->getRouteMatch()->getMatchedRouteName() . ":" . $controller."-".$action,false); + //view::Dump($e->getRouteMatch()->getMatchedRouteName() . ":" . $controller."-".$action,false); if($rsp = $this->preCookieCheck($e) !== false) + { return $rsp; + } try{ if(!$this->acl->hasResource($controller)) @@ -116,10 +118,8 @@ class AuthenticationService } } - else - { - return false; - } + + return false; } public function response($e) diff --git a/Westdc/Helpers/Auth.php b/Westdc/Helpers/Auth.php index 11c132c..c12b762 100644 --- a/Westdc/Helpers/Auth.php +++ b/Westdc/Helpers/Auth.php @@ -23,7 +23,8 @@ class Auth public function clearIndentity() { - return $this->auth->clearIdentity(); + $this->auth->clearIdentity(); + return true; } public function getIdentity($field = "") From cbe18f569902eab9b827e4ab7411fa76d0f967b0 Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Mon, 19 Jan 2015 22:03:34 +0800 Subject: [PATCH 3/8] fixed bug Account Service --- Westdc/Member/Account.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Westdc/Member/Account.php b/Westdc/Member/Account.php index ce6c771..5dfedd5 100644 --- a/Westdc/Member/Account.php +++ b/Westdc/Member/Account.php @@ -111,7 +111,6 @@ class Account extends AbstractEventManager implements ServiceManagerAwareInterfa $mb->putcookie($data[$this->FieldUsername],$data[$this->FieldPasword]); } - $data = $registerData; $params = compact('data','id'); $this->getEventManager()->trigger('register.success', $this, $params); return array("success" => 1); From 84611b854794b56a8c0b4b879f033479de52646a Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 23 Jan 2015 16:19:03 +0800 Subject: [PATCH 4/8] add singleton pattern in Db service of getPdo() function --- Westdc/Db/Dbh.php | 2 +- Westdc/Db/Pdo.php | 25 +++++++++++++++---------- Westdc/Member/Cookie.php | 4 ++-- Westdc/Service/ServiceAgent/Db.php | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Westdc/Db/Dbh.php b/Westdc/Db/Dbh.php index 9fe07b5..f12f429 100644 --- a/Westdc/Db/Dbh.php +++ b/Westdc/Db/Dbh.php @@ -11,7 +11,7 @@ class Dbh function __construct($db = NULL) { if($db == NULL) - $this->db = new PDO(); + $this->db = PDO::getInstance(); else $this->db = $db; } diff --git a/Westdc/Db/Pdo.php b/Westdc/Db/Pdo.php index ce6f3f5..4146ecc 100644 --- a/Westdc/Db/Pdo.php +++ b/Westdc/Db/Pdo.php @@ -3,26 +3,31 @@ namespace Westdc\Db; use Zend\Config\Config as Zend_Config; -class Pdo extends \PDO +class Pdo { - private $debug = 0; //调试模式 + + private static $_instance = NULL; - private $config_local_path = "config/autoload/local.php"; - - function __construct($DSN = NULL) + private function __construct($DSN = NULL) { - if (!empty($DSN)) { - parent::__construct($DSN); - } else { - $config_local = new Zend_Config(include $this->config_local_path); + + } + + public static function getInstance() + { + if (self::$_instance === null) { + $config_local = new Zend_Config(include "config/autoload/local.php"); $dsn = "pgsql:host={$config_local->db->hostname};" . "port={$config_local->db->port};" . "dbname={$config_local->db->database};" . "user={$config_local->db->username};" . "password={$config_local->db->password}"; - parent::__construct($dsn); + self::$_instance = new \PDO($dsn); } + + return self::$_instance; } + } \ No newline at end of file diff --git a/Westdc/Member/Cookie.php b/Westdc/Member/Cookie.php index 5d3f557..4d56afd 100644 --- a/Westdc/Member/Cookie.php +++ b/Westdc/Member/Cookie.php @@ -2,7 +2,7 @@ namespace Westdc\Member; use Westdc\Helpers\Config; -use Westdc\Db\PDO as Db; +use Westdc\Db\Pdo as Db; class Cookie { @@ -27,7 +27,7 @@ class Cookie function __construct() { - $this->db = new Db(); + $this->db = Db::getInstance(); $this->config = Config::get(); if(!empty($_COOKIE['scr'])) diff --git a/Westdc/Service/ServiceAgent/Db.php b/Westdc/Service/ServiceAgent/Db.php index 0792cfe..7faf5e8 100644 --- a/Westdc/Service/ServiceAgent/Db.php +++ b/Westdc/Service/ServiceAgent/Db.php @@ -21,7 +21,7 @@ class Db { public function getPdo() { - return new WestdcDb\Pdo; + return WestdcDb\Pdo::getInstance(); } public function getDbh() From 03cb772bfc01e3295111a1316a501283afd43e05 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 23 Jan 2015 16:27:48 +0800 Subject: [PATCH 5/8] fixed using error in Pdo class --- Westdc/Db/Pdo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Westdc/Db/Pdo.php b/Westdc/Db/Pdo.php index 8691867..0836108 100644 --- a/Westdc/Db/Pdo.php +++ b/Westdc/Db/Pdo.php @@ -1,7 +1,7 @@ Date: Sat, 24 Jan 2015 16:37:09 +0800 Subject: [PATCH 6/8] add singleton pattern in db service --- Westdc/Db/Db.php | 44 +++++++++++++----------------- Westdc/Service/ServiceAgent/Db.php | 5 +--- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Westdc/Db/Db.php b/Westdc/Db/Db.php index f1097ec..46f316d 100644 --- a/Westdc/Db/Db.php +++ b/Westdc/Db/Db.php @@ -1,43 +1,37 @@ getServiceManager()->get('Config'); - $config_local = $configService->get("local.php"); - - if(is_array($param) && count($param)>0) - { - $this->db = new Adapter($param); - }else{ - $this->db = new Adapter(array( + private static $_instance = NULL; + + private function __construct(){ + + } + + public static function getInstance(){ + + if (self::$_instance === NULL) { + + $config_local = new Zend_Config(include "config/autoload/local.php"); + + self::$_instance = new Adapter(array( 'driver' => $config_local->db->driver, 'hostname' => $config_local->db->hostname, - 'port' => $config_local->db->port, + 'port' => $config_local->db->port, 'database' => $config_local->db->database, 'username' => $config_local->db->username, 'password' => $config_local->db->password )); + } - $db = $this->db; + return self::$_instance; + } - - public function getAdapter() - { - return $this->db; - } - - public function setConfig() - { - - } } \ No newline at end of file diff --git a/Westdc/Service/ServiceAgent/Db.php b/Westdc/Service/ServiceAgent/Db.php index f415995..e480381 100644 --- a/Westdc/Service/ServiceAgent/Db.php +++ b/Westdc/Service/ServiceAgent/Db.php @@ -13,10 +13,7 @@ use Westdc\Db as WestdcDb; class Db { public function getZendDb(){ - - $dbObject = new WestdcDb\Db(); - - return $dbObject->getAdapter(); + return WestdcDb\Db::getInstance(); } public function getPdo() From 9361b1ba9b121b198d3e0ab8477e6cacd285bb3b Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Sun, 25 Jan 2015 17:19:22 +0800 Subject: [PATCH 7/8] change Db object import function in Account module --- Westdc/User/Account.php | 6 +++--- Westdc/User/Handle/LoginHandle.php | 2 +- Westdc/User/Handle/RegisterHandle.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Westdc/User/Account.php b/Westdc/User/Account.php index a8143f0..c5b85ec 100644 --- a/Westdc/User/Account.php +++ b/Westdc/User/Account.php @@ -34,7 +34,7 @@ class Account implements EventManagerAwareInterface function __construct() { - $this->db = new Db(); + $this->db = Db::getInstance(); $this->config = Config::get(); $Listener = new Listener(); @@ -173,8 +173,8 @@ class Account implements EventManagerAwareInterface { $auth = new AuthenticationService(); $auth->setStorage(new SessionStorage($this->config->session_namespace)); - - new Zend_Db($dbAdapter); + + $dbAdapter = Zend_Db::getInstance(); $authAdapter = new \Zend\Authentication\Adapter\DbTable( $dbAdapter, diff --git a/Westdc/User/Handle/LoginHandle.php b/Westdc/User/Handle/LoginHandle.php index 48d1f8d..7630bed 100644 --- a/Westdc/User/Handle/LoginHandle.php +++ b/Westdc/User/Handle/LoginHandle.php @@ -19,7 +19,7 @@ class LoginHandle function __construct() { - $this->db = new Db(); + $this->db = Db::getInstance(); } public function checkParam(EventInterface $e){ diff --git a/Westdc/User/Handle/RegisterHandle.php b/Westdc/User/Handle/RegisterHandle.php index 4b543cc..0ddc32d 100644 --- a/Westdc/User/Handle/RegisterHandle.php +++ b/Westdc/User/Handle/RegisterHandle.php @@ -21,7 +21,7 @@ class RegisterHandle function __construct($db = NULL) { - $this->db = new Db(); + $this->db = Db::getInstance(); $this->config = Config::get(); } From 79b7ab0030e402801dec0e44ec78fb0d69891875 Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Sun, 25 Jan 2015 19:53:13 +0800 Subject: [PATCH 8/8] add reset function of reviews in Review Service --- Westdc/Review/Review.php | 60 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/Westdc/Review/Review.php b/Westdc/Review/Review.php index 99362ea..3675250 100644 --- a/Westdc/Review/Review.php +++ b/Westdc/Review/Review.php @@ -163,12 +163,23 @@ class Review extends AbstractEventManager implements ServiceManagerAwareInterfac * @return bool */ public function cancel($id){ - if(!is_numeric($id) || $id<1) - return false; - $this->getEventManager()->trigger('review.canceled', $this, compact('id')); + if(is_numeric($id)) + { + $this->getEventManager()->trigger('review.canceled', $this, compact('id')); - return $this->changeStatus($id,self::REVIEW_STATUS_CANCELED); + return $this->changeStatus($id,self::REVIEW_STATUS_CANCELED); + }elseif(is_array($id)){ + foreach($id as $item){ + $item = (int)$item; + if($this->changeStatus($item,self::REVIEW_STATUS_CANCELED) === false) + return "ID:$item:取消失败"; + } + $this->getEventManager()->trigger('review.canceled', $this, compact('id')); + return true; + } + + return false; } /** @@ -198,8 +209,49 @@ class Review extends AbstractEventManager implements ServiceManagerAwareInterfac }//accept($id) + /** + * 重置评审,将评审的状态设置为投稿元数据状态 + * @param $id + * @return array|bool + */ public function reset($id){ + if(!is_array($id)) + { + if($this->getStatus($id) != self::REVIEW_STATUS_CANCELED) + return [ + '此条评审的当前状态已经不属于被取消的评审,可能已经由其他管理人员重置', + ]; + + $status = $this->changeStatus($id,self::REVIEW_STATUS_DEFAULT); + + if(false === $status) + return false; + + }else{ + $message = []; + foreach($id as $item) + { + $item = (int)$item; + if($this->getStatus($item) != self::REVIEW_STATUS_CANCELED){ + $message[] = "ID:$item:此条评审的当前状态已经不属于被取消的评审,可能已经由其他管理人员重置"; + continue; + } + + $status = $this->changeStatus($item,self::REVIEW_STATUS_DEFAULT); + + if(false === $status) + $message[] = "ID:$item:重置失败"; + } + + if(count($message) > 0) + return $message; + } + + + $this->getEventManager()->trigger('review.reset', $this, compact('id')); + + return true; } /**