1
This commit is contained in:
43
app/admin/middleware/CheckAuth.php
Normal file
43
app/admin/middleware/CheckAuth.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware;
|
||||
|
||||
use app\common\service\AuthService;
|
||||
use app\common\traits\JumpTrait;
|
||||
use app\Request;
|
||||
use Closure;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
|
||||
class CheckAuth
|
||||
{
|
||||
use JumpTrait;
|
||||
|
||||
/**
|
||||
* @throws ModelNotFoundException
|
||||
* @throws DbException
|
||||
* @throws DataNotFoundException
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$adminUserInfo = $request->adminUserInfo;
|
||||
if (empty($adminUserInfo)) return $next($request);
|
||||
$adminConfig = config('admin');
|
||||
$adminId = $adminUserInfo['id'];
|
||||
|
||||
$authService = app(AuthService::class, ['adminId' => $adminId]);
|
||||
$currentNode = $authService->getCurrentNode();
|
||||
$currentController = parse_name($request->controller());
|
||||
|
||||
if (!in_array($currentController, $adminConfig['no_auth_controller']) && !in_array($currentNode, $adminConfig['no_auth_node'])) {
|
||||
$check = $authService->checkNode($currentNode);
|
||||
!$check && $this->error('无权限访问');
|
||||
// 判断是否为演示环境
|
||||
if (env('EASYADMIN.IS_DEMO', false) && $request->isPost()) {
|
||||
if (!in_array($currentNode, ['system.log/record', 'mall.goods/aiOptimization'])) $this->error('演示环境下不允许修改');
|
||||
}
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
21
app/admin/middleware/CheckInstall.php
Normal file
21
app/admin/middleware/CheckInstall.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware;
|
||||
|
||||
use app\common\traits\JumpTrait;
|
||||
use app\Request;
|
||||
use Closure;
|
||||
|
||||
class CheckInstall
|
||||
{
|
||||
use JumpTrait;
|
||||
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$controller = $request->controller();
|
||||
if (!is_file(root_path() . 'config' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'lock' . DIRECTORY_SEPARATOR . 'install.lock')) {
|
||||
if ($controller != 'Install') return redirect('/install');
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
60
app/admin/middleware/CheckLogin.php
Normal file
60
app/admin/middleware/CheckLogin.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware;
|
||||
|
||||
use app\common\traits\JumpTrait;
|
||||
use app\Request;
|
||||
use Closure;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use app\admin\service\annotation\MiddlewareAnnotation;
|
||||
|
||||
class CheckLogin
|
||||
{
|
||||
use JumpTrait;
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$controller = $request->controller();
|
||||
if (empty($controller)) return $next($request);
|
||||
if (str_contains($controller, '.')) $controller = str_replace('.', '\\', $controller);
|
||||
$action = $request->action();
|
||||
$controllerClass = 'app\\admin\\controller\\' . $controller;
|
||||
$classObj = new ReflectionClass($controllerClass);
|
||||
$properties = $classObj->getDefaultProperties();
|
||||
// 整个控制器是否忽略登录
|
||||
$ignoreLogin = $properties['ignoreLogin'] ?? false;
|
||||
$adminUserInfo = session('admin');
|
||||
if (!$ignoreLogin) {
|
||||
$noNeedCheck = $properties['noNeedCheck'] ?? [];
|
||||
if (in_array($action, $noNeedCheck)) {
|
||||
return $next($request);
|
||||
}
|
||||
try {
|
||||
$reflectionMethod = new \ReflectionMethod($controllerClass, $action);
|
||||
$attributes = $reflectionMethod->getAttributes(MiddlewareAnnotation::class);
|
||||
foreach ($attributes as $attribute) {
|
||||
$annotation = $attribute->newInstance();
|
||||
$_ignore = (array)$annotation->ignore;
|
||||
// 控制器中的某个方法忽略登录
|
||||
if (in_array('LOGIN', $_ignore)) return $next($request);
|
||||
}
|
||||
}catch (\Throwable) {
|
||||
}
|
||||
if (empty($adminUserInfo)) {
|
||||
return redirect(__url('login/index'));
|
||||
}
|
||||
// 判断是否登录过期
|
||||
$expireTime = $adminUserInfo['expire_time'];
|
||||
if ($expireTime !== 0 && time() > $expireTime) {
|
||||
session('admin', null);
|
||||
$this->error('登录已过期,请重新登录', [], __url(env('EASYADMIN.ADMIN') . '/login/index'));
|
||||
}
|
||||
}
|
||||
$request->adminUserInfo = $adminUserInfo ?: [];
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
45
app/admin/middleware/RateLimiting.php
Normal file
45
app/admin/middleware/RateLimiting.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware;
|
||||
|
||||
use app\common\traits\JumpTrait;
|
||||
use app\Request;
|
||||
use Closure;
|
||||
use Wolfcode\RateLimiting\Bootstrap;
|
||||
|
||||
class RateLimiting
|
||||
{
|
||||
use JumpTrait;
|
||||
|
||||
/**
|
||||
* 启用限流器需要开启Redis
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): mixed
|
||||
{
|
||||
// 是否启用限流器
|
||||
if (!env('RATE_LIMITING_STATUS', false)) return $next($request);
|
||||
if ($request->method() == 'GET') return $next($request);
|
||||
$controller = $request->controller();
|
||||
$module = app('http')->getName();
|
||||
$appNamespace = config('app.app_namespace');
|
||||
$controllerClass = "app\\{$module}\\controller\\{$controller}{$appNamespace}";
|
||||
$controllerClass = str_replace('.', '\\', $controllerClass);
|
||||
$action = $request->action();
|
||||
try {
|
||||
Bootstrap::init($controllerClass, $action, [
|
||||
# Redis 相关配置
|
||||
'host' => env('REDIS_HOST', '127.0.0.1'),
|
||||
'port' => (int)env('REDIS_PORT', 6379),
|
||||
'password' => env('REDIS_PASSWORD', ''),
|
||||
'prefix' => env('REDIS_PREFIX', ''),
|
||||
'database' => (int)env('REDIS_DATABASE', 0),
|
||||
]);
|
||||
}catch (\Throwable $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
104
app/admin/middleware/SystemLog.php
Normal file
104
app/admin/middleware/SystemLog.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware;
|
||||
|
||||
use app\admin\service\annotation\ControllerAnnotation;
|
||||
use app\admin\service\annotation\MiddlewareAnnotation;
|
||||
use app\admin\service\annotation\NodeAnnotation;
|
||||
use app\admin\service\SystemLogService;
|
||||
use app\common\traits\JumpTrait;
|
||||
use app\Request;
|
||||
use Closure;
|
||||
use ReflectionException;
|
||||
|
||||
class SystemLog
|
||||
{
|
||||
use JumpTrait;
|
||||
|
||||
/**
|
||||
* 敏感信息字段,日志记录时需要加密
|
||||
* @var array
|
||||
*/
|
||||
protected array $sensitiveParams = [
|
||||
'password',
|
||||
'password_again',
|
||||
'phone',
|
||||
'mobile',
|
||||
];
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$response = $next($request);
|
||||
if (!env('APP_ADMIN_SYSTEM_LOG', true)) return $response;
|
||||
$params = $request->param();
|
||||
if (isset($params['s'])) unset($params['s']);
|
||||
foreach ($params as $key => $val) {
|
||||
in_array($key, $this->sensitiveParams) && $params[$key] = "***********";
|
||||
}
|
||||
$method = strtolower($request->method());
|
||||
$url = $request->url();
|
||||
|
||||
if (env('APP_DEBUG')) {
|
||||
trace(['url' => $url, 'method' => $method, 'params' => $params,], 'requestDebugInfo');
|
||||
}
|
||||
if ($request->isAjax()) {
|
||||
if (in_array($method, ['post', 'put', 'delete'])) {
|
||||
|
||||
$title = '';
|
||||
try {
|
||||
$pathInfo = $request->pathinfo();
|
||||
$pathInfoExp = explode('/', $pathInfo);
|
||||
$_action = end($pathInfoExp) ?? '';
|
||||
$pathInfoExp = explode('.', $pathInfoExp[0] ?? '');
|
||||
$_name = $pathInfoExp[0] ?? '';
|
||||
$_controller = ucfirst($pathInfoExp[1] ?? '');
|
||||
$className = $_controller ? "app\admin\controller\\{$_name}\\{$_controller}" : "app\admin\controller\\{$_name}";
|
||||
if ($_name && $_action) {
|
||||
$reflectionMethod = new \ReflectionMethod($className, $_action);
|
||||
$attributes = $reflectionMethod->getAttributes(MiddlewareAnnotation::class);
|
||||
foreach ($attributes as $attribute) {
|
||||
$annotation = $attribute->newInstance();
|
||||
$_ignore = (array)$annotation->ignore;
|
||||
if (in_array('log', array_map('strtolower', $_ignore))) return $response;
|
||||
}
|
||||
$controllerTitle = $nodeTitle = '';
|
||||
$controllerAttributes = (new \ReflectionClass($className))->getAttributes(ControllerAnnotation::class);
|
||||
$actionAttributes = $reflectionMethod->getAttributes(NodeAnnotation::class);
|
||||
foreach ($controllerAttributes as $controllerAttribute) {
|
||||
$controllerAnnotation = $controllerAttribute->newInstance();
|
||||
$controllerTitle = $controllerAnnotation->title ?? '';
|
||||
}
|
||||
foreach ($actionAttributes as $actionAttribute) {
|
||||
$actionAnnotation = $actionAttribute->newInstance();
|
||||
$nodeTitle = $actionAnnotation->title ?? '';
|
||||
}
|
||||
$title = $controllerTitle . ' - ' . $nodeTitle;
|
||||
}
|
||||
}catch (\Throwable $exception) {
|
||||
}
|
||||
|
||||
$ip = $request->ip();
|
||||
// 限制记录的响应内容,避免过大
|
||||
$_response = json_encode($response->getData(), JSON_UNESCAPED_UNICODE);
|
||||
$_response = mb_substr($_response, 0, 3000, 'utf-8');
|
||||
|
||||
$data = [
|
||||
'admin_id' => session('admin.id'),
|
||||
'title' => $title,
|
||||
'url' => $url,
|
||||
'method' => $method,
|
||||
'ip' => $ip,
|
||||
'content' => json_encode($params, JSON_UNESCAPED_UNICODE),
|
||||
'response' => $_response,
|
||||
'useragent' => $request->server('HTTP_USER_AGENT'),
|
||||
'create_time' => time(),
|
||||
];
|
||||
SystemLogService::instance()->save($data);
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user