Get user id from tasklist in JSF CDI Bean

Hi,
I use the camunda tasklist and external jsf forms. I know that I have to set the userId from the logged in user in the identityService from camunda to handle my jsf form authentication. Is there a way to get the userid from tasklist? Or from anywhere else?
Thanks a lot,
Nicole

Hi @NickiMueller,

I don’t know if it works in a JSF environment, but the engine has a built-in-expression: ${currentUser()}.

Here are the details: https://docs.camunda.org/manual/latest/user-guide/process-engine/expression-language/#internal-context-functions.

Hope this helps, Ingo

HI @Ingo_Richtsmeier,
thanks you for your answer. But I am really confused and do not understand where and how to use this code.
I think I would need a service task like this:
grafik

So in my bpmn-file I see the following code
<bpmn:serviceTask id=“setUserTask” name=“setUserTask” camunda:expression="${currentUser}">

But how can I now read in in my cdi bean?
Sorry for my confusion.
Thanks so much for your help.
Nicole

@NickiMueller it should be like below:

<bpmn:serviceTask id="setUserTask" name="setUserTask" camunda:expression="${currentUser()}">

Function Return Type Description
currentUser() String Returns the user id of the currently authenticated user or null no user is authenticated at the moment.

Hi @NickiMueller,

I havn’t been deep in JSF development, but I would try to use the expression in the JSF page itself. If the user is logged in, it should get the ID.

What do you want to use the userId for?

BTW, in your sceenshot you’re missing the parenthesis for the method call.

Here is the implementation if the function: https://github.com/camunda/camunda-bpm-platform/blob/master/engine/src/main/java/org/camunda/bpm/engine/impl/el/CommandContextFunctionMapper.java#L59-L67.

Hope this helps, Ingo

Good morning,

thanks for your ideas.
@Ingo_Richtsmeier I tried to directly use the method in jsf but that does not work.

@Ingo_Richtsmeier Why I need the logged in user:
I work with external jsf forms. A camunda user (for example a user named “studi1” in group “StudentGroup”) can start a process named “Abschlussarbeit beantragen” via the camunda tasklist. The studi1 user can now add his master data to the form.
Now I want to save the data for my studi1 user and therefore I need the username.
Another thing is that I want to show the logged in user name in my jsf external forms.

I tried to set a processVariable via using the currentUser() method:
I took pizza order example. I added the following code in placeorder.jsf:

   <label for="currentUser">CurrentUser</label>
      <!-- create process variables using the processVariables map. -->

<h:outputText id=“currentUser” value="#{processVariables[’$currentUser()’]}" required=“true” />

Then I tried to read it in OrderBusinessLogic.java via:

Map<String, Object> variables = delegateExecution.getVariables();

  String currentUser = (String) variables.get("currentUser");

But the currentUser is null.

Any more ideas? Do you know a way via javascript to read the logged in user? So I could write the user
in HttpSession and try to read in my CDI Bean via HTTPSession Object? Unfortunately I am not used to write javascipts.

Thank you a lot for you help!
Nicole

Current user is neither stored as process variables nor task variables

Hi @NickiMueller,

some additional ideas (but just ideas) from my side:

  public void notify(DelegateExecution execution) throws Exception {
    String userId = execution
        .getProcessEngineServices()
        .getIdentityService()
        .getCurrentAuthentication()
        .getUserId();
    execution.setVariable("userId", userId);
  }
  • maybe the identityService.getCurrentAuthentication().getUserId() works directly in a JSF page?

Hope this helps, Ingo

Hi @Ingo_Richtsmeier,

I can not use

< script cam-script type=“text/form-script”>
in my jsf page. Perhaps there is a way to load a library for cam-script? But I do not know how.

I tried to add a listener to my start event. I tried the following:
Here is my java class:

package org.camunda.bpm.getstarted.pizza;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;

public class UserListener implements ExecutionListener {

	 public void notify(DelegateExecution execution) throws Exception {
		    String userId = execution
		        .getProcessEngineServices()
		        .getIdentityService()
		        .getCurrentAuthentication()
		        .getUserId();
		    execution.setVariable("userId", userId);
	 }
}

grafik

But in my bpmn file there where only the following entry:
grafik

So I tried to write it directly into the file.

    <bpmn:process id="orderPizza" name="Order Pizza" isExecutable="true">

      <bpmn:extensionElements>
        <camunda:executionListener class="org.camunda.bpm.getstarted.pizza.UserListener" event="start" />
      </bpmn:extensionElements>

That worked and the method notify() is called. But unfortunately the userId is null. That is what I excepted because I think the identityService has to be “filled”.

I am really irritated that it seems that camunda is not usable with jsf only when I implement an own tasklist. And for that I have to learn Javascript/Angular.
@Ingo_Richtsmeier I took your tasklist example and combined it with a ServletFilter to manage the authorization. It works.

I hope you have some more ideas?
Thanks you so much,
Nicole