All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.mizhousoft.bmc.role.controller.EditRoleController Maven / Gradle / Ivy
package com.mizhousoft.bmc.role.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.mizhousoft.bmc.BMCException;
import com.mizhousoft.bmc.auditlog.constants.AuditLogResult;
import com.mizhousoft.bmc.auditlog.controller.BaseAuditController;
import com.mizhousoft.bmc.auditlog.domain.OperationLog;
import com.mizhousoft.bmc.auditlog.util.AuditLogUtils;
import com.mizhousoft.bmc.role.domain.Permission;
import com.mizhousoft.bmc.role.domain.Role;
import com.mizhousoft.bmc.role.request.RoleRequest;
import com.mizhousoft.bmc.role.service.PermissionViewService;
import com.mizhousoft.bmc.role.service.RoleCacheService;
import com.mizhousoft.bmc.role.service.RoleViewService;
import com.mizhousoft.commons.json.JSONException;
import com.mizhousoft.commons.json.JSONUtils;
import com.mizhousoft.commons.web.ActionRespBuilder;
import com.mizhousoft.commons.web.ActionResponse;
import com.mizhousoft.commons.web.AssertionException;
import com.mizhousoft.commons.web.antd.TreeNode;
import com.mizhousoft.commons.web.i18n.util.I18nUtils;
/**
* 编辑角色控制器
*
* @version
*/
@RestController
public class EditRoleController extends BaseAuditController
{
private static final Logger LOG = LoggerFactory.getLogger(EditRoleController.class);
@Autowired
private PermissionViewService permissionViewService;
@Autowired
private RoleViewService roleViewService;
@Autowired
private RoleCacheService roleCacheService;
@RequestMapping(value = "/role/editRole.action", method = RequestMethod.GET)
public ModelMap editRole(@RequestParam(name = "id") Integer id)
{
ModelMap map = new ModelMap();
try
{
Role role = roleViewService.loadById(id);
map.put("role", role);
List rolePerms = roleViewService.queryPermissionsByRoleName(role.getName());
List authzPerms = permissionViewService.queryAuthzPermissions();
Set checkedIds = new HashSet(10);
Set halfCheckedIds = new HashSet(10);
List treeNodes = buildTreeNodes(authzPerms, rolePerms, checkedIds, halfCheckedIds);
String treeData = JSONUtils.toJSONString(treeNodes);
map.put("treeData", treeData);
map.put("checkedKeys", checkedIds);
}
catch (BMCException e)
{
LOG.error("Fetch role info failed, role id is " + id + '.');
String error = I18nUtils.getMessage(e.getErrorCode(), e.getCodeParams());
map.putAll(ActionRespBuilder.buildFailedMap(error));
}
catch (JSONException e)
{
LOG.error("TreeNode Object to json string failed.", e);
}
return map;
}
@RequestMapping(value = "/role/modifyRole.action", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ActionResponse modifyRole(@RequestBody RoleRequest request)
{
ActionResponse response = null;
OperationLog operLog = null;
try
{
request.validate();
Role role = roleViewService.modifyRole(request);
List permissions = roleViewService.queryPermissionsByRoleName(role.getName());
roleCacheService.refreshRolePermissions(role.getSrvId(), role.getName(), permissions);
response = ActionRespBuilder.buildSucceedResp();
operLog = buildOperLog(AuditLogResult.Success, request.toString(), null);
}
catch (BMCException | AssertionException e)
{
LOG.error("Modify role failed.", e);
String error = I18nUtils.getMessage(e.getErrorCode(), e.getCodeParams());
response = ActionRespBuilder.buildFailedResp(error);
operLog = buildOperLog(AuditLogResult.Failure, e.getMessage(), request.toString());
}
AuditLogUtils.addOperationLog(operLog);
return response;
}
private List buildTreeNodes(List authzPerms, List rolePerms, Set checkedIds,
Set halfCheckedIds)
{
TreeNode rootNode = new TreeNode();
Map rolePermMap = new HashMap(10);
rolePerms.forEach(perm -> rolePermMap.put(perm.getId(), perm));
Permission parentPerm = new Permission();
parentPerm.setName(null);
recursiveTree(rootNode, parentPerm, authzPerms, rolePermMap, checkedIds, halfCheckedIds);
return rootNode.getChildren();
}
private void recursiveTree(TreeNode parent, Permission parentPerm, List perms, Map rolePermMap,
Set checkedIds, Set halfCheckedIds)
{
List children = new ArrayList();
Iterator iter = perms.iterator();
while (iter.hasNext())
{
Permission perm = iter.next();
if (StringUtils.equals(perm.getParentName(), parentPerm.getName()))
{
TreeNode treeNode = new TreeNode();
treeNode.setKey(perm.getId() + "");
treeNode.setTitle(perm.getDisplayNameCN());
children.add(treeNode);
recursiveTree(treeNode, perm, perms, rolePermMap, checkedIds, halfCheckedIds);
}
}
if (children.isEmpty())
{
if (rolePermMap.containsKey(parentPerm.getId()))
{
checkedIds.add(parentPerm.getId() + "");
}
parent.setLeaf(true);
}
else
{
parent.setChildren(children);
}
}
/**
* {@inheritDoc}
*/
@Override
protected String getAuditOperation()
{
return "bmc.role.modify.operation";
}
/**
* {@inheritDoc}
*/
@Override
protected String getAuditSource()
{
return "bmc.role.source";
}
}