123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace app\http\middleware;
- use think\Container;
- use think\facade\Log;
- use think\Request;
- use traits\controller\Jump;
- class Auth
- {
- use Jump;
- private $url_path;
- public function handle(Request $request, \Closure $next)
- {
- if(!is_login()){
- return redirect(url('admin/index/login'));
- }
- $this->url_path = strtolower($request->module() . '/' . $request->controller() . '/' . $request->action());
- if (!in_array(strtolower($request->url()), array('admin/index/login', 'admin/index/logout', 'admin/index/verify'))) {
- // 是否是超级管理员
- define('IS_ROOT', is_administrator());
- // 检测系统权限
- $authstatus = true;
- if (!IS_ROOT) {
- $access = $this->accessControl($request);
- if (false === $access) {
- $authstatus = false;
- } elseif (null === $access) {
- $dynamic = $this->checkDynamic(); //检测分类栏目有关的各项动态权限
- if ($dynamic === null) {
- //检测访问权限
- if (!$this->checkRule($this->url_path, '1,2')) {
- $authstatus = false;
- } else {
- // 检测分类及内容有关的各项动态权限
- $dynamic = $this->checkDynamic();
- if (false === $dynamic) {
- $authstatus = false;
- }
- }
- } elseif ($dynamic === false) {
- $authstatus = false;
- }
- }
- }
- if(!$authstatus && $request->header('referer') ){
- $this->error('未授权访问!',$request->header('referer'));
- }else if(!$authstatus){
- $this->error('未授权访问!','admin/index/login');
- }
- $this->setMenu($request);
- }
- return $next($request);
- }
- protected function checkDynamic() {
- if (IS_ROOT) {
- return true; //管理员允许访问任何页面
- }
- return null; //不明,需checkRule
- }
- final protected function accessControl(Request $request) {
- $allow = config('siteinfo.allow_visit');
- $deny = config('siteinfo.deny_visit');
- $check = strtolower($request->controller() . '/' . $request->action());
- if (!empty($deny) && in_array_case($check, $deny)) {
- return false; //非超管禁止访问deny中的方法
- }
- if (!empty($allow) && in_array_case($check, $allow)) {
- return true;
- }
- return null; //需要检测节点权限
- }
- protected function setMenu(Request $request)
- {
- $hover_url = $request->module() . '/' . $request->controller();
- $controller = $this->url_path;
- $menu = array(
- 'main' => array(),
- 'child' => array(),
- );
- $map['pid'] = 0;
- $map['hide'] = 0;
- $map['type'] = 'admin';
- if (!config('siteinfo.develop_mode')) {
- // 是否开发者模式
- $map['is_dev'] = 0;
- }
- $row = db('menu')->field('nid,title,url,icon,"" as style')->where($map)->order('sort asc')->select();
- foreach ($row as $key => $value) {
- //此处用来做权限判断
- if (IS_ROOT || $this->checkRule($value['url'], 2, null)) {
- if ($controller == $value['url']) {
- $value['style'] = "active";
- }
- $menu['main'][$value['nid']] = $value;
- }
- }
- if(count($menu['main'])<=0){
- Container::get('app')['view']->assign('__menu__', $menu);
- return false;
- }
- // 查找当前子菜单
- $pid = db('menu')->where("pid !=0 AND url like '%{$hover_url}%'")->value('pid');
- $id = db('menu')->where("pid = 0 AND url like '%{$hover_url}%'")->value('nid');
- $pid = $pid ? $pid : $id;
- if ($pid) {
- $map['pid'] = $pid;
- $map['hide'] = 0;
- $map['type'] = 'admin';
- $row = db('menu')->field("nid,title,url,icon,`group`,pid,'' as style")->where($map)->order('sort asc')->select();
- foreach ($row as $key => $value) {
- if (IS_ROOT || $this->checkRule($value['url'], 2, null) || 'test') {
- if ($controller == $value['url']) {
- $menu['main'][$value['pid']]['style'] = "active";
- $value['style'] = "active";
- }
- $menu['child'][] = $value;
- }
- }
- }
- Container::get('app')['view']->assign('__menu__', $menu);
- }
- /**
- * 权限检测
- * @param string $rule 检测的规则
- * @param string $mode check模式
- * @return boolean
- * @author 朱亚杰 <xcoolcc@gmail.com>
- */
- final protected function checkRule($rule, $type = 1, $mode = 'url') {
- static $Auth = null;
- if (!$Auth) {
- $Auth = new \author\Auth();
- }
- if (!$Auth->check($rule, session('user_auth.sid'), $type, $mode)) {
- return false;
- }
- return true;
- }
- }
|