网站首页技术博客

基于Thinkphp权限管理的实现​

洞天水月2021-03-12 14:32:211875人次阅读
摘要基于Thinkphp权限管理的实现 基本思路,用户,角色,权限,三层管理结构 对所有的控制器和方法进行注册写入权限表,不同的角色分配不同权限,不同用户划分入不同角色 在登录时查询用户角色所拥有权限并进行缓存

基于Thinkphp权限管理的实现

基本思路,用户,角色,权限,三层管理结构

对所有的控制器和方法进行注册写入权限表,不同的角色分配不同权限,不同用户划分入不同角色

在登录时查询用户角色所拥有权限并进行缓存

数据表

wf_jurisdiction.sql

wf_roles.sql

wf_sys_user.sql

核心代码

角色中分配权限部分

RoleController.class.php

 /**
     * 权限分配弹框
     */
    public function juris(){
        if (IS_POST){
            $data=I('post.');
            $data['id'] = $data['role_id'];
            unset($data['role_id']);
            $data['juris'] = implode(",",$data['juris']);
            $res = M('roles')->save($data);
            if ($res){
                //删除权限缓存
                S(session("uname")."_juris",null);
                $this->success("权限分配成功!");
            }else{
                $this->error("权限分配失败!");
            }
            return ;
        }
        $id = I('get.id');
        //1、获取所有权限
        $juris = M('jurisdiction')->field("id,name,controller,action")->select();
        //已获得的权限
        $juris_done = M('roles')->field("juris")->find($id);
        foreach ($juris as &$temp){
            $isset = array_search($temp['id'],explode(",", $juris_done['juris']));
            if ($isset!==false){
                $temp['checked']=true;
            }else{
                $temp['checked']=false;
            }
            if($temp['controller'] == "Index"){
                $temp['checked'] =true;
            }
        }
        $this->assign("role_id",$id);
        $this->assign("juris",$juris);
        $this->display();
    }

base控制器权限验证部分,带缓存,可在用户注销或分配权限时清除缓存

 //获取权限
       $juris = S(session("uname")."_juris");
       if (empty($juris)){
            $juris_done = M('roles')->field("juris")->find(session("user")['roleid']);
            if (empty($juris_done['juris'])){
                $this->error("你没有这个系统的任何权限,请联系管理员为你分配权限!",U('Login/logout'));
            }
            //权限缓存
            $juris = M('jurisdiction')->field("controller,action")->where("id in (".$juris_done['juris'].")")->select();
            S(session("uname")."_juris",$juris);
        }
        $array=['controller'=>CONTROLLER_NAME,'action'=>ACTION_NAME];
        $array2 = ['controller'=>CONTROLLER_NAME,'action'=>""];
        //判断是否具有权限
        $res = array_search($array, $juris)!==false||array_search($array2, $juris)!==false;
       if ($res===false){
            $this->error("你无权访问本页面!");
            return ;
       }

判断是否拥有某权限

function.php

/**
 * 判断是否拥有权限
 * @param 权限ID $id或者多id数组 传入数组时有一个满足则返回true
 * @return boolean 拥有该权限返回true 否则返回false
 */
function hasJuris($id){
   $flag = false;
   $roleID = session('user')['roleid'];
   $juris = M('roles')->field("juris")->find($roleID);
   $juris = explode(",", $juris['juris']);
   
   $res = false;
   //传入为数组
   if(is_array($id)){
       foreach ($id as $vo){
          $res = $res||array_search($vo, $juris)!==false;
       }
   }
   //传入为单个id
   else{
       $res = array_search($id, $juris);
   }
   
   
   if ($res===false){
       $flag = false;
   }else{
       $flag =  true;
   }
   return $flag;
}


权限管理截图:

image.png

分配角色权限截图:


image.png


注:权限项中只填写了控制器名没有方法名,则代表拥有该控制器的所有权限

文章评论