2014-11-04 09:05:21 +00:00
|
|
|
<?php
|
|
|
|
namespace Westdc\Authentication;
|
|
|
|
|
|
|
|
use Zend\Permissions\Acl\Acl;
|
|
|
|
use Westdc\Helpers\Assist as view;
|
|
|
|
use Westdc\Member\Cookie;
|
2015-01-12 09:13:14 +00:00
|
|
|
use Westdc\User\Account;
|
2014-11-04 09:05:21 +00:00
|
|
|
|
|
|
|
class AuthenticationService
|
|
|
|
{
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
public $acl;
|
|
|
|
|
|
|
|
protected $role;
|
|
|
|
|
|
|
|
public $loginRouterName = "login";
|
2015-01-19 12:18:46 +00:00
|
|
|
public $logoutRouterName = "logout";
|
2014-11-04 09:05:21 +00:00
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
//初始化配置
|
|
|
|
$this->configure();
|
|
|
|
$this->role = new \stdClass();
|
|
|
|
$this->role->guest = 'guest';
|
|
|
|
|
|
|
|
$user = view::User();
|
|
|
|
|
|
|
|
if(!$user)
|
|
|
|
{
|
|
|
|
$this->role->current = 'guest';
|
|
|
|
}else{
|
|
|
|
$this->role->current = $user->usertype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run($e)
|
|
|
|
{
|
|
|
|
$module = $e->getRouteMatch()->getParam('module');
|
|
|
|
$namespace = $e->getRouteMatch()->getParam('__NAMESPACE__');
|
|
|
|
$controller = $e->getRouteMatch()->getParam('controller');
|
|
|
|
$action = $e->getRouteMatch()->getParam('action');
|
2014-12-24 07:59:42 +00:00
|
|
|
|
|
|
|
if($module == 'Engine' && $namespace == 'ConsoleApp')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-04 09:05:21 +00:00
|
|
|
|
2015-01-19 13:12:40 +00:00
|
|
|
//view::Dump($e->getRouteMatch()->getMatchedRouteName() . ":" . $controller."-".$action,false);
|
2014-12-13 14:53:47 +00:00
|
|
|
|
2015-01-19 12:18:46 +00:00
|
|
|
if($rsp = $this->preCookieCheck($e) !== false)
|
2015-01-19 13:12:40 +00:00
|
|
|
{
|
2015-01-19 12:18:46 +00:00
|
|
|
return $rsp;
|
2015-01-19 13:12:40 +00:00
|
|
|
}
|
2014-11-04 09:05:21 +00:00
|
|
|
|
|
|
|
try{
|
|
|
|
if(!$this->acl->hasResource($controller))
|
|
|
|
{
|
|
|
|
$this->badRequest($e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->acl->isAllowed($this->role->current,$controller) === true)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
if($this->acl->isAllowed($this->role->current,$controller,$action) === true)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
$this->response($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception $e) {
|
|
|
|
$this->badRequest($e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-19 12:18:46 +00:00
|
|
|
public function preCookieCheck($e)
|
2014-11-04 09:05:21 +00:00
|
|
|
{
|
|
|
|
if(!view::User())
|
|
|
|
{
|
|
|
|
$mb = new Cookie;
|
|
|
|
//view::Dump($mb->checkcookie());
|
|
|
|
if($mb->checkcookie())
|
|
|
|
{
|
|
|
|
$account = new Account();
|
2015-01-12 09:13:14 +00:00
|
|
|
$account->cookieLogin(array('username'=>$mb->user));
|
2015-01-19 12:18:46 +00:00
|
|
|
|
|
|
|
$response = $e->getResponse();
|
|
|
|
$response->setStatusCode(200);
|
|
|
|
$response->sendHeaders();
|
|
|
|
|
|
|
|
$layout = $e->getViewModel();
|
|
|
|
|
|
|
|
$viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager');
|
|
|
|
$partial = $viewHelperManager->get('partial');
|
|
|
|
|
|
|
|
$page_content = $partial(
|
|
|
|
'layout/layout/message',
|
|
|
|
array(
|
|
|
|
'message' => '您的账号已自动登陆',
|
|
|
|
'url'=> [
|
|
|
|
['title' => '立即跳转', 'url' => $_SERVER['REQUEST_URI']],
|
|
|
|
['title'=>'退出登陆','url'=>$e->getRouter()->assemble(array(), array('name' => $this->logoutRouterName))]
|
|
|
|
],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$layout->setVariable('content',$page_content);
|
|
|
|
$layout->setTemplate('layout/layout');
|
|
|
|
|
|
|
|
$e->stopPropagation();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
2014-11-04 09:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 13:12:40 +00:00
|
|
|
|
|
|
|
return false;
|
2014-11-04 09:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function response($e)
|
|
|
|
{
|
2014-11-19 04:21:05 +00:00
|
|
|
|
2014-11-04 09:05:21 +00:00
|
|
|
//用户已经登录的情况
|
|
|
|
if(view::User() !== false)
|
|
|
|
{
|
|
|
|
$this->badRequest($e,403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//没有登录的情况
|
|
|
|
if(view::isXmlHttpRequest())
|
|
|
|
{
|
|
|
|
|
|
|
|
}else{
|
2014-11-19 04:21:05 +00:00
|
|
|
|
2014-11-04 09:05:21 +00:00
|
|
|
$response = $e->getResponse();
|
|
|
|
$response->setStatusCode(404);
|
|
|
|
$response->sendHeaders();
|
|
|
|
|
|
|
|
$layout = $e->getViewModel();
|
|
|
|
|
|
|
|
$viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager');
|
|
|
|
$partial = $viewHelperManager->get('partial');
|
|
|
|
|
|
|
|
$page_content = $partial(
|
|
|
|
'layout/layout/message',
|
|
|
|
array(
|
|
|
|
'message' => '请先登陆',
|
|
|
|
'url'=> $e->getRouter()->assemble(array(), array('name' => $this->loginRouterName))."?href=".$_SERVER['REQUEST_URI'],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$layout->setVariable('content',$page_content);
|
|
|
|
$layout->setTemplate('layout/layout');
|
|
|
|
|
|
|
|
$e->stopPropagation();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function badRequest($e,$type = 404)
|
|
|
|
{
|
|
|
|
$response = $e->getResponse();
|
|
|
|
$response->setStatusCode(404);
|
|
|
|
$response->sendHeaders();
|
|
|
|
|
|
|
|
$layout = $e->getViewModel();
|
|
|
|
|
|
|
|
$viewHelperManager = $e->getApplication()->getServiceManager()->get('viewHelperManager');
|
|
|
|
$partial = $viewHelperManager->get('partial');
|
|
|
|
|
|
|
|
if($type == 404)
|
|
|
|
{
|
|
|
|
$page_content = $partial(
|
|
|
|
'error/404',
|
|
|
|
array(
|
|
|
|
'message' => 'This page has been eaten by dinosaurs',
|
|
|
|
'controller'=>$controller = $e->getRouteMatch()->getParam('controller'),
|
|
|
|
'display_exceptions' => true,
|
|
|
|
'reason' => 'error-controller-invalid',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}else{
|
|
|
|
$page_content = $partial(
|
|
|
|
'error/404',
|
|
|
|
array(
|
|
|
|
'message' => '您没有权限访问此页面',
|
|
|
|
'controller'=>$controller = $e->getRouteMatch()->getParam('controller'),
|
|
|
|
'reason' => 'error-controller-invalid',
|
|
|
|
'display_exceptions' => true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$layout->setVariable('content',$page_content);
|
|
|
|
$layout->setTemplate('layout/layout');
|
|
|
|
|
|
|
|
$e->stopPropagation();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
//加载配置
|
|
|
|
public function configure()
|
|
|
|
{
|
|
|
|
//初始化ACL
|
|
|
|
$this->acl = new Acl();
|
|
|
|
$this->acl->deny();
|
|
|
|
|
|
|
|
//加载资源
|
|
|
|
new AclResource($this->acl);
|
|
|
|
|
|
|
|
//加载权限
|
|
|
|
new AclAuthorize($this->acl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|