AccessAuth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Middleware\Manager;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use App\Models\Manager\AuthRule;
  6. use App\Models\Manager\Personnel\RolesAuthRule;
  7. // 访问权限验证
  8. class AccessAuth
  9. {
  10. // 无需验证的路径
  11. protected $except = [
  12. 'manager/collect/product/product_name_specs_list',
  13. 'manager/process/sub_notice',
  14. 'manager/login/index',
  15. 'manager/login/mobile',
  16. 'manager/login/email',
  17. 'manager/login/send_code',
  18. 'manager/login/mobile_code',
  19. 'manager/citys/list',
  20. 'manager/login/wechat',
  21. 'manager/login/auth_rules',
  22. 'manager/upload/uploadimg',
  23. 'manager/upload/get_sign_url',
  24. 'manager/personnel_employee/personal_info',
  25. 'manager/login/wechat_bind',
  26. 'manager/citys/list_zoning',
  27. 'manager/statistics/product_surveillance/push_data_notice',
  28. 'manager/process/low_price_goods/data_cleaning',
  29. 'manager/process/Collect_data/low_price_product_collect_data',
  30. 'manager/process/Collect_data/update_collect_data'
  31. ];
  32. //默认配置
  33. protected $_config = [
  34. 'auth_on' => true, // 认证开关
  35. 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
  36. 'auth_group' => 'auth_group', // 用户组数据表名
  37. 'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
  38. 'auth_rule' => 'auth_rule' // 权限规则表
  39. ];
  40. /**
  41. * $prefix表前缀
  42. */
  43. public function __construct()
  44. {
  45. // 判断配置
  46. if (config('AUTH_CONFIG')) {
  47. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  48. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  49. }
  50. }
  51. /**
  52. * Handle an incoming request.
  53. *
  54. * @param \Illuminate\Http\Request $request
  55. * @param \Closure $next
  56. * @return mixed
  57. */
  58. public function handle(Request $request, Closure $next)
  59. {
  60. // 当前路径
  61. $path = ltrim($request->getPathInfo(), '/');
  62. // 判断是否需要验证登录
  63. if (!in_array($path, $this->except)) {
  64. // 用户ID
  65. $uid = request('access_token.uid', 0);
  66. // 如果未登录
  67. if (!$uid) return json_send(['code' => 'no_login', 'msg' => '请登录账号', 'data' => ['请您登录']]);
  68. // 如果不是超级管理员
  69. if (!request('access_token.is_super', 0)) {
  70. // 进行验证
  71. if (!$this->check($path,$uid)) return json_send(['code' => 'error', 'msg' => '您没有操作权限', 'data' => ['没有操作权限']]);
  72. }
  73. }
  74. // 返回下一个闭包
  75. return $next($request);
  76. }
  77. /**
  78. * 检查权限
  79. * @param name string|array 需要验证的规则列表
  80. * @param uid int 认证用户的id
  81. * @return boolean 通过验证返回true;失败返回false
  82. */
  83. private function check($path, $uid)
  84. {
  85. // 未开启验证,直接通过
  86. if (!$this->_config['auth_on']) return true;
  87. $is_admin = request('access_token.is_admin');
  88. if($is_admin == 1){
  89. // 获取用户需要验证的所有有效规则列表
  90. $authList = (new AuthRule())->getAuthList($uid, 'manager');
  91. }else{
  92. // 获取用户需要验证的所有有效规则列表
  93. $authList = (new RolesAuthRule())->getAuthList($uid, 'manager');
  94. }
  95. // 切割path
  96. $path = explode('/', $path);
  97. // 没有控制器
  98. if (count($path) < 2) $path[] = 'index';
  99. // 没有方法
  100. if (count($path) < 3) $path[] = 'index';
  101. // 切割path
  102. $path = implode('/', $path);
  103. // 判断是否通过验证
  104. return in_array($path, $authList);
  105. }
  106. }