get logged in user (id and username)

1
0
-1

Hello,

I am trying to write a groovy script that manages the condition in the gateway based on the logged in user's organization (I have the same need in the business variable default value initiation script). I have found responses on this matter, but they don't give the full information.

I understand that i can use the identity api to get data regarding the users, but i need an ID or a username to search by. How can i get these variables?

for example:

IdentityAPI identity = apiAccessor.getIdentityAPI();
User user = apiAccessor.getIdentityAPI().findUserByUserName(username);

\\ or \\

User user = apiAccessor.getIdentityAPI().getUser(userId);

Thank you

1 answer

1
+1
-1

If the script is on a sequence flow you can retrieve the userId of the user that perform the previous tasks (if it is what you are looking for) using the process api:

import org.bonitasoft.engine.bpm.flownode.ActivityStates
import org.bonitasoft.engine.bpm.flownode.ArchivedHumanTaskInstanceSearchDescriptor
import org.bonitasoft.engine.search.SearchOptionsBuilder

def result = apiAccessor.getProcessAPI().searchArchivedHumanTasks(new SearchOptionsBuilder(0, 1)
        .filter(ArchivedHumanTaskInstanceSearchDescriptor.ROOT_PROCESS_INSTANCE_ID, rootProcessInstanceId)
        .filter(ArchivedHumanTaskInstanceSearchDescriptor.NAME,  'Step1')
        .filter(ArchivedHumanTaskInstanceSearchDescriptor.STATE_NAME, ActivityStates.COMPLETED_STATE)
        .done()).result
def userId = result[0].executedBy
...

HTH
Romain

Notifications