CheckAuth.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\admin\middleware;
  3. use app\common\service\AuthService;
  4. use app\common\traits\JumpTrait;
  5. use app\Request;
  6. use Closure;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. class CheckAuth
  11. {
  12. use JumpTrait;
  13. /**
  14. * @throws ModelNotFoundException
  15. * @throws DbException
  16. * @throws DataNotFoundException
  17. */
  18. public function handle(Request $request, Closure $next)
  19. {
  20. $adminUserInfo = $request->adminUserInfo;
  21. if (empty($adminUserInfo)) return $next($request);
  22. $adminConfig = config('admin');
  23. $adminId = $adminUserInfo['id'];
  24. $authService = app(AuthService::class, ['adminId' => $adminId]);
  25. $currentNode = $authService->getCurrentNode();
  26. $currentController = parse_name($request->controller());
  27. if (!in_array($currentController, $adminConfig['no_auth_controller']) && !in_array($currentNode, $adminConfig['no_auth_node'])) {
  28. $check = $authService->checkNode($currentNode);
  29. !$check && $this->error('无权限访问');
  30. // 判断是否为演示环境
  31. if (env('EASYADMIN.IS_DEMO', false) && $request->isPost()) {
  32. if (!in_array($currentNode, ['system.log/record', 'mall.goods/aiOptimization'])) $this->error('演示环境下不允许修改');
  33. }
  34. }
  35. return $next($request);
  36. }
  37. }