浏览代码

修复租户切换导致报错

fuyuchuan 1 周之前
父节点
当前提交
2fccc22339

+ 2 - 1
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/SysUserController.java

@@ -118,6 +118,7 @@ public class SysUserController extends BaseController
     @GetMapping(value = { "/", "/{userId}" })
     public ApiResult<Map<String,Object>> getInfo(@PathVariable(value = "userId", required = false) Long userId)
     {
+        Integer tenantId = SecurityUtils.getTenantId();
         Map<String,Object> ajax = new HashMap<>();
         List<SysRole> roles = roleService.selectRoleAll();
         ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
@@ -126,7 +127,7 @@ public class SysUserController extends BaseController
         {
             ajax.put("data", userService.selectUserById(userId));
             ajax.put("postIds", postService.selectPostListByUserId(userId));
-            ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
+            ajax.put("roleIds", roleService.selectRoleListByUserId(userId, tenantId));
         }
         return ApiResult.success(ajax);
     }

+ 2 - 1
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/mapper/SysRoleMapper.java

@@ -45,7 +45,8 @@ public interface SysRoleMapper extends CrudMapper<SysRole>
      * @param userId 用户ID
      * @return 选中角色ID列表
      */
-    public List<Integer> selectRoleListByUserId(@Param("userId") Long userId);
+    public List<Integer> selectRoleListByUserId(@Param("userId") Long userId,
+                                                @Param("tenantId") Integer tenantId);
 
     /**
      * 通过角色ID查询角色

+ 4 - 4
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/mapper/SysUserRoleMapper.java

@@ -22,7 +22,7 @@ public interface SysUserRoleMapper extends CrudMapper<SysUserRole>
      * @param userId 用户ID
      * @return 结果
      */
-    public int deleteUserRoleByUserId(@Param("userId") Long userId);
+    public int deleteUserRoleByUserId(@Param("userId") Long userId, @Param("tenantId") Integer tenantId);
 
     /**
      * 批量删除用户和角色关联
@@ -30,7 +30,7 @@ public interface SysUserRoleMapper extends CrudMapper<SysUserRole>
      * @param ids 需要删除的数据ID
      * @return 结果
      */
-    public int deleteUserRole(@Param("ids") Long[] ids);
+    public int deleteUserRole(@Param("ids") Long[] ids, @Param("tenantId") Integer tenantId);
 
     /**
      * 通过角色ID查询角色使用数量
@@ -38,7 +38,7 @@ public interface SysUserRoleMapper extends CrudMapper<SysUserRole>
      * @param roleId 角色ID
      * @return 结果
      */
-    public int countUserRoleByRoleId(@Param("roleId") Long roleId);
+    public int countUserRoleByRoleId(@Param("roleId") Long roleId, @Param("tenantId") Integer tenantId);
 
     /**
      * 批量新增用户角色信息
@@ -63,5 +63,5 @@ public interface SysUserRoleMapper extends CrudMapper<SysUserRole>
      * @param userIds 需要删除的用户数据ID
      * @return 结果
      */
-    public int deleteUserRoleInfos(@Param("roleId") Long roleId, @Param("userIds") Long[] userIds);
+    public int deleteUserRoleInfos(@Param("roleId") Long roleId, @Param("userIds") Long[] userIds, @Param("tenantId") Integer tenantId);
 }

+ 1 - 1
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/ISysRoleService.java

@@ -52,7 +52,7 @@ public interface ISysRoleService extends CrudService<SysRole>
      * @param userId 用户ID
      * @return 选中角色ID列表
      */
-    public List<Integer> selectRoleListByUserId(Long userId);
+    public List<Integer> selectRoleListByUserId(Long userId, Integer tenantId);
 
     /**
      * 通过角色ID查询角色

+ 5 - 4
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysRoleServiceImpl.java

@@ -125,9 +125,9 @@ public class SysRoleServiceImpl extends AbstractCrudService<SysRoleMapper, SysRo
      * @return 选中角色ID列表
      */
     @Override
