使用事件驱动的登录模块,修复cookie无法保存的bug,添加了验证码助手Helpers\Captcha
This commit is contained in:
parent
c2225d4e21
commit
920b71add7
|
@ -1,7 +1,9 @@
|
|||
<?php
|
||||
use Helpers\View as view;
|
||||
use Mail\Mail;
|
||||
use Open\Client;
|
||||
use Users\Account;
|
||||
use Helpers\Captcha;
|
||||
use Helpers\View as view;
|
||||
|
||||
class AccountController extends Zend_Controller_Action
|
||||
{
|
||||
|
@ -289,9 +291,6 @@ class AccountController extends Zend_Controller_Action
|
|||
|
||||
function loginAction()
|
||||
{
|
||||
|
||||
$success=false;
|
||||
|
||||
$options = array(
|
||||
'module' => $this->_request->getModuleName(),
|
||||
'controller' => $this->_request->getControllerName(),
|
||||
|
@ -322,7 +321,7 @@ class AccountController extends Zend_Controller_Action
|
|||
$this->view->href = $tohref;
|
||||
}
|
||||
|
||||
$captcha = $this->loadCaptcha();
|
||||
$captcha = new Captcha();
|
||||
|
||||
$submit = $this->_getParam("submit");
|
||||
if(!empty($submit))
|
||||
|
@ -331,57 +330,31 @@ class AccountController extends Zend_Controller_Action
|
|||
$password = trim($this->_request->getParam('password'));
|
||||
$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 = "请输入用户名";
|
||||
$this->view->error = $status;
|
||||
$this->view->captcha = $captcha->setCaptcha();
|
||||
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->captcha = $captcha->setCaptcha();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!$this->login($username,$password))
|
||||
{
|
||||
$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()
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
</div>
|
||||
<? }else{ ?>
|
||||
<div class="alert alert-error alert-login">
|
||||
<?= $this->error ?>
|
||||
<?= $this->error['error'] ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<div class="control-group">
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -179,11 +179,11 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
|||
->setIdentityColumn($this->FieldUsername)
|
||||
->setCredentialColumn($this->FieldPasword);
|
||||
|
||||
if($data[$this->FieldPasword] == 0)
|
||||
if($data[$this->FieldPasword] === 0)
|
||||
{
|
||||
$password = "0";
|
||||
}else{
|
||||
if($md5verify === false)
|
||||
if($md5verify == false)
|
||||
{
|
||||
$password = $data[$this->FieldPasword];
|
||||
}else{
|
||||
|
@ -192,8 +192,8 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
|||
}
|
||||
|
||||
$authAdapter->setIdentity($data[$this->FieldUsername])->setCredential($password);
|
||||
|
||||
$result = $auth->authenticate($authAdapter);
|
||||
|
||||
if ($result->isValid()) {
|
||||
|
||||
$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'));
|
||||
|
||||
return array('success'=>1);
|
||||
}else{
|
||||
return array("error"=>"用户信息验证失败,请重新登录");
|
||||
}
|
||||
return array('error'=>'处理中发现错误,请重试');
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
namespace \Users;
|
||||
namespace Users;
|
||||
|
||||
class Member
|
||||
{
|
||||
|
|
|
@ -45,9 +45,9 @@ class LoginOperate implements \Users\Event\LoginEvent
|
|||
|
||||
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
|
||||
|
||||
|
||||
//检查token表记录
|
||||
public function checkOAuthToken()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue