From cbf6486631b819a2779b8849a827f8cd9105c8a3 Mon Sep 17 00:00:00 2001 From: xiangwb Date: Wed, 15 Nov 2017 13:46:40 +0800 Subject: [PATCH] merge --- .../repository/SysAuthorityRepository.java | 2 + .../xwbing/service/SysAuthorityService.java | 57 ++++++----- .../service/SysRoleAuthorityService.java | 14 +++ .../com/xwbing/service/SysRoleService.java | 8 ++ .../xwbing/service/SysUserRoleService.java | 14 +++ .../com/xwbing/service/SysUserService.java | 10 +- .../controller/SysAuthorityControl.java | 98 +++++++++++++++++++ .../com/xwbing/controller/SysRoleControl.java | 23 ++--- 8 files changed, 185 insertions(+), 41 deletions(-) create mode 100644 boot-web/src/main/java/com/xwbing/controller/SysAuthorityControl.java diff --git a/boot-service/src/main/java/com/xwbing/repository/SysAuthorityRepository.java b/boot-service/src/main/java/com/xwbing/repository/SysAuthorityRepository.java index 7e81e88..deb26f9 100644 --- a/boot-service/src/main/java/com/xwbing/repository/SysAuthorityRepository.java +++ b/boot-service/src/main/java/com/xwbing/repository/SysAuthorityRepository.java @@ -18,6 +18,8 @@ public interface SysAuthorityRepository extends JpaRepository getByParentIdAndEnable(String parentId, String enable); + List getByParentId(String parentId); + List getByEnableAndIdIn(String enable, List ids); } diff --git a/boot-service/src/main/java/com/xwbing/service/SysAuthorityService.java b/boot-service/src/main/java/com/xwbing/service/SysAuthorityService.java index 9827c56..bda2645 100644 --- a/boot-service/src/main/java/com/xwbing/service/SysAuthorityService.java +++ b/boot-service/src/main/java/com/xwbing/service/SysAuthorityService.java @@ -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 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 queryAllChildren(String parentId) { - //获取结果 - List sysAuthoritys = listByParentEnable(parentId, CommonConstant.ISENABLE); - //遍历子集 - List 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 queryAllChildren(String parentId, String enable) { List list = new ArrayList<>(); - List authoritys = sysAuthorityRepository.getByParentIdAndEnable(parentId, enable); + List 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 queryAllChildren(String parentId) { + //获取结果 + List sysAuthoritys = listByParentEnable(parentId, CommonConstant.ISENABLE); + //遍历子集 + List 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唯一 * diff --git a/boot-service/src/main/java/com/xwbing/service/SysRoleAuthorityService.java b/boot-service/src/main/java/com/xwbing/service/SysRoleAuthorityService.java index 1b7fdc7..3461e0b 100644 --- a/boot-service/src/main/java/com/xwbing/service/SysRoleAuthorityService.java +++ b/boot-service/src/main/java/com/xwbing/service/SysRoleAuthorityService.java @@ -62,4 +62,18 @@ public class SysRoleAuthorityService { public List listByRoleId(String roleId) { return sysRoleAuthorityRepository.getByRoleId(roleId); } + + /** + * 批量删除 + * + * @param roleAuthorities + * @return + */ + public RestMessage removeBatch(List roleAuthorities) { + RestMessage result = new RestMessage(); + sysRoleAuthorityRepository.deleteInBatch(roleAuthorities); + result.setSuccess(true); + result.setMessage("批量删除成功"); + return result; + } } diff --git a/boot-service/src/main/java/com/xwbing/service/SysRoleService.java b/boot-service/src/main/java/com/xwbing/service/SysRoleService.java index d68a127..f699d0e 100644 --- a/boot-service/src/main/java/com/xwbing/service/SysRoleService.java +++ b/boot-service/src/main/java/com/xwbing/service/SysRoleService.java @@ -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 roleAuthorities = sysRoleAuthorityService.listByRoleId(id); + if (CollectionUtils.isNotEmpty(roleAuthorities)) + sysRoleAuthorityService.removeBatch(roleAuthorities); result.setMessage("删除成功"); result.setSuccess(true); return result; diff --git a/boot-service/src/main/java/com/xwbing/service/SysUserRoleService.java b/boot-service/src/main/java/com/xwbing/service/SysUserRoleService.java index 30b2f69..8d00274 100644 --- a/boot-service/src/main/java/com/xwbing/service/SysUserRoleService.java +++ b/boot-service/src/main/java/com/xwbing/service/SysUserRoleService.java @@ -61,4 +61,18 @@ public class SysUserRoleService { public List listByUserId(String userId) { return sysUserRoleRepository.getByUserId(userId); } + + /** + * 批量删除 + * + * @param userRoles + * @return + */ + public RestMessage removeBatch(List userRoles) { + RestMessage result = new RestMessage(); + sysUserRoleRepository.deleteInBatch(userRoles); + result.setSuccess(true); + result.setMessage("批量删除成功"); + return result; + } } diff --git a/boot-service/src/main/java/com/xwbing/service/SysUserService.java b/boot-service/src/main/java/com/xwbing/service/SysUserService.java index c9fbd05..6c0c2bc 100644 --- a/boot-service/src/main/java/com/xwbing/service/SysUserService.java +++ b/boot-service/src/main/java/com/xwbing/service/SysUserService.java @@ -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 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 sysRoles = sysRoleService.listByUserIdEnable(userId, enable); if (CollectionUtils.isEmpty(sysRoles)) return list; - List temp; //遍获取每个角色拥有的权限,并去重 + List temp; for (SysRole sysRole : sysRoles) { temp = sysAuthorityService.listByRoleIdEnable(sysRole.getId(), enable); if (CollectionUtils.isNotEmpty(temp)) { diff --git a/boot-web/src/main/java/com/xwbing/controller/SysAuthorityControl.java b/boot-web/src/main/java/com/xwbing/controller/SysAuthorityControl.java new file mode 100644 index 0000000..ae33bd4 --- /dev/null +++ b/boot-web/src/main/java/com/xwbing/controller/SysAuthorityControl.java @@ -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 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 authoritys = sysAuthorityService.listByEnable(enable); + return JSONObjResult.toJSONObj(authoritys, true, ""); + } + + @LogInfo("递归查询所有权限") + @PostMapping("queryTree") + public JSONObject queryTree(String enable) { + List authoritys = sysAuthorityService.queryAllChildren(CommonConstant.ROOT, enable); + return JSONObjResult.toJSONObj(authoritys, true, ""); + } +} diff --git a/boot-web/src/main/java/com/xwbing/controller/SysRoleControl.java b/boot-web/src/main/java/com/xwbing/controller/SysRoleControl.java index 4d7f9cb..53edd0c 100644 --- a/boot-web/src/main/java/com/xwbing/controller/SysRoleControl.java +++ b/boot-web/src/main/java/com/xwbing/controller/SysRoleControl.java @@ -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 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 list = new ArrayList<>(); for (String id : ids) {