Permission.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. if (strpos($controller, ".") !== false) {
  20. $paths = array_filter(explode(".", $controller));
  21. $classpath = "\app\admin\controller\\" . implode("\\", $paths);
  22. $ref = new \ReflectionClass($classpath);
  23. } else {
  24. $ref = new \ReflectionClass("\app\admin\controller\\" . $controller);
  25. }
  26. $action = $request->action();
  27. $comment = $ref->getMethod($action)->getDocComment();
  28. $old_auth_url = "";
  29. if ($comment) {
  30. if (preg_match("/(?<=@auth {{).*?(?=}})/", $comment, $result)) {
  31. //成功提取@auth {{}}出中间内容则需要判断权限
  32. $old_auth_url = strtolower($result[0]);
  33. //对比权限
  34. }
  35. }
  36. $module = strtolower(app("http")->getName());
  37. $controller = strtolower($request->controller());
  38. $action = strtolower($request->action());
  39. $url = sprintf("/%s/%s/%s", $module, $controller, $action);
  40. if (!\app\common\api\MenuApi::chkPermission($url, $old_auth_url))
  41. return json(["code" => 403, "msg" => "没有权限"])->code(403);
  42. return $next($request);
  43. }
  44. }