系统中,角色表和操作表是多对多的关系,总体思路就是先显示出系统的可分配的菜单和操作(就是权限),然后显示该角色已经拥有的权限,即发送两次请求,就是下面这样:
然后你要授权就勾选,按下授权按钮,向后台传输的数据其实很简单那,就是该角色的ID(roleId)和所授权权限的ID集合(ids):
var submitForm = function($dialog, $grid, $pjq) { var nodes = $('#tree').tree('getChecked', [ 'checked', 'indeterminate' ]); var ids = []; for (var i = 0; i < nodes.length; i++) { ids.push(nodes[i].id); } $.post('role/grantRole', { roleId : $('#roleId').val(), ids : ids.join(',') }, function(result) { if (result.success) { $dialog.dialog('destroy'); } else { $pjq.messager.alert('提示', result.msg, 'error'); } $pjq.messager.alert('提示', '授权成功!', 'info'); }, 'json'); };
然后后台接受,在遍历权限集合,把每个权限都添加到该角色上就行了.
Controller:
@RequestMapping("/grantRole") public void grantRole(HttpServletRequest request,HttpServletResponse response){ int roleId = Integer.valueOf(request.getParameter("roleId")) ; String ids=request.getParameter("ids"); System.out.println(ids); roleService.grant(roleId,ids); Json json = new Json(); json.setSuccess(true); writeJson(json,response); }
Service中的 roleService.grant(roleId,ids):
public void grant(int roleId, String ids) { // TODO Auto-generated method stub Role role=roleDao.getById(Role.class, roleId); if(role!=null){ role.setOpeations(new HashSet<Operation>()); for(String operationId: ids.split(",")){ if (!StringUtils.isBlank(operationId)){ Operation o=operationDao.getById(Operation.class, operationId); if(o!=null){ role.getOpeations().add(o); } } } } }
时间: 2024-11-13 20:34:39