feat:优化角色

V0.5.x
荭琪枫 2023-05-31 23:14:12 +08:00
parent 871544b1b9
commit fa5e281317
6 changed files with 157 additions and 48 deletions

View File

@ -17,26 +17,30 @@
<module>iot-test-tool</module>
</modules>
<properties>
<codec.version>1.14</codec.version>
<java.version>11</java.version>
<easyexcel.version>3.2.1</easyexcel.version>
<io.version>2.11.0</io.version>
<graalvm.version>21.1.0</graalvm.version>
<vertx.version>4.2.2</vertx.version>
<hutool.version>5.8.18</hutool.version>
<jackson.version>2.13.1</jackson.version>
<spring-boot.version>2.7.11</spring-boot.version>
<revision>0.4.5-SNAPSHOT</revision>
<mapstruct-plus.version>1.2.1</mapstruct-plus.version>
<poi.version>5.2.3</poi.version>
<ip2region.version>2.7.0</ip2region.version>
<satoken.version>1.34.0</satoken.version>
<mapstruct-plus.version>1.2.1</mapstruct-plus.version>
<lang3.version>3.7</lang3.version>
<aws-java-sdk-s3.version>1.12.400</aws-java-sdk-s3.version>
<redisson.version>3.20.1</redisson.version>
<alibaba-ttl.version>2.14.2</alibaba-ttl.version>
<beanutils.version>1.9.3</beanutils.version>
<lock4j.version>2.2.4</lock4j.version>
<codec.version>1.14</codec.version>
<java.version>11</java.version>
<jap-comment.version>1.0.0</jap-comment.version>
<validateion.version>1.1.0.Final</validateion.version>
<io.version>2.11.0</io.version>
<knife4j.version>2.0.5</knife4j.version>
<revision>0.4.5-SNAPSHOT</revision>
<ip2region.version>2.7.0</ip2region.version>
<satoken.version>1.34.0</satoken.version>
<redisson.version>3.20.1</redisson.version>
<beanutils.version>1.9.3</beanutils.version>
<velocity.version>2.3</velocity.version>
</properties>
<dependencyManagement>
<dependencies>
@ -120,8 +124,8 @@
<version>${satoken.version}</version>
<exclusions>
<exclusion>
<artifactId>hutool-all</artifactId>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</exclusion>
</exclusions>
</dependency>
@ -262,8 +266,8 @@
<version>${easyexcel.version}</version>
<exclusions>
<exclusion>
<artifactId>poi-ooxml-schemas</artifactId>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
@ -422,6 +426,26 @@
<artifactId>iot-virtual-device</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<dependency>
<groupId>com.github.biyanwen</groupId>
<artifactId>jpa-comment-spring-boot-starter</artifactId>
<version>${jap-comment.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validateion.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>

View File

@ -69,6 +69,10 @@
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -26,8 +26,8 @@
<version>3.0.0</version>
<exclusions>
<exclusion>
<artifactId>swagger-models</artifactId>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>

View File

@ -2,7 +2,7 @@ package cc.iotkit.system.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.system.dto.SysUserRole;
import cc.iotkit.model.system.SysUserRole;
import cc.iotkit.system.dto.bo.SysRoleBo;
import cc.iotkit.system.dto.vo.SysRoleVo;
@ -110,7 +110,7 @@ public interface ISysRoleService {
* @param bo
* @return
*/
void insertRole(SysRoleBo bo);
int insertRole(SysRoleBo bo);
/**
*
@ -135,7 +135,7 @@ public interface ISysRoleService {
* @param bo
* @return
*/
void authDataScope(SysRoleBo bo);
int authDataScope(SysRoleBo bo);
/**
* ID
@ -143,7 +143,7 @@ public interface ISysRoleService {
* @param roleId ID
* @return
*/
void deleteRoleById(Long roleId);
int deleteRoleById(Long roleId);
/**
*
@ -151,7 +151,7 @@ public interface ISysRoleService {
* @param roleIds ID
* @return
*/
void deleteRoleByIds(Long[] roleIds);
int deleteRoleByIds(Long[] roleIds);
/**
*
@ -159,7 +159,7 @@ public interface ISysRoleService {
* @param userRole
* @return
*/
void deleteAuthUser(SysUserRole userRole);
int deleteAuthUser(SysUserRole userRole);
/**
*
@ -168,7 +168,7 @@ public interface ISysRoleService {
* @param userIds ID
* @return
*/
void deleteAuthUsers(Long roleId, Long[] userIds);
int deleteAuthUsers(Long roleId, Long[] userIds);
/**
*
@ -177,7 +177,7 @@ public interface ISysRoleService {
* @param userIds ID
* @return
*/
void insertAuthUsers(Long roleId, Long[] userIds);
int insertAuthUsers(Long roleId, Long[] userIds);
void cleanOnlineUserByRole(Long roleId);
}

View File

@ -4,28 +4,31 @@ import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.undefined.LoginUser;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.common.utils.StreamUtils;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.system.ISysRoleData;
import cc.iotkit.data.system.ISysRoleDeptData;
import cc.iotkit.data.system.ISysRoleMenuData;
import cc.iotkit.data.system.ISysUserRoleData;
import cc.iotkit.model.system.SysRole;
import cc.iotkit.system.dto.SysRoleDept;
import cc.iotkit.system.dto.SysUserRole;
import cc.iotkit.model.system.SysRoleDept;
import cc.iotkit.model.system.SysRoleMenu;
import cc.iotkit.model.system.SysUserRole;
import cc.iotkit.system.dto.bo.SysRoleBo;
import cc.iotkit.system.dto.vo.SysRoleVo;
import cc.iotkit.system.service.ISysRoleService;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.hibernate.service.spi.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
*
@ -37,6 +40,7 @@ import java.util.Set;
public class SysRoleServiceImpl implements ISysRoleService {
private final ISysRoleData iSysRoleData;
private final ISysRoleMenuData iSysRoleMenuData;
private final ISysUserRoleData iSysUserRoleData;
private final ISysRoleDeptData iSysRoleDeptData;
@ -207,10 +211,10 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void insertRole(SysRoleBo bo) {
public int insertRole(SysRoleBo bo) {
SysRole role = iSysRoleData.save(bo.to(SysRole.class));
bo.setRoleId(role.getId());
insertRoleMenu(bo);
return insertRoleMenu(bo);
}
/**
@ -222,11 +226,11 @@ public class SysRoleServiceImpl implements ISysRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public int updateRole(SysRoleBo bo) {
SysRole role = iSysRoleData.save(bo.to(SysRole.class));
if (ObjectUtil.isNull(role)) {
return 0;
}
return 1;
// 修改角色信息
iSysRoleData.updateById(bo.to(SysRole.class));
// 删除角色与菜单关联
iSysRoleMenuData.deleteByRoleId(List.of(bo.getRoleId()));
return insertRoleMenu(bo);
}
/**
@ -253,13 +257,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void authDataScope(SysRoleBo bo) {
public int authDataScope(SysRoleBo bo) {
// 修改角色信息
iSysRoleData.updateById(MapstructUtils.convert(bo, SysRole.class));
// 删除角色与部门关联
iSysRoleDeptData.delete(bo.getRoleId());
iSysRoleDeptData.deleteByRoleId(List.of(bo.getRoleId()));
// 新增角色和部门信息(数据权限)
insertRoleDept(bo);
return insertRoleDept(bo);
}
@ -268,7 +272,20 @@ public class SysRoleServiceImpl implements ISysRoleService {
*
* @param role
*/
private void insertRoleMenu(SysRoleBo role) {
private int insertRoleMenu(SysRoleBo role) {
long rows = 1;
// 新增用户与角色管理
List<SysRoleMenu> list = new ArrayList<>();
for (Long menuId : role.getMenuIds()) {
SysRoleMenu rm = new SysRoleMenu();
rm.setRoleId(role.getRoleId());
rm.setMenuId(menuId);
list.add(rm);
}
if (list.size() > 0) {
rows = iSysRoleMenuData.insertBatch(list);
}
return Integer.parseInt(rows + "");
}
/**
@ -276,10 +293,10 @@ public class SysRoleServiceImpl implements ISysRoleService {
*
* @param role
*/
private void insertRoleDept(SysRoleBo role) {
/* int rows = 1;
private int insertRoleDept(SysRoleBo role) {
long rows = 1;
// 新增角色与部门(数据权限)管理
List<SysRoleDept> list = new ArrayList();
List<SysRoleDept> list = new ArrayList<SysRoleDept>();
for (Long deptId : role.getDeptIds()) {
SysRoleDept rd = new SysRoleDept();
rd.setRoleId(role.getRoleId());
@ -287,9 +304,9 @@ public class SysRoleServiceImpl implements ISysRoleService {
list.add(rd);
}
if (list.size() > 0) {
rows = roleDeptMapper.insertBatch(list) ? list.size() : 0;
rows = iSysRoleDeptData.insertBatch(list);
}
return rows;*/
return Integer.parseInt(rows + "");
}
/**
@ -300,7 +317,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteRoleById(Long roleId) {
public int deleteRoleById(Long roleId) {
// 删除角色与菜单关联
iSysRoleMenuData.deleteByRoleId(List.of(roleId));
// 删除角色与部门关联
iSysRoleDeptData.deleteByRoleId(List.of(roleId));
long num = iSysRoleData.deleteById(roleId);
return Integer.parseInt(num + "");
}
/**
@ -311,7 +334,21 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteRoleByIds(Long[] roleIds) {
public int deleteRoleByIds(Long[] roleIds) {
for (Long roleId : roleIds) {
checkRoleAllowed(roleId);
checkRoleDataScope(roleId);
SysRole role = iSysRoleData.findById(roleId);
if (countUserRoleByRoleId(roleId) > 0) {
throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
}
}
List<Long> ids = Arrays.asList(roleIds);
// 删除角色与菜单关联
iSysRoleMenuData.deleteByRoleId(ids);
// 删除角色与部门关联
iSysRoleDeptData.deleteByRoleId(ids);
return iSysRoleData.deleteByIds(ids);
}
/**
@ -321,7 +358,12 @@ public class SysRoleServiceImpl implements ISysRoleService {
* @return
*/
@Override
public void deleteAuthUser(SysUserRole userRole) {
public int deleteAuthUser(SysUserRole userRole) {
long rows = iSysUserRoleData.delete(userRole.getRoleId(), List.of(userRole.getUserId()));
if (rows > 0) {
cleanOnlineUserByRole(userRole.getRoleId());
}
return Integer.parseInt(rows + "");
}
/**
@ -332,7 +374,12 @@ public class SysRoleServiceImpl implements ISysRoleService {
* @return
*/
@Override
public void deleteAuthUsers(Long roleId, Long[] userIds) {
public int deleteAuthUsers(Long roleId, Long[] userIds) {
long rows = iSysUserRoleData.delete(roleId, Arrays.asList(userIds));
if (rows > 0) {
cleanOnlineUserByRole(roleId);
}
return Integer.parseInt(rows + "");
}
/**
@ -343,10 +390,44 @@ public class SysRoleServiceImpl implements ISysRoleService {
* @return
*/
@Override
public void insertAuthUsers(Long roleId, Long[] userIds) {
public int insertAuthUsers(Long roleId, Long[] userIds) {
// 新增用户与角色管理
long rows = 1;
List<SysUserRole> list = StreamUtils.toList(List.of(userIds), userId -> {
SysUserRole ur = new SysUserRole();
ur.setUserId(userId);
ur.setRoleId(roleId);
return ur;
});
if (CollUtil.isNotEmpty(list)) {
rows = iSysUserRoleData.insertBatch(list);
}
if (rows > 0) {
cleanOnlineUserByRole(roleId);
}
return Integer.parseInt(rows + "");
}
@Override
public void cleanOnlineUserByRole(Long roleId) {
List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);
if (CollUtil.isEmpty(keys)) {
return;
}
// 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作
keys.parallelStream().forEach(key -> {
String token = StringUtils.substringAfterLast(key, ":");
// 如果已经过期则跳过
if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) {
return;
}
LoginUser loginUser = LoginHelper.getLoginUser(token);
if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) {
try {
StpUtil.logoutByTokenValue(token);
} catch (NotLoginException ignored) {
}
}
});
}
}