嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-bweb/src/main/java/com/bweb/controller/SecPermissionController.java

181 lines
8.0 KiB

package com.bweb.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.SecPermission;
import com.hai.model.ResponseData;
import com.hai.service.SecPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value = "/secPermission")
@Api(value = "权限管理")
public class SecPermissionController {
private static Logger log = LoggerFactory.getLogger(SecPermissionController.class);
@Resource
private SecPermissionService secPermissionService;
@RequestMapping(value = "/getPermissionList", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取权限列表")
public ResponseData getPermissionList(String permissionName, Integer type, Integer pageNum, Integer pageSize) {
try {
if (pageNum == null || pageSize == null) {
log.error("SecPermissionController --> getPermissionList() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
PageInfo<SecPermission> permissionList = secPermissionService.findPage(permissionName,type,pageNum, pageSize);
return ResponseMsgUtil.success(permissionList);
} catch (Exception e) {
log.error("SecPermissionController --> getPermissionList() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/getAllPermission", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取所有权限")
public ResponseData getAllPermission() {
try {
List<SecPermission> permissionList = secPermissionService.getPermissions();
return ResponseMsgUtil.success(permissionList);
} catch (Exception e) {
log.error("SecPermissionController --> getAllPermission() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/getNoAllot", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取未分配权限")
public ResponseData getNoAllot(Long roleId) {
try {
if (roleId == null) {
log.error("SecPermissionController --> getUnAllot() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
List<SecPermission> permissionList = secPermissionService.getNoPermissionsByRoleId(roleId);
return ResponseMsgUtil.success(permissionList);
} catch (Exception e) {
log.error("SecPermissionController --> getUnAllot() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/getPermissionsByRoleId", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取已分配权限")
public ResponseData getPermissionsByRoleId(Long roleId) {
try {
if (roleId == null) {
log.error("SecPermissionController --> getPermissionsByRoleId() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
List<SecPermission> permissionList = secPermissionService.getPermissionsByRoleId(roleId);
return ResponseMsgUtil.success(permissionList);
} catch (Exception e) {
log.error("SecPermissionController --> getPermissionsByRoleId() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/allotPermission", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "分配权限")
public ResponseData allotPermission(@RequestBody String rolePms) {
try {
if (StringUtils.isBlank(rolePms)) {
log.error("SecPermissionController --> allotPermission() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
JSONObject jsonObject = JSONObject.parseObject(rolePms);
Long roleId = jsonObject.getLong("roleId"); //角色id
JSONArray menuIds = jsonObject.getJSONArray("pmsIds"); //菜单id
List<Long> menuIdList = new ArrayList<>();
if (menuIds != null && menuIds.size() != 0) {
for (int i = 0; i < menuIds.size(); i++) {
menuIdList.add(menuIds.getLong(i));
}
}
if (roleId == null || menuIdList == null) {
log.error("SecPermissionController --> allotPermission() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
if(menuIdList != null && menuIdList.size() > 0){
//查询对应的权限Id
List<Long> pmsIdList = secPermissionService.findPmsIds(menuIdList);
if (pmsIdList != null && pmsIdList.size() != 0) {
secPermissionService.assignPmsToRole(roleId,pmsIdList);
}
}else{
secPermissionService.deleteRolePms(roleId);
}
return ResponseMsgUtil.success(null);
} catch (Exception e) {
log.error("SecPermissionController --> allotPermission() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/allotTemplatePermission", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "给角色模板分配权限")
public ResponseData allotTemplatePermission(@RequestBody String rolePms) {
try {
if (StringUtils.isBlank(rolePms)) {
log.error("SecPermissionController --> allotTemplatePermission() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
JSONObject jsonObject = JSONObject.parseObject(rolePms);
Long roleId = jsonObject.getLong("roleId"); //角色id
JSONArray menuIds = jsonObject.getJSONArray("pmsIds"); //菜单id
List<Long> menuIdList = new ArrayList<>();
if (menuIds != null && menuIds.size() != 0) {
for (int i = 0; i < menuIds.size(); i++) {
menuIdList.add(menuIds.getLong(i));
}
}
if (roleId == null || menuIdList == null) {
log.error("SecPermissionController --> allotTemplatePermission() error!", "参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
if(menuIdList != null && menuIdList.size() > 0){
//查询对应的权限Id
List<Long> pmsIdList = secPermissionService.findPmsIds(menuIdList);
if (pmsIdList != null && pmsIdList.size() != 0) {
secPermissionService.assignTemplatePmsToRole(roleId,pmsIdList);
}
}else{
secPermissionService.deleteRolePms(roleId);
}
return ResponseMsgUtil.success(null);
} catch (Exception e) {
log.error("SecPermissionController --> allotTemplatePermission() error!", e);
return ResponseMsgUtil.exception(e);
}
}
}