Getting process instance initiator

Hello,

i hope that you can help me with my problem.

I want to save the initiator of a process instance in a process variable.

My idea to get this is this:
I am using an external form as form key for my start event and a REST Call to start the instance.
I added an execution listener to this start event with the event type “end”. This listener starts a java class where i try to save the initiator with this code:

	Authentication auth = execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication();
	String userId = auth.getUserId();
	execution.setVariable("initiator", userId);

The execution listener is triggeed correctly.
With this code no instance is started, but without this code an instance is started.
So i guess something is wrong with getting the logged in user.

EDIT: I found out that execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication(); is returning null.
So i know the problem. But i dont know how to solve it.

Do i have to enable anything for this to work?
Is there an alternative way to get the initiator?

Can anyone of you help me solving my problem? I you need further information please let me know.

Hi,

Have you tried using camunda’s initiator property on start event? Engine saves currently authenticated user to variable with the name provided to initiator property. And then you can use that variable throughout the process.

image

You have to set currently authenticated user, before you start process like this.

engineIdentityService.setAuthenticatedUserId(userId)

Hi and thank you for your reply.

What do you mean with setting the currently authenticated user? I dont know which user will start the process, so i cannot set it to a certain value.

Yes i tried it with this property, but then i have to enable the authentication via web.xml file from the engine-rest webapp.

I am looking for a way without enabling this authentication, so i tried to do it with an Execution Listener and with a JavaDelegate class.

I also tried to get the initiator with:

		HistoricProcessInstance historicProcessInstance = execution.getProcessEngineServices().getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(execution.getProcessInstance().getId()).singleResult().getStartUserId();

and with

		Context.getCommandContext().getAuthenticatedUserId();

but these two methods return null as well.

Maybe i should explain what i want to do in more detail:
I have a start event with an external form, in which i start the instance via rest call.
Then a service task follows where i want to set a process variable depending on the initiator of this instance.

So i tried to implement an execution listener and i tried to implement it in the Service Task as with in the named ways.

As said above i am looking for a way to get the initiator without enabling the authentication for the engine-rest webapp.

Any ideas or alternative ways?

Thank you very much.

You don’t authenticate users before rest call? I mean the user who calls your rest api. You should authenticate users before they call rest api. If you dont that will cause security issues.

Start event calls the service task in same transaction and no variables persist in the engine before this transaction ends. If that is the case you should mark start event as asyncbefore=true to make transaction boundary and initiator will be saved to engine database before continuing to service task. Of course this initiator variable needs to be known by engine by setting authenticated user as I said in my previous post.
https://docs.camunda.org/manual/7.7/user-guide/process-engine/transactions-in-processes/

If you still need help, it will be helpful if you upload your model and answer above questions.

The process instance is started from tasklist, so the user has to log into tasklist to start the process instance.
I dont do another authentication. I thought i can get the logged in user from there?

I got to say that i just decided to work with camunda as a hobby and it runs on a virtual machine on my PC. So it may be possible that i do not have best security Settings, as i focus on getting the process to work for now.

Thanks for the link about transactions, didnt see that before. I will try to mark the start event as async and have a look at your link later that day or tomorrow.

If i still need help then i will tell it here.

But many thanks to you for now for your help :slight_smile:

That is normal, then you don’t need to set authenticated user id. Making start event async should solve the problem. Hope this will help.

Hi,

i tried the async but it does not work.
I tried with asyncAfter ans asyncBefore at start event and asyncBefore at service task.

Maybe i should explain better how i want my process to work:

Here is the start of the process:
image

To start a process the user has to log into tasklist and select the process he wants to start via “Start process” in tasklist.
The start event calls an external form. When the user hits the send button in this external form a REST Call starts the process instance.

The assignee of the user task should be a person depending on the initiator of the process, so the service task starts a JavaDelegation class to set a process variable. This variable is used as assignee for the user task.

the methods:

		Context.getCommandContext().getAuthenticatedUserId();

and

		execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication();

and

execution.getProcessEngineServices().getHistoryService().createHistoricProcessInstanceQuery()
			.processInstanceId(execution.getProcessInstance().getId()).singleResult()

always return null.

So now i dont know how to get the initiator or the authenticated user?
What should be the best way?

Do i understand your answer wrong? Itf this is the case i am very sorry, but i really dont know how to get this.

Hope for your help.

Can you upload your model?

i created a smaller project so i can better focus on the problem. Got the same issues with that.

Here is my Process Model

process.bpmn (4.4 KB)

As said the Start event calls an external form. In this form the user starts the process via a button click and a rest call.

In the servise task i want to get the initiator of the process and then decide which value the variable “vorgesetzter” gets.

This variable is the assignee of the user task.

Thats what i thought of. Maybe you got an better idea to achieve this?

Hi,

I see, form key in start event makes authentication scope fall out. In this case you can simply take initiator like this.

var initiatorId = delegateExecution.getVariable(“initiatorBPM”);

Summary

I also found it is populated in the historical data as you mentioned earlier.

var startUserId = delegateExecution.getProcessEngineServices().getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(delegateExecution.getProcessInstance().getId()).singleResult().getStartUserId();

Hi,

in the java class of my service task i already tried following code:

		String userId = execution.getVariable("initiatorBPM").toString();

and

		String userId = execution.getProcessEngineServices().getHistoryService()
				.createHistoricProcessInstanceQuery().processInstanceId(execution.getProcessInstance().getId())
				.singleResult().getStartUserId();

But both methods return null. The variable initiatorBPM is not set when starting the instance.

I already found this in the docs:
https://docs.camunda.org/manual/7.7/reference/rest/overview/authentication/

If i understood it right, i have to activate this to get any information about the initiator or the logged in user. Is this correct?

But here is my problem, that no process instance is started when i activate that.
I should pass a user and password with the rest call that starts the process instance. But i dont want to do a second login and then pass this username to the engine. I want the user that is already logged into tasklist.

Maybe it helps if you know that i am using the tomcat pre-packaged distribution and the maven archetype servlet-war?

Do i have to set the init-param as described in the link above, because i use the tomcat distribution? If yes:

  • What should be the param-value?
  • Do i have to change something from the rest of the RESTeasy filter configuration?

Here is my web.xml: (I added the configuration from the link above already at the end)
web.xml (3.0 KB)

I have already tested your model in my side, and it works.

That is related to Rest Api authentication. Here you make request from Tasklist.

Are you able to look at any logs for error message? Maybe you have something incorrect in configuration or accidentally changed some configuration.

To be honest i dont know which logs to look at. The only log file i know is the catalina.out from the tomcat server, but everything seems fine here.

Did you create a sample Project to test it?
If yes, could you Share it so i can have a look at it?

Because i use the pre-packaged tomcat distribution and used the maven Camunda servlet war Archetype. I Changed nothing in the configs.