thinkphp3.2 rbac 使用方法,亲测,记录主要的点
官方的文件就不多说了,现在主要说如何使用,包括数据结构表,也会截图展示
相关数据表结构和数据
access表

node表

角色表

角色用户中间表

用户表

rbac的控制器代码 RbacController.class.php
class RbacController extends Controller
{
//角色列表
public function role_list() {
$this->role = M('role')->select();
$this->display();
}
//添加角色
public function add_role() {
$this->display();
}
//添加角色表单处理
public function add_role_handle() {
if(M('role')->add($_POST)) {
$this->success('添加成功',U('role_list','',''));
} else {
$this->error('添加失败');
}
}
//节点列表
public function node_list() {
$field = array('id', 'name', 'title', 'pid');
$node = M('node')->field($field)->order('sort asc')->select();
$this->node = node_regroup($node);//p($this->node);die;
$this->display();
}
//添加节点
public function add_node() {
$this->pid = I('get.pid', 0, 'int');//如果没有传递的pid参数,则默认为0
$this->level = I('get.level', 1, 'int');//如果没有传递的level参数,则level是1,代表顶级(模块)
switch($this->level) {
case 1:
$this->type = '模块';
break;
case 2:
$this->type = '控制器';
break;
case 3:
$this->type = '方法';
break;
}
$this->display();
}
//添加节点表单处理
public function add_node_handle() {
if(M('node')->add($_POST)) {
$this->success('添加成功',U('node_list','',''));
} else {
$this->error('添加失败');
}
}
//配置权限
public function access() {
$rid = I('get.rid', 0, 'int');//角色id
$field = array('id', 'name', 'title', 'pid');
$node = M('node')->field($field)->order('sort asc')->select();
$access = M('access')->where('role_id = '.$rid)->getField('node_id', true);//已经拥有的权限
$node = node_regroup($node, 0, $access); //递归节点
$this->rid = $rid;
$this->node = $node;
$this->display();
}
//权限配置的表单提交处理
public function access_handle() {
$rid = I('rid', 0, 'int');
$db = M('access');
$db->where('role_id = '.$rid)->delete();//删除原有权限
$data = array();
if(!empty($_POST['access'])) {
foreach($_POST['access'] as $v) {
$tmp = explode('_', $v);
$data[] = array(
'role_id'=>$rid,
'node_id'=>$tmp[0],
'level'=>$tmp[1]
);
}
if($db->addAll($data)) { //写入新权限
$this->success('分配权限成功', U('role_list','',''));
} else {
$this->error('分配权限失败');
}
}
}
//添加用户
function add_user() {
$this->role = M('role')->select();
$this->display();
}
//添加用户的表单提交处理
public function add_user_handle() {
$user = array(
'username'=>I('post.username', ''),
'password'=>I('post.password','','md5'),
);
$uid = M('user')->add($user);
$rold = array();
if($uid) {
foreach($_POST['role_id'] as $v) {
$role[] = array(
'role_id'=>$v,
'user_id'=>$uid
);
}
M('role_user')->addAll($role);
$this->success('添加成功', U('user_list','',''));
} else {
$this->error('添加失败');
}
}
//用户列表
public function user_list(){
$this->assign('user',D('UserRelation')->select());
$this->display();
}
}
rbac的view文件
添加角色 add_role.html

<!DOCTYPE html>
<html>
<head>
<title>添加角色</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="__PUBLIC__/Css/public.css"/>
</head>
<body>
<form action="{:U('add_role_handle','','')}" method="post">
<table class="table">
<tr>
<th colspan="2">添加角色:</th>
</tr>
<tr>
<td align="right">角色名称:</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td align="right">角色描述:</td>
<td>
<input type="text" name="remark" />
</td>
</tr>
<tr>
<td align="right">是否开启:</td>
<td>
<input type="radio" name="status" value="1" checked = "checked" />开启
<input type="radio" name="status" value="0" />关闭
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="保存添加">
</td>
</tr>
</table>
</form>
</body>
</html>
角色列表role_list.html

