thinkphp5 rbac权限

thinkphp 5 rbac权限

先创建一个数据库;

例如:创建一个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

namespace appadmincontroller;

use thinkController;

use thinkUrl;

use thinkRequest;

use thinkSession;

use appadminmodelAuth as AuthModel

use appadminmodelRole 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',$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',$role_auth_ids)->select();

$authB = $this->auth->where('auth_level',1)->where('auth_id',$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 的内容;

namspace appadmincontroller;

use thinkController;

use thinkRequest;

use appadminmodelCommon 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('没有登陆!
');

}

$resCommon = $res->auth();

if(Request::instance()->isAjax()){

$this->ajaxReturn(['msg'=>'没有操作权限!','code'=>'201'],'json');

} else {

$this->error('没有操作权限!
');

}

}

}

}

权限控制

管理员登陆后台 访问属于自己权限的操作业务,如果管理员想要越级查看不属于自己权限的业务,控制器 会让管理员重定向到自己的操作页面.

namespace appadminmodel;

use thinkModel;

use thinkDb;

use thinkSession;

use thinkRequest;

use appadminmodelAdmin as AdminModel;

use appadminmodelRole as RoleModel;

use appadminmodelAuth 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',$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 中被调用,来进行管理员权限等级的判断.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的
(1)创建模版:/App/Home/View/Form/edit.html   <FORM method=\"post\" action=\"__URL__/update\">
自定义配置文件user.php: <?php return array(    \'sex\'=>\'man\', ); config.php: <?php return array(
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦后台中系统设置),以方便站长在后台修改配置文件;那么这个功能是如果实现的呢?在ThinkPHP中有没有捷径可走呢?答案肯定是有的。下面大概说一下这个功能
废话不多说先上图预览下,即本博客的分页; 这个分页类是在thinkphp框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;
在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助。
thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。
配置数据库:/app/Common/Conf/config.php 方法一: // 添加数据库配置信息 \'DB_TYPE\'   => \'mysql\',// 数据库类型
/app/Home/Controller/IndexController.class.php
(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_data` (   `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
(1)控制器设置:/app/Home/Controller/IndexController.class.php <?php namespace HomeController; use ThinkController;
(1)普通模式 http://localhost/index.php?m=module&a=action&var=value m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。
入库的时候用htmlspecialchars()处理含有html代码的内容 输出的时候用htmlspecialchars_decode()处理含有html代码的内容
<?php define(\'APP_NAME\',\'app\'); define(\'APP_PATH\',\'./app/\'); define(\'APP_DEBUG\',TRUE); // 开启调试模式
(1)创建控制器中定义read方法:/App/Home/Controller/FormController.class.php public function read($id=0){
一、实现不同字段相同的查询条件 $User = M(\"User\"); // 实例化User对象 $map[\'name|title\'] = \'thinkphp\';
如果你的数据完全是内部操作写入而不是通过表单的话(也就是说可以充分信任数据的安全),那么可以直接使用add方法,如:
查询表达式的使用格式: $map[\'字段名\'] = array(\'表达式\',\'查询条件\'); 表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
一、使用字符串作为查询条件 $User = M(\"User\"); // 实例化User对象 $User->where(\'type=1 AND status=1\')->select();