常用登陆验证-后端AuthController.php

 1 <?php
 2 
 3 
 4 namespace app\admin;
 5 
 6 use app\model\AdminNode;
 7 use think\facade\View;
 8 
 9 class AuthController extends BaseController
10 {
11     // 初始化
12     public $user;
13     public $active = 'setting';
14 
15     protected function initialize()
16     {
17         $this->request->user = session('user');
18         if (empty($this->request->user)) {
19             if ($this->request->isAjax()) {
20                 ErrorException('请登录');
21             }
22             cms_redirect(url('/auth/login'));
23         }
24 
25         if (empty($this->request->user['auth_node']['is_admin_role'])) {
26             cms_redirect(url('/'));
27         }
28 
29         //获取当前访问路由
30         $url = strtolower('/admin/' . $this->request->controller() . '/' . $this->request->action());
31 
32         $auth = $this->request->user['auth_node']['node'];
33 
34         //菜单栏
35         $menu = AdminNode::menu($auth);
36         $menu = list_to_tree($menu->toArray(), 64, 'pid');
37         View::assign(['menu' => $menu, 'active' => $this->active, 'user' => $this->request->user]);
38 
39         //获取路由映射,权限判断
40         $route = !empty(config('auth.route')[$url]) ? config('auth.route')[$url] : $url;
41         if ($route != 'allow_access' && $auth != 'all') {
42             //获取用户授权路由
43             $node = AdminNode::node($auth);
44             if (!in_array($route, $node)) {
45                 throw new AuthErrorException("未授权访问", 10000, 403);
46             }
47         }
48     }
49 }

 

posted @ 2022-04-13 10:16  糖粿  阅读(53)  评论(0编辑  收藏  举报