AccessAuth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Http\Middleware\Company;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use App\Models\Manager\AuthRuleCompany;
  6. // 访问权限验证
  7. class AccessAuth
  8. {
  9. // 无需验证的路径
  10. protected $except = [
  11. 'company/login/index',
  12. 'company/upload/sign_url',
  13. 'company/shop/list',
  14. 'company/auth_rule/list',
  15. 'company/city/list',
  16. 'company/product/list_except',
  17. 'company/custom/index_except',
  18. 'company/coupon/index_except',
  19. 'company/product_class/get_list',
  20. 'company/open_plat/install/wxwork_auth',
  21. 'company/product/get_spec_html',
  22. 'company/charging_record/index',
  23. 'company/charging_record/get_data',
  24. 'company/charging_record/create',
  25. 'company/charging_record/get_paycode_url',
  26. 'company/charging_record/get_detail',
  27. 'company/charging_record/get_use_record',
  28. 'company/orders/express_list',
  29. 'company/tag/tags/get_all',
  30. 'company/short_link/create',
  31. 'company/company_limit/get_remind',
  32. ];
  33. //默认配置
  34. protected $_config = [
  35. 'auth_on' => true, // 认证开关
  36. 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
  37. 'auth_group' => 'auth_group', // 用户组数据表名
  38. 'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
  39. 'auth_rule' => 'auth_rule' // 权限规则表
  40. ];
  41. /**
  42. * $prefix表前缀
  43. */
  44. public function __construct()
  45. {
  46. // 判断配置
  47. if ( config('AUTH_CONFIG') ) {
  48. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  49. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  50. }
  51. }
  52. /**
  53. * Handle an incoming request.
  54. *
  55. * @param \Illuminate\Http\Request $request
  56. * @param \Closure $next
  57. * @return mixed
  58. */
  59. public function handle(Request $request, Closure $next)
  60. {
  61. // 当前路径
  62. $path = ltrim($request->getPathInfo(), '/');
  63. // 小程序/公众号的授权需要单独判断
  64. if( stripos('&'.$path,'company/open_plat/install/wxwork_auth') == 1 ) return $next($request);
  65. // 判断是否需要验证登录
  66. if ( !in_array($path, $this->except) ) {
  67. // 用户ID
  68. $uid = request('access_token.uid',0);
  69. // 如果未登录
  70. if( !$uid ) return json_send(['code'=>'no_login','msg'=>'请登录账号','data'=>['请您登录']]);
  71. // 如果不是超级管理员
  72. if ( !request('access_token.is_super',0) ) {
  73. // 进行验证
  74. if ( !$this->check($path, $uid ) ) return json_send(['code'=>'error','msg'=>'您没有操作权限','data'=>['没有操作权限']]);
  75. }
  76. }
  77. // 返回下一个闭包
  78. return $next($request);
  79. }
  80. /**
  81. * 检查权限
  82. * @param name string|array 需要验证的规则列表
  83. * @param uid int 认证用户的id
  84. * @return boolean 通过验证返回true;失败返回false
  85. */
  86. private function check($path, $uid)
  87. {
  88. // 未开启验证,直接通过
  89. if ( !$this->_config['auth_on'] ) return true;
  90. // 获取用户需要验证的所有有效规则列表
  91. $authList = (new AuthRuleCompany())->getAuthList($uid,'company');
  92. // 切割path
  93. $path = explode('/', $path);
  94. // 没有控制器
  95. if( count($path) < 2 ) $path[] = 'index';
  96. // 没有方法
  97. if( count($path) < 3 ) $path[] = 'index';
  98. // 切割path
  99. $path = implode('/', $path);
  100. // 判断是否通过验证
  101. return in_array($path, $authList);
  102. }
  103. }