Browse Source

启用和禁用角色时,添加业务逻辑的判断和拦截

v_HuilingDeng 4 years ago
parent
commit
a201189696

+ 9 - 0
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/dao/mapper/UserInfoMapper.java

@@ -8,6 +8,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 /**
  * <p>
  * 用户信息表 Mapper 接口
@@ -25,4 +27,11 @@ public interface UserInfoMapper extends BaseMapper<UserInfo> {
      * @return
      */
     int batchEnableUser(BatchEnableUserReq req);
+
+    /**
+     * 根据rolecode查询该角色下启用中的用户
+     * @param roleCode
+     * @return
+     */
+    List<UserInfo> selectUserInfoByRoleCodeEnable(@Param("roleCode") String roleCode);
 }

+ 12 - 3
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/dao/mapper/UserRoleMapper.java

@@ -42,11 +42,20 @@ public interface UserRoleMapper extends BaseMapper<UserRole> {
      */
     Long countList(@Param("roleCode")String roleCode);
 
+    /**
+     * 查询禁用的上级角色
+     * @param roleCode
+     * @return
+     */
+    List<UserRole> superiorRoleDisable(@Param("roleCode")String roleCode);
 
 
-
-
-
+    /**
+     * 查询启用中的下级角色
+     * @param roleCode
+     * @return
+     */
+    List<UserRole> subordinateRoleEnable(@Param("roleCode")String roleCode, @Param("roleLevel")Integer roleLevel);
 
 
 }

+ 24 - 6
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/RoleManagerServiceImpl.java

@@ -1,10 +1,10 @@
 package com.abi.qms.platform.service.impl;
 
-import cn.hutool.core.collection.CollectionUtil;
 import com.abi.qms.platform.dao.entity.UserInfo;
 import com.abi.qms.platform.dao.entity.UserResource;
 import com.abi.qms.platform.dao.entity.UserRole;
 import com.abi.qms.platform.dao.enums.RoleLevelEnum;
+import com.abi.qms.platform.dao.mapper.UserInfoMapper;
 import com.abi.qms.platform.dao.mapper.UserMappingRoleResourceMapper;
 import com.abi.qms.platform.dao.mapper.UserResourceMapper;
 import com.abi.qms.platform.dao.mapper.UserRoleMapper;
@@ -24,11 +24,9 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * <p>
@@ -52,6 +50,9 @@ public class RoleManagerServiceImpl implements RoleManagerService {
     @Autowired
     private UserResourceService userResourceService;
 
+    @Autowired
+    private UserInfoMapper userInfoMapper;
+
     /**
      * 创建角色
      **/
@@ -364,6 +365,7 @@ public class RoleManagerServiceImpl implements RoleManagerService {
      * 启用角色
      **/
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void enableRole(EnableRoleReq req) {
         List<String> roleCodeList = req.getRoleCodeList();
 
@@ -375,7 +377,11 @@ public class RoleManagerServiceImpl implements RoleManagerService {
             if(role.getIsEnable()==1){
                 continue;
             }
-
+            //查询禁用的上级角色
+            List<UserRole> userRoles = userRoleMapper.superiorRoleDisable(roleCode);
+            if(Objects.nonNull(userRoles) && userRoles.size() > 0){
+                throw new BusinessException("角色" + role.getRoleName() + "的上级角色为停用状态,无法启用。");
+            }
             //2-修改状态
             role.setIsEnable(1);
             userRoleMapper.updateById(role);
@@ -387,6 +393,7 @@ public class RoleManagerServiceImpl implements RoleManagerService {
      * 禁用角色
      **/
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void disableRole(DisableRoleReq req) {
         List<String> roleCodeList = req.getRoleCodeList();
 
@@ -398,6 +405,17 @@ public class RoleManagerServiceImpl implements RoleManagerService {
             if(role.getIsEnable()==0){
                 continue;
             }
+            //查询该角色下启用中的用户
+            List<UserInfo> userInfos = userInfoMapper.selectUserInfoByRoleCodeEnable(roleCode);
+            if(Objects.nonNull(userInfos) && userInfos.size() > 0){
+                throw new BusinessException("角色" + role.getRoleName() + "已与" + userInfos.size() + "名用户绑定,请解绑后再停用。");
+            }
+
+            //查询启用中的下级角色
+            List<UserRole> userRoles = userRoleMapper.subordinateRoleEnable(role.getRoleCode(), role.getRoleLevel());
+            if(Objects.nonNull(userRoles) && userRoles.size() > 0){
+                throw new BusinessException("角色" + role.getRoleName() + "存在未停用子角色" + userRoles.get(0).getRoleName() + ",请停用子角色后再试。");
+            }
 
             //2-修改状态
             role.setIsEnable(0);

+ 8 - 0
abi-cloud-qr-platform-server/src/main/resources/dao/mapper/UserInfoMapper.xml

@@ -69,4 +69,12 @@
         </foreach>
         and is_delete = 0
     </update>
+
+    <!--根据rolecode查询该角色下启用中的用户-->
+    <select id="selectUserInfoByRoleCodeEnable" resultMap="BaseResultMap">
+        SELECT
+        <include refid="Base_Column_List"/>
+        from user_info
+        WHERE role_code = #{roleCode} AND is_enable = 1 and is_delete = 0
+    </select>
 </mapper>

+ 53 - 0
abi-cloud-qr-platform-server/src/main/resources/dao/mapper/UserRoleMapper.xml

@@ -89,9 +89,62 @@
             and is_delete=0
     </select>
 
+    <!--查询禁用的上级角色-->
+    <select id="superiorRoleDisable" resultMap="BaseResultMap">
+        select <include refid="Base_Column_List"/>
+        from user_role
+        where role_code in (
+            select l1_role_code
+            from user_role
+            where role_code=#{roleCode}
 
+            union all
+            select l2_role_code
+            from user_role
+            where role_code=#{roleCode}
 
+            union all
+            select l3_role_code
+            from user_role
+            where role_code=#{roleCode}
 
+            union all
+            select l4_role_code
+            from user_role
+            where role_code=#{roleCode}
 
+            union all
+            select l5_role_code
+            from user_role
+            where role_code=#{roleCode}
+        )
+        and role_code != #{roleCode}
+        and is_enable=1 and is_delete=0
+    </select>
+
+    <!--查询启用中的下级角色-->
+    <select id="subordinateRoleEnable" resultMap="BaseResultMap">
+        select <include refid="Base_Column_List"/>
+        from user_role
+        where
+            <!-- 仅可以查看当前角色树下的角色 -->
+            <if test="roleLevel==1">
+                and l1_role_code=#{roleCode}
+            </if>
+            <if test="roleLevel==2">
+                and l2_role_code=#{roleCode}
+            </if>
+            <if test="roleLevel==3">
+                and l3_role_code=#{roleCode}
+            </if>
+            <if test="roleLevel==4">
+                and l4_role_code=#{roleCode}
+            </if>
+            <if test="roleLevel==5">
+                and l5_role_code=#{roleCode}
+            </if>
+        and role_code != #{roleCode}
+        and is_enable=0 and is_delete=0
+    </select>
 
 </mapper>