返回系统权限访问控......登陆

系统权限访问控制

JasonKim2019-05-09 16:43:17244
<?php
namespace app\admin\controller;

use think\Controller;
use think\facade\Session;
use think\Request;
use Util\SysDb;

class Base extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->_admin = Session::get('admin');
        if(!$this->_admin) {
            header('Location:/index.php/admin/account/login');
        }
        $this->assign('admin',$this->_admin);
        // 限制用户访问权限
        $group = SysDb::getnstance()->table('admin_groups')->where(['gid'=>$this->_admin['gid']])->item();
        if(!$group) {
            $this->RequestError('对不起,您没有权限');
        }
        $rights = json_decode($group['rights']);

        // 当前控制名称
        $controller = $request->controller();
        // 方法名称
        $method  = $request->action();
        // 获取菜单表数据
        $res = SysDb::getnstance()->table('admin_menus')->where(['controller'=>$controller,'method'=>$method])->item();
        if(!$res) {
            $this->RequestError('对不起,您访问的功能不存在');
        }

        // 查看是否呗禁用
        if($res['status'] == 1){
            $this->RequestError('对不起,该功能已禁止使用');
        }

        // 查看菜单权限
        if(!in_array($res['mid'],$rights)) {
            $this->RequestError('对不起,您没有权限');
        }

    }

    // 处理返回数据
    private function RequestError($msg)
    {
        $request = new  Request();
        if($request->isAjax()) {
            $this->error(['code'=>0,'msg'=>$msg]);
            //exit(['code'=>0,'msg'=>$msg]);
        }
        $this->error($msg);
        //exit($msg);
    }
}


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送