用户规则权限管理
用于将用户表manger与规则表auth_rule表关系对应到用户组表auth_group中
修改功能
控制器
//修改
public function update($id)
{
if(request()->isPost()){
$data = input('post.');
if($data['rules']){
$data['rules'] = implode(',', $data['rules']);
}
$res = db('auth_group')->update($data,['id'=>$id]);
if($res){
$this->success('添加成功',url('index'));
}else{
$this->error('添加失败');
}
}else{
//用户列表
$data = db('manger')->select();
$this->assign('data',$data);
//权限列表 主要是无限分列函数的调用
$AuthRuleModel = new AuthRuleModel();
$rule = $AuthRuleModel->coltree();
$this->assign('rule', $rule);
//查询修改前数据信息
$auth_group = db('auth_group')->find($id);
$this->assign('auth_group', $auth_group);
return view();
}
}
前端模板
{layout name="public/layout" /}
<div class="col-md-10">
<ol class="breadcrumb">
<li><a href="#"><span class="glyphicon glyphicon-home"></span> 首页</a></li>
<li><a href="#">权限管理</a></li>
<li class="active">添加权限</li>
<a href="" style="float:right;height:25px;" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></a>
<span style="clear:both"></span>
</ol>
<div class="panel panel-default">
<div class="panel-body">
<form action="" method="POST">
<div class="form-group">
<label for="">管理员名称</label>
<select name="title" class="form-control">
{volist name="data" id="data"}
<option value="{$data.id}" {$auth_group.title==$data.id?'selected':''}>{$data.username}</option>
{/volist}
</select>
</div>
<div class="form-group">
<label for="">状态:</label>
<label class="radio-inline">
<input type="radio" name="status" id="" value="0" {$auth_group.status==0?'checked':''}> 禁用
</label>
<label class="radio-inline">
<input type="radio" name="status" id="" value="1" {$auth_group.status==1?'checked':''}> 开启
</label>
</div>
<div class="form-group">
<label for="">规则</label>
<table>
{volist name="rule" id="tree"}
<tr>
<td>
<?php echo str_repeat(" ", $tree['level']*5) ?>
<input type="checkbox" dataid="{$tree.dataid}" value="{$tree.id}" class="checkbox-parent {$tree.level==0?'':'checkbox-child'}" name="rules[]"
<?php
$arr = explode(',', $auth_group['rules']);
if(in_array($tree['id'],$arr)){
echo 'checked';
}
?>
>
<span>{$tree.title}</span>
</td>
</tr>
{/volist}
</table>
<input type="hidden" name="id" value="{$auth_group['id']}">
</div>
<div class="form-group">
<input value="提交" class="btn btn-success" type="submit">
<input type="reset" value="重置" class="btn btn-danger">
</div>
</form>
</div>
<div class="panel-footer">
</div>
</div>
</div>
<script>
/* 权限配置 */
$(function () {
//动态选择框,上下级选中状态变化
$('input.checkbox-parent').on('change', function () {
var dataid = $(this).attr("dataid");
$('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
});
$('input.checkbox-child').on('change', function () {
var dataid = $(this).attr("dataid");
dataid = dataid.substring(0, dataid.lastIndexOf("-"));
var parent = $('input[dataid=' + dataid + ']');
if ($(this).is(':checked')) {
parent.prop('checked', true);
} else {
//父级
if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
parent.prop('checked', false);
}
}
});
});
</script>
效果图
删除功能
控制器
//删除
public function del($id)
{
$res = db('auth_group')->delete($id);
if($res){
$this->success('删除成功',url('index'));
}else{
$this->error('删除失败');
}
}