<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="__PUBLIC__/Css/Public.css">
</head>
<body>
<table class="table">
<tr>
<th>ID</th>
<th>角色名称</th>
<th>角色描述</th>
<th>开启状态</th>
<th>操作</th>
</tr>
<foreach name="role" item="v">
<tr>
<td>{$v.id}</td>
<td>{$v.name}</td>
<td>{$v.remark}</td>
<td>
<if condition="$v['status'] eq 1">开启<else />关闭</if>
</td>
<td>
<a href="{:U('access',array('rid'=>$v['id']),'')}">配置权限</a>
</td>
</tr>
</foreach>
</table>
</body>
</html>
添加节点add_node.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="__PUBLIC__/Css/public.css">
</head>
<body>
<form action="{:U('add_node_handle','','')}" method="post">
<table class="table">
<tr><th colspan="2">添加{$type}</th></tr>
<tr>
<td align="right">{$type}名称:</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td align="right">节点描述:</td>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<td align="right">是否开启:</td>
<td>
<input type="radio" name="status" value="1" checked="checked" />开启
<input type="radio" name="status" value="0" />关闭
</td>
</tr>
<tr>
<td align="right">排序:</td>
<td>
<input type="text" name="sort" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="pid" value="{$pid}" />
<input type="hidden" name="level" value="{$level}" />
<input type="submit" value="添加{$type}" />
</td>
</tr>
</table>
</form>
</body>
</html>
节点列表node_list.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="__PUBLIC__/Css/public.css">
</head>
<body>
<div id="wrap">
<a href="{:U('add_node','','')}">添加模块</a>
<table class="table">
<foreach name="node" item="app">
<div class="app">
<p>
<strong>{$app.title}</strong>
<a href="{:U('add_node',array('pid'=>$app['id'],'level'=>2),'')}">
[添加控制器]
</a>
<a href="">[修改]</a>
<a href="">[删除]</a>
</p>
<foreach name="app.child" item="controller">
<dl>
<dt>
-
<strong>{$controller.title}</strong>
<a href="{:U('add_node',array('pid'=>$controller['id'],'level'=>3),'')}">
[添加方法]
</a>
<a href="">[修改]</a>
<a href="">[删除]</a>
</dt>
<foreach name="controller.child" item="method">
<div>
-
<strong>{$method.title}</strong>
<a href="">[修改]</a>
<a href="">[删除]</a>
</div>
</foreach>
</dl>
</foreach>
</div>
</foreach>
</table>
</div>
</body>
</html>
添加用户add_user.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<css file="__PUBLIC__/Css/public.css" />
<js file="__PUBLIC__/Js/jquery-1.7.2.min.js" />
<style>
.add-role{
display:inline-block;
width:100px;
height:26px;
line-height: 26px;
text-align: center;
border: 1px solid #ccc;
border-radius: 4px;
margin-left: 20px;
cursor:pointer;
}
</style>
</head>
<body>
<form action="{:U('add_user_handle','','')}" method="post">
<table class="table">
<tr>
<th colspan="2">添加用户</th>
</tr>
<tr>
<td align="right" width="40%">用户账号</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td align="right">密码:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td align="right">所属角色:</td>
<td>
<select name="role_id[]" id="">
<option value="">请选择角色</option>
<foreach name="role" item="v">
<option value="{$v.id}">{$v.name}({$v.remark})</option>
</foreach>
</select>
<span class="add-role">添加一个角色</span>
</td>
</tr>
<tr id="last">
<td colspan="2" align="center">
<input type="submit" value="保存">
</td>
</tr>
</table>
</form>
</body>
<script>
$(function(){
$(".add-role").click(function(){
var obj = $(this).parents("tr").clone();
obj.find(".add-role").remove();
$("#last").before(obj);
});
});
</script>
</html>
配置权限 access.html
点开角色列表就看到配置权限功能

点击配置权限出现以下页面

打钩的有权限查看,没打钩的是Index模块,默认不判断
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="__PUBLIC__/Css/public.css">
<link rel="stylesheet" href="__PUBLIC__/Css/node.css">
<script src="__PUBLIC__/Js/jquery-1.7.2.min.js"></script>
</head>
<body>
<div id="wrap">
<a id="return" href="{:U('role_list','','')}">返回</a>
<form action="{:U('access_handle')}" method="post">
<table class="table">
<foreach name="node" item="app">
<div class="app">
<p>
<strong>{$app.title}</strong>
<input type="checkbox" name="access[]" value="{$app.id}_1" level="1" <if condition="$app['access'] eq 1">checked="checked"</if>>
</p>
<foreach name="app.child" item="controller">
<div class="app_child">
<dl class="controller">
<dt>
<strong>{$controller.title}</strong>
<input type="checkbox" name="access[]" value="{$controller.id}_2" level="2" <if condition="$controller['access'] eq 1">checked="checked"</if>>
</dt>
</dl>
<foreach name="controller.child" item="method">
<span class="method">
<strong>{$method.title}</strong>
<input type="checkbox" name="access[]" value="{$method.id}_3" level="3" <if condition="$method['access'] eq 1">checked="checked"</if>>
</span>
</foreach>
<div style="clear:both"></div>
</div>
</foreach>
</div>
</foreach>
</table>
<input type="submit" value="提交" style="display: block; margin:0 auto; cursor:pointer">
<input type="hidden" name="rid" value="{$rid}">
</form>
</div>
</body>
<script>
$(function(){
$('input[level=1]').click(function(){
var inputs = $(this).parents('.app').find('input');
$(this).prop('checked') == true ? inputs.prop('checked', true) : inputs.prop('checked', false);
});
$('input[level=2]').click(function(){
var inputs = $(this).parents('.app_child').find('input');
$(this).prop('checked') == true ? inputs.prop('checked', true) : inputs.prop('checked', false);
});
});
</script>
</html>

浙公网安备 33010602011771号