2011-11-04 07:30:44 +00:00
|
|
|
<?php
|
2013-12-27 02:39:18 +00:00
|
|
|
|
|
|
|
use Users\Member;
|
|
|
|
use Users\Account;
|
|
|
|
use Helpers\View as view;
|
2011-11-04 07:30:44 +00:00
|
|
|
class CustomControllerAclManager extends Zend_Controller_Plugin_Abstract
|
|
|
|
{
|
|
|
|
// default user role if not logged or (or invalid role found)
|
|
|
|
private $_defaultRole = 'guest';
|
|
|
|
// the action to dispatch if a user doesn't have sufficient privileges
|
|
|
|
private $_authController = array('module'=>'','controller' => 'account',
|
|
|
|
'action' => 'login');
|
2012-03-13 03:26:50 +00:00
|
|
|
|
2011-11-04 07:30:44 +00:00
|
|
|
public function __construct(Zend_Auth $auth)
|
|
|
|
{
|
|
|
|
$this->db=Zend_Registry::get('db');
|
|
|
|
$this->auth = $auth;
|
|
|
|
$this->acl = new Zend_Acl();
|
|
|
|
// add the different user roles
|
|
|
|
$this->acl->addRole(new Zend_Acl_Role($this->_defaultRole));
|
|
|
|
$this->acl->addRole(new Zend_Acl_Role('member'));
|
|
|
|
$this->acl->addRole(new Zend_Acl_Role('administrator'), 'member');
|
|
|
|
// add the resources we want to have control over
|
|
|
|
$this->acl->add(new Zend_Acl_Resource('account'));
|
|
|
|
$this->acl->add(new Zend_Acl_Resource('data'));
|
|
|
|
$this->acl->add(new Zend_Acl_Resource('water'));
|
|
|
|
$this->acl->add(new Zend_Acl_Resource('admin'));
|
|
|
|
$this->acl->add(new Zend_Acl_Resource('upload'));
|
2012-03-13 03:26:50 +00:00
|
|
|
$this->acl->add(new Zend_Acl_Resource('author'));
|
2012-10-22 08:31:49 +00:00
|
|
|
$this->acl->add(new Zend_Acl_Resource('heihe'));
|
2011-11-04 07:30:44 +00:00
|
|
|
// allow access to everything for all users by default
|
|
|
|
// except for the account management and administration areas
|
|
|
|
$this->acl->allow();
|
|
|
|
$this->acl->deny(null, 'account');
|
|
|
|
$this->acl->deny(null, 'admin');
|
2012-03-13 03:26:50 +00:00
|
|
|
$this->acl->deny(null, 'author');
|
2011-11-04 07:30:44 +00:00
|
|
|
// add an exception so guests can log in or register
|
|
|
|
// in order to gain privilege
|
2013-11-22 03:16:53 +00:00
|
|
|
$this->acl->allow('guest', 'account', array('login','oauth2login','callback',
|
2013-01-16 08:09:14 +00:00
|
|
|
'logout',
|
2013-06-20 08:59:32 +00:00
|
|
|
'captcha',
|
2011-11-04 07:30:44 +00:00
|
|
|
'fetchpwd',
|
|
|
|
'register',
|
|
|
|
'registercomplete'));
|
|
|
|
$this->acl->deny('guest','data',array('download','order'));
|
2012-03-13 03:26:50 +00:00
|
|
|
$this->acl->deny('guest','water',array('download','order'));
|
2012-10-22 08:31:49 +00:00
|
|
|
$this->acl->deny('guest','heihe',array('submit'));
|
2011-11-04 07:30:44 +00:00
|
|
|
// allow members access to the account management area
|
2012-03-13 03:26:50 +00:00
|
|
|
$this->acl->allow('guest','author',array('index'));
|
2011-11-04 07:30:44 +00:00
|
|
|
$this->acl->allow('member', 'account');
|
2012-03-13 03:26:50 +00:00
|
|
|
$this->acl->allow('member', 'author');
|
2011-11-04 07:30:44 +00:00
|
|
|
// allows administrators access to the admin area
|
|
|
|
$this->acl->allow('administrator', 'admin');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* preDispatch
|
|
|
|
*
|
|
|
|
* Before an action is dispatched, check if the current user
|
|
|
|
* has sufficient privileges. If not, dispatch the default
|
|
|
|
* action instead
|
|
|
|
*
|
|
|
|
* @param Zend_Controller_Request_Abstract $request
|
|
|
|
*/
|
|
|
|
public function preDispatch(Zend_Controller_Request_Abstract $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$phpSessId = $request->getParam('PHPSESSID');
|
2013-01-16 08:09:14 +00:00
|
|
|
|
2011-11-04 07:30:44 +00:00
|
|
|
if (!empty($phpSessId) && session_id() != $phpSessId) {
|
2013-01-16 08:09:14 +00:00
|
|
|
session_destroy();
|
|
|
|
session_id($phpSessId);
|
2014-06-12 15:10:00 +00:00
|
|
|
ini_set('session.cookie_domain', '.sanjiangyuan.org.cn' );
|
|
|
|
session_set_cookie_params(0, '/', '.sanjiangyuan.org.cn');
|
2013-01-16 08:09:14 +00:00
|
|
|
session_start();
|
2011-11-04 07:30:44 +00:00
|
|
|
}
|
|
|
|
// check if a user is logged in and has a valid role,
|
|
|
|
// otherwise, assign them the default role (guest)
|
2013-01-24 09:33:42 +00:00
|
|
|
|
|
|
|
if(!$this->auth->hasIdentity())
|
2011-11-04 07:30:44 +00:00
|
|
|
{
|
2013-12-27 02:39:18 +00:00
|
|
|
$member = new Member();
|
|
|
|
|
|
|
|
if($member->checkcookie())
|
2013-01-24 09:33:42 +00:00
|
|
|
{
|
2013-12-27 02:39:18 +00:00
|
|
|
$data = array(
|
|
|
|
'username' => $member->user,
|
|
|
|
'password' => $member->srpwd
|
|
|
|
);
|
|
|
|
|
|
|
|
$account = new Account();
|
|
|
|
$status = $account->storeLogin($data,false);
|
|
|
|
|
|
|
|
if(isset($status['error']))
|
|
|
|
{
|
|
|
|
$auth = Zend_Auth::getInstance();
|
|
|
|
$auth->clearIdentity();
|
|
|
|
Member::flushcookie();
|
2013-01-24 09:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-04 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->auth->hasIdentity())
|
|
|
|
$role = $this->auth->getIdentity()->usertype;
|
|
|
|
else
|
|
|
|
$role = $this->_defaultRole;
|
|
|
|
if (!$this->acl->hasRole($role))
|
|
|
|
$role = $this->_defaultRole;
|
|
|
|
// the ACL resource is the requested controller name
|
|
|
|
$resource = $request->controller;
|
|
|
|
if ($request->module<>"default") $resource=$request->module;
|
|
|
|
// the ACL privilege is the requested action name
|
|
|
|
$privilege = $request->action;
|
2011-11-18 03:34:33 +00:00
|
|
|
if ($request->module<>"default") $privilege = $request->controller;
|
2011-11-04 07:30:44 +00:00
|
|
|
// if we haven't explicitly added the resource, check
|
|
|
|
// 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']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|