From fa5e2813170299899b6b52c5acf5aefd98a4dec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=AD=E7=90=AA=E6=9E=AB?= <374696376@qq.com> Date: Wed, 31 May 2023 23:14:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=BC=98=E5=8C=96=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .flattened-pom.xml | 46 ++++-- iot-common/iot-common-core/.flattened-pom.xml | 4 + iot-common/iot-common-doc/.flattened-pom.xml | 2 +- iot-common/iot-common-log/.flattened-pom.xml | 2 +- .../system/service/ISysRoleService.java | 16 +-- .../service/impl/SysRoleServiceImpl.java | 135 ++++++++++++++---- 6 files changed, 157 insertions(+), 48 deletions(-) diff --git a/.flattened-pom.xml b/.flattened-pom.xml index 1b17a158..4860ea73 100644 --- a/.flattened-pom.xml +++ b/.flattened-pom.xml @@ -17,26 +17,30 @@ iot-test-tool - 1.14 - 11 3.2.1 - 2.11.0 21.1.0 4.2.2 5.8.18 2.13.1 2.7.11 - 0.4.5-SNAPSHOT - 1.2.1 5.2.3 - 2.7.0 - 1.34.0 + 1.2.1 3.7 1.12.400 - 3.20.1 2.14.2 - 1.9.3 2.2.4 + 1.14 + 11 + 1.0.0 + 1.1.0.Final + 2.11.0 + 2.0.5 + 0.4.5-SNAPSHOT + 2.7.0 + 1.34.0 + 3.20.1 + 1.9.3 + 2.3 @@ -120,8 +124,8 @@ ${satoken.version} - hutool-all cn.hutool + hutool-all @@ -262,8 +266,8 @@ ${easyexcel.version} - poi-ooxml-schemas org.apache.poi + poi-ooxml-schemas @@ -422,6 +426,26 @@ iot-virtual-device ${project.version} + + org.apache.velocity + velocity-engine-core + ${velocity.version} + + + com.github.biyanwen + jpa-comment-spring-boot-starter + ${jap-comment.version} + + + com.github.xiaoymin + knife4j-spring-boot-starter + ${knife4j.version} + + + javax.validation + validation-api + ${validateion.version} + diff --git a/iot-common/iot-common-core/.flattened-pom.xml b/iot-common/iot-common-core/.flattened-pom.xml index 7287395e..0b6e1ceb 100644 --- a/iot-common/iot-common-core/.flattened-pom.xml +++ b/iot-common/iot-common-core/.flattened-pom.xml @@ -69,6 +69,10 @@ jakarta.validation jakarta.validation-api + + javax.validation + validation-api + org.slf4j slf4j-api diff --git a/iot-common/iot-common-doc/.flattened-pom.xml b/iot-common/iot-common-doc/.flattened-pom.xml index 646313df..90e2602e 100644 --- a/iot-common/iot-common-doc/.flattened-pom.xml +++ b/iot-common/iot-common-doc/.flattened-pom.xml @@ -26,8 +26,8 @@ 3.0.0 - swagger-models io.swagger + swagger-models diff --git a/iot-common/iot-common-log/.flattened-pom.xml b/iot-common/iot-common-log/.flattened-pom.xml index ad2df9a8..4efdd680 100644 --- a/iot-common/iot-common-log/.flattened-pom.xml +++ b/iot-common/iot-common-log/.flattened-pom.xml @@ -1,5 +1,5 @@ - 4.0.0 diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysRoleService.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysRoleService.java index b312b9c2..93e98162 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysRoleService.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/ISysRoleService.java @@ -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); } diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysRoleServiceImpl.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysRoleServiceImpl.java index fee6b42d..1bc0cab3 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysRoleServiceImpl.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysRoleServiceImpl.java @@ -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 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 list = new ArrayList(); + List list = new ArrayList(); 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 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 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 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) { + } + } + }); } }