cancel
Showing results for 
Search instead for 
Did you mean: 

Start process instance by user (using rest api)

gertp
Champ in-the-making
Champ in-the-making
Hello, in an older thread (titled "start process instance by user") somebody could not see the user set in the "start_user_id_" column in the "ACT_HI_PROCINST" table, and thus could not see the process instance in activiti-explorer. The solution to this was a follows:

<startEvent id="request" activiti:initiator="initiator" />
The authenticated user must be set with the method BEFORE the process instance is started,
try {
  identityService.setAuthenticatedUserId("bono");
  runtimeService.startProcessInstanceByKey("someProcessKey");
} finally {
  identityService.setAuthenticatedUserId(null);
}


I want to start a new process using the rest api, the command to start this is:

C:\Users\upf>curl -uX:X -X POST http://XXX:8989/activiti-rest/service/runtime/process-instances –header "Content-Type:application/json" -d @x


where the file x contains the following:

{
   "processDefinitionKey":"XWorkflow",
    "variables": [
      {
        "name":"priceDate",
        "value":""
      }
   ]
}


The process starts up fine, but it does not set the authenticated user, the "start_user_id_" is null and I cannot see the instance in actitvit-explorer. Does anyone know how how one can set the authenticated user with rest so it is equivalent to the java code that launches the process. I thought it might be curl that is not passing in the user, so I tried the rest plugin for chrome called "Postman" it also did not manage to set the user.

Thanks in advance.

3 REPLIES 3

jbarrez
Star Contributor
Star Contributor
We've got a unit test for this: ProcessInstanceCollectionResourceTest.testStartProcess().

I ran this here, and it fills the start user just fine (user is set by     client.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit");
)

Did you somehowe tweak the rest api or have a custom authenticator?

youtianhong
Champ in-the-making
Champ in-the-making
Hi Jbarrez,

I just did a customzation for removing authentication in rest API service side.
I can remove authentication  in rest API side succesfully according  below way.

My question is I can not pass the user variable when I start a processInstance by rest API.
<code>
ProcessInstanceCreateRequest processInst = new ProcessInstanceCreateRequest();
   processInst.setProcessDefinitionKey("simpleLeave");
   List<RestVariable> variables = new ArrayList<RestVariable>();
   RestVariable variable = new RestVariable();
   variable.setName("applyUserId");
   variable.setValue("Frank");
   processInst.setVariables(variables);
   client.startProcessInstanceWithVariables(processInst);
</code>
The request will start a processInstance successfully, but I checked the field start_user_id in table act_hi_procinst, it always is 'system user'
which I setted in override method "authenticate".
So Can you help me to solve this issue and show  a demo ?  Thanks a lot in advance !
I can not remove the override  method "authenticate" as below code.



My customzation way is below:
1. add customzation class in web.xml
<code>
  <!– Restlet adapter –> 
  <servlet> 
    <servlet-name>RestletServlet</servlet-name> 
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
      <!– Application class name –>
      <param-name>org.restlet.application</param-name>
      <param-value>com.phzc.activiti.custom.filter.CustomActivitiRestServicesApplication</param-value>
    </init-param>
  </servlet>
</code>
2. create a CustomActivitiRestServicesApplication  class
<code>
public class CustomActivitiRestServicesApplication extends ActivitiRestServicesApplication {

protected RestResponseFactory restResponseFactory;
  
   public CustomActivitiRestServicesApplication() {
     super();
     restAuthenticator = new RestAuthenticatorImpl();
     setRestAuthenticator(restAuthenticator);
   }
          //The activiti user guide not mention that override this method 'authenticate' to remove authentication in rest API
          //But for my testing , I need add that to resolve this problem, otherwise it doesn't work
   @Override
   public String authenticate(Request request, Response response) {
    System.out.println("go in ….authenticate()");
    if(request.getClientInfo() != null) {
     if(request.getClientInfo().getUser() != null) {
      return request.getClientInfo().getUser().getIdentifier();
     }
    }
    return "system user";
   }
}
</code>
3.  Create  RestAuthenticatorImpl class
<code>
public class RestAuthenticatorImpl implements RestAuthenticator {
@Override
public boolean requestRequiresAuthentication(Request request) {
  return false;
}
@Override
public boolean isRequestAuthorized(Request request) {
  return false;

}
</code>

gertp
Champ in-the-making
Champ in-the-making
Big oops, my mistake, I did not add the activiti:initiator="initiator" to the bpmn. Now it works using curl.

Thanks for the reply anyway.