更新Users操作类

This commit is contained in:
Li Jianxuan 2013-11-21 02:09:45 +00:00
parent 1c3075584e
commit 608b8f29f4
9 changed files with 127 additions and 19 deletions

View File

@ -1,27 +1,35 @@
<?php
namespace Users;
use helpers\View as view;
use helpers\dbh as dbh;
use Users\Member;
use Mail\Mail;
use \Helpers\View as view;
use \Helpers\dbh as dbh;
use \Users\Member;
use \Mail\Mail;
use \Users\Listener\AccountListener;
use \Users\Listener\EditListener;
use \Users\Listener\PwdListener;
class Account extends \Zend_Controller_Plugin_Abstract
{
public $memberTable = "users";
public $FieldIndex = "id";
public $FieldUsername = "username";
public $FieldPasword = "password";
public $FieldLastlogin = "ts_last_login";
public $FieldEmail = "email";
public $FieldLastloginIp = "last_login_ip";
public $GravatarEmailField = "gravatar_email";
public $FieldRealname = "realname";
public $RoleMember = "member";
private $db;
protected $events = NULL; //事件
function __construct($db = NULL)
/*
需要挂载的事件分别放在不同的listener中将各种操作进行模块化细分
*/
function __construct($initializingListener = TRUE,$db = NULL)
{
if(empty($db))
{
@ -32,7 +40,25 @@ class Account extends \Zend_Controller_Plugin_Abstract
$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)
@ -60,7 +86,6 @@ class Account extends \Zend_Controller_Plugin_Abstract
//注册
public function register($data)
{
$params = compact('data');
$results = $this->events()->trigger('register.checkParam', $this, $params);
$cache_data = $results->bottom();
@ -145,6 +170,7 @@ class Account extends \Zend_Controller_Plugin_Abstract
}//login
//storeLogin
//将登录信息保存在session和cookie中
public function storeLogin($data)
{
$auth = \Zend_Auth::getInstance();
@ -152,7 +178,16 @@ class Account extends \Zend_Controller_Plugin_Abstract
$authAdapter->setTableName($this->memberTable)
->setIdentityColumn($this->FieldUsername)
->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);
if ($result->isValid()) {

View File

@ -1,8 +1,8 @@
<?php
namespace Users\Listener;
use Users\Operation\RegisterOperate;
use Users\Operation\LoginOperate;
use \Users\Operation\RegisterOperate;
use \Users\Operation\LoginOperate;
class AccountListener implements \Zend_EventManager_ListenerAggregate
{

View File

@ -1,7 +1,7 @@
<?php
namespace Users\Listener;
use Users\Operation\EditOperate;
use \Users\Operation\EditOperate;
class EditListener implements \Zend_EventManager_ListenerAggregate
{

View File

@ -1,7 +1,7 @@
<?php
namespace Users\Listener;
use Users\Operation\PwdOperate;
use \Users\Operation\PwdOperate;
class PwdListener implements \Zend_EventManager_ListenerAggregate
{

View File

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

View File

@ -1,8 +1,8 @@
<?php
namespace Users\Operation;
use Mail\Mail;
use helpers\View as view;
use \Mail\Mail;
use \Helpers\View as view;
class EditOperate implements \Users\Event\EditEvent
{

View File

@ -1,8 +1,8 @@
<?php
namespace Users\Operation;
use Helpers\dbh as dbh;
use Users\Gravatar;
use \Helpers\dbh as dbh;
use \Users\Gravatar;
class LoginOperate implements \Users\Event\LoginEvent
{
@ -89,7 +89,7 @@ class LoginOperate implements \Users\Event\LoginEvent
$update = array(
$this->FieldLastlogin => date("Y-m-d H:i:s"),
$this->FieldLastloginIp => $_SERVER["REMOTE_ADDR"]
//$this->FieldLastloginIp => $_SERVER["REMOTE_ADDR"]
);
$dbh = new dbh();

View File

@ -1,8 +1,8 @@
<?php
namespace Users\Operation;
use Mail\Mail;
use Helpers\View as view;
use \Mail\Mail;
use \Helpers\View as view;
class RegisterOperate implements \Users\Event\RegisterEvent
{

View File

@ -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;
}
}
}