Auth.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types=1);
  8. namespace app\admin\middleware;
  9. use think\facade\Cache;
  10. use think\facade\Db;
  11. use think\facade\Session;
  12. class Auth
  13. {
  14. public $Authconfig;
  15. public function __construct()
  16. {
  17. $this->Authconfig = [
  18. "admin/field.field/add", // 财政局属性设置
  19. "admin/field.fieldcompany/add", // 公司属性设置
  20. "admin/field.fieldproprietor/add", // 业主属性设置
  21. "admin/project.cost_company/get_yezhu_tree", // 获取业主
  22. "admin/project.cost_company/get_employee", // 获取雇主
  23. "admin/api/upload", // 上传文件
  24. "admin/project.cost/getemployeelist", // 获取单位
  25. "admin/project.cost_company/get_department_tree", // 获取部门
  26. "admin/project.cost/get_customer_head", // 委托单位负责人
  27. "admin/project.cost/get_customer", // 选择委托单位
  28. // "admin/project.api/add_file", // 上传附件
  29. // "admin/project.api/delete_file", // 删除附件
  30. "admin/message/index", // 消息中心
  31. "admin/message/read", //消息中心的已读
  32. "admin/project.api/eliminate",//小红点消息消除
  33. "admin/project.api/add_file",
  34. "admin/project.received/get_department_tree",
  35. "admin/project.received/get_employee",
  36. "admin/index/people",//公司
  37. "admin/project.api/get_department_tree",//获取人
  38. "admin/project.api/get_employee",
  39. "admin/project.api/get_all_people_company",
  40. "admin/project.api/get_all_people",
  41. "admin/project.api/test", //zjl的测试方法
  42. "admin/project.api/get_project",
  43. "admin/project.appropriation/edit_company",
  44. "admin/project.appropriation/c_edit",
  45. // "admin/project.audit/read_company",
  46. // "admin/project.appropriation/c_read",
  47. "admin/project.comment/add_company",
  48. "admin/project.comment/add",
  49. "admin/project.comment/add_proprietor",
  50. "admin/project.api/get_entrust",
  51. "admin/project.api/get_entrust_people",
  52. "admin/contract.api/check_node",
  53. "admin/contract.api/relevancy_p",
  54. "admin/project.api/get_self_project",
  55. "admin/contract.api/get_contract",
  56. "admin/project.api/get_sent_review",
  57. "admin/project.api/get_sent_review_people",
  58. "admin/datastat.datastat/test"
  59. ];
  60. }
  61. public function handle($request, \Closure $next)
  62. {
  63. //获取模块名称
  64. $controller = app('http')->getName();
  65. $pathInfo = str_replace('' . $request->ext(), '', $request->pathInfo());
  66. // dump($pathInfo);
  67. $action = explode('/', $pathInfo)[0];
  68. //var_dump($pathInfo);exit;
  69. if ($pathInfo == '' || $action == '') {
  70. redirect('/admin/index/index.html')->send();
  71. exit;
  72. }
  73. //验证用户登录
  74. if ($action !== 'login') {
  75. $session_admin = get_config('app.session_admin');
  76. if (!Session::has($session_admin)) {
  77. if ($request->isAjax()) {
  78. return to_assign(404, '请先登录');
  79. } else {
  80. redirect('/admin/login/index.html')->send();
  81. exit;
  82. }
  83. }
  84. $uid = Session::get($session_admin)['id'];
  85. //验证用户访问权限
  86. // if ($action !== 'index' && $action !== 'api') {
  87. // if (!$this->checkAuth($controller, $pathInfo, $action, $uid)) {
  88. // if ($request->isAjax()) {
  89. // return to_assign(202, '没有使用权限!');
  90. // } else {
  91. // echo '<div style="text-align:center;color:red;margin-top:20%;">没有使用权限!</div>';
  92. // exit;
  93. // }
  94. // }
  95. // }
  96. }
  97. $response = $next($request);
  98. // dump($request);
  99. // dump($response);
  100. return $response;
  101. }
  102. /**
  103. * 验证用户访问权限
  104. * @DateTime 2020-12-21
  105. * @param string $controller 当前访问控制器
  106. * @param string $action 当前访问方法
  107. * @param string $uid 当前用户id
  108. * @return [type]
  109. */
  110. protected function checkAuth($controller, $pathInfo, $action, $uid)
  111. {
  112. //Cache::delete('RulesSrc' . $uid);
  113. if (!Cache::get('RulesSrc' . $uid) || !Cache::get('RulesSrc0')) {
  114. //用户所在权限组及所拥有的权限
  115. // 执行查询
  116. $user_groups = Db::name('AdminGroupAccess')
  117. ->alias('a')
  118. ->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
  119. ->where("a.uid='{$uid}' and g.status='1'")
  120. ->select()
  121. ->toArray();
  122. $groups = $user_groups ?: [];
  123. $ids = []; //保存用户所属用户组设置的所有权限规则id
  124. foreach ($groups as $g) {
  125. $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
  126. }
  127. $ids = array_unique($ids);
  128. //读取所有权限规则
  129. $rules_all = Db::name('AdminRule')->field('src')->select();
  130. //读取用户组所有权限规则
  131. $rules = Db::name('AdminRule')->where('id', 'in', $ids)->field('src')->select();
  132. //循环规则,判断结果。
  133. $auth_list_all = [];
  134. $auth_list = [];
  135. foreach ($rules_all as $rule_all) {
  136. $auth_list_all[] = strtolower($rule_all['src']);
  137. }
  138. foreach ($rules as $rule) {
  139. $auth_list[] = strtolower($rule['src']);
  140. }
  141. //规则列表结果保存到Cache
  142. Cache::tag('adminRules')->set('RulesSrc0', $auth_list_all, 36000);
  143. Cache::tag('adminRules')->set('RulesSrc' . $uid, $auth_list, 36000);
  144. } else {
  145. $auth_list_all = Cache::get('RulesSrc0');
  146. $auth_list = Cache::get('RulesSrc' . $uid);
  147. }
  148. // dump($auth_list);
  149. $pathUrl = $controller . '/' . $pathInfo;
  150. // dump($pathUrl);
  151. if (!in_array($pathUrl, $auth_list)) {
  152. if (in_array($pathUrl, $this->Authconfig)) {
  153. // dump($this->Authconfig);
  154. return true;
  155. }
  156. return false;
  157. } else {
  158. return true;
  159. }
  160. }
  161. }