2019-01-17 11:05:47 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\project\middleware;
|
|
|
|
|
|
|
|
|
|
use app\common\Model\ProjectNode;
|
2019-01-29 18:13:18 +08:00
|
|
|
|
use service\JwtService;
|
2019-01-17 11:05:47 +08:00
|
|
|
|
use service\NodeService;
|
|
|
|
|
use think\Request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 系统权限访问管理
|
|
|
|
|
* Class Auth
|
|
|
|
|
* @package app\admin\middleware
|
|
|
|
|
*/
|
|
|
|
|
class Auth
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param \Closure $next
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
|
* @throws \think\exception\DbException
|
|
|
|
|
*/
|
|
|
|
|
public function handle($request, \Closure $next)
|
|
|
|
|
{
|
|
|
|
|
list($module, $controller, $action) = [$request->module(), $request->controller(), $request->action()];
|
|
|
|
|
$access = $this->buildAuth($node = NodeService::parseNodeStr("{$module}/{$controller}/{$action}"));
|
|
|
|
|
$currentOrganizationCode = $request->header('organizationCode');
|
|
|
|
|
if ($currentOrganizationCode) {
|
2019-01-29 18:13:18 +08:00
|
|
|
|
setCurrentOrganizationCode($currentOrganizationCode);
|
2019-01-17 11:05:47 +08:00
|
|
|
|
}
|
2019-01-29 18:13:18 +08:00
|
|
|
|
$authorization = $request->header('Authorization');
|
|
|
|
|
$accessToken = '';
|
|
|
|
|
if ($authorization) {
|
|
|
|
|
$accessToken = explode(' ', $authorization)[1];
|
|
|
|
|
}
|
|
|
|
|
$data = JwtService::decodeToken($accessToken);
|
2019-01-17 11:05:47 +08:00
|
|
|
|
// 登录状态检查
|
2019-01-29 18:13:18 +08:00
|
|
|
|
if (!empty($access['is_login'])) {
|
|
|
|
|
$isError = isError($data);
|
|
|
|
|
if ($isError) {
|
|
|
|
|
if ($data['errno'] == 3) {
|
|
|
|
|
$msg = ['code' => 4010, 'msg' => 'accessToken过期'];
|
|
|
|
|
return json($msg);
|
|
|
|
|
}
|
|
|
|
|
$msg = ['code' => 401, 'msg' => 'token过期,请重新登录'];
|
|
|
|
|
return json($msg);
|
|
|
|
|
}
|
|
|
|
|
setCurrentMember(get_object_vars($data->data));
|
2019-01-17 11:05:47 +08:00
|
|
|
|
}
|
|
|
|
|
// 访问权限检查
|
|
|
|
|
if (!empty($access['is_auth']) && !auth($node, 'project')) {
|
|
|
|
|
return json(['code' => 403, 'msg' => '无权限操作资源,访问被拒绝']);
|
|
|
|
|
}
|
|
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据节点获取对应权限配置
|
|
|
|
|
* @param string $node 权限节点
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
|
* @throws \think\exception\DbException
|
|
|
|
|
*/
|
|
|
|
|
private function buildAuth($node)
|
|
|
|
|
{
|
|
|
|
|
$info = ProjectNode::cache(true, 30)->where(['node' => $node])->find();
|
|
|
|
|
return [
|
|
|
|
|
'is_menu' => intval(!empty($info['is_menu'])),
|
|
|
|
|
'is_auth' => intval(!empty($info['is_auth'])),
|
|
|
|
|
'is_login' => empty($info['is_auth']) ? intval(!empty($info['is_login'])) : 1,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|