AccessAuth.php 3.8 KB

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