AndroidGateRequest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Authority;
  4. use App\Components\ApiResponse;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. class AndroidGateRequest extends FormRequest
  7. {
  8. use ApiResponse;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. /**
  14. * 通用鉴权
  15. *
  16. * @return bool
  17. */
  18. public function authorize():bool
  19. {
  20. //去除参数
  21. $routes = explode("/",ltrim(explode("?",$this->getPathInfo())[0],"/api/"));
  22. //去除前缀
  23. array_shift($routes);
  24. $routeStr = implode("/",$routes);
  25. $authorities = app("AuthorityService")->getUserAndroidAuthority();
  26. foreach ($authorities as $authority){
  27. if ($this->method() != Authority::METHOD[$authority->method])continue;
  28. if ($authority->route == $routeStr)return true; //相等
  29. if (!str_contains($authority->route, "*"))continue;//无泛匹配符
  30. $route = explode("/",$authority->route);
  31. $crLen = count($routes);
  32. $trLen = count($route);
  33. if ($crLen<$trLen)continue;
  34. if ($crLen>$trLen && $route[$trLen-1]=='*'){
  35. $routes = array_slice($routes,0,$trLen);
  36. $crLen = $trLen;
  37. }
  38. if ($crLen!=$trLen)continue;
  39. foreach ($route as $index=>$item){
  40. if ($item=='*')$route[$index] = $routes[$index];
  41. }
  42. if (implode("/",$route)==$routeStr)return true;
  43. }
  44. return false;
  45. }
  46. public function failedAuthorization()
  47. {
  48. $this->response(false,403,"权限不足");
  49. }
  50. public function rules():array
  51. {
  52. return [];
  53. }
  54. }