增加了在preDispatch Hook中增加了flash session bug的处理

This commit is contained in:
Li Jianxuan 2011-11-04 07:30:44 +00:00
parent 6a8d4b34b4
commit 9d86bfc6f2
1 changed files with 103 additions and 96 deletions

View File

@ -1,96 +1,103 @@
<?php <?php
class CustomControllerAclManager extends Zend_Controller_Plugin_Abstract class CustomControllerAclManager extends Zend_Controller_Plugin_Abstract
{ {
// default user role if not logged or (or invalid role found) // default user role if not logged or (or invalid role found)
private $_defaultRole = 'guest'; private $_defaultRole = 'guest';
// the action to dispatch if a user doesn't have sufficient privileges // the action to dispatch if a user doesn't have sufficient privileges
private $_authController = array('module'=>'','controller' => 'account', private $_authController = array('module'=>'','controller' => 'account',
'action' => 'login'); 'action' => 'login');
public function __construct(Zend_Auth $auth) public function __construct(Zend_Auth $auth)
{ {
$this->db=Zend_Registry::get('db'); $this->db=Zend_Registry::get('db');
$this->auth = $auth; $this->auth = $auth;
$this->acl = new Zend_Acl(); $this->acl = new Zend_Acl();
// add the different user roles // add the different user roles
$this->acl->addRole(new Zend_Acl_Role($this->_defaultRole)); $this->acl->addRole(new Zend_Acl_Role($this->_defaultRole));
$this->acl->addRole(new Zend_Acl_Role('member')); $this->acl->addRole(new Zend_Acl_Role('member'));
$this->acl->addRole(new Zend_Acl_Role('administrator'), 'member'); $this->acl->addRole(new Zend_Acl_Role('administrator'), 'member');
// add the resources we want to have control over // add the resources we want to have control over
$this->acl->add(new Zend_Acl_Resource('account')); $this->acl->add(new Zend_Acl_Resource('account'));
$this->acl->add(new Zend_Acl_Resource('data')); $this->acl->add(new Zend_Acl_Resource('data'));
$this->acl->add(new Zend_Acl_Resource('water')); $this->acl->add(new Zend_Acl_Resource('water'));
$this->acl->add(new Zend_Acl_Resource('admin')); $this->acl->add(new Zend_Acl_Resource('admin'));
// allow access to everything for all users by default $this->acl->add(new Zend_Acl_Resource('upload'));
// except for the account management and administration areas // allow access to everything for all users by default
$this->acl->allow(); // except for the account management and administration areas
$this->acl->deny(null, 'account'); $this->acl->allow();
$this->acl->deny(null, 'admin'); $this->acl->deny(null, 'account');
// add an exception so guests can log in or register $this->acl->deny(null, 'admin');
// in order to gain privilege // add an exception so guests can log in or register
$this->acl->allow('guest', 'account', array('login', // in order to gain privilege
'fetchpwd', $this->acl->allow('guest', 'account', array('login',
'register', 'fetchpwd',
'registercomplete')); 'register',
$this->acl->deny('guest','data',array('download','order')); 'registercomplete'));
$this->acl->deny('guest','water',array('download','order')); $this->acl->deny('guest','data',array('download','order'));
// allow members access to the account management area $this->acl->deny('guest','water',array('download','order'));
$this->acl->allow('member', 'account'); // allow members access to the account management area
// allows administrators access to the admin area $this->acl->allow('member', 'account');
$this->acl->allow('administrator', 'admin'); // allows administrators access to the admin area
$this->acl->allow('administrator', 'admin');
} }
/** /**
* preDispatch * preDispatch
* *
* Before an action is dispatched, check if the current user * Before an action is dispatched, check if the current user
* has sufficient privileges. If not, dispatch the default * has sufficient privileges. If not, dispatch the default
* action instead * action instead
* *
* @param Zend_Controller_Request_Abstract $request * @param Zend_Controller_Request_Abstract $request
*/ */
public function preDispatch(Zend_Controller_Request_Abstract $request) public function preDispatch(Zend_Controller_Request_Abstract $request)
{ {
// check if a user is logged in and has a valid role,
// otherwise, assign them the default role (guest) $phpSessId = $request->getParam('PHPSESSID');
$mb = new member(); if (!empty($phpSessId) && session_id() != $phpSessId) {
$mb->db=$this->db; session_destroy();
session_id($phpSessId);
if($mb->checkcookie()) session_start();
{ }
$auth = Zend_Auth::getInstance(); // check if a user is logged in and has a valid role,
$authAdapter = new Zend_Auth_Adapter_DbTable($this->db); // otherwise, assign them the default role (guest)
$authAdapter->setTableName('users') $mb = new member();
->setIdentityColumn('username') $mb->db=$this->db;
->setCredentialColumn('password');
$authAdapter->setIdentity($mb->user)->setCredential($mb->srpwd); if($mb->checkcookie())
$result = $auth->authenticate($authAdapter); {
if ($result->isValid()) { $auth = Zend_Auth::getInstance();
$data = $authAdapter->getResultRowObject(null,'password'); $authAdapter = new Zend_Auth_Adapter_DbTable($this->db);
$auth->getStorage()->write($data); $authAdapter->setTableName('users')
$this->db->query("update users set ts_last_login=now() where username=?",array($mb->user)); ->setIdentityColumn('username')
} ->setCredentialColumn('password');
} $authAdapter->setIdentity($mb->user)->setCredential($mb->srpwd);
$result = $auth->authenticate($authAdapter);
if ($this->auth->hasIdentity()) if ($result->isValid()) {
$role = $this->auth->getIdentity()->usertype; $data = $authAdapter->getResultRowObject(null,'password');
else $auth->getStorage()->write($data);
$role = $this->_defaultRole; $this->db->query("update users set ts_last_login=now() where username=?",array($mb->user));
if (!$this->acl->hasRole($role)) }
$role = $this->_defaultRole; }
// the ACL resource is the requested controller name
$resource = $request->controller; if ($this->auth->hasIdentity())
if ($request->module<>"default") $resource=$request->module; $role = $this->auth->getIdentity()->usertype;
// the ACL privilege is the requested action name else
$privilege = $request->action; $role = $this->_defaultRole;
// if we haven't explicitly added the resource, check if (!$this->acl->hasRole($role))
// the default global permissions $role = $this->_defaultRole;
if (!$this->acl->has($resource)) // the ACL resource is the requested controller name
$resource = null; $resource = $request->controller;
// access denied - reroute the request to the default action handler if ($request->module<>"default") $resource=$request->module;
if (!$this->acl->isAllowed($role, $resource, $privilege)) { // the ACL privilege is the requested action name
$request->setModuleName($this->_authController['module']); $privilege = $request->action;
$request->setControllerName($this->_authController['controller']); // if we haven't explicitly added the resource, check
$request->setActionName($this->_authController['action']); // the default global permissions
} if (!$this->acl->has($resource))
} $resource = null;
} // access denied - reroute the request to the default action handler
if (!$this->acl->isAllowed($role, $resource, $privilege)) {
$request->setModuleName($this->_authController['module']);
$request->setControllerName($this->_authController['controller']);
$request->setActionName($this->_authController['action']);
}
}
}