一
先創建一個數據庫;
例如:創建一個test數據庫;然后創建3個 表分別為:test_admin (管理員表), test_role,test_auth.
這個是新創建的test庫
管理員表
這個是新創建的admin表, 這個表是用戶表是管理后臺的用戶。
這個表的issuper這個字段代表是否是超級管理員 , 這個超級管理員可以管理全部的角色和執行所有的權限。
admin_role_id 這個字段主要描述的是除了超級管理員之外的管理員所對應的角色表id 下面我們會給出角色表.
角色表
這個表是角色表,他的主id 和管理員的admin_role_id可以分出管理員都處于什么角色管理.
權限表
這個表是權限表,他的主id 所對應的是角色表的role_auth_id 可以得出不同的角色有著不同的權限可以執行.
二
網站后臺管理頁面登陸不同的管理員對角色和角色權限的顯示.
在tinkphp的application的admin文件的model層創建Admin.php,Role.php,Auth.php進行業務處理.
然后在controller層創建index.php
?phpnamespace app/admin/controller;use think/Controller;use think/Url;use think/Request;use think/Session;use app/admin/model/Auth as AuthModeluse app/admin/model/Role as RoleModelhtml' target='_blank'>class Index extends CommonController public $role; public $auth; public $view;public funtion __construct() $this- role = new RoleModel() $this- auth = new AuthModel() $this- view = new View(); publci function auth()//角色id; $admin_id = sesison( admin_id $admin_name = session( admin_name $resAdmin = $this- admin- where([ admin_id = $admin_id])- select(); if($resAdmin[0]- issuper == 1){//超級管理員擁有全部權限;//一級權限; $authA = $this- auth- where([ auth_level ]= 0)- select();//二級權限 $authB = $this- auth- where([ auth_level = 1])- select();} else { //權限ids; $role_auth_ids = $this- role- where([ role_id = $admin_role_id])- select(); $authA = $this- auth- where( auth_level , 0)- where( auth_id , in , $role_auth_ids)- select(); $authB = $this- auth- where( auth_level , 1])- where( auth_id , in , $role_auth_ids)- select(); $auth = array( authA = $authA , authB = $authB); $this- redirect( admin/ .$auth[ authA ][0]- auth_c. / .$auth[ authA ][0]- auth_a);public function leftnav() $admin_id = session( admin_id $amin_name = session( admin_name //角色id; $resAdmin = $this- admin- where([ admin_id ]= $admin_id)- select(); $admin_role_id = $resAdmin[0]- $admin_role_id; if($resAdmin[0]- issuper == 1){ //超級管理員super擁有全部權限; //一級權限; $authA = $this- auth- where([ auth_level = 0])- select(); //二級權限; $authB = $this- auth- where([ auth_level = 1])- select();} else { //權限ids $role_auth_ids = $this- role- where([ role_id = $admin_role_id])- select(); $role_auth_ids = $role_auth_ids[0]- role_auth_ids; $authA = $this- auth- where( auth_level , 0)- where( auth_id , in , $role_auth_ids)- select(); $authB = $this- auth- where( auth_level , 1)- where( auth_id , in , $role_aut_ids)- select(); $auth = array( authA = $authA , authB = $authB); $this- view- assign( authA , $auth[ authA $this- view- assign( authB , $auth[ authB
}
現在我來解釋一下上面auth方法的作用是用來重定向的如果登陸的管理者向url地址輸入了不屬于他的權限的地址我們會讓他重定向到他自己的管理頁面.
還有繼承的CommonController 的內容;
?phpnamspace app/admin/controller;use think/Controller;use think/Request;use app/admin/model/Common as Controller public function __construct() parent::__construct(); $res = new CommonModel(); $resquest = Request::instance(); if(session( admin_id ) == null){ if(strtolower($resquest- controller()) == index strtolower($resquest- action()) == login ){ return true;} else { $this- error( 沒有登陸! br / span >三
權限控制
管理員登陸后臺 訪問屬于自己權限的操作業務 , 如果管理員想要越級查看不屬于自己權限的業務 , 控制器 會讓管理員重定向到自己的操作頁面.
?phpnamespace app/admin/model;use think/Model;use think/Db;use think/Session;use think/Request;use app/admin/model/Admin as AdminModel;use app/admin/model/Role as RoleModel;use app/admin/model/Auth as AuthModel;class Common extends Model public function auth() //當前控制器和操作方法; $request= Request::instance(); $auth_ac = strtolower(trim($request- controller())). / .strtolower(trim($request- action())); //var_dump($auth_ac); $auth = array(); $res = new AdminModel(); $resRole = new RoleModel(); $resAuth = new AuthModel(); $resAdmin = $res- where([ admin_id = session( admin_id )])- select(); //非超級管理員控制權限; if($resAdmin[0]- issuper != 1){ $admin_role_id = $resAdmin[0]- admin_role_id; //$admin_role_id = $info[ admin_role_id //$info = $this- info( Role , [ role_id = $admin_role_id] , role_auth_ids $info = $resRole- where( role_id , $admin_role_id)- select(); $role_auth_ids = $info[0]- role_auth_ids; $infos = $resAuth- where( auth_id , in , $role_auth_ids)- select(); //$infos = $this- infos( Auth , [ auth_id = [ in , $role_auth_ids] , auth_level = 1] , auth_c , auth_a foreach($infos as $key= $val){ $auth[] = $val[ auth_c ]. / .$val[ auth_a $result = array_merge($auth , [ index/auth ] , [ index/login //var_dump($result); if(in_array($auth_ac , $result)){ return true; } else { return false; } else { return true;}上面的CommonModel 在CommonController 中被調用 , 來進行管理員權限等級的判斷.
相關教程:PHP視頻教程
以上就是用thinkphp5 實現基于角色的訪問控制(rbac權限)的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答