不同的角色访问不同的资源,一个角色可以访问不同资源,一个资源可以被多个角色访问,属于多对多的关系,因此需要建立中间表。
CREATE TABLE `res_roles` (
`role_id` int(11) NOT NULL,
`resc_id` int(11) NOT NULL,
PRIMARY KEY (`resc_id`, `role_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
$("#permissions").click("click", function() {
var cbox = grid.getSelectedCheckbox();
if (cbox.length != 1 ) {
parent.$.ligerDialog.alert("请选择一条记录");
return;
}
parent.addTabEvent("permissions", "分配权限", rootPath + '/background/resources/aution.html?roleId=' + cbox);
});
说明:
1、权限分配按钮绑定事件。
2、判断选项只能勾选一条记录。
3、弹出新页面,并将勾选的角色编号传递。
/**
* 权限分配资源列表
**/
@RequestMapping("aution")
public String aution(Model model) throws Exception {
List<Resources> rs = resourcesService.queryAll(new Resources());
List<TreeObject> treeObjects = new ArrayList<TreeObject>();
for (Resources res : rs) {//转换为树对象
TreeObject t = new TreeObject();
PropertyUtils.copyProperties(t, res);
treeObjects.add(t);
}
List<TreeObject> ns = TreeUtil.getChildResourcess(treeObjects, 0);
model.addAttribute("permissions", ns);
return Common.BACKGROUND_PATH + "/resources/permissions";
}
说明:
1、查询所有资源数据,将数据组织成树对象。
2、将数据保存到model,跳转到页面渲染树对象数据。
<form method="post" id="from" name="form">
<input type="hidden" name="roleId" id="roleId" value="${param.roleId}">
<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">
<tr>
<th scope="row" abbr="L2 Cache" class="specalt">一级菜单</th>
<th scope="row" abbr="L2 Cache" class="specalt"><span>二级菜单</span><span style="float: right;margin-right: 150px;">按扭</span></th>
</tr>
<c:forEach items="${permissions}" var="k">
<tr>
<th scope="row" abbr="L2 Cache" class="specalt" width="140px">
<input type="checkbox" name="id" id="menu" _key="menu_${k.id}" onclick="smenu(this,'${k.id}')" value="${k.id}">
${k.name}
</th>
<th scope="row" abbr="L2 Cache" class="specalt">
<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series" style="width: 100%;height: 100%;">
<c:forEach items="${k.children}" var="kc">
<tr>
<th scope="row" abbr="L2 Cache" class="specalt">
<input type="checkbox" name="id" id="menu" _key="menu_1_${k.id}" _key_1="menu_1_1_${kc.id}" onclick="menu_1(this,'${kc.id}','${k.id}')" value="${kc.id}">
${kc.name}
</th>
<th>
<c:if test="${not empty kc.children}">
<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series" style="width: 100%;height: 100%;">
<c:forEach items="${kc.children}" var="kcc">
<tr>
<th scope="row" abbr="L2 Cache" class="specalt">
<input type="checkbox" name="id" id="menu" _key="menu_1_1_${k.id}" _key_2="menu_1_1_${kc.id}" onclick="menu_1_1(this,'${kc.id}','${k.id}')" value="${kcc.id}">
${kcc.name}
</th>
</tr>
</c:forEach>
</table>
</c:if>
</th>
</tr>
</c:forEach>
</table>
</th>
</tr>
</c:forEach>
<tr>
<td colspan="10">
<div class="l_btn_centent">
<a class="btn btn-primary" href="javascript:void(0)"
id="saveWin_form" onclick="sub();"><span>保存</span> </a>
<a class="btn btn-primary" href="javascript:void(0)" id="closeWin"
onclick="closeWin()"><span>关闭</span> </a>
</div></td>
</tr>
</table>
</form>
说明:
1、利用el表达式渲染树对象数据。
2、js控制勾选事件。
3、将上送的角色编号放置表单隐藏域。
4、保存绑定事件。
function smenu(obj,id){
$("input[_key='menu_1_"+id+"']").each(function(){
$(this).attr("checked",obj.checked);
});
$("input[_key='menu_1_1_"+id+"']").each(function(){
$(this).attr("checked",obj.checked);
});
};
function menu_1(obj,id,pid){
$("input[_key_2='menu_1_1_"+id+"']").each(function(){
$(this).attr("checked",obj.checked);
});
if(obj.checked==true){
$("input[_key='menu_"+pid+"']").each(function(){
$(this).attr("checked",obj.checked);
});
}
};
function menu_1_1(obj,id,pid){
if(obj.checked==true){
$("input[_key_1='menu_1_1_"+id+"']").each(function(){
$(this).attr("checked",obj.checked);
});
$("input[_key='menu_"+pid+"']").each(function(){
$(this).attr("checked",obj.checked);
});
}
}
保存绑定事件如下:
function sub(){
if ("${param.roleId}" != "") {
$.ajax({
async : false, //请勿改成异步,下面有些程序依赖此请数据
type : "POST",
data : $("#from").serialize(),
url : rootPath + '/background/resources/addRoleRes.html',
dataType : 'json',
success : function(json) {
if (json.flag == "true") {
parent.$.ligerDialog.confirm('分配成功!是否关闭窗口?', function(yes) {
if(yes){
parent.tab.removeSelectedTabItem();
}
});
} else {
$.ligerDialog.error("分配失败");
}
;
}
});
} else {
$.ligerDialog.warn("该账号还没有分配角色或账号的角色被删除了,请重新分配角色");
};
}
说明:
1、首先判断角色编号不能为空,否则无法进行角色的权限分配。
2、调用后端角色分配权限接口。
3、成功后确认是否需要关闭对话框。
/**
* 分配权限
**/
@ResponseBody
@RequestMapping("addRoleRes")
public Map<String, Object> addRoleRes(String roleId, Params params) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("flag", "false");
List<String> list = params.getId();
try {
if (null != list && list.size() > 0) {
resourcesService.addRoleRes(roleId, list);
map.put("flag", "true");
}
} catch (Exception e) {
logger.error("分配权限发生异常", e);
}
return map;
}
说明:
1、Params接收勾选的资源列表。
2、调用资源服务的增加角色资源方法。
3、分配资源方法逻辑是将原角色分配的资源全部删除,然后在重新添加,达到更新角色分配资源。
<insert id="addRoleRes" parameterType="com.lanyuan.entity.ResourcesRole">
INSERT INTO res_roles (role_id,resc_id) VALUE (#{roleId},#{resId})
</insert>
软件定制及其他业务
请加微信号:13128600812
公众号: