Commit 82b03d7f7d3cebd8a38c19a77e57737ae62ed18a
1 parent
cdb31679
【功能新增】工作流:API 创建流程时,设置 userId(解决 Job 下,无法设置 userId 的问题)
Showing
2 changed files
with
19 additions
and
5 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/FlowableUtils.java
| @@ -22,6 +22,7 @@ import java.util.HashMap; | @@ -22,6 +22,7 @@ import java.util.HashMap; | ||
| 22 | import java.util.List; | 22 | import java.util.List; |
| 23 | import java.util.Map; | 23 | import java.util.Map; |
| 24 | import java.util.Objects; | 24 | import java.util.Objects; |
| 25 | +import java.util.concurrent.Callable; | ||
| 25 | 26 | ||
| 26 | /** | 27 | /** |
| 27 | * Flowable 相关的工具方法 | 28 | * Flowable 相关的工具方法 |
| @@ -40,6 +41,17 @@ public class FlowableUtils { | @@ -40,6 +41,17 @@ public class FlowableUtils { | ||
| 40 | Authentication.setAuthenticatedUserId(null); | 41 | Authentication.setAuthenticatedUserId(null); |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 44 | + public static <V> V executeAuthenticatedUserId(Long userId, Callable<V> callable) { | ||
| 45 | + setAuthenticatedUserId(userId); | ||
| 46 | + try { | ||
| 47 | + return callable.call(); | ||
| 48 | + } catch (Exception e) { | ||
| 49 | + throw new RuntimeException(e); | ||
| 50 | + } finally { | ||
| 51 | + clearAuthenticatedUserId(); | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + | ||
| 43 | public static String getTenantId() { | 55 | public static String getTenantId() { |
| 44 | Long tenantId = TenantContextHolder.getTenantId(); | 56 | Long tenantId = TenantContextHolder.getTenantId(); |
| 45 | return tenantId != null ? String.valueOf(tenantId) : ProcessEngineConfiguration.NO_TENANT_ID; | 57 | return tenantId != null ? String.valueOf(tenantId) : ProcessEngineConfiguration.NO_TENANT_ID; |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java
| @@ -557,11 +557,13 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | @@ -557,11 +557,13 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | ||
| 557 | 557 | ||
| 558 | @Override | 558 | @Override |
| 559 | public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO createReqDTO) { | 559 | public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO createReqDTO) { |
| 560 | - // 获得流程定义 | ||
| 561 | - ProcessDefinition definition = processDefinitionService.getActiveProcessDefinition(createReqDTO.getProcessDefinitionKey()); | ||
| 562 | - // 发起流程 | ||
| 563 | - return createProcessInstance0(userId, definition, createReqDTO.getVariables(), createReqDTO.getBusinessKey(), | ||
| 564 | - createReqDTO.getStartUserSelectAssignees()); | 560 | + return FlowableUtils.executeAuthenticatedUserId(userId, () -> { |
| 561 | + // 获得流程定义 | ||
| 562 | + ProcessDefinition definition = processDefinitionService.getActiveProcessDefinition(createReqDTO.getProcessDefinitionKey()); | ||
| 563 | + // 发起流程 | ||
| 564 | + return createProcessInstance0(userId, definition, createReqDTO.getVariables(), createReqDTO.getBusinessKey(), | ||
| 565 | + createReqDTO.getStartUserSelectAssignees()); | ||
| 566 | + }); | ||
| 565 | } | 567 | } |
| 566 | 568 | ||
| 567 | private String createProcessInstance0(Long userId, ProcessDefinition definition, | 569 | private String createProcessInstance0(Long userId, ProcessDefinition definition, |