Auth.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\http\middleware;
  3. use think\Container;
  4. use think\facade\Log;
  5. use think\Request;
  6. use traits\controller\Jump;
  7. class Auth
  8. {
  9. use Jump;
  10. private $url_path;
  11. public function handle(Request $request, \Closure $next)
  12. {
  13. if(!is_login()){
  14. return redirect(url('admin/index/login'));
  15. }
  16. $this->url_path = strtolower($request->module() . '/' . $request->controller() . '/' . $request->action());
  17. if (!in_array(strtolower($request->url()), array('admin/index/login', 'admin/index/logout', 'admin/index/verify'))) {
  18. // 是否是超级管理员
  19. define('IS_ROOT', is_administrator());
  20. // 检测系统权限
  21. $authstatus = true;
  22. if (!IS_ROOT) {
  23. $access = $this->accessControl($request);
  24. if (false === $access) {
  25. $authstatus = false;
  26. } elseif (null === $access) {
  27. $dynamic = $this->checkDynamic(); //检测分类栏目有关的各项动态权限
  28. if ($dynamic === null) {
  29. //检测访问权限
  30. if (!$this->checkRule($this->url_path, '1,2')) {
  31. $authstatus = false;
  32. } else {
  33. // 检测分类及内容有关的各项动态权限
  34. $dynamic = $this->checkDynamic();
  35. if (false === $dynamic) {
  36. $authstatus = false;
  37. }
  38. }
  39. } elseif ($dynamic === false) {
  40. $authstatus = false;
  41. }
  42. }
  43. }
  44. if(!$authstatus && $request->header('referer') ){
  45. $this->error('未授权访问!',$request->header('referer'));
  46. }else if(!$authstatus){
  47. $this->error('未授权访问!','admin/index/login');
  48. }
  49. $this->setMenu($request);
  50. }
  51. return $next($request);
  52. }
  53. protected function checkDynamic() {
  54. if (IS_ROOT) {
  55. return true; //管理员允许访问任何页面
  56. }
  57. return null; //不明,需checkRule
  58. }
  59. final protected function accessControl(Request $request) {
  60. $allow = config('siteinfo.allow_visit');
  61. $deny = config('siteinfo.deny_visit');
  62. $check = strtolower($request->controller() . '/' . $request->action());
  63. if (!empty($deny) && in_array_case($check, $deny)) {
  64. return false; //非超管禁止访问deny中的方法
  65. }
  66. if (!empty($allow) && in_array_case($check, $allow)) {
  67. return true;
  68. }
  69. return null; //需要检测节点权限
  70. }
  71. protected function setMenu(Request $request)
  72. {
  73. $hover_url = $request->module() . '/' . $request->controller();
  74. $controller = $this->url_path;
  75. $menu = array(
  76. 'main' => array(),
  77. 'child' => array(),
  78. );
  79. $map['pid'] = 0;
  80. $map['hide'] = 0;
  81. $map['type'] = 'admin';
  82. if (!config('siteinfo.develop_mode')) {
  83. // 是否开发者模式
  84. $map['is_dev'] = 0;
  85. }
  86. $row = db('menu')->field('nid,title,url,icon,"" as style')->where($map)->order('sort asc')->select();
  87. foreach ($row as $key => $value) {
  88. //此处用来做权限判断
  89. if (IS_ROOT || $this->checkRule($value['url'], 2, null)) {
  90. if ($controller == $value['url']) {
  91. $value['style'] = "active";
  92. }
  93. $menu['main'][$value['nid']] = $value;
  94. }
  95. }
  96. if(count($menu['main'])<=0){
  97. Container::get('app')['view']->assign('__menu__', $menu);
  98. return false;
  99. }
  100. // 查找当前子菜单
  101. $pid = db('menu')->where("pid !=0 AND url like '%{$hover_url}%'")->value('pid');
  102. $id = db('menu')->where("pid = 0 AND url like '%{$hover_url}%'")->value('nid');
  103. $pid = $pid ? $pid : $id;
  104. if ($pid) {
  105. $map['pid'] = $pid;
  106. $map['hide'] = 0;
  107. $map['type'] = 'admin';
  108. $row = db('menu')->field("nid,title,url,icon,`group`,pid,'' as style")->where($map)->order('sort asc')->select();
  109. foreach ($row as $key => $value) {
  110. if (IS_ROOT || $this->checkRule($value['url'], 2, null) || 'test') {
  111. if ($controller == $value['url']) {
  112. $menu['main'][$value['pid']]['style'] = "active";
  113. $value['style'] = "active";
  114. }
  115. $menu['child'][] = $value;
  116. }
  117. }
  118. }
  119. Container::get('app')['view']->assign('__menu__', $menu);
  120. }
  121. /**
  122. * 权限检测
  123. * @param string $rule 检测的规则
  124. * @param string $mode check模式
  125. * @return boolean
  126. * @author 朱亚杰 <xcoolcc@gmail.com>
  127. */
  128. final protected function checkRule($rule, $type = 1, $mode = 'url') {
  129. static $Auth = null;
  130. if (!$Auth) {
  131. $Auth = new \author\Auth();
  132. }
  133. if (!$Auth->check($rule, session('user_auth.sid'), $type, $mode)) {
  134. return false;
  135. }
  136. return true;
  137. }
  138. }