This commit is contained in:
xiangwb
2017-11-15 13:46:40 +08:00
parent 3879c4e287
commit cbf6486631
8 changed files with 185 additions and 41 deletions
@@ -18,6 +18,8 @@ public interface SysAuthorityRepository extends JpaRepository<SysAuthority, Stri
List<SysAuthority> getByParentIdAndEnable(String parentId, String enable);
List<SysAuthority> getByParentId(String parentId);
List<SysAuthority> getByEnableAndIdIn(String enable, List<String> ids);
}
@@ -56,7 +56,6 @@ public class SysAuthorityService {
result.setMessage("保存权限失败");
}
return result;
}
/**
@@ -128,7 +127,7 @@ public class SysAuthorityService {
}
/**
* 查询所有子节点
* 根据状态查询所有子节点
*
* @param parentId
* @param enable
@@ -137,7 +136,10 @@ public class SysAuthorityService {
public List<SysAuthority> listByParentEnable(String parentId, String enable) {
if (StringUtils.isEmpty(parentId))
parentId = CommonConstant.ROOT;
return sysAuthorityRepository.getByParentIdAndEnable(parentId, enable);
if (StringUtils.isNotEmpty(enable))
return sysAuthorityRepository.getByParentIdAndEnable(parentId, enable);
else
return sysAuthorityRepository.getByParentId(parentId);
}
/**
@@ -158,27 +160,6 @@ public class SysAuthorityService {
return list;
}
/**
* 递归查询父节点下所有权限的id集合,并将状态设置为禁用(禁用权限时用)
*
* @param parentId
* @return
*/
public List<SysAuthority> queryAllChildren(String parentId) {
//获取结果
List<SysAuthority> sysAuthoritys = listByParentEnable(parentId, CommonConstant.ISENABLE);
//遍历子集
List<SysAuthority> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(sysAuthoritys)) {
for (SysAuthority sysAuthority : sysAuthoritys) {
sysAuthority.setEnable(CommonConstant.ISNOTENABLE);
sysAuthority.setModifiedTime(new Date());
list.add(sysAuthority);
list.addAll(queryAllChildren(sysAuthority.getId()));
}
}
return list;
}
/**
* 根据父节点禁用所有子节点
@@ -204,7 +185,11 @@ public class SysAuthorityService {
*/
public List<SysAuthVo> queryAllChildren(String parentId, String enable) {
List<SysAuthVo> list = new ArrayList<>();
List<SysAuthority> authoritys = sysAuthorityRepository.getByParentIdAndEnable(parentId, enable);
List<SysAuthority> authoritys;
if (StringUtils.isNotEmpty(enable))
authoritys = sysAuthorityRepository.getByParentIdAndEnable(parentId, enable);
else
authoritys = sysAuthorityRepository.getByParentId(parentId);
if (CollectionUtils.isEmpty(authoritys))
return list;
SysAuthVo vo;
@@ -216,6 +201,28 @@ public class SysAuthorityService {
return list;
}
/**
* 递归查询父节点下所有权限的id集合,并将状态设置为禁用(禁用权限时用)
*
* @param parentId
* @return
*/
private List<SysAuthority> queryAllChildren(String parentId) {
//获取结果
List<SysAuthority> sysAuthoritys = listByParentEnable(parentId, CommonConstant.ISENABLE);
//遍历子集
List<SysAuthority> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(sysAuthoritys)) {
for (SysAuthority sysAuthority : sysAuthoritys) {
sysAuthority.setEnable(CommonConstant.ISNOTENABLE);
sysAuthority.setModifiedTime(new Date());
list.add(sysAuthority);
list.addAll(queryAllChildren(sysAuthority.getId()));
}
}
return list;
}
/**
* 检查code是否唯一 true唯一
*
@@ -62,4 +62,18 @@ public class SysRoleAuthorityService {
public List<SysRoleAuthority> listByRoleId(String roleId) {
return sysRoleAuthorityRepository.getByRoleId(roleId);
}
/**
* 批量删除
*
* @param roleAuthorities
* @return
*/
public RestMessage removeBatch(List<SysRoleAuthority> roleAuthorities) {
RestMessage result = new RestMessage();
sysRoleAuthorityRepository.deleteInBatch(roleAuthorities);
result.setSuccess(true);
result.setMessage("批量删除成功");
return result;
}
}
@@ -1,6 +1,7 @@
package com.xwbing.service;
import com.xwbing.entity.SysRole;
import com.xwbing.entity.SysRoleAuthority;
import com.xwbing.entity.SysUserRole;
import com.xwbing.exception.BusinessException;
import com.xwbing.repository.SysRoleRepository;
@@ -30,6 +31,8 @@ public class SysRoleService {
private SysRoleRepository sysRoleRepository;
@Resource
private SysUserRoleService sysUserRoleService;
@Resource
private SysRoleAuthorityService sysRoleAuthorityService;
private final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
/**
@@ -68,7 +71,12 @@ public class SysRoleService {
SysRole one = getById(id);
if (one == null)
throw new BusinessException("该角色不存在");
//删除角色
sysRoleRepository.delete(id);
//删除角色权限
List<SysRoleAuthority> roleAuthorities = sysRoleAuthorityService.listByRoleId(id);
if (CollectionUtils.isNotEmpty(roleAuthorities))
sysRoleAuthorityService.removeBatch(roleAuthorities);
result.setMessage("删除成功");
result.setSuccess(true);
return result;
@@ -61,4 +61,18 @@ public class SysUserRoleService {
public List<SysUserRole> listByUserId(String userId) {
return sysUserRoleRepository.getByUserId(userId);
}
/**
* 批量删除
*
* @param userRoles
* @return
*/
public RestMessage removeBatch(List<SysUserRole> userRoles) {
RestMessage result = new RestMessage();
sysUserRoleRepository.deleteInBatch(userRoles);
result.setSuccess(true);
result.setMessage("批量删除成功");
return result;
}
}
@@ -36,6 +36,8 @@ public class SysUserService {
private SysRoleService sysRoleService;
@Resource
private SysAuthorityService sysAuthorityService;
@Resource
private SysUserRoleService sysUserRoleService;
/**
* 增
@@ -89,7 +91,13 @@ public class SysUserService {
if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) {
throw new BusinessException("不能对管理员进行删除操作");
}
//删除用户
sysUserRepository.delete(id);
//删除用户角色
List<SysUserRole> sysUserRoles = sysUserRoleService.listByUserId(id);
if (CollectionUtils.isNotEmpty(sysUserRoles)) {
sysUserRoleService.removeBatch(sysUserRoles);
}
result.setMessage("删除成功");
result.setSuccess(true);
return result;
@@ -302,8 +310,8 @@ public class SysUserService {
List<SysRole> sysRoles = sysRoleService.listByUserIdEnable(userId, enable);
if (CollectionUtils.isEmpty(sysRoles))
return list;
List<SysAuthority> temp;
//遍获取每个角色拥有的权限,并去重
List<SysAuthority> temp;
for (SysRole sysRole : sysRoles) {
temp = sysAuthorityService.listByRoleIdEnable(sysRole.getId(), enable);
if (CollectionUtils.isNotEmpty(temp)) {
@@ -0,0 +1,98 @@
package com.xwbing.controller;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.annotation.LogInfo;
import com.xwbing.constant.CommonConstant;
import com.xwbing.constant.CommonEnum;
import com.xwbing.entity.SysAuthority;
import com.xwbing.entity.vo.SysAuthVo;
import com.xwbing.service.SysAuthorityService;
import com.xwbing.util.JSONObjResult;
import com.xwbing.util.RestMessage;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2017/11/15 10:36
* 作者: xiangwb
* 说明: 权限控制层
*/
@Api(tags = "authorityApi", description = "权限相关接口")
@RestController
@RequestMapping("/authority/")
public class SysAuthorityControl {
@Resource
private SysAuthorityService sysAuthorityService;
@LogInfo("添加权限")
@PostMapping("save")
public JSONObject save(@RequestBody SysAuthority sysAuthority) {
RestMessage save = sysAuthorityService.save(sysAuthority);
return JSONObjResult.toJSONObj(save);
}
@LogInfo("删除权限")
@GetMapping("removeById")
public JSONObject removeById(@RequestParam String id) {
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
RestMessage result = sysAuthorityService.removeById(id);
return JSONObjResult.toJSONObj(result);
}
@LogInfo("修改权限")
@PostMapping("update")
public JSONObject update(@RequestBody SysAuthority sysAuthority) {
if (StringUtils.isEmpty(sysAuthority.getId()))
return JSONObjResult.toJSONObj("主键不能为空");
String enable = sysAuthority.getEnable();
// 如果禁用,查询是否有子节点,如果有,子节点也要被禁用
if (CommonConstant.ISNOTENABLE.equals(enable)) {
// 子节点的权限都禁用
if (!sysAuthorityService.disableChildrenByParentId(sysAuthority.getId())) {
return JSONObjResult.toJSONObj("禁用子节点权限失败");
}
} else {
// 如果是启用看父节点是否被禁用,一级的不需要判断
if (!CommonConstant.ROOT.equals(sysAuthority.getParentId())) {
SysAuthority parent = sysAuthorityService.getById(sysAuthority.getParentId());
if (parent != null && CommonEnum.YesOrNoEnum.NO.getCode().equals(parent.getEnable()))
return JSONObjResult.toJSONObj("父节点已被禁用,请先启用父节点");
}
}
RestMessage result = sysAuthorityService.update(sysAuthority);
return JSONObjResult.toJSONObj(result);
}
@LogInfo("根据父节点查询子节点(非递归)")
@PostMapping("listByParentId")
public JSONObject listByParentId(String parentId) {
if (StringUtils.isEmpty(parentId))
parentId = CommonConstant.ROOT;
if (!CommonConstant.ROOT.equals(parentId) && sysAuthorityService.getById(parentId) == null)
return JSONObjResult.toJSONObj("父节点不存在");
List<SysAuthority> queryByParentId = sysAuthorityService.listByParentEnable(parentId, null);
return JSONObjResult.toJSONObj(queryByParentId, true, "");
}
@LogInfo("根据是否启用查询所有权限")
@PostMapping("listAuthorityByEnable")
public JSONObject listAuthorityByEnable(@RequestParam String enable) {
if (StringUtils.isEmpty(enable))
return JSONObjResult.toJSONObj("是否启用不能为空");
List<SysAuthority> authoritys = sysAuthorityService.listByEnable(enable);
return JSONObjResult.toJSONObj(authoritys, true, "");
}
@LogInfo("递归查询所有权限")
@PostMapping("queryTree")
public JSONObject queryTree(String enable) {
List<SysAuthVo> authoritys = sysAuthorityService.queryAllChildren(CommonConstant.ROOT, enable);
return JSONObjResult.toJSONObj(authoritys, true, "");
}
}
@@ -45,9 +45,8 @@ public class SysRoleControl {
@LogInfo("删除角色")
@GetMapping("removeById")
public JSONObject removeById(@RequestParam String id) {
if (StringUtils.isEmpty(id)) {
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
}
RestMessage result = sysRoleService.removeById(id);
return JSONObjResult.toJSONObj(result);
}
@@ -55,9 +54,8 @@ public class SysRoleControl {
@LogInfo("修改角色")
@PostMapping("update")
public JSONObject update(@RequestBody SysRole sysRole) {
if (StringUtils.isEmpty(sysRole.getId())) {
if (StringUtils.isEmpty(sysRole.getId()))
return JSONObjResult.toJSONObj("主键不能为空");
}
RestMessage result = sysRoleService.update(sysRole);
return JSONObjResult.toJSONObj(result);
}
@@ -65,31 +63,27 @@ public class SysRoleControl {
@LogInfo("获取角色详情")
@GetMapping("getById")
public JSONObject getById(@RequestParam String id) {
if (StringUtils.isEmpty(id)) {
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
}
SysRole sysRole = sysRoleService.getById(id);
if (sysRole == null) {
if (sysRole == null)
return JSONObjResult.toJSONObj("该角色不存在");
}
return JSONObjResult.toJSONObj(sysRole, true, "");
}
@LogInfo("根据角色主键查找权限")
@PostMapping("listAuthorityByRoleId")
public JSONObject listAuthorityByRoleId(@RequestParam String roleId, String enable) {
if (StringUtils.isEmpty(roleId)) {
if (StringUtils.isEmpty(roleId))
return JSONObjResult.toJSONObj("角色主键不能为空");
}
SysRole sysRole = sysRoleService.getById(roleId);
if (sysRole == null) {
if (sysRole == null)
return JSONObjResult.toJSONObj("该角色不存在");
}
List<SysAuthority> authoritys = sysAuthorityService.listByRoleIdEnable(roleId, enable);
return JSONObjResult.toJSONObj(authoritys, true, "");
}
@LogInfo("保存角色 权限主键")
@LogInfo("保存角色权限")
@PostMapping("saveAuthoritys")
public JSONObject saveAuthoritys(@RequestParam String authorityIds, @RequestParam String roleId) {
if (StringUtils.isEmpty(authorityIds))
@@ -97,9 +91,8 @@ public class SysRoleControl {
if (StringUtils.isEmpty(roleId))
return JSONObjResult.toJSONObj("角色主键不能为空");
SysRole sysRole = sysRoleService.getById(roleId);
if (sysRole == null) {
if (sysRole == null)
return JSONObjResult.toJSONObj("该角色不存在");
}
String ids[] = authorityIds.split(",");
List<SysRoleAuthority> list = new ArrayList<>();
for (String id : ids) {