-    public List<Integer> selectRoleListByUserId(Long userId)
+    public List<Integer> selectRoleListByUserId(Long userId, Integer tenantId)
     {
-        return roleMapper.selectRoleListByUserId(userId);
+        return roleMapper.selectRoleListByUserId(userId,tenantId);
     }
 
     /**
@@ -201,7 +201,7 @@ public class SysRoleServiceImpl extends AbstractCrudService<SysRoleMapper, SysRo
     @Override
     public int countUserRoleByRoleId(Long roleId)
     {
-        return userRoleMapper.countUserRoleByRoleId(roleId);
+        return userRoleMapper.countUserRoleByRoleId(roleId,SecurityUtils.getTenantId());
     }
 
     /**
@@ -395,7 +395,8 @@ public class SysRoleServiceImpl extends AbstractCrudService<SysRoleMapper, SysRo
     @Override
     public int deleteAuthUsers(Long roleId, Long[] userIds)
     {
-        return userRoleMapper.deleteUserRoleInfos(roleId, userIds);
+        Integer tenantId = SecurityUtils.getTenantId();
+        return userRoleMapper.deleteUserRoleInfos(roleId, userIds, tenantId);
     }
 
     /**

+ 6 - 6
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysUserServiceImpl.java

@@ -406,7 +406,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
     public int updateUser(SysUser user) {
         Long userId = user.getUserId();
         // 删除用户与角色关联
-        userRoleMapper.deleteUserRoleByUserId(userId);
+        userRoleMapper.deleteUserRoleByUserId(userId,SecurityUtils.getTenantId());
         // 新增用户与角色管理
         insertUserRole(user);
         // 删除用户与岗位关联
@@ -437,7 +437,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
     @Override
     @Transactional
     public void insertUserAuth(Long userId, Long[] roleIds) {
-        userRoleMapper.deleteUserRoleByUserId(userId);
+        userRoleMapper.deleteUserRoleByUserId(userId, SecurityUtils.getTenantId());
         insertUserRole(userId, roleIds);
     }
 
@@ -512,7 +512,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
                 SysUserRole ur = new SysUserRole();
                 ur.setUserId(user.getUserId());
                 ur.setRoleId(roleId);
-                ur.setTenantId(user.getTenantId());
+                ur.setTenantId(SecurityUtils.getTenantId());
                 list.add(ur);
             }
             if (list.size() > 0) {
@@ -577,7 +577,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
     @Transactional
     public int deleteUserById(Long userId) {
         // 删除用户与角色关联
-        userRoleMapper.deleteUserRoleByUserId(userId);
+        userRoleMapper.deleteUserRoleByUserId(userId, SecurityUtils.getTenantId());
         // 删除用户与岗位表
         userPostMapper.deleteUserPostByUserId(userId);
         return userMapper.deleteUserById(userId);
@@ -596,7 +596,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
             checkUserAllowed(new SysUser(userId));
         }
         // 删除用户与角色关联
-        userRoleMapper.deleteUserRole(userIds);
+        userRoleMapper.deleteUserRole(userIds, SecurityUtils.getTenantId());
         // 删除用户与岗位关联
         userPostMapper.deleteUserPost(userIds);
         return userMapper.deleteUserByIds(userIds);
@@ -609,7 +609,7 @@ public class SysUserServiceImpl extends AbstractCrudService<SysUserMapper, SysUs
         SysUser user = this.selectUserById(userId);
         if (user.getPassword().equals(password)) {
             // 删除用户与角色关联
-            userRoleMapper.deleteUserRoleByUserId(userId);
+            userRoleMapper.deleteUserRoleByUserId(userId, SecurityUtils.getTenantId());
             // 删除用户与岗位表
             userPostMapper.deleteUserPostByUserId(userId);
             return userMapper.deleteUserById(userId);

+ 25 - 11
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysUserTenantServiceImpl.java

@@ -6,7 +6,9 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.common.core.exception.BusinessException;
 import com.usky.system.RemoteDeptService;
 import com.usky.system.domain.SysTenant;
+import com.usky.system.domain.SysUserRole;
 import com.usky.system.domain.SysUserTenant;
+import com.usky.system.mapper.SysUserRoleMapper;
 import com.usky.system.mapper.SysUserTenantMapper;
 import com.usky.system.service.SysTenantService;
 import com.usky.system.service.SysUserTenantService;
@@ -33,6 +35,9 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
     @Autowired
     private SysTenantService sysTenantService;
 
+    @Autowired
+    private SysUserRoleMapper sysUserRoleMapper;
+
     /**
      * 企业邀请用户
      *
@@ -42,9 +47,16 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
     @Transactional
     public void insertInviteUser(SysUserTenant sysUserTenant) {
         int userCount = baseMapper.checkUserIdUnique(sysUserTenant.getTenantId(), sysUserTenant.getUserId());
-        if (userCount > 0){
+        if (userCount > 0) {
             throw new BusinessException("用户已绑定,无法重复绑定!");
-        }else {
+        } else {
+            // SysUserRole sysUserRole = sysUserRoleMapper.selectOne(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, sysUserTenant.getUserId()).eq(SysUserRole::getTenantId, sysUserTenant.getTenantId()));
+            // if (sysUserRole == null) {
+            //     SysUserRole sysUserRole1 = new SysUserRole();
+            //     sysUserRole1.setUserId(sysUserTenant.getUserId());
+            //     sysUserRole1.setTenantId(sysUserTenant.getTenantId());
+            //     sysUserRoleMapper.insert(sysUserRole1);
+            // }
             this.save(sysUserTenant);
         }
     }
@@ -57,8 +69,10 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
      */
     @Override
     @Transactional
