mirror of https://github.com/elunez/eladmin
优化带关联项的删除:角色、权限、菜单、部门
parent
3595f182d1
commit
2b1af497f5
|
@ -1,5 +1,8 @@
|
|||
package me.zhengjie.utils;
|
||||
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
|
@ -25,4 +28,15 @@ public class ThrowableUtil {
|
|||
pw.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void throwForeignKeyException(Throwable e, String msg){
|
||||
Throwable t = e.getCause();
|
||||
while ((t != null) && !(t instanceof ConstraintViolationException)) {
|
||||
t = t.getCause();
|
||||
}
|
||||
if (t instanceof ConstraintViolationException) {
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
throw new BadRequestException("删除失败:" + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package me.zhengjie.modules.system.repository;
|
||||
|
||||
import me.zhengjie.modules.system.domain.Permission;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
|
|
@ -3,6 +3,8 @@ package me.zhengjie.modules.system.repository;
|
|||
import me.zhengjie.modules.system.domain.Role;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -21,5 +23,11 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
|
|||
|
||||
Set<Role> findByUsers_Id(Long id);
|
||||
|
||||
Set<Role> findByMenus_Id(Long id);
|
||||
@Modifying
|
||||
@Query(value = "delete from roles_permissions where permission_id = ?1",nativeQuery = true)
|
||||
void untiedPermission(Long id);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "delete from roles_menus where menu_id = ?1",nativeQuery = true)
|
||||
void untiedMenu(Long id);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import me.zhengjie.modules.system.domain.Dept;
|
|||
import me.zhengjie.modules.system.service.DeptService;
|
||||
import me.zhengjie.modules.system.service.dto.DeptDTO;
|
||||
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria;
|
||||
import me.zhengjie.utils.ThrowableUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -63,7 +64,11 @@ public class DeptController {
|
|||
@DeleteMapping(value = "/dept/{id}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','DEPT_ALL','DEPT_DELETE')")
|
||||
public ResponseEntity delete(@PathVariable Long id){
|
||||
deptService.delete(id);
|
||||
try {
|
||||
deptService.delete(id);
|
||||
}catch (Throwable e){
|
||||
ThrowableUtil.throwForeignKeyException(e, "该部门存在岗位或者角色关联,请取消关联后再试");
|
||||
}
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import me.zhengjie.exception.BadRequestException;
|
|||
import me.zhengjie.modules.system.service.PermissionService;
|
||||
import me.zhengjie.modules.system.service.dto.PermissionDTO;
|
||||
import me.zhengjie.modules.system.service.dto.PermissionQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.mapper.PermissionMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -13,7 +14,9 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
@ -26,6 +29,9 @@ public class PermissionController {
|
|||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
@Autowired
|
||||
private PermissionMapper permissionMapper;
|
||||
|
||||
private static final String ENTITY_NAME = "permission";
|
||||
|
||||
/**
|
||||
|
@ -68,7 +74,11 @@ public class PermissionController {
|
|||
@DeleteMapping(value = "/permissions/{id}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','PERMISSION_ALL','PERMISSION_DELETE')")
|
||||
public ResponseEntity delete(@PathVariable Long id){
|
||||
permissionService.delete(id);
|
||||
List<Permission> permissions = permissionService.findByPid(id);
|
||||
Set<Permission> permissionSet = new HashSet<>();
|
||||
permissionSet.add(permissionMapper.toEntity(permissionService.findById(id)));
|
||||
permissionSet = permissionService.getDeletePermission(permissions, permissionSet);
|
||||
permissionService.delete(permissionSet);
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import me.zhengjie.modules.system.service.RoleService;
|
|||
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
|
||||
import me.zhengjie.utils.SecurityUtils;
|
||||
import me.zhengjie.utils.ThrowableUtil;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
@ -15,6 +17,7 @@ import org.springframework.data.web.PageableDefault;
|
|||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.transaction.TransactionSystemException;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.Collections;
|
||||
|
@ -106,7 +109,11 @@ public class RoleController {
|
|||
@DeleteMapping(value = "/roles/{id}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','ROLES_ALL','ROLES_DELETE')")
|
||||
public ResponseEntity delete(@PathVariable Long id){
|
||||
roleService.delete(id);
|
||||
try {
|
||||
roleService.delete(id);
|
||||
}catch (Throwable e){
|
||||
ThrowableUtil.throwForeignKeyException(e, "该角色存在用户关联,请取消关联后再试");
|
||||
}
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.springframework.cache.annotation.CacheEvict;
|
|||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
@ -41,10 +42,10 @@ public interface PermissionService {
|
|||
|
||||
/**
|
||||
* delete
|
||||
* @param id
|
||||
* @param permissions
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Long id);
|
||||
void delete(Set<Permission> permissions);
|
||||
|
||||
/**
|
||||
* permission tree
|
||||
|
@ -76,4 +77,6 @@ public interface PermissionService {
|
|||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
List<PermissionDTO> queryAll(PermissionQueryCriteria criteria);
|
||||
|
||||
Set<Permission> getDeletePermission(List<Permission> permissions, Set<Permission> permissionSet);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package me.zhengjie.modules.system.service;
|
||||
|
||||
import me.zhengjie.modules.system.domain.Menu;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.modules.system.service.dto.RoleDTO;
|
||||
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
|
||||
|
@ -10,7 +9,6 @@ import org.springframework.cache.annotation.CacheEvict;
|
|||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -80,7 +78,7 @@ public interface RoleService {
|
|||
void updateMenu(Role resources, RoleDTO roleDTO);
|
||||
|
||||
@CacheEvict(allEntries = true)
|
||||
void untiedMenu(Menu menu);
|
||||
void untiedMenu(Long id);
|
||||
|
||||
/**
|
||||
* queryAll
|
||||
|
@ -106,4 +104,7 @@ public interface RoleService {
|
|||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
List<RoleDTO> queryAll(RoleQueryCriteria criteria);
|
||||
|
||||
@CacheEvict(allEntries = true)
|
||||
void untiedPermission(Long id);
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ public class MenuServiceImpl implements MenuService {
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Set<Menu> menuSet) {
|
||||
for (Menu menu : menuSet) {
|
||||
roleService.untiedMenu(menu);
|
||||
roleService.untiedMenu(menu.getId());
|
||||
menuRepository.deleteById(menu.getId());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import me.zhengjie.exception.BadRequestException;
|
|||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.modules.system.repository.PermissionRepository;
|
||||
import me.zhengjie.modules.system.service.PermissionService;
|
||||
import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.dto.PermissionDTO;
|
||||
import me.zhengjie.modules.system.service.dto.PermissionQueryCriteria;
|
||||
import me.zhengjie.modules.system.service.mapper.PermissionMapper;
|
||||
|
@ -30,6 +31,9 @@ public class PermissionServiceImpl implements PermissionService {
|
|||
@Autowired
|
||||
private PermissionMapper permissionMapper;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Override
|
||||
public List<PermissionDTO> queryAll(PermissionQueryCriteria criteria) {
|
||||
return permissionMapper.toDto(permissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
||||
|
@ -74,14 +78,26 @@ public class PermissionServiceImpl implements PermissionService {
|
|||
permissionRepository.save(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Permission> getDeletePermission(List<Permission> permissions, Set<Permission> permissionSet) {
|
||||
// 递归找出待删除的菜单
|
||||
for (Permission permission : permissions) {
|
||||
permissionSet.add(permission);
|
||||
List<Permission> permissionList = permissionRepository.findByPid(permission.getId());
|
||||
if(permissionList!=null && permissionList.size()!=0){
|
||||
getDeletePermission(permissionList, permissionSet);
|
||||
}
|
||||
}
|
||||
return permissionSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long id) {
|
||||
List<Permission> permissionList = permissionRepository.findByPid(id);
|
||||
for (Permission permission : permissionList) {
|
||||
public void delete(Set<Permission> permissions) {
|
||||
for (Permission permission : permissions) {
|
||||
roleService.untiedPermission(permission.getId());
|
||||
permissionRepository.delete(permission);
|
||||
}
|
||||
permissionRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package me.zhengjie.modules.system.service.impl;
|
||||
|
||||
import me.zhengjie.modules.system.domain.Menu;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.modules.system.repository.RoleRepository;
|
||||
|
@ -10,7 +9,6 @@ import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
|
|||
import me.zhengjie.modules.system.service.dto.RoleSmallDTO;
|
||||
import me.zhengjie.modules.system.service.mapper.RoleMapper;
|
||||
import me.zhengjie.modules.system.service.mapper.RoleSmallMapper;
|
||||
import me.zhengjie.utils.FileUtil;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import me.zhengjie.utils.QueryHelp;
|
||||
import me.zhengjie.utils.ValidationUtil;
|
||||
|
@ -20,8 +18,6 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -112,13 +108,15 @@ public class RoleServiceImpl implements RoleService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void untiedMenu(Menu menu) {
|
||||
Set<Role> roles = roleRepository.findByMenus_Id(menu.getId());
|
||||
for (Role role : roles) {
|
||||
menu.getRoles().remove(role);
|
||||
role.getMenus().remove(menu);
|
||||
roleRepository.save(role);
|
||||
}
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void untiedMenu(Long id) {
|
||||
roleRepository.untiedMenu(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void untiedPermission(Long id) {
|
||||
roleRepository.untiedPermission(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue