Auth.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $lastPasswordChange = Db::name('Admin')->where('id', $uid)->value('last_password_change');
  87. if($pathInfo != "api/password_submit"){
  88. // 检查是否需要更新密码
  89. if ($lastPasswordChange === null && $pathInfo != "api/edit_password.") {
  90. redirect('/admin/api/edit_password.html')->send();
  91. } else if ($lastPasswordChange != null && $pathInfo != "api/edit_password.") {
  92. $daysSinceLastChange = (time() - $lastPasswordChange) / (60 * 60 * 24);
  93. if ($daysSinceLastChange > 180) {
  94. redirect('/admin/api/edit_password.html')->send();
  95. }
  96. }
  97. }
  98. //验证用户访问权限
  99. // if ($action !== 'index' && $action !== 'api') {
  100. // if (!$this->checkAuth($controller, $pathInfo, $action, $uid)) {
  101. // if ($request->isAjax()) {
  102. // return to_assign(202, '没有使用权限!');
  103. // } else {
  104. // echo '<div style="text-align:center;color:red;margin-top:20%;">没有使用权限!</div>';
  105. // exit;
  106. // }
  107. // }
  108. // }
  109. }
  110. $response = $next($request);
  111. // dump($request);
  112. // dump($response);
  113. return $response;
  114. }
  115. /**
  116. * 验证用户访问权限
  117. * @DateTime 2020-12-21
  118. * @param string $controller 当前访问控制器
  119. * @param string $action 当前访问方法
  120. * @param string $uid 当前用户id
  121. * @return [type]
  122. */
  123. protected function checkAuth($controller, $pathInfo, $action, $uid)
  124. {
  125. //Cache::delete('RulesSrc' . $uid);
  126. if (!Cache::get('RulesSrc' . $uid) || !Cache::get('RulesSrc0')) {
  127. //用户所在权限组及所拥有的权限
  128. // 执行查询
  129. $user_groups = Db::name('AdminGroupAccess')
  130. ->alias('a')
  131. ->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
  132. ->where("a.uid='{$uid}' and g.status='1'")
  133. ->select()
  134. ->toArray();
  135. $groups = $user_groups ?: [];
  136. $ids = []; //保存用户所属用户组设置的所有权限规则id
  137. foreach ($groups as $g) {
  138. $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
  139. }
  140. $ids = array_unique($ids);
  141. //读取所有权限规则
  142. $rules_all = Db::name('AdminRule')->field('src')->select();
  143. //读取用户组所有权限规则
  144. $rules = Db::name('AdminRule')->where('id', 'in', $ids)->field('src')->select();
  145. //循环规则,判断结果。
  146. $auth_list_all = [];
  147. $auth_list = [];
  148. foreach ($rules_all as $rule_all) {
  149. $auth_list_all[] = strtolower($rule_all['src']);
  150. }
  151. foreach ($rules as $rule) {
  152. $auth_list[] = strtolower($rule['src']);
  153. }
  154. //规则列表结果保存到Cache
  155. Cache::tag('adminRules')->set('RulesSrc0', $auth_list_all, 36000);
  156. Cache::tag('adminRules')->set('RulesSrc' . $uid, $auth_list, 36000);
  157. } else {
  158. $auth_list_all = Cache::get('RulesSrc0');
  159. $auth_list = Cache::get('RulesSrc' . $uid);
  160. }
  161. // dump($auth_list);
  162. $pathUrl = $controller . '/' . $pathInfo;
  163. // dump($pathUrl);
  164. if (!in_array($pathUrl, $auth_list)) {
  165. if (in_array($pathUrl, $this->Authconfig)) {
  166. // dump($this->Authconfig);
  167. return true;
  168. }
  169. return false;
  170. } else {
  171. return true;
  172. }
  173. }
  174. }