后台管理系统-权限访问控制功能

Original 2019-05-28 15:43:52 354
abstract:<?phpnamespace app\admin\controller;use think\App;use think\Controller;use think\facade\Session;use app\model\Groups as GroupsModel;use app\model\Menu as MenuModel;class Common extends Controller{

<?php

namespace app\admin\controller;


use think\App;
use think\Controller;
use think\facade\Session;
use app\model\Groups as GroupsModel;
use app\model\Menu as MenuModel;

class Common extends Controller
{
   public function __construct()
   {
       parent::__construct();
       $username = Session::get('username');
       $gid = Session::get('gid');
       if (!$username) {
           $this->error('您还未登录!请返回登录!','Account/login');
       }
       $this->assign('username',$username);
       //判断用户是否有权限
       $group = GroupsModel::where('gid',$gid)->find();
//        dump($group);return false;
       if (!$group) {
           $this->requestError('对不起,您没有权限');
       }
       $rights = json_decode($group['rights']);
       //获取前当访问的控制器和方法
       $controller = request()->controller();
       $method = request()->action();
       $result = MenuModel::where(['controller'=>$controller,'method'=>$method])->find();
       if (!$result) {
           $this->requestError('对不起,您访问的功能不存在');
       }
       if ($result['status'] == 1){
           $this->requestError('对不起,该功能已被禁用');
       }
       if(!in_array($result['mid'],$rights)){
           $this->requestError('对不起,您没有操作权限');
       }
   }

   //定义请求错误返回信息方法
   private function requestError($msg)
   {
       if(request()->isAjax()){
           exit(json_encode(['code'=>1,'msg'=>$msg]));
       }
       exit($msg);
   }
}

Correcting teacher:天蓬老师Correction time:2019-05-28 15:59:45
Teacher's summary:权限控制是基于会话机制的, 所有项目都是以这个为基础, 其实也不难学, 对不对?

Release Notes

Popular Entries