Admin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | 后台中间件
  5. * +----------------------------------------------------------------------
  6. */
  7. namespace app\sys\middleware;
  8. use think\facade\Config;
  9. use think\facade\Session;
  10. use think\facade\Cache;
  11. use think\Response;
  12. use think\exception\HttpResponseException;
  13. use think\facade\Request;
  14. use think\model\Collection;
  15. class Admin
  16. {
  17. public function handle($request, \Closure $next)
  18. {
  19. // 获取当前用户
  20. $admin_id = Session::get('adminuser.userid');
  21. if (empty($admin_id)) {
  22. return redirect((string)url('login/index'));
  23. }
  24. // 查找当前控制器和方法,控制器首字母大写,方法名首字母小写 如:Index/index
  25. $route = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::controller())) . '/' . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::action()));
  26. // 权限认证
  27. if (!$this->checkAuth($route, Session::get('adminuser.roleid'))) {
  28. $this->error('您无此操作权限!');
  29. }
  30. // 进行操作日志的记录
  31. $this->syslogRecord($route);
  32. // 中间件handle方法的返回值必须是一个Response对象。
  33. return $next($request);
  34. }
  35. protected function syslogRecord($route = '')
  36. {
  37. // 定义方法白名单(不记录日志)
  38. $allow = [
  39. ];
  40. $action = Request::action();
  41. if ($action != 'index' && !in_array($route, $allow)) {
  42. \app\common\model\SysLog::record();
  43. }
  44. }
  45. /**
  46. * 检查权限
  47. * @param string|array $route 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
  48. * @param integer $rid 认证用户角色ID
  49. * @return boolean 通过验证返回true;失败返回false
  50. */
  51. public function checkAuth($route, $rid)
  52. {
  53. // 超级管理员不检查权限
  54. if ($rid==1) {
  55. return true;
  56. }
  57. $menus = Cache::get('menus');
  58. if (!$menus) {
  59. $menus = \app\common\model\SysMenu::getUserMenuList($rid);
  60. Cache::set('menus', $menus);
  61. }
  62. if (!Config::get('app.auth_on')) {
  63. return true;
  64. }
  65. // 定义方法白名单
  66. $allow = [
  67. 'index/index', // 首页
  68. 'index/usedspace', // 使用空间
  69. 'index/clearcache', // 清除缓存
  70. 'file_manager/uploadimg', // 图片上传
  71. ];
  72. // 查询所有不验证的方法并放入白名单
  73. $menuOpen = \app\common\model\SysMenu::where('open', 1)->column('url');
  74. $allow = array_merge($allow, $menuOpen);
  75. foreach ($menus as $value) {
  76. if ($value->type == 0) {
  77. continue;
  78. }
  79. $allow[] = $value->url;
  80. }
  81. $allow = array_unique($allow);
  82. if (in_array($route, $allow)) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. * 操作错误跳转的快捷方法 抽的 liliuwei Jump error 方法
  89. * @access protected
  90. * @param mixed $msg 提示信息
  91. * @return void
  92. */
  93. protected function error($msg = '')
  94. {
  95. $url = Request::isAjax() ? '' : 'javascript:history.back(-1);';
  96. $result = [
  97. 'code' => 0,
  98. 'msg' => $msg,
  99. 'data' => '',
  100. 'url' => $url,
  101. 'wait' => 3,
  102. ];
  103. $type = Request::isJson() || Request::isAjax() ? 'json' : 'html';;
  104. if ('html' == strtolower($type)) {
  105. $type = 'view';
  106. $dispatch_error_tmpl = app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl';
  107. $response = Response::create($dispatch_error_tmpl, $type)->assign($result)->header([]);
  108. } else {
  109. $response = Response::create($result, $type)->header([]);
  110. }
  111. throw new HttpResponseException($response);
  112. }
  113. }