cancel
Showing results for 
Search instead for 
Did you mean: 

HistoricProcessInstance.getStartUserId() return null

limcheekin
Champ on-the-rise
Champ on-the-rise
Hi there,

When I call historyService.createHistoricProcessInstanceQuery().list() the process instance 'getStartUserId()' for each resulting instance always returns a null.

I re-post the statement here which originated from Grails Activiti Plugin user, Ned.

I know the reason why it return null, this is because the plugin didn't pass in the user id when starting the process instance. By the way, when I refer to the API doc at http://activiti.org/javadocs/org/activiti/engine/RuntimeService.html, I don't see is there a way to pass in user id in startProcessInstanceById() or startProcessInstanceByKey().

Am I overlook something? Please advice.

Best regards,
Chee Kin
8 REPLIES 8

frederikherema1
Star Contributor
Star Contributor
The current logged-in user should be set using:
org.activiti.engine.impl.identity.Authentication.setAuthenticatedUser(…);
Best to clear the authenticated user after your requests to the engine have finished (authentication is thread-local)

limcheekin
Champ on-the-rise
Champ on-the-rise
Thanks for quick response.

Did you means this API method?
http://www.activiti.org/javadocs/org/activiti/engine/IdentityService.html#setAuthenticatedUserId%28j...

How do you defined the "finished" state? Is the process instance started then considered finished?

How to clear it by the way? setAuthenticatedUserId(null)?

Best regards,
Chee Kin

jbarrez
Star Contributor
Star Contributor
This is how we currently do it in the webapp: when a new request comes in, we set the authenticated user. When all is done, and the response is sent back, we set null.

limcheekin
Champ on-the-rise
Champ on-the-rise
This is how we currently do it in the webapp: when a new request comes in, we set the authenticated user. When all is done, and the response is sent back, we set null.

Hi Joram,

Thanks for writing.

I still not sure when should I set the authenticated user and when to clear it.

Can you elaborate more about "When all is done", how do the request considered "done", do you means on user task basis? The application need to set the authenticated user before the user task started and clear it after the user task completed?

I will be very much appreciated if you can elaborate more about your webapp scenario.

Best regards,
Chee Kin

jbarrez
Star Contributor
Star Contributor
Sure: in a regular webapp, the best way would be using a Servlet Listener that catches all incoming requests.
When a request is receivd, the authenticated user is set, then the next request handler in the chain is called. When that method returns, the authenticated user is removed again.

And this is how we do it in our Vaadin app:

public void onRequestStart(HttpServletRequest request, HttpServletResponse response) {
   
   // Authentication: check if user is found, otherwise send to login page
    LoggedInUser user = (LoggedInUser) getUser();
    if (user == null) {
      …
    } else {
      // Set thread-local userid of logged in user (needed for Activiti user logic)
      Authentication.setAuthenticatedUserId(user.getId());
    }
  }
 
  public void onRequestEnd(HttpServletRequest request, HttpServletResponse response) {
    Authentication.setAuthenticatedUserId(null);
  }

limcheekin
Champ on-the-rise
Champ on-the-rise
Hi Joram,

Thanks for "show me the code". Smiley Happy I am clearer now.

But I have two more questions:
1) Is IdentityService.setAuthenticatedUserId() same with Authentication.setAuthenticatedUserId()?
2) What if I implement it using Servlet Filter? Just curious why do you choose Servlet Listener over Servlet Filter?

Wish to hear from you again.

Best regards,
Chee Kin

jbarrez
Star Contributor
Star Contributor
1) yup exactly the same. My bad for using the wrong method.
2) And I indeed meant Filter, not listener !

limcheekin
Champ on-the-rise
Champ on-the-rise
1) yup exactly the same. My bad for using the wrong method.
2) And I indeed meant Filter, not listener !

Thanks for further clarification and confirmation. I implemented this servlet filter in Activiti Spring Security Integration plugin 0.3.

Best regards,
Chee Kin