Admin.php 3.8 KB

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