2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\index\middleware\system;
|
|
|
|
|
|
|
|
|
|
use app\common\library\Auth;
|
2022-11-28 19:11:12 +08:00
|
|
|
use app\common\library\ResultCode;
|
2022-08-19 19:48:37 +08:00
|
|
|
use support\View;
|
|
|
|
|
use Webman\MiddlewareInterface;
|
|
|
|
|
use Webman\Http\Response;
|
|
|
|
|
use Webman\Http\Request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 管理员权限
|
|
|
|
|
* @package app\admin\middleware\system
|
|
|
|
|
* @author meystack <
|
|
|
|
|
*/
|
|
|
|
|
class IndexPermissions implements MiddlewareInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 控制器登录鉴权
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public bool $needLogin = false;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 禁止登录重复
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public array $repeatLogin = ['login', 'register'];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 非鉴权方法
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public array $noNeedAuth = [];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 跳转URL地址
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public string $JumpUrl = '/user/index';
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验权限
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param callable $handler
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function process(Request $request, callable $handler): Response
|
|
|
|
|
{
|
|
|
|
|
$app = request()->getApp();
|
|
|
|
|
$controller = request()->getController();
|
|
|
|
|
$action = request()->getAction();
|
|
|
|
|
|
|
|
|
|
// 控制器是否存在
|
|
|
|
|
$className = '\app' . $app . '\\controller\\' . $controller;
|
|
|
|
|
$className = str_replace('/', '\\', $className);
|
|
|
|
|
|
|
|
|
|
if (class_exists($className)) {
|
|
|
|
|
$refClass = new \ReflectionClass($className);
|
|
|
|
|
$property = $refClass->getDefaultProperties();
|
|
|
|
|
$this->needLogin = $property['needLogin'] ?? false;
|
|
|
|
|
$this->noNeedAuth = $property['noNeedAuth'] ?? [];
|
|
|
|
|
$this->repeatLogin = $property['repeatLogin'] ?? ['login', 'register'];
|
2022-08-22 19:07:32 +08:00
|
|
|
$this->JumpUrl = $property['JumpUrl'] ?? '/user/index';
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否验证登录器
|
|
|
|
|
$auth = Auth::instance();
|
|
|
|
|
if ($auth->isLogin()) {
|
2022-11-28 19:11:12 +08:00
|
|
|
$request->user_id = $auth->userData['id'];
|
|
|
|
|
$request->userData = $auth->userData;
|
|
|
|
|
// 禁止重复登录
|
2022-08-19 19:48:37 +08:00
|
|
|
if (in_array($action, $this->repeatLogin)) {
|
|
|
|
|
return redirect($this->JumpUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 19:11:12 +08:00
|
|
|
View::assign('user', $auth->userData);
|
2022-08-19 19:48:37 +08:00
|
|
|
} else {
|
|
|
|
|
if ($this->needLogin && !in_array($action, $this->noNeedAuth)) {
|
2022-11-28 19:11:12 +08:00
|
|
|
if (\request()->isAjax()) {
|
|
|
|
|
return json(ResultCode::PLEASELOGININ);
|
|
|
|
|
} else {
|
|
|
|
|
return redirect('/user/login');
|
|
|
|
|
}
|
2022-08-19 19:48:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $handler($request);
|
|
|
|
|
}
|
|
|
|
|
}
|