在完成任务之前,Spring Activiti会验证受理人或候选人组

时间:2017-12-18 19:09:23

标签: java spring activiti

我正在与Spring Acitviti合作管理项目的工作流程。我是Activiti的新手,我试图让它与自定义用户群一起工作。以下是我正在使用的示例项目

https://github.com/ConsciousObserver/AcitvitySpringTest.git

目前,我正在尝试限制可以在taskA进程中执行特定任务simple-test-process的用户。

似乎在调用taskService.complete(task.getId())时Activiti完全忽略了任务分配给谁。由于项目中尚未配置安全性,并且taskA已分配给Hello World,因此匿名用户尝试执行此操作时会失败。

活动是否要求我们在执行任务之前手动检查assigneecandidateUserscandidateGroups

如果没有,那么我们如何使用表示Activiti来让只有特定用户完成任务。

包括以下相关代码 simple-test-process.bpmn图

simple-test-process.bpmn diagram

简单试验process.bpmn

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
    xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
    typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
    targetNamespace="http://www.example.org/test">
    <process id="simple-test-process" name="simple-test-process">
        <startEvent id="startEvent" name="startEvent"></startEvent>
        <sequenceFlow id="sequenceFlow1" targetRef="initTask" sourceRef="startEvent" />
        <serviceTask id="initTask" name="initTask" activiti:expression="${myService.init(photos)}"></serviceTask>
        <sequenceFlow targetRef="taskA" sourceRef="initTask"></sequenceFlow>
        <userTask id="taskA" name="taskA" activiti:assignee="Hello World" ></userTask>
        <sequenceFlow id="sequenceFlow2" targetRef="taskB" sourceRef="taskA"></sequenceFlow>
        <userTask id="taskB" name="taskB" activiti:assignee="$INITIATOR"></userTask>
        <sequenceFlow id="sequenceFlow3" targetRef="endEvent" sourceRef="taskB" />
        <endEvent id="endEvent" name="endEvent"></endEvent>
    </process>
</definitions>

ActivitiTestApplication.java

package com.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ActivitiTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivitiTestApplication.class, args);
    }

    @Bean
    InitializingBean usersAndGroupsInitializer(final IdentityService identityService) {

        return () -> {
                Group group = identityService.newGroup("user");
                group.setName("users");
                group.setType("security-role");
                identityService.saveGroup(group);

                User admin = identityService.newUser("admin");
                admin.setPassword("admin");
                identityService.saveUser(admin);
            };
    }
}

@RestController
class ProcessRest {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @GetMapping("/start")
    public List<Map<String, String>> start() {
        List<String> photos = Arrays.asList("photo1", "photo2");
        runtimeService.startProcessInstanceByKey("simple-test-process", Collections.singletonMap("photos", photos));

        long runningProcessCount = runtimeService.createProcessInstanceQuery().count();
        List<ProcessInstance> processes = runtimeService.createProcessInstanceQuery().list();

        return processes
                .stream()
                .map(p -> getMap(p.getId(), p.getProcessInstanceId(), p.getProcessDefinitionName()))
                .collect(Collectors.toList());
    }

    @GetMapping("/getTasks/{processInstanceId}")
    public List<Map<String, String>> getTasks(@PathVariable String processInstanceId) {
        List<Task> tasks = taskService
            .createTaskQuery()
            .processInstanceId(processInstanceId)
            .list();

        return tasks
                .stream()
                .map(t -> getMap(t.getId(), t.getProcessInstanceId(), t.getName()))
                .collect(Collectors.toList());
    }

    @GetMapping("/completeTask/{processInstanceId}")
    public Map<String, String> completeNextTask(@PathVariable String processInstanceId) {
        Task task = taskService.createTaskQuery()
                        .processInstanceId(processInstanceId)
                        .singleResult();

        if(task == null) {
            throw new RuntimeException("No pending task for this processInstance");
        }

        System.out.println("variables: " + task.getProcessVariables());

        taskService.complete(task.getId());

        return getMap(task.getId(), task.getProcessInstanceId(), task.getName());
    }

    public Map<String, String> getMap(String id, String instanceId, String name) {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("processInstanceId", instanceId);
        map.put("name", name);

        return map;
    }
}
@Component
class MyService {
    public void init(List<String> photos) {
        System.out.println("################################### Running Init Task ########################");
        for(String photo: photos) {
            System.out.println("Photo name: " + photo);
        }
    }

    public String processPhoto(List<String> photos) {
        for(String photo: photos) {
            System.out.println("Photo name: " + photo);
        }
        return "processed";
    }
}

1 个答案:

答案 0 :(得分:1)

不幸的是,没有魔法会为你做到这一点,所以你需要验证这些事情,比如任务的受让人和诸如此类的东西。

我打赌你会想要比硬编码的'Hello World&#34;更动态的东西,所以为了创造更动态的东西,作为一个简单的例子,我会改变你的/start端点GETPOST并将受理人作为参数检索。

@PostMapping("/start")
public List<Map<String, String>> start(String assignee) {

并传递processVariables

中的属性

类似的东西:

Map<String, Object> variables = Collections.singletonMap("photos", photos);
variables.put("person", assignee);

将您的bpmn从activiti:assignee="Hello World"更改为activiti:assignee=${person}

添加一个TaskListener,用于验证每个任务用户的受让人。 您会发现TaskListener

seconds=$(echo "60 * $1" | bc) for ((; seconds > 0; seconds --)); do echo $seconds sleep 1 done echo boom 示例