Skip to content

Commit

Permalink
3.5.0版本多租户的几个问题 #4647
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangdaiscott committed Apr 18, 2023
1 parent 53684d2 commit e273008
Showing 1 changed file with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.jeecg.modules.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -815,6 +816,10 @@ public Result<JSONObject> setLoginTenant(SysUser sysUser, JSONObject obj, Strin
//设置用户登录缓存租户
this.updateUserDepart(username, null,sysUser.getLoginTenantId());
log.info(" 登录接口用户的租户ID = {}", sysUser.getLoginTenantId());
if(sysUser.getLoginTenantId()!=null){
//登录的时候需要手工设置下会话中的租户ID,不然登录接口无法通过租户隔离查询到数据
TenantContext.setTenant(sysUser.getLoginTenantId()+"");
}
return null;
}

Expand Down Expand Up @@ -852,12 +857,19 @@ private void saveUserTenant(String userId, String relTenantIds) {
}else{
//是否开启系统管理模块的多租户数据隔离【SAAS多租户模式】
if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) {
SysUserTenant relation = new SysUserTenant();
relation.setUserId(userId);
String tenantId = oConvertUtils.getString(TenantContext.getTenant(), "0");
relation.setTenantId(Integer.valueOf(tenantId));
relation.setStatus(CommonConstant.STATUS_1);
relationMapper.insert(relation);
//update-begin---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------
String tenantId = TenantContext.getTenant();
if(oConvertUtils.isNotEmpty(tenantId)){
Integer count = relationMapper.userTenantIzExist(userId, Integer.parseInt(tenantId));
if(count == 0){
SysUserTenant relation = new SysUserTenant();
relation.setUserId(userId);
relation.setTenantId(Integer.parseInt(tenantId));
relation.setStatus(CommonConstant.STATUS_1);
relationMapper.insert(relation);
}
}
//update-end---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------
}
}
}
Expand All @@ -873,21 +885,24 @@ private void editUserTenants(String userId, String relTenantIds) {
//数据库的租户id
List<Integer> oldTenantIds = relationMapper.getTenantIdsNoStatus(userId);
//如果传过来的租户id为空,那么就删除租户
if (oConvertUtils.isEmpty(relTenantIds)) {
if (oConvertUtils.isEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)) {
this.deleteTenantByUserId(userId, null);
} else if (oConvertUtils.isNotEmpty(relTenantIds) && oConvertUtils.isEmpty(oldTenantIds)) {
} else if (oConvertUtils.isNotEmpty(relTenantIds) && CollectionUtils.isEmpty(oldTenantIds)) {
//如果传过来的租户id不为空但是数据库的租户id为空,那么就新增
this.saveUserTenant(userId, relTenantIds);
} else {
//都不为空,需要比较,进行添加或删除
if(oConvertUtils.isNotEmpty(oldTenantIds)){
if(oConvertUtils.isNotEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)){
//找到新的租户id与原来的租户id不同之处,进行删除
List<Integer> tenantIdList = oldTenantIds.stream().filter(item -> !relTenantIds.contains(item.toString())).collect(Collectors.toList());
for (Integer tenantId : tenantIdList) {
String[] relTenantIdArray = relTenantIds.split(SymbolConstant.COMMA);
List<String> relTenantIdList = Arrays.asList(relTenantIdArray);

List<Integer> deleteTenantIdList = oldTenantIds.stream().filter(item -> !relTenantIdList.contains(item.toString())).collect(Collectors.toList());
for (Integer tenantId : deleteTenantIdList) {
this.deleteTenantByUserId(userId, tenantId);
}
//找到原来租户的用户id与新的租户id不同之处,进行新增
String tenantIds = Arrays.stream(relTenantIds.split(SymbolConstant.COMMA)).filter(item -> !oldTenantIds.contains(Integer.valueOf(item))).collect(Collectors.joining(","));
String tenantIds = relTenantIdList.stream().filter(item -> !oldTenantIds.contains(Integer.valueOf(item))).collect(Collectors.joining(","));
this.saveUserTenant(userId, tenantIds);
}
}
Expand Down

0 comments on commit e273008

Please sign in to comment.