AccessAuth.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace hook;
  14. use think\Config;
  15. use think\Db;
  16. use think\exception\HttpResponseException;
  17. use think\Request;
  18. use think\View;
  19. /**
  20. * 访问权限管理
  21. * Class AccessAuth
  22. * @package hook
  23. * @author Anyon <zoujingli@qq.com>
  24. * @date 2017/05/12 11:59
  25. */
  26. class AccessAuth
  27. {
  28. /**
  29. * 当前请求对象
  30. * @var Request
  31. */
  32. protected $request;
  33. /**
  34. * 行为入口
  35. * @param $params
  36. */
  37. public function run(&$params)
  38. {
  39. $this->request = Request::instance();
  40. list($module, $controller, $action) = [$this->request->module(), $this->request->controller(), $this->request->action()];
  41. $node = strtolower("{$module}/{$controller}/{$action}");
  42. $info = Db::name('SystemNode')->where('node', $node)->find();
  43. $access = [
  44. 'is_menu' => intval(!empty($info['is_menu'])),
  45. 'is_auth' => intval(!empty($info['is_auth'])),
  46. 'is_login' => empty($info['is_auth']) ? intval(!empty($info['is_login'])) : 1
  47. ];
  48. // 用户登录状态检查
  49. if (!empty($access['is_login']) && !session('user')) {
  50. if ($this->request->isAjax()) {
  51. $this->response('抱歉,您还没有登录获取访问权限!', 0, url('@admin/login'));
  52. }
  53. throw new HttpResponseException(redirect('@admin/login'));
  54. }
  55. // 访问权限节点检查
  56. if (!empty($access['is_auth']) && !auth($node)) {
  57. $this->response('抱歉,您没有访问该模块的权限!', 0);
  58. }
  59. // 权限正常, 默认赋值
  60. $view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  61. $view->assign('classuri', strtolower("{$module}/{$controller}"));
  62. }
  63. /**
  64. * 返回消息对象
  65. * @param string $msg 消息内容
  66. * @param int $code 返回状态码
  67. * @param string $url 跳转URL地址
  68. * @param array $data 数据内容
  69. * @param int $wait
  70. */
  71. protected function response($msg, $code = 0, $url = '', $data = [], $wait = 3)
  72. {
  73. $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait];
  74. throw new HttpResponseException(json($result));
  75. }
  76. }