ThinkPHP許可權問題

來源:互聯網
上載者:User

標籤:log   status   前台   module   許可權表   php   管理   檢查   stat   

 1 <?php 2 namespace Common\Controller; 3 use Think\Controller; 4 use Think\Auth; 5  6 class AuthController extends Controller { 7     //判斷使用者是否有該操作的許可權,並判斷是否要將這個許可權插入規則表 8     private function checkAuth() { 9         $user = session(‘result.username‘);10         $uid = session(‘result.id‘);11         $controllerIndex = MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME;12         $dbAuthRule = M(‘auth_rule‘);13         $map[‘name‘] = $controllerIndex;14         $rule = $dbAuthRule->where($map)->select();15         16         //如果許可權表中沒有這個操作,就將其插入表中17         if(!$rule) {18             $data[‘name‘] = $controllerIndex;19             $data[‘status‘] = ‘1‘;20             $data[‘type‘] = MODULE_NAME;21             $dbAuthRule->add($data);22         }23         24         //判斷目前使用者是否在登入狀態25         if (empty($user)) {26             $this->error("請重新登入");27             die;28         }29         30         //執行個體化權限類別,檢查使用者的存取權限31         $auth = new Auth();32         //$test = $auth->check($controllerIndex, $uid);33         if (!$auth->check($controllerIndex, $uid)) {34             $this->error(‘您沒有許可權訪問,請聯絡管理員‘);35         }36     }37     //執行個體化AuthController時,會自動調用_initialize函數38     protected function _initialize() {39         $this->checkAuth();40     }41 }

使用ThinkPHP架構時,在公用模組Common的Controller 建立AuthController類。之後無論是在前台模組或者後台模組中,在編寫介面時,首先執行個體化AuthController,如果沒有存取權限,會自動判斷。當然前提是資料庫中已建立Think/Auth中指定的表。

 

ThinkPHP許可權問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.