更新Users操作类
This commit is contained in:
parent
1c3075584e
commit
608b8f29f4
|
@ -1,27 +1,35 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users;
|
namespace Users;
|
||||||
|
|
||||||
use helpers\View as view;
|
use \Helpers\View as view;
|
||||||
use helpers\dbh as dbh;
|
use \Helpers\dbh as dbh;
|
||||||
use Users\Member;
|
use \Users\Member;
|
||||||
use Mail\Mail;
|
use \Mail\Mail;
|
||||||
|
use \Users\Listener\AccountListener;
|
||||||
|
use \Users\Listener\EditListener;
|
||||||
|
use \Users\Listener\PwdListener;
|
||||||
|
|
||||||
class Account extends \Zend_Controller_Plugin_Abstract
|
class Account extends \Zend_Controller_Plugin_Abstract
|
||||||
{
|
{
|
||||||
public $memberTable = "users";
|
public $memberTable = "users";
|
||||||
|
public $FieldIndex = "id";
|
||||||
public $FieldUsername = "username";
|
public $FieldUsername = "username";
|
||||||
public $FieldPasword = "password";
|
public $FieldPasword = "password";
|
||||||
public $FieldLastlogin = "ts_last_login";
|
public $FieldLastlogin = "ts_last_login";
|
||||||
public $FieldEmail = "email";
|
public $FieldEmail = "email";
|
||||||
public $FieldLastloginIp = "last_login_ip";
|
public $FieldLastloginIp = "last_login_ip";
|
||||||
public $GravatarEmailField = "gravatar_email";
|
public $GravatarEmailField = "gravatar_email";
|
||||||
|
public $FieldRealname = "realname";
|
||||||
|
|
||||||
public $RoleMember = "member";
|
public $RoleMember = "member";
|
||||||
|
|
||||||
private $db;
|
private $db;
|
||||||
protected $events = NULL; //事件
|
protected $events = NULL; //事件
|
||||||
|
|
||||||
function __construct($db = NULL)
|
/*
|
||||||
|
需要挂载的事件分别放在不同的listener中,将各种操作进行模块化细分
|
||||||
|
*/
|
||||||
|
function __construct($initializingListener = TRUE,$db = NULL)
|
||||||
{
|
{
|
||||||
if(empty($db))
|
if(empty($db))
|
||||||
{
|
{
|
||||||
|
@ -32,7 +40,25 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
||||||
|
|
||||||
$this->config = \Zend_Registry::get('config');
|
$this->config = \Zend_Registry::get('config');
|
||||||
|
|
||||||
|
if($initializingListener === TRUE)
|
||||||
|
{
|
||||||
|
$this->loadListener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadListener()
|
||||||
|
{
|
||||||
|
//主要操作,账号注册,登录,设置session等
|
||||||
|
$AccountListener = new AccountListener();
|
||||||
|
@$this->events()->attachAggregate($AccountListener);
|
||||||
|
|
||||||
|
//账户编辑
|
||||||
|
$EditListener = new EditListener();
|
||||||
|
@$this->events()->attachAggregate($EditListener);
|
||||||
|
|
||||||
|
//账户安全性(找回密码)
|
||||||
|
$PwdListener = new PwdListener();
|
||||||
|
@$this->events()->attachAggregate($PwdListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function events(\Zend_EventManager_EventCollection $events = NULL)
|
public function events(\Zend_EventManager_EventCollection $events = NULL)
|
||||||
|
@ -60,7 +86,6 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
||||||
//注册
|
//注册
|
||||||
public function register($data)
|
public function register($data)
|
||||||
{
|
{
|
||||||
|
|
||||||
$params = compact('data');
|
$params = compact('data');
|
||||||
$results = $this->events()->trigger('register.checkParam', $this, $params);
|
$results = $this->events()->trigger('register.checkParam', $this, $params);
|
||||||
$cache_data = $results->bottom();
|
$cache_data = $results->bottom();
|
||||||
|
@ -145,6 +170,7 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
||||||
}//login
|
}//login
|
||||||
|
|
||||||
//storeLogin
|
//storeLogin
|
||||||
|
//将登录信息保存在session和cookie中
|
||||||
public function storeLogin($data)
|
public function storeLogin($data)
|
||||||
{
|
{
|
||||||
$auth = \Zend_Auth::getInstance();
|
$auth = \Zend_Auth::getInstance();
|
||||||
|
@ -152,7 +178,16 @@ class Account extends \Zend_Controller_Plugin_Abstract
|
||||||
$authAdapter->setTableName($this->memberTable)
|
$authAdapter->setTableName($this->memberTable)
|
||||||
->setIdentityColumn($this->FieldUsername)
|
->setIdentityColumn($this->FieldUsername)
|
||||||
->setCredentialColumn($this->FieldPasword);
|
->setCredentialColumn($this->FieldPasword);
|
||||||
$authAdapter->setIdentity($data[$this->FieldUsername])->setCredential(md5($data[$this->FieldPasword]));
|
|
||||||
|
if($data[$this->FieldPasword] == 0)
|
||||||
|
{
|
||||||
|
$password = "0";
|
||||||
|
}else{
|
||||||
|
$password = md5($data[$this->FieldPasword]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$authAdapter->setIdentity($data[$this->FieldUsername])->setCredential($password);
|
||||||
|
|
||||||
$result = $auth->authenticate($authAdapter);
|
$result = $auth->authenticate($authAdapter);
|
||||||
if ($result->isValid()) {
|
if ($result->isValid()) {
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Listener;
|
namespace Users\Listener;
|
||||||
|
|
||||||
use Users\Operation\RegisterOperate;
|
use \Users\Operation\RegisterOperate;
|
||||||
use Users\Operation\LoginOperate;
|
use \Users\Operation\LoginOperate;
|
||||||
|
|
||||||
class AccountListener implements \Zend_EventManager_ListenerAggregate
|
class AccountListener implements \Zend_EventManager_ListenerAggregate
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Listener;
|
namespace Users\Listener;
|
||||||
|
|
||||||
use Users\Operation\EditOperate;
|
use \Users\Operation\EditOperate;
|
||||||
|
|
||||||
class EditListener implements \Zend_EventManager_ListenerAggregate
|
class EditListener implements \Zend_EventManager_ListenerAggregate
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Listener;
|
namespace Users\Listener;
|
||||||
|
|
||||||
use Users\Operation\PwdOperate;
|
use \Users\Operation\PwdOperate;
|
||||||
|
|
||||||
class PwdListener implements \Zend_EventManager_ListenerAggregate
|
class PwdListener implements \Zend_EventManager_ListenerAggregate
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users;
|
namespace \Users;
|
||||||
|
|
||||||
class Member
|
class Member
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Operation;
|
namespace Users\Operation;
|
||||||
|
|
||||||
use Mail\Mail;
|
use \Mail\Mail;
|
||||||
use helpers\View as view;
|
use \Helpers\View as view;
|
||||||
|
|
||||||
class EditOperate implements \Users\Event\EditEvent
|
class EditOperate implements \Users\Event\EditEvent
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Operation;
|
namespace Users\Operation;
|
||||||
|
|
||||||
use Helpers\dbh as dbh;
|
use \Helpers\dbh as dbh;
|
||||||
use Users\Gravatar;
|
use \Users\Gravatar;
|
||||||
|
|
||||||
class LoginOperate implements \Users\Event\LoginEvent
|
class LoginOperate implements \Users\Event\LoginEvent
|
||||||
{
|
{
|
||||||
|
@ -89,7 +89,7 @@ class LoginOperate implements \Users\Event\LoginEvent
|
||||||
|
|
||||||
$update = array(
|
$update = array(
|
||||||
$this->FieldLastlogin => date("Y-m-d H:i:s"),
|
$this->FieldLastlogin => date("Y-m-d H:i:s"),
|
||||||
$this->FieldLastloginIp => $_SERVER["REMOTE_ADDR"]
|
//$this->FieldLastloginIp => $_SERVER["REMOTE_ADDR"]
|
||||||
);
|
);
|
||||||
|
|
||||||
$dbh = new dbh();
|
$dbh = new dbh();
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Users\Operation;
|
namespace Users\Operation;
|
||||||
|
|
||||||
use Mail\Mail;
|
use \Mail\Mail;
|
||||||
use Helpers\View as view;
|
use \Helpers\View as view;
|
||||||
|
|
||||||
class RegisterOperate implements \Users\Event\RegisterEvent
|
class RegisterOperate implements \Users\Event\RegisterEvent
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
namespace Users;
|
||||||
|
|
||||||
|
use \Helpers\View as view;
|
||||||
|
use \Helpers\dbh as dbh;
|
||||||
|
use \Users\Account;
|
||||||
|
use \Helpers\Table;
|
||||||
|
|
||||||
|
/*
|
||||||
|
对 \Users\Account 再次进行抽象,满足后台调用的需求,屏蔽一些错误等等
|
||||||
|
!!!!important!!!!大部分操作直接来自 \User\Account,慎重修改
|
||||||
|
*/
|
||||||
|
class Users extends \Zend_Controller_Plugin_Abstract
|
||||||
|
{
|
||||||
|
private $db;
|
||||||
|
protected $events = NULL; //事件
|
||||||
|
public $table;
|
||||||
|
|
||||||
|
public $account;
|
||||||
|
|
||||||
|
function __construct($accountClass = FALSE,$db = NULL)
|
||||||
|
{
|
||||||
|
if(empty($db))
|
||||||
|
{
|
||||||
|
$this->db = \Zend_Registry::get('db');
|
||||||
|
}else{
|
||||||
|
$this->db = $db;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->table = new Table();
|
||||||
|
|
||||||
|
$this->config = \Zend_Registry::get('config');
|
||||||
|
|
||||||
|
if($accountClass === TRUE)
|
||||||
|
{
|
||||||
|
$this->account = new Account();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//通过email地址返回用户信息是否存在
|
||||||
|
public function userExists($email = NULL)
|
||||||
|
{
|
||||||
|
if(empty($email))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($this->account))
|
||||||
|
{
|
||||||
|
$account = new Account(FALSE);
|
||||||
|
}else{
|
||||||
|
$account = $this->account;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = "SELECT * FROM {$account->memberTable} WHERE {$account->FieldEmail}=? LIMIT 1";
|
||||||
|
$sth = $this->db->prepare($sql);
|
||||||
|
$sth->execute(array($email));
|
||||||
|
$row = $sth->fetch();
|
||||||
|
|
||||||
|
unset($account);
|
||||||
|
|
||||||
|
if(isset($row['id']) && !empty($row['id']))
|
||||||
|
{
|
||||||
|
return $row;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue