Permission.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\middleware;
  4. /**
  5. * Description of Permission
  6. * 访问权限中间件
  7. * @author sgq
  8. */
  9. class Permission {
  10. /**
  11. * 处理请求
  12. *
  13. * @param \think\Request $request
  14. * @param \Closure $next
  15. * @return Response
  16. */
  17. public function handle($request, \Closure $next) {
  18. $controller = $request->controller();
  19. $ref = new \ReflectionClass("\app\admin\controller\\" . $controller);
  20. $action = $request->action();
  21. $comment = $ref->getMethod($action)->getDocComment();
  22. $old_auth_url = "";
  23. if ($comment) {
  24. if (preg_match("/(?<=@auth {{).*?(?=}})/", $comment, $result)) {
  25. //成功提取@auth {{}}出中间内容则需要判断权限
  26. $old_auth_url = strtolower($result[0]);
  27. //对比权限
  28. }
  29. }
  30. $module = strtolower(app("http")->getName());
  31. $controller = strtolower($request->controller());
  32. $action = strtolower($request->action());
  33. $url = sprintf("/%s/%s/%s", $module, $controller, $action);
  34. if (!\app\common\api\MenuApi::chkPermission($url, $old_auth_url))
  35. return json(["code" => 403, "msg" => "没有权限"])->code(403);
  36. return $next($request);
  37. }
  38. }