1. 增加 Flowable 的多租户字段过滤逻辑

2. 增加 Flowable 对多租户多 DB 的支持
This commit is contained in:
YunaiV 2023-03-11 23:22:16 +08:00
parent b42fe75b88
commit 998ee168d7
13 changed files with 49 additions and 3 deletions

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.framework.flowable.core.util;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
@ -26,6 +28,21 @@ public class FlowableUtils {
Authentication.setAuthenticatedUserId(null);
}
/**
* 获得租户编号
*
* 因为 Flowable 的租户编号是字符串所以封装该方法
*
* @return 租户编号
*/
public static String getTenantId() {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
if (loginUser == null || loginUser.getTenantId() == null) {
return null;
}
return String.valueOf(loginUser.getTenantId());
}
// ========== BPMN 相关的工具方法 ==========
/**

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.bpm.service.definition;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormUpdateReqVO;
@ -29,6 +30,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
*/
@Service
@Validated
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmFormServiceImpl implements BpmFormService {
@Resource

View File

@ -7,6 +7,8 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.object.PageUtils;
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
import cn.iocoder.yudao.framework.flowable.core.util.FlowableUtils;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.*;
import cn.iocoder.yudao.module.bpm.convert.definition.BpmModelConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
@ -47,6 +49,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
@Service
@Validated
@Slf4j
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmModelServiceImpl implements BpmModelService {
@Resource
@ -71,7 +74,8 @@ public class BpmModelServiceImpl implements BpmModelService {
modelQuery.modelCategory(pageVO.getCategory());
}
// 执行查询
List<Model> models = modelQuery.orderByCreateTime().desc()
List<Model> models = modelQuery.modelTenantId(FlowableUtils.getTenantId())
.orderByCreateTime().desc()
.listPage(PageUtils.getStart(pageVO), pageVO.getPageSize());
// 获得 Form Map
@ -107,6 +111,7 @@ public class BpmModelServiceImpl implements BpmModelService {
// 创建流程定义
Model model = repositoryService.newModel();
BpmModelConvert.INSTANCE.copy(model, createReqVO);
model.setTenantId(FlowableUtils.getTenantId());
// 保存流程定义
repositoryService.saveModel(model);
// 保存 BPMN XML

View File

@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.PageUtils;
import cn.iocoder.yudao.framework.flowable.core.util.FlowableUtils;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionListReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageItemRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageReqVO;
@ -49,6 +50,7 @@ import static java.util.Collections.emptyList;
@Service
@Validated
@Slf4j
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmProcessDefinitionServiceImpl implements BpmProcessDefinitionService {
private static final String BPMN_FILE_SUFFIX = ".bpmn";
@ -124,6 +126,7 @@ public class BpmProcessDefinitionServiceImpl implements BpmProcessDefinitionServ
Deployment deploy = repositoryService.createDeployment()
.key(createReqDTO.getKey()).name(createReqDTO.getName()).category(createReqDTO.getCategory())
.addBytes(createReqDTO.getKey() + BPMN_FILE_SUFFIX, createReqDTO.getBpmnBytes())
.tenantId(FlowableUtils.getTenantId())
.deploy();
// 设置 ProcessDefinition category 分类
@ -234,6 +237,7 @@ public class BpmProcessDefinitionServiceImpl implements BpmProcessDefinitionServ
definitionQuery.active();
}
// 执行查询
definitionQuery.processDefinitionTenantId(FlowableUtils.getTenantId());
List<ProcessDefinition> processDefinitions = definitionQuery.list();
if (CollUtil.isEmpty(processDefinitions)) {
return Collections.emptyList();

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.flowable.core.util.FlowableUtils;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.rule.BpmTaskAssignRuleCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.rule.BpmTaskAssignRuleRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.rule.BpmTaskAssignRuleUpdateReqVO;
@ -53,6 +54,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
@Service
@Validated
@Slf4j
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
@Resource

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.bpm.service.definition;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupUpdateReqVO;
@ -30,6 +31,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
*/
@Service
@Validated
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmUserGroupServiceImpl implements BpmUserGroupService {
@Resource

View File

@ -8,7 +8,10 @@ import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcess
import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenTaskCreatedReqDTO;
import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
@ -32,6 +35,7 @@ public class BpmMessageServiceImpl implements BpmMessageService {
private WebProperties webProperties;
@Override
@Async // 必须使用异步因为是在 Flowable 事务内调用无法切换数据源
public void sendMessageWhenProcessInstanceApprove(BpmMessageSendWhenProcessInstanceApproveReqDTO reqDTO) {
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
@ -41,6 +45,7 @@ public class BpmMessageServiceImpl implements BpmMessageService {
}
@Override
@Async // 必须使用异步因为是在 Flowable 事务内调用无法切换数据源
public void sendMessageWhenProcessInstanceReject(BpmMessageSendWhenProcessInstanceRejectReqDTO reqDTO) {
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());
@ -51,6 +56,7 @@ public class BpmMessageServiceImpl implements BpmMessageService {
}
@Override
@Async // 必须使用异步因为是在 Flowable 事务内调用无法切换数据源
public void sendMessageWhenTaskAssigned(BpmMessageSendWhenTaskCreatedReqDTO reqDTO) {
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("processInstanceName", reqDTO.getProcessInstanceName());

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.bpm.service.oa;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.BpmOALeaveCreateReqVO;
@ -29,6 +30,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.OA_LEAVE_NOT_
*/
@Service
@Validated
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmOALeaveServiceImpl implements BpmOALeaveService {
/**

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.bpm.service.task;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.activity.BpmActivityRespVO;
import cn.iocoder.yudao.module.bpm.convert.task.BpmActivityConvert;
import lombok.extern.slf4j.Slf4j;
@ -20,6 +21,7 @@ import java.util.List;
@Service
@Slf4j
@Validated
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmActivityServiceImpl implements BpmActivityService {
@Resource

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.common.util.object.PageUtils;
import cn.iocoder.yudao.framework.tenant.core.db.dynamic.TenantDS;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.*;
import cn.iocoder.yudao.module.bpm.convert.task.BpmTaskConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.task.BpmTaskExtDO;
@ -51,6 +52,7 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
*/
@Slf4j
@Service
@TenantDS // 工作流的 Service 必须添加 @TenantDS 注解原因是Flowable 使用事务无法切换数据源需要提使用 @TenantDS 切到它的数据源
public class BpmTaskServiceImpl implements BpmTaskService {
@Resource

View File

@ -1 +0,0 @@
package cn.iocoder.yudao.module.bpm.service.task;

View File

@ -145,6 +145,9 @@ public class DeptServiceImpl implements DeptService {
@Override
public List<DeptDO> getDeptList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyList();
}
return deptMapper.selectBatchIds(ids);
}