AuthMiddleware.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Ycbl\AdminAuth\Middleware;
  3. use FastRoute\Dispatcher;
  4. use Hyperf\Di\Annotation\AnnotationCollector;
  5. use Hyperf\Di\Annotation\Inject;
  6. use Hyperf\HttpServer\Contract\RequestInterface;
  7. use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
  8. use Hyperf\HttpServer\Router\Dispatched;
  9. use Psr\Container\ContainerInterface;
  10. use Psr\Http\Message\ResponseInterface;
  11. use Psr\Http\Message\ServerRequestInterface;
  12. use Psr\Http\Server\MiddlewareInterface;
  13. use Psr\Http\Server\RequestHandlerInterface;
  14. use Ycbl\AdminAuth\Annotation\Auth as AuthAnnotation;
  15. use Ycbl\AdminAuth\Auth;
  16. class AuthMiddleware implements MiddlewareInterface
  17. {
  18. /**
  19. * @var ContainerInterface
  20. */
  21. protected $container;
  22. /**
  23. * @var RequestInterface
  24. */
  25. protected $request;
  26. /**
  27. * @var HttpResponse
  28. */
  29. protected $response;
  30. const NEED_LOGIN = 1001;
  31. const NEED_RIGHT = 1002;
  32. /**
  33. * @Inject
  34. * @var Auth
  35. */
  36. protected $auth;
  37. public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
  38. {
  39. $this->container = $container;
  40. $this->response = $response;
  41. $this->request = $request;
  42. }
  43. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  44. {
  45. [$no_need_login, $no_need_right] = $this->checkWhiteList($request);
  46. // 无需登录直接执行
  47. if ($no_need_login){
  48. return $handler->handle($request);
  49. }
  50. //未登录返回错误信息
  51. if (!$this->auth->isLogin()){
  52. return $this->errorResult(self::NEED_LOGIN);
  53. }
  54. //无需权限认证直接执行
  55. if ($no_need_right){
  56. return $handler->handle($request);
  57. }
  58. $uri = $this->request->path();
  59. if (!$this->auth->check($uri)){
  60. return $this->errorResult(self::NEED_RIGHT);
  61. }
  62. return $handler->handle($request);
  63. }
  64. public function errorResult($error_code)
  65. {
  66. if ($error_code == self::NEED_LOGIN) {
  67. return $this->response->json(['code' => $error_code, 'msg' => '请先登录']);
  68. } else {
  69. return $this->response->json(['code' => $error_code, 'msg' => '您没有权限']);
  70. }
  71. }
  72. public function checkWhiteList(ServerRequestInterface $request)
  73. {
  74. $dispatched = $request->getAttribute(Dispatched::class);
  75. if ($dispatched->status !== Dispatcher::FOUND) {
  76. return true;
  77. }
  78. $action = $dispatched->handler->callback;
  79. if ($action instanceof \Closure){
  80. return true;
  81. }
  82. if (is_string($action)) {
  83. $division = strstr($action, '@') ? '@' : "::";
  84. $action = explode($division, $action);
  85. }
  86. list($class, $method) = $action;
  87. $annotations = AnnotationCollector::getClassMethodAnnotation($class, $method);
  88. if (isset($annotations[AuthAnnotation::class])) {
  89. $white_list = $annotations[AuthAnnotation::class];
  90. $no_need_login = $white_list->noNeedLogin;
  91. $no_need_right = $white_list->noNeedRight;
  92. } else {
  93. $no_need_login = false;
  94. $no_need_right = false;
  95. }
  96. return [$no_need_login, $no_need_right];
  97. }
  98. }