feat: 删除角色功能
This commit is contained in:
@@ -45,4 +45,10 @@ public class RoleController {
|
|||||||
roleService.editRole(request);
|
roleService.editRole(request);
|
||||||
return ResponseEntity.ok(null);
|
return ResponseEntity.ok(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id:\\d+}")
|
||||||
|
public ResponseEntity<Void> deleteRole(@PathVariable Long id) {
|
||||||
|
roleService.deleteRole(id);
|
||||||
|
return ResponseEntity.ok(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.onixbyte.helix.manager;
|
||||||
|
|
||||||
|
import com.onixbyte.helix.repository.RoleAuthorityRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RoleAuthorityManager {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(RoleAuthorityManager.class);
|
||||||
|
private final RoleAuthorityRepository roleAuthorityRepository;
|
||||||
|
|
||||||
|
public RoleAuthorityManager(RoleAuthorityRepository roleAuthorityRepository) {
|
||||||
|
this.roleAuthorityRepository = roleAuthorityRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteByRoleId(Long roleId) {
|
||||||
|
var affectedRows = roleAuthorityRepository.deleteByRoleId(roleId);
|
||||||
|
log.info("角色 {} 关联的权限绑定已全部移除(共 {} 条)", roleId, affectedRows);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.onixbyte.helix.manager;
|
|||||||
import com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper;
|
import com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper;
|
||||||
import com.onixbyte.helix.domain.entity.Role;
|
import com.onixbyte.helix.domain.entity.Role;
|
||||||
import com.onixbyte.helix.exception.BizException;
|
import com.onixbyte.helix.exception.BizException;
|
||||||
|
import com.onixbyte.helix.mapper.RoleAuthorityMapper;
|
||||||
import com.onixbyte.helix.mapper.RoleMapper;
|
import com.onixbyte.helix.mapper.RoleMapper;
|
||||||
import com.onixbyte.helix.repository.RoleRepository;
|
import com.onixbyte.helix.repository.RoleRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -19,11 +20,13 @@ public class RoleManager {
|
|||||||
|
|
||||||
private final RoleMapper roleMapper;
|
private final RoleMapper roleMapper;
|
||||||
private final RoleRepository roleRepository;
|
private final RoleRepository roleRepository;
|
||||||
|
private final RoleAuthorityMapper roleAuthorityMapper;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public RoleManager(RoleMapper roleMapper, RoleRepository roleRepository) {
|
public RoleManager(RoleMapper roleMapper, RoleRepository roleRepository, RoleAuthorityMapper roleAuthorityMapper) {
|
||||||
this.roleMapper = roleMapper;
|
this.roleMapper = roleMapper;
|
||||||
this.roleRepository = roleRepository;
|
this.roleRepository = roleRepository;
|
||||||
|
this.roleAuthorityMapper = roleAuthorityMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validateRoles(List<Long> roleIds) {
|
public void validateRoles(List<Long> roleIds) {
|
||||||
@@ -70,4 +73,8 @@ public class RoleManager {
|
|||||||
Optional.ofNullable(role.getStatus())
|
Optional.ofNullable(role.getStatus())
|
||||||
.ifPresent(roleToUpdate::setStatus);
|
.ifPresent(roleToUpdate::setStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteRole(Long id) {
|
||||||
|
roleRepository.deleteById(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.onixbyte.helix.manager;
|
package com.onixbyte.helix.manager;
|
||||||
|
|
||||||
import com.onixbyte.helix.domain.entity.UserRole;
|
import com.onixbyte.helix.domain.entity.UserRole;
|
||||||
|
import com.onixbyte.helix.domain.entity.embeddable.UserRoleId;
|
||||||
import com.onixbyte.helix.mapper.UserRoleMapper;
|
import com.onixbyte.helix.mapper.UserRoleMapper;
|
||||||
import com.onixbyte.helix.repository.UserRoleRepository;
|
import com.onixbyte.helix.repository.UserRoleRepository;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -31,4 +32,9 @@ public class UserRoleManager {
|
|||||||
var affectedRows = userRoleRepository.deleteByUserId(userId);
|
var affectedRows = userRoleRepository.deleteByUserId(userId);
|
||||||
log.info("用户 {} 的角色关联被全部移除(共 {} 条)。", userId, affectedRows);
|
log.info("用户 {} 的角色关联被全部移除(共 {} 条)。", userId, affectedRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteByRoleId(Long roleId) {
|
||||||
|
var affectedRows = userRoleRepository.deleteByRoleId(roleId);
|
||||||
|
log.info("角色 {} 的用户关联被全部移除(共 {} 条)。", roleId, affectedRows);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.onixbyte.helix.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface RoleAuthorityMapper {
|
||||||
|
|
||||||
|
int deleteByRoleId(@Param("roleId") Long roleId);
|
||||||
|
}
|
||||||
@@ -3,8 +3,13 @@ package com.onixbyte.helix.repository;
|
|||||||
import com.onixbyte.helix.domain.entity.RoleAuthority;
|
import com.onixbyte.helix.domain.entity.RoleAuthority;
|
||||||
import com.onixbyte.helix.domain.entity.embeddable.RoleAuthorityId;
|
import com.onixbyte.helix.domain.entity.embeddable.RoleAuthorityId;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface RoleAuthorityRepository extends JpaRepository<RoleAuthority, RoleAuthorityId> {
|
public interface RoleAuthorityRepository extends JpaRepository<RoleAuthority, RoleAuthorityId> {
|
||||||
|
@Modifying
|
||||||
|
@Query("DELETE FROM RoleAuthority ra WHERE ra.id.roleId = :roleId")
|
||||||
|
int deleteByRoleId(Long roleId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,4 +13,8 @@ public interface UserRoleRepository extends JpaRepository<UserRole, UserRoleId>
|
|||||||
@Modifying
|
@Modifying
|
||||||
@Query("DELETE FROM UserRole ur WHERE ur.id.userId = :userId")
|
@Query("DELETE FROM UserRole ur WHERE ur.id.userId = :userId")
|
||||||
int deleteByUserId(Long userId);
|
int deleteByUserId(Long userId);
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Query("DELETE FROM UserRole ur WHERE ur.id.roleId = :roleId")
|
||||||
|
int deleteByRoleId(Long roleId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import com.onixbyte.helix.domain.entity.Role;
|
|||||||
import com.onixbyte.helix.domain.web.request.AddRoleRequest;
|
import com.onixbyte.helix.domain.web.request.AddRoleRequest;
|
||||||
import com.onixbyte.helix.domain.web.request.EditRoleRequest;
|
import com.onixbyte.helix.domain.web.request.EditRoleRequest;
|
||||||
import com.onixbyte.helix.domain.web.request.QueryRoleRequest;
|
import com.onixbyte.helix.domain.web.request.QueryRoleRequest;
|
||||||
|
import com.onixbyte.helix.manager.RoleAuthorityManager;
|
||||||
import com.onixbyte.helix.manager.RoleManager;
|
import com.onixbyte.helix.manager.RoleManager;
|
||||||
|
import com.onixbyte.helix.manager.UserRoleManager;
|
||||||
|
import com.onixbyte.helix.repository.UserRoleRepository;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -20,10 +23,18 @@ import java.util.Optional;
|
|||||||
public class RoleService {
|
public class RoleService {
|
||||||
|
|
||||||
private final RoleManager roleManager;
|
private final RoleManager roleManager;
|
||||||
|
private final RoleAuthorityManager roleAuthorityManager;
|
||||||
|
private final UserRoleManager userRoleManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public RoleService(RoleManager roleManager) {
|
public RoleService(
|
||||||
|
RoleManager roleManager,
|
||||||
|
RoleAuthorityManager roleAuthorityManager,
|
||||||
|
UserRoleManager userRoleManager
|
||||||
|
) {
|
||||||
this.roleManager = roleManager;
|
this.roleManager = roleManager;
|
||||||
|
this.roleAuthorityManager = roleAuthorityManager;
|
||||||
|
this.userRoleManager = userRoleManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<Role> getRoles(Pageable pageable, QueryRoleRequest request) {
|
public Page<Role> getRoles(Pageable pageable, QueryRoleRequest request) {
|
||||||
@@ -78,4 +89,11 @@ public class RoleService {
|
|||||||
.orElse(null))
|
.orElse(null))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteRole(Long id) {
|
||||||
|
roleAuthorityManager.deleteByRoleId(id);
|
||||||
|
userRoleManager.deleteByRoleId(id);
|
||||||
|
roleManager.deleteRole(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.onixbyte.helix.mapper.RoleAuthorityMapper">
|
||||||
|
<delete id="deleteByRoleId" parameterType="long">
|
||||||
|
DELETE
|
||||||
|
FROM role_authorities
|
||||||
|
WHERE role_id = #{roleId}
|
||||||
|
</delete>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user