AdminCkeckRoleMiddleware.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-30 14:59
  10. // +----------------------------------------------------------------------
  11. namespace app\system\middleware;
  12. use app\model\system\Admin as AdminModel;
  13. use app\model\system\AdminRole;
  14. use app\model\system\RolePath;
  15. use app\Request;
  16. use Firebase\JWT\ExpiredException;
  17. use Firebase\JWT\JWT;
  18. use library\exceptions\AuthException;
  19. use library\interfaces\MiddlewareInterface;
  20. use library\utils\AdminLogUtils;
  21. use think\facade\Cache;
  22. use think\facade\Config;
  23. class AdminCkeckRoleMiddleware implements MiddlewareInterface
  24. {
  25. public function handle(Request $request, \Closure $next)
  26. {
  27. $adminInfo = $request->adminInfo;
  28. $rules = (new AdminRole())->getRoleId($adminInfo['role_id']);
  29. if ($rules['is_system']) {
  30. $log = \config('log');
  31. //记录日志
  32. if($log['LOG']) {
  33. (new AdminLogUtils($request))
  34. ->path(app()->getAppPath() . "/route")
  35. ->log();
  36. }
  37. return $next($request);
  38. }
  39. $rolePathData = RolePath::getMoule($rules['module']);
  40. if (empty($rolePathData)) {
  41. //无权限
  42. throw new AuthException('无法操作当前功能,无操作权限', -66);
  43. }
  44. $rolePathAr = array_column($rolePathData, 'role_path');
  45. $rAr = [];
  46. foreach ($rolePathAr as $v) {
  47. $r = explode(',', $v);
  48. $vAr = array_filter($r, function ($item) {
  49. return !empty($item) ? true : false;
  50. });
  51. if (!empty($vAr)) {
  52. $vAr = array_values($vAr);
  53. $rAr = array_merge($rAr, $vAr);
  54. }
  55. }
  56. $pathinfo = $request->pathinfo();
  57. $bool = false;
  58. foreach ($rAr as $v) {
  59. if (trim($v) == trim($pathinfo)) {
  60. $bool = true;
  61. }
  62. }
  63. if (!$bool) {
  64. throw new AuthException('无法操作当前功能,无操作权限', -66);
  65. }
  66. $log = \config('log');
  67. //记录日志
  68. if($log['LOG']) {
  69. (new AdminLogUtils($request))
  70. ->path(app()->getAppPath() . "/route")
  71. ->log();
  72. }
  73. return $next($request);
  74. }
  75. }