-    public void deleteUserTenant(Integer tenantId,Long userId) {
+    public void deleteUserTenant(Integer tenantId, Long userId) {
         baseMapper.deleteUserTenant(tenantId, userId);
+        Long[] userIds = new Long[]{userId};
+        sysUserRoleMapper.deleteUserRole(userIds, tenantId);
     }
 
     /**
@@ -66,19 +80,19 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
      */
     @Override
     @Transactional
-    public List<SysTenant> getTenantByUser(Long userId){
+    public List<SysTenant> getTenantByUser(Long userId) {
         LambdaQueryWrapper<SysUserTenant> queryWrapper = Wrappers.lambdaQuery();
-        queryWrapper.eq(SysUserTenant::getUserId,userId);
+        queryWrapper.eq(SysUserTenant::getUserId, userId);
         List<SysUserTenant> userTenants = this.list(queryWrapper);
         List<Integer> tenantIdList = new ArrayList<>();
         List<SysTenant> tenantList = new ArrayList<>();
         LambdaQueryWrapper<SysTenant> queryWrapper1 = Wrappers.lambdaQuery();
-        if (CollectionUtils.isNotEmpty(userTenants)){
+        if (CollectionUtils.isNotEmpty(userTenants)) {
             for (int i = 0; i < userTenants.size(); i++) {
                 tenantIdList.add(userTenants.get(i).getTenantId());
             }
-            queryWrapper1.in(SysTenant::getId,tenantIdList)
-                    .eq(SysTenant::getStatus,0);
+            queryWrapper1.in(SysTenant::getId, tenantIdList)
+                    .eq(SysTenant::getStatus, 0);
             tenantList = sysTenantService.list(queryWrapper1);
         }
         return tenantList;
@@ -86,10 +100,10 @@ public class SysUserTenantServiceImpl extends AbstractCrudService<SysUserTenantM
 
     @Override
     @Transactional
-    public Boolean getIdByUser(Integer tenantId,Long userId){
+    public Boolean getIdByUser(Integer tenantId, Long userId) {
         LambdaQueryWrapper<SysUserTenant> queryWrapper = Wrappers.lambdaQuery();
-        queryWrapper.eq(SysUserTenant::getUserId,userId)
-                .eq(SysUserTenant::getTenantId,tenantId);
+        queryWrapper.eq(SysUserTenant::getUserId, userId)
+                .eq(SysUserTenant::getTenantId, tenantId);
         List<SysUserTenant> userTenants = this.list(queryWrapper);
         return userTenants.get(0).getIsDefault();
     }

+ 7 - 7
base-modules/service-system/service-system-biz/src/main/resources/mapper/system/SysRoleMapper.xml

@@ -68,13 +68,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 	<select id="selectRoleAll" resultMap="SysRoleResult">
 		<include refid="selectRoleVo"/>
 	</select>
-	
-	<select id="selectRoleListByUserId" parameterType="Long" resultType="Integer">
-		select r.role_id
-        from sys_role r
-	        left join sys_user_role ur on ur.role_id = r.role_id
-	        left join sys_user u on u.user_id = ur.user_id
-	    where u.user_id = #{userId}
+
+	<select id="selectRoleListByUserId" parameterType="map" resultType="Integer">
+		SELECT r.role_id
+		FROM sys_role r
+				 LEFT JOIN sys_user_role ur ON ur.role_id = r.role_id
+				 LEFT JOIN sys_user u ON u.user_id = ur.user_id
+		WHERE u.user_id = #{userId, jdbcType=BIGINT} AND r.tenant_id = #{tenantId, jdbcType=INTEGER}
 	</select>
 	
 	<select id="selectRoleById" parameterType="Long" resultMap="SysRoleResult">

+ 52 - 40
base-modules/service-system/service-system-biz/src/main/resources/mapper/system/SysUserRoleMapper.xml

@@ -1,45 +1,57 @@
 <?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">
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.usky.system.mapper.SysUserRoleMapper">
 
-	<resultMap type="com.usky.system.domain.SysUserRole" id="SysUserRoleResult">
-		<result property="userId"     column="user_id"      />
-		<result property="roleId"     column="role_id"      />
-		<result property="tenantId"   column="tenant_id"    />
-	</resultMap>
-
-	<delete id="deleteUserRoleByUserId" parameterType="Long">
-		delete from sys_user_role where user_id=#{userId}
-	</delete>
-	
-	<select id="countUserRoleByRoleId" resultType="Integer">
-	    select count(1) from sys_user_role where role_id=#{roleId}  
-	</select>
-	
-	<delete id="deleteUserRole" parameterType="Long">
- 		delete from sys_user_role where user_id in
- 		<foreach collection="ids" item="ids" open="(" separator="," close=")">
- 			#{ids}
-        </foreach> 
- 	</delete>
-	
-	<insert id="batchUserRole">
-		insert into sys_user_role(user_id, role_id, tenant_id) values
-		<foreach item="item" index="index" collection="list" separator=",">
-			(#{item.userId},#{item.roleId},#{item.tenantId})
-		</foreach>
-	</insert>
-	
-	<delete id="deleteUserRoleInfo" parameterType="com.usky.system.domain.SysUserRole">
-		delete from sys_user_role where user_id=#{userId} and role_id=#{roleId}
-	</delete>
-	
-	<delete id="deleteUserRoleInfos">
-	    delete from sys_user_role where role_id=#{roleId} and user_id in
- 	    <foreach collection="userIds" item="userId" open="(" separator="," close=")">
- 	        #{userId}
-            </foreach> 
-	</delete>
+    <resultMap type="com.usky.system.domain.SysUserRole" id="SysUserRoleResult">
+        <result property="userId" column="user_id"/>
+        <result property="roleId" column="role_id"/>
+        <result property="tenantId" column="tenant_id"/>
+    </resultMap>
+
+    <delete id="deleteUserRoleByUserId" parameterType="map">
+        delete
+        from sys_user_role
+        where user_id = #{userId, jdbcType=BIGINT}
+          and tenant_id = #{tenantId, jdbcType=INTEGER}
+    </delete>
+
+    <select id="countUserRoleByRoleId" resultType="Integer">
+        select count(1)
+        from sys_user_role
+        where role_id = #{roleId, jdbcType=BIGINT}
+          and tenant_id = #{tenantId, jdbcType=INTEGER}
+    </select>
+
+    <delete id="deleteUserRole" parameterType="map">
+        delete from sys_user_role where user_id in
+        <foreach collection="ids" item="id" open="(" separator="," close=")">
+            #{id, jdbcType=BIGINT}
+        </foreach>
+        and tenant_id=#{tenantId, jdbcType=INTEGER}
+    </delete>
+
+    <insert id="batchUserRole">
+        insert into sys_user_role(user_id, role_id, tenant_id) values
+        <foreach item="item" index="index" collection="list" separator=",">
+            (#{item.userId, jdbcType=BIGINT}, #{item.roleId, jdbcType=INTEGER}, #{item.tenantId, jdbcType=INTEGER})
+        </foreach>
+    </insert>
+
+    <delete id="deleteUserRoleInfo" parameterType="map">
+        delete
+        from sys_user_role
+        where user_id = #{userId, jdbcType=BIGINT}
+          and role_id = #{roleId, jdbcType=INTEGER}
+    </delete>
+
+    <delete id="deleteUserRoleInfos" parameterType="map">
+        delete from sys_user_role where role_id=#{roleId, jdbcType=INTEGER} and user_id in
+        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
+            #{userId, jdbcType=BIGINT}
+        </foreach>
+        and tenant_id=#{tenantId, jdbcType=INTEGER}
+    </delete>
+
 </mapper>