使用事件驱动的登录模块,修复cookie无法保存的bug,添加了验证码助手Helpers\Captcha

This commit is contained in:
Li Jianxuan 2013-12-26 02:50:10 +00:00
parent c2225d4e21
commit 920b71add7
6 changed files with 1041 additions and 1010 deletions

View File

@ -1,7 +1,9 @@
<?php <?php
use Helpers\View as view;
use Mail\Mail; use Mail\Mail;
use Open\Client; use Open\Client;
use Users\Account;
use Helpers\Captcha;
use Helpers\View as view;
class AccountController extends Zend_Controller_Action class AccountController extends Zend_Controller_Action
{ {
@ -289,9 +291,6 @@ class AccountController extends Zend_Controller_Action
function loginAction() function loginAction()
{ {
$success=false;
$options = array( $options = array(
'module' => $this->_request->getModuleName(), 'module' => $this->_request->getModuleName(),
'controller' => $this->_request->getControllerName(), 'controller' => $this->_request->getControllerName(),
@ -322,7 +321,7 @@ class AccountController extends Zend_Controller_Action
$this->view->href = $tohref; $this->view->href = $tohref;
} }
$captcha = $this->loadCaptcha(); $captcha = new Captcha();
$submit = $this->_getParam("submit"); $submit = $this->_getParam("submit");
if(!empty($submit)) if(!empty($submit))
@ -331,57 +330,31 @@ class AccountController extends Zend_Controller_Action
$password = trim($this->_request->getParam('password')); $password = trim($this->_request->getParam('password'));
$captchaword = trim($this->_request->getParam('captcha')); $captchaword = trim($this->_request->getParam('captcha'));
if(empty($username)) $account = new Account();
$status = $account->login(array(
'username' => $username,
'password' => $password
));
if(isset($status['error']))
{ {
$this->setCaptcha($captcha); $this->view->error = $status;
$this->view->error = "请输入用户名"; $this->view->captcha = $captcha->setCaptcha();
return true; return true;
} }
if(empty($password)) if(!$captcha->isValid($captchaword))
{ {
$this->setCaptcha($captcha);
$this->view->error = "请输入密码";
return true;
}
if(empty($captchaword))
{
$this->setCaptcha($captcha);
$this->view->error = "请输入验证码";
return true;
}
if(!isset($_SESSION['captcha']))
{
$_SESSION['captcha'] = md5(time());
}
if ($captchaword != $_SESSION['captcha']) {
$this->setCaptcha($captcha);
$this->view->error = "验证码错误"; $this->view->error = "验证码错误";
$this->view->captcha = $captcha->setCaptcha();
return true; return true;
} }
if (!$this->login($username,$password)) view::Post($this,"登录成功,正在跳转",$tohref);
{ return true;
$this->setCaptcha($captcha);
$this->view->error = "用户名或密码错误";
$this->view->userid = $username;
return true;
}
else
{
if(!empty($tohref))
{
view::Post($this,"登录成功,正在跳转",$tohref);
return true;
}
}
}else{
$this->setCaptcha($captcha);
} }
$this->view->captcha = $captcha->setCaptcha();
} }
function loadCaptcha() function loadCaptcha()

View File

@ -29,7 +29,7 @@
</div> </div>
<? }else{ ?> <? }else{ ?>
<div class="alert alert-error alert-login"> <div class="alert alert-error alert-login">
<?= $this->error ?> <?= $this->error['error'] ?>
</div> </div>
<?php } ?> <?php } ?>
<div class="control-group"> <div class="control-group">

View File

@ -0,0 +1,52 @@
<?php
namespace Helpers;
class Captcha extends \Zend_Controller_Plugin_Abstract
{
public $captcha;
private $sessionName = "captcha";
function __construct($db = NULL)
{
$this->loadCaptcha();
}
public function loadCaptcha()
{
$this->captcha = new \Zend_Captcha_Image(array(
'captcha' => 'Image',
'wordLen' => 4,
'fontsize'=>16,
'width' => 100,
'height' => 38,
'dotNoiseLevel'=>2,
'lineNoiseLevel'=>1,
'timeout' => 300,
'font' => '../data/fonts/ggbi.ttf',
'imgDir' => 'vdimg/',
'imgUrl' => '/vdimg',
));
}
public function setCaptcha(){
$this->captcha->generate();
$_SESSION[$this->sessionName] = $this->captcha->getWord();
$url = $this->captcha->getImgUrl()
.$this->captcha->getId()
.$this->captcha->getSuffix();
return $url;
}
public function isValid($captchaword)
{
if($captchaword == $_SESSION[$this->sessionName])
{
return true;
}else{
return false;
}
}
}

View File

@ -179,11 +179,11 @@ class Account extends \Zend_Controller_Plugin_Abstract
->setIdentityColumn($this->FieldUsername) ->setIdentityColumn($this->FieldUsername)
->setCredentialColumn($this->FieldPasword); ->setCredentialColumn($this->FieldPasword);
if($data[$this->FieldPasword] == 0) if($data[$this->FieldPasword] === 0)
{ {
$password = "0"; $password = "0";
}else{ }else{
if($md5verify === false) if($md5verify == false)
{ {
$password = $data[$this->FieldPasword]; $password = $data[$this->FieldPasword];
}else{ }else{
@ -192,8 +192,8 @@ class Account extends \Zend_Controller_Plugin_Abstract
} }
$authAdapter->setIdentity($data[$this->FieldUsername])->setCredential($password); $authAdapter->setIdentity($data[$this->FieldUsername])->setCredential($password);
$result = $auth->authenticate($authAdapter); $result = $auth->authenticate($authAdapter);
if ($result->isValid()) { if ($result->isValid()) {
$user = $authAdapter->getResultRowObject(null,$this->FieldPasword); $user = $authAdapter->getResultRowObject(null,$this->FieldPasword);
@ -206,6 +206,8 @@ class Account extends \Zend_Controller_Plugin_Abstract
@$results = $this->events()->trigger('login.success.updateStatus', $this, compact('id')); @$results = $this->events()->trigger('login.success.updateStatus', $this, compact('id'));
return array('success'=>1); return array('success'=>1);
}else{
return array("error"=>"用户信息验证失败,请重新登录");
} }
return array('error'=>'处理中发现错误,请重试'); return array('error'=>'处理中发现错误,请重试');
} }

View File

@ -1,5 +1,5 @@
<?php <?php
namespace \Users; namespace Users;
class Member class Member
{ {

View File

@ -45,9 +45,9 @@ class LoginOperate implements \Users\Event\LoginEvent
if(!empty($data['username'])) if(!empty($data['username']))
{ {
if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/",$data['username'])) if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]{2,15}$/",$data['username']))
{ {
return array('error'=>"用户名应当以字母开头,由字母数字和下划线组成,并且长度在5到25个字符之间",'place'=>'username'); return array('error'=>"用户名应当以字母开头,由字母数字和下划线组成,并且长度在3到25个字符之间",'place'=>'username');
} }
} }
@ -106,6 +106,10 @@ class LoginOperate implements \Users\Event\LoginEvent
}//loginSuccess }//loginSuccess
//检查token表记录
public function checkOAuthToken()
{
}
} }