程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 使用php實現權限管理模塊

使用php實現權限管理模塊

編輯:PHP綜合

 

在說權限管理模塊前,應該先知道權限管理模塊要有哪些功能:

  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框架):

 function __construct(){    
     //假設管理員編號為99
     $manage_user_id = 99;
     //獲取要訪問的控制器和方法
     $controlName = $this->uri->segment(1);
     $methodName = $this->uri->segment(2);
     //獲取該用戶所擁有的控制器和方法
     $sql = 'select d.control_name,e.method_name from 
         authority_user_role as a,
         authority_role as b,
         authority_role_controlmethod as c,
         authority_control as d,
         authority_method as e
         where
         a.user_id={$manage_user_id} and
         a.role_id=b.id and
         b.id=c.role_id and
         c.control_id=d.id and
         c.method_id=e.id;';
     $authorityData = $this->db->query($sql)->result_array();
     //判斷有沒有權限
     $returnData = $this->judgeAuthority($controlName, $methodName, $authorityData);
     if($returnData['responseCode'] == '101'){
         echo '可以訪問,就不die()了';
     }($returnData['responseCode'] == '100'){
         echo '沒有權限';
         die();
     }
 
 }
 private function judgeAuthority($controlName, $methodName, $authorityData){
     foreach ($authorityData as $k => $v) {
         if($v['control_name'] == $controlName && $v['method_name'] == $methodName){
             $responseData = array('responseCode'=>'101', 'responseMessage'=>'可以訪問');
             return $responseData;
             break;
         }
     }
     $responseData = array('responseCode'=>'100', 'responseMessage'=>'沒有權限');
     return $responseData;
 }

 

當然了,這個權限管理模塊是在沒參考的情況下完成的,如果有發現不對勁,請幫忙回復指出。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved