TP 3.2.3 权限控制源码

<?php
namespace Common\Controller;
use Think\Controller;
class CommonController extends Controller
{
    public function _initialize()
    {
        $access = $this->checkAccess();
        if($access == 0){
            echo "<script>alert('Sorry, you do not currently have permission to do this...')</script>";
        }
    }

    //检测session值是否存在
    public function checkSession()
    {

    }

    //检测变量是否存在二维数组中
    public function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }

            if(in_array($value, $item)) {
                return true;
            } else if($this->deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }

    //检测权限
    public function checkAccess()
    {
        $controller = CONTROLLER_NAME;
        $action =  ACTION_NAME;
        $access = $this->getAccess();
        $con = $this->deep_in_array($controller,$access);
        $act = $this->deep_in_array($action,$access);
        if($con){
            if($act){
                return 1;
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }

    //获取权限
    public function getAccess()
    {
        $sql = "select * from admin where id = 2";
        $model = M();
        $admin = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sql);
        $rid = $admin[0]["rid"];
        $sqls = "select * from role where id in($rid)";
        $role = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqls);
        foreach($role as $k=>$v){
            $jid[] = explode(",",$v['jid']);
        }
        $jid = array_unique($jid);
        $jid = implode(",",$jid[0]);
        $sqlsd = "select * from jurisdiction where id in($jid)";
        $jurisdiction = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqlsd);
        return $jurisdiction;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luyaran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值