Admin.php 4.0 KB

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