Back to All

Can’t get onifyElevatedApiRequest to work from a script task

  • We have 39 settings at /admin/settings?term=role:flow
  • Being logged in as a user with low priviliges I can get all 39 in a flow using the Connector with onifyElevatedApiRequest and role:flow on the endpoint my/config/settings.
  • But when I try to do the same in a script task I only get the few settings that the user has access to by its real roles. I basically used the example at https://support.onify.co/discuss/615fdbb786f1fa006bde76cd but changed the endpoint. Then tried adding roles.

So this works:

But this doesn't:

Here is the script:

(async () => {

	const onifyHeaders = {
		'Content-Type': 'application/json',
		'Authorization': environment.settings.authorization
	}
	
	const resourceOptions = {
		url: `my/config/settings`,
		//url: `admin/config/settings`, //This gives an error, even with role:'flow' or role:['flow']
		headers: onifyHeaders //Doesn't make a difference to the result when commented out
	}

	const result = await environment.services.onifyElevatedApiRequest.call({
		id: content.id,
		name: content.name,
		environment,
		//role:'flow' //Doesn't make a difference to the result
		//role:['flow'] //Doesn't make a difference to the result
	  }, resourceOptions);

	console.log( "Settings via script:", result.body )
	console.log("environment", environment )
	next()

})()

Can you please check if I’m missing something in the script or if this is a bug?

PS. To make it easy to try this with low privilige I just replaced the content of nrt-create-incident.bpmn so i can just fill out the incident form which runs this flow. Is there an easier way to “simulate” running a flow as another user?

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bioc="http://bpmn.io/schema/bpmn/biocolor/1.0" xmlns:color="http://www.omg.org/spec/BPMN/non-normative/color/1.0" id="Definitions_0sdny8d" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.24.0">
  <bpmn:collaboration id="Collaboration_1vlu0n3">
    <bpmn:participant id="CREATEINCIDENTParticipant" name="CREATE INCIDENT" processRef="nrt-create-incident" />
  </bpmn:collaboration>
  <bpmn:process id="nrt-create-incident" name="dynamicsCreateIncident" isExecutable="true">
    <bpmn:laneSet id="LaneSet_02o4jdr">
      <bpmn:lane id="ONIFYLane" name="ONIFY">
        <bpmn:flowNodeRef>StartStartEvent</bpmn:flowNodeRef>
        <bpmn:flowNodeRef>StopEndEvent</bpmn:flowNodeRef>
        <bpmn:flowNodeRef>GetSettingsViaScriptAndLogTask</bpmn:flowNodeRef>
        <bpmn:flowNodeRef>GetSettingsViaConnectorAndLogTask</bpmn:flowNodeRef>
      </bpmn:lane>
    </bpmn:laneSet>
    <bpmn:startEvent id="StartStartEvent" name="Start">
      <bpmn:outgoing>Flow_1urzh60</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:endEvent id="StopEndEvent" name="Stop">
      <bpmn:incoming>Flow_10cxtcq</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:scriptTask id="GetSettingsViaScriptAndLogTask" name="Get settings via script and log" scriptFormat="js" camunda:resultVariable="dummy">
      <bpmn:incoming>Flow_12p9hx4</bpmn:incoming>
      <bpmn:outgoing>Flow_10cxtcq</bpmn:outgoing>
      <bpmn:script>(async () =&gt; {

	const onifyHeaders = {
		'Content-Type': 'application/json',
		'Authorization': environment.settings.authorization
	}
	
	const resourceOptions = {
		url: `my/config/settings`,
		//url: `admin/config/settings`, //This gives an error, even with role:'flow' or role:['flow']
		headers: onifyHeaders //Doesn't make a difference to the result when commented out
	}

	const result = await environment.services.onifyElevatedApiRequest.call({
		id: content.id,
		name: content.name,
		environment,
		//role:'flow' //Doesn't make a difference to the result
		//role:['flow'] //Doesn't make a difference to the result
	  }, resourceOptions);

	console.log( "Settings via script:", result.body )

console.log("environment", environment )
	next()

})()</bpmn:script>
    </bpmn:scriptTask>
    <bpmn:serviceTask id="GetSettingsViaConnectorAndLogTask" name="Get settings via Connector and log">
      <bpmn:extensionElements>
        <camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="url">/my/config/settings</camunda:inputParameter>
            <camunda:inputParameter name="role">flow</camunda:inputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>onifyElevatedApiRequest</camunda:connectorId>
        </camunda:connector>
        <camunda:inputOutput>
          <camunda:outputParameter name="settings">${content.output.body}</camunda:outputParameter>
          <camunda:outputParameter name="state">
            <camunda:script scriptFormat="js">next(null, {id: content.id, result: {done: true}});</camunda:script>
          </camunda:outputParameter>
        </camunda:inputOutput>
        <camunda:executionListener event="end">
          <camunda:script scriptFormat="js">console.log("Settings via connector:")
console.log(content.output.settings)
next()</camunda:script>
        </camunda:executionListener>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_1urzh60</bpmn:incoming>
      <bpmn:outgoing>Flow_12p9hx4</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_1urzh60" sourceRef="StartStartEvent" targetRef="GetSettingsViaConnectorAndLogTask" />
    <bpmn:sequenceFlow id="Flow_12p9hx4" sourceRef="GetSettingsViaConnectorAndLogTask" targetRef="GetSettingsViaScriptAndLogTask" />
    <bpmn:sequenceFlow id="Flow_10cxtcq" sourceRef="GetSettingsViaScriptAndLogTask" targetRef="StopEndEvent" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_1vlu0n3">
      <bpmndi:BPMNShape id="Participant_1b38ks9_di" bpmnElement="CREATEINCIDENTParticipant" isHorizontal="true">
        <dc:Bounds x="153" y="120" width="647" height="190" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Lane_0ougcvv_di" bpmnElement="ONIFYLane" isHorizontal="true">
        <dc:Bounds x="183" y="120" width="617" height="190" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartStartEvent" bioc:stroke="#43a047" bioc:fill="#c8e6c9" color:background-color="#c8e6c9" color:border-color="#43a047">
        <dc:Bounds x="252" y="205" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="258" y="248" width="25" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_10fxhsy_di" bpmnElement="StopEndEvent" bioc:stroke="#e53935" bioc:fill="#ffcdd2" color:background-color="#ffcdd2" color:border-color="#e53935">
        <dc:Bounds x="702" y="205" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="709" y="248" width="24" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_1llod87" bpmnElement="GetSettingsViaScriptAndLogTask">
        <dc:Bounds x="520" y="183" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_1ws3ddk" bpmnElement="GetSettingsViaConnectorAndLogTask">
        <dc:Bounds x="360" y="183" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_1urzh60_di" bpmnElement="Flow_1urzh60">
        <di:waypoint x="288" y="223" />
        <di:waypoint x="360" y="223" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_12p9hx4_di" bpmnElement="Flow_12p9hx4">
        <di:waypoint x="460" y="223" />
        <di:waypoint x="520" y="223" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_10cxtcq_di" bpmnElement="Flow_10cxtcq">
        <di:waypoint x="620" y="223" />
        <di:waypoint x="702" y="223" />
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>