一
先建立一个数据库;
比方:建立一个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
<?php namespace app\admin\controller; use think\Controller; use think\Url; use think\Request; use think\Session; use app\admin\model\Auth as AuthModel use app\admin\model\Role as RoleModel 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 的内容;
<?php namspace 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 style="color:gray;">...</span> '); } $resCommon = $res->auth(); if(Request::instance()->isAjax()){ $this->ajaxReturn(['msg'=>'没有操纵权限!' , 'code'=>'201'] , 'json'); } else { $this->error('没有操纵权限!<br><span style="color:gray;">...</span>'); } } } }
三
权限掌握
治理员上岸背景 接见属于本身权限的操纵营业 , 假如治理员想要越级检察不属于本身权限的营业 , 掌握器 会让治理员重定向到本身的操纵页面.
<?php namespace 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权限)的细致内容,更多请关注ki4网别的相干文章!