1. 程式人生 > >20181104 tp中的許可權(RBAC)

20181104 tp中的許可權(RBAC)

目錄檔案(僅僅寫的許可權的程式碼)

                   

Role/showlist.html 程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

        <title>角色列表</title>

        <link href="__CSS__/mine.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <style>
            .tr_color{background-color: #9F88FF}
        </style>
        <div class="div_head">
            <span>
                <span style="float: left;">當前位置是:許可權管理-》角色列表</span>
                <span style="float: right; margin-right: 8px; font-weight: bold;">
                    <a style="text-decoration: none;" href="__URL__/add">【新增角色】</a>
                </span>
            </span>
        </div>
        <div style="font-size: 13px; margin: 10px 5px;">
            <table class="table_a" border="1" width="100%">
                <tbody><tr style="font-weight: bold;">
                        <td >角色id</td>
                        <td >角色名稱</td>
                        <td >許可權ids</td>
                        <td >role_auth_ac</td>
                        <td  align="center">操作</td>
                    </tr>
             <volist name="list" id="vo">
                    
                    <tr id="product{$i}">
                        <td>{$vo.role_id}</td>
                        <td><a href="#">{$vo.role_name}</a></td>
                        <td>{$vo.role_auth_ids}</td>
                        <td>{$vo.role_auth_ac}</td>
                        <td>
                        <a href="__URL__/showauth/rid/{$vo.role_id}">分配許可權</a>
                        <a href="#">修改</a>
                        <a href="#">刪除</a>
                        </td>
                    </tr>
             </volist>
                    <tr>
                        <td colspan="20" style="text-align: center;">
                            {$page}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

  

Role/showautht.html 程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>給角色分配許可權</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <link href="__CSS__/mine.css" type="text/css" rel="stylesheet" />
    </head>

    <body>
        <div class="div_head">
            <span>
                <span style="float:left">當前位置是:角色管理-》分配許可權資訊【{$role_info['role_name']}】</span>
                <span style="float:right;margin-right: 8px;font-weight: bold">
                    <a style="text-decoration: none" href="__CONTROLLER__/showlist">【返回】</a>
                </span>
            </span>
        </div>
        <div></div>

        <div style="font-size: 13px;margin: 10px 5px">
            <form action='__SELF__' method='post'>
                <table cellspacing='1' id="list-table">
                    <volist name='auth_infoA' id='v'>
                    <tr>
                        <td width="25%" valign="top" class="first-cell" style='border-bottom:2px solid gray; font-weight:bold;'>
                            <input type="checkbox" name="auth_id[]" value="{$v['auth_id']}" class="checkbox"
                                   <if condition="in_array($v['auth_id'],$authidsarr)">checked='checked'</if>
                                   />{$v['auth_name']}
                                   </td>
                        <td width='75%' style='border-bottom:2px solid gray;'>
                            <volist name='auth_infoB' id="vv">
                            <if condition="$vv['auth_pid']  eq $v['auth_id']">
                            <div style="width:200px;float:left;">
                                <input type="checkbox" name="auth_id[]" value="{$vv['auth_id']}" 
                                     <if condition="in_array($vv['auth_id'],$authidsarr)">checked='checked'</if>
                                       />{$vv['auth_name']}
                            </div>
                            </if>
                            </volist>
                        </td>
                    </tr>
                    </volist>
                </table>
                <input type='submit' value='分配許可權' />
            </form>
        </div>
    </body>
</html>

  

Auth/showlist.html  程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

        <title>許可權列表</title>

        <link href="__CSS__/mine.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <style>
            .tr_color{background-color: #9F88FF}
        </style>
        <div class="div_head">
            <span>
                <span style="float: left;">當前位置是:許可權管理-》許可權列表</span>
                <span style="float: right; margin-right: 8px; font-weight: bold;">
                    <a style="text-decoration: none;" href="__URL__/addauth">【新增許可權】</a>
                </span>
            </span>
        </div>
        <div style="font-size: 13px; margin: 10px 5px;">
            <table class="table_a" border="1" width="100%">
                <tbody><tr style="font-weight: bold;">
                        <td >序號</td>
                        <td >許可權名稱</td>
                        <td >父許可權id</td>
                        <td >控制器</td>
                        <td >方法</td>
                        <td >全路徑</td>
                        <td >級別</td>
                        <td  align="center">操作</td>
                    </tr>
             <volist name="list" id="vo">
                    
                    <tr id="product{$i}">
                        <td>{$vo.auth_id}</td>
                        <td>{$ge|str_repeat=$vo['auth_level']}{$vo.auth_name}</td>
                        <td>{$vo.auth_pid}</td>
                        <td>{$vo.auth_c}</td>
                        <td>{$vo.auth_a}</td>
                        <td>{$vo.auth_path}</td>
                        <td>{$vo.auth_level}</td>
                        <td>
                        <a href="#">修改</a>
                        <a href="__URL__/del/id/{$vo.auth_id}">刪除</a>
                        </td>
                    </tr>
             </volist>
                    <tr>
                        <td colspan="20" style="text-align: center;">
                            {$page}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

  

Auth/addauth.html  程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>新增許可權</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8">
        <link href="./css/mine.css" type="text/css" rel="stylesheet">
    </head>

    <body>

        <div class="div_head">
            <span>
                <span style="float:left">當前位置是:許可權管理-》新增許可權資訊</span>
                <span style="float:right;margin-right: 8px;font-weight: bold">
                    <a style="text-decoration: none" href="__URL__/showlist">【返回】</a>
                </span>
            </span>
        </div>
        <div></div>

        <div style="font-size: 13px;margin: 10px 5px">
            <form action="__SELF__" method="post" enctype="multipart/form-data">
            <table border="1" width="100%" class="table_a">
                <tr>
                    <td>許可權名稱</td>
                    <td><input type="text" name="auth_name" /></td>
                </tr>
                <tr>
                    <td>許可權父id</td>
                    <td>
                        <select name="auth_pid">
                            <option value="0">請選擇</option>
                             <foreach name="list" item="vo">
                            <option value="{$vo.auth_id}">{$vo.auth_name}</option>
                           </foreach>
                        </select>
                    </td>
                </tr>
                  <tr>
                    <td>控制器</td>
                    <td><input type="text" name="auth_c" /></td>
                </tr>
                  <tr>
                    <td>方法</td>
                    <td><input type="text" name="auth_a" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="新增">
                    </td>
                </tr>  
            </table>
            </form>
        </div>
    </body>
</html>

  

Model/AuthModel.class.php  程式碼

<?php
namespace Admin\Model;
use Think\Model;
//Admin模組的manager控制器
class  AuthModel  extends  Model{
	function  saveDate($info){
		//根據已有的四個欄位生成一條記錄
		$newid=$this->add($info);
		//根據新增主鍵進一步製作auth_path和auth——level
		if($info["auth_pid"]==0){
			//dingji 
			$path=$newid;
		}else{
			//非頂級
			$pinfo=$this->find($info["auth_pid"]);
			$p_path=$pinfo["auth_path"];
			$path=$p_path."-".$newid;
		}
		//全路徑陣列之後的個數減一的結果
		$level=count(explode("-",$path))-1;
		$sql="update tp_auth  set auth_path='$path',auth_level='$level'where auth_id='$newid'";
		return $this->execute($sql);
		
	}
}
	

  

Controller/RoleController.class.PHP   程式碼

<?php
namespace Admin\Controller;//宣告
use Think\Controller;//用Controller在什麼位置
//Admin模組的login控制器
class  RoleController  extends  Controller{
	function showlist(){
		$list=M("role")->select();
		$this->assign("list",$list);
		
		$this->display();
	}
	function showauth($rid){
		if(IS_POST){
		   $auth_ids=$_POST["auth_id"];//接收表單
			$role_auth_ids=implode(",",$auth_ids);//陣列轉字串
			$authnames=M("auth")->field("auth_c,auth_a")->where("auth_id in($role_auth_ids)")->select();//根據許可權id查控制器方法
			
			
			//組織成字串313883132
			
			$str="";
			foreach($authnames as $v){
				if(!empty($v["auth_c"]))
				$str.=$v["auth_c"]."-".$v["auth_a"].",";
			}
			$str=rtrim($str,",");//去除逗號
			
			//執行SQL語句
		  $aql="update tp_role set role_auth_ids='$role_auth_ids',role_auth_ac='$str' where role_id=$rid";
			M("role")->execute($aql);
			$this->redirect("showlist",array("rid"=>$rid),1,"分配完成");	
				
		
		}
		//增加許可權
		$auth_infoA=M("auth")->where("auth_level=0 ")->select();
		$auth_infoB=M("auth")->where("auth_level=1 ")->select();
			
		$role=M("role")->find($rid);
		$authidsarr=explode(",",$role["role_auth_ids"]);
	     //註冊到模板
		$this->assign("auth_infoA",$auth_infoA);
		$this->assign("auth_infoB",$auth_infoB);
		$this->assign("authidsarr",$authidsarr);
			
		$this->display();
	
  }
}

  

Controller/AuthController.class.PHP   程式碼

<?php
namespace Admin\Controller;//宣告
use Think\Controller;//用Controller在什麼位置
//Admin模組的login控制器
class  AuthController  extends  Controller{
	function showlist(){
		$data=M("auth")->order('auth_path')->select();
		$this->assign("list",$data);
		$this->assign('ge',"--/");
		$this->display();
	}
	function addauth(){
		$auth=new \Admin\Model\AuthModel();
		if(!empty($_POST)){
			$info=$auth->create();
			if($auth->saveDate($info)){
				
				
				 $this->redirect("showlist",array(),1,"新增成功");
			}else{
				echo  "老哥,幹啥呢!";
			}
		  }
		  
		
		$pid=M("auth")->field("auth_id,auth_name")->where("auth_level=0")->select();
		$this->assign("list",$pid);
		
		$this->display();
	}
	//刪除的方法
	function del( $id){//引數的鍵名叫id
		$goods=M('auth');
		if($goods->delete($id)){
			$this->success("刪除成功",U("showlist"),1);
		}else{
			$this->error("刪除失敗",U("showlist"),1);
		}
	}
	
	
	
	
	
	
	
}