系统权限访问控制

Original 2019-05-09 16:43:17 244
abstract:<?php namespace app\admin\controller; use think\Controller; use think\facade\Session; use think\Request; use Util\SysDb; class Base extends Controller { &n
<?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);
    }
}


Correcting teacher:查无此人Correction time:2019-05-10 14:13:26
Teacher's summary:完成的不错。后台管理系统,最主要的就是权限,以免别人破解。继续加油。

Release Notes

Popular Entries