cancel
Showing results for 
Search instead for 
Did you mean: 

Run workflow script task tenant admin

nicoo
Champ in-the-making
Champ in-the-making
Hello,

I use a custom workflow definition on a multi tenant alfresco 5.0.a (for testing purposes).
In the bpmn process definition there are some script task, which are setting Tags on contained documents.


<serviceTask id="alfrescoScripttask1" name="add tag" activiti:class="org.alfresco.repo.workflow.activiti.script.AlfrescoScriptDelegate">
  <extensionElements>
   <activiti:field name="script">
     <activiti:string><![CDATA[if(bpm_package && bpm_package.children){ for (var i = 0; i < bpm_package.children.length; i++) {  bpm_package.children.addTag("Some tag");  }}]]></activiti:string>
   </activiti:field>
  </extensionElements>
</serviceTask>


As this is working as expected, I'd like to execute this script as admin. In my naive thinking, I tried to use the "run as" property, like this, as addition to the service task:


<activiti:field name="runAs">
     <activiti:string><![CDATA[admin]]></activiti:string>
</activiti:field>



-
The problem arises with the multi tenant system. Of course the user "admin" is not existing for a tenant, which would be admin@example.com.

My question is:
How can I run a script task of a workflow as admin of the "current" tenant (the tenant of the user executing the workflow)?

Thank you very much.
4 REPLIES 4

hiten_rastogi1
Star Contributor
Star Contributor

Hi Nico O _

Were you able to solve this issue? Facing a similar situation here.

Cheers !!!

Hiten Rastogi

afaust
Legendary Innovator
Legendary Innovator

The only way to deal with this would require you to use an expression instead of a hard-coded user ID. The expression would have to make use of some Java class / code to dynamically determine the correct user name. The Java class org.alfresco.repo.tenant.TenantUtil is typically used to execute code as a specific user in a specific tenant, while the org.alfresco.repo.tenant.TenantService interface provides utilities to translate abstract identifiers (including user names) to tenant specific identifiers.

Hi Axel,

As per your suggestion, I inserted an executionListener in the startEvent of the workflow as below

<startEvent id="startevent1" name="Start" activiti:formKey="scwf:submitReviewTask">
   <extensionElements>
      <activiti:executionListener event="end" class="com.xyz.abc.workflow.SetAdminUser" />
   </extensionElements>
</startEvent>

In the SetAdminUser class, in the end, I set the userName in an execution variable as below.

execution.setVariable("admin", "admin"+SEPARATOR+tenantDomain);

 and now I am trying to use this username in my runAs field in one of my serviceTask as below

<activiti:field name="runAs">
   <activiti:string>
      <![CDATA[execution.getVariable("admin")]]>
   </activiti:string>
</activiti:field>

but the expression isn't resolving and is taken as it is. Hence the below error.

2019-02-18 15:14:16,224 DEBUG [jscript.RhinoScriptProcessor.calls] [http-bio-8080-exec-2] workspace://SpacesStore/486e54d2-0947-4984-b12b-453b8f88609e Exception
org.mozilla.javascript.WrappedException: Wrapped org.alfresco.service.cmr.workflow.WorkflowException: 01180026 runas user 'execution.getVariable("admin");' does not exist. (workspace://SpacesStore/486e54d2-0947-4984-b12b-453b8f88609e#12)

Any help would be great here.

Cheers !!!

To answer the above reply I posted we just have to replace the below code in bpmn 

<activiti:field name="runAs">
   <activiti:string>
      <![CDATA[execution.getVariable("admin")]]>
   </activiti:string>
</activiti:field>

with

<activiti:field name="runAs">
   <activiti:expression>${execution.getVariable("admin")}</activiti:expression>
</activiti:field>

OR(make it less verbose)

<activiti:field name="runAs" expression="${execution.getVariable('admin')}" />

Reference: https://www.activiti.org/userguide/#serviceTaskFieldInjection

Cheers !!!