在說權限管理模塊前,應該先知道權限管理模塊要有哪些功能:
1、用戶只能訪問,指定的控制器,指定的方法
2、用戶可以存在於多個用戶組里
3、用戶組可以選擇,指定的控制器,指定的方法
4、后台可以添加控制器和方法
好了,需求知道了那么設計數據庫,如下圖:
從圖中可知主要表之間的關系
authority_user與authority_role,多對多
authority_role與authority_control,多對多
authority_role與authority_method,多對多
authority_control與authority_method,1對多
數據表設計好,那就應該寫程序判斷了(php程序)。判斷思路如下:
1、獲取用戶要訪問的控制器和方法。
2、從數據庫中獲取,該用戶擁有的控制器和方法。
3、判斷要訪問的控制器和方法,是否存在用戶擁有的控制器和方法里。
思路有了,那就寫個demo程序測試下(php程序的ci框架):
1 function __construct(){ 2 //假設管理員編號為99 3 $manage_user_id = 99; 4 //獲取要訪問的控制器和方法 5 $controlName = $this->uri->segment(1); 6 $methodName = $this->uri->segment(2); 7 //獲取該用戶所擁有的控制器和方法 8 $sql = 'select d.control_name,e.method_name from 9 authority_user_role as a, 10 authority_role as b, 11 authority_role_controlmethod as c, 12 authority_control as d, 13 authority_method as e 14 where 15 a.user_id={$manage_user_id} and 16 a.role_id=b.id and 17 b.id=c.role_id and 18 c.control_id=d.id and 19 c.method_id=e.id;'; 20 $authorityData = $this->db->query($sql)->result_array(); 21 //判斷有沒有權限 22 $returnData = $this->judgeAuthority($controlName, $methodName, $authorityData); 23 if($returnData['responseCode'] == '101'){ 24 echo '可以訪問,就不die()了'; 25 }($returnData['responseCode'] == '100'){ 26 echo '沒有權限'; 27 die(); 28 } 29 30 } 31 private function judgeAuthority($controlName, $methodName, $authorityData){ 32 foreach ($authorityData as $k => $v) { 33 if($v['control_name'] == $controlName && $v['method_name'] == $methodName){ 34 $responseData = array('responseCode'=>'101', 'responseMessage'=>'可以訪問'); 35 return $responseData; 36 break; 37 } 38 } 39 $responseData = array('responseCode'=>'100', 'responseMessage'=>'沒有權限'); 40 return $responseData; 41 }
當然了,這個權限管理模塊是在沒參考的情況下完成的,如果有發現不對勁,請幫忙回復指出。
權限管理模塊的demo:http://pan.baidu.com/s/1slyzXsp
