{ "ownerApp" : "Orchestrator", "name" : "create-user-add-to-vre", "createBy" : "Marco Lettere", "description": "Batch create a user with a membership in a specific group", "version" : 1, "ownerEmail" : "m.lettere@gmail.com", "inputParameters" : ["user", "first-name", "last-name", "email", "password", "group"], "tasks" : [ { "name": "INLINE_TASK", "taskReferenceName": "init", "type": "INLINE", "inputParameters": { "keycloak": "{{ keycloak }}/{{ keycloak_realm }}", "keycloak_admin" : "{{ keycloak_admin }}/{{ keycloak_realm }}", "group" : "${workflow.input.group}", "evaluatorType" : "javascript", "expression": "function f(){var path = $.group.split('%2F').slice(1); return { 'tree' : Java.to(path, 'java.lang.Object[]'), 'name' : path.slice(path.length-1)[0]}} f()" } }, { "name" : "pyrest", "taskReferenceName" : "authorize", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak}/protocol/openid-connect/token", "method" : "POST", "headers" : { "Accept" : "application/json" }, "body" : { "client_id" : "orchestrator", "client_secret" : "{{ keycloak_auth }}", "grant_type" : "client_credentials" } } }, { "name" : "pyrest", "taskReferenceName" : "create_user", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak_admin}/users", "expect" : 201, "method" : "POST", "body" : { "username": "${workflow.input.user}", "firstName": "${workflow.input.first-name}", "lastName": "${workflow.input.last-name}", "email": "${workflow.input.email}", "credentials": [ { "temporary": true, "type": "password", "value": "${workflow.input.password}" } ], "requiredActions": ["UPDATE_PASSWORD"], "emailVerified": true, "enabled": true }, "headers" : { "Authorization" : "Bearer ${authorize.output.body.access_token}", "Content-Type" : "application/json" } } }, { "name" : "pyrest", "taskReferenceName" : "lookup_user", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak_admin}/users?username=${workflow.input.user}", "method" : "GET", "headers" : { "Authorization" : "Bearer ${authorize.output.body.access_token}", "Accept" : "application/json" } } }, { "name": "INLINE_TASK", "taskReferenceName": "select_user", "inputParameters": { "foundusers": "${lookup_user.output.body}", "username": "${workflow.input.user}", "evaluatorType" : "javascript", "expression": "function f(){for(var i=0; i < $.foundusers.length;i++){if($.foundusers[i]['username'] == $.username) return $.foundusers[i]}} f()" }, "type": "INLINE" }, { "name" : "pyrest", "taskReferenceName" : "lookup_client", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak_admin}/clients", "params" : { "clientId" : "${workflow.input.group}"}, "method" : "GET", "headers" : { "Authorization" : "Bearer ${authorize.output.body.access_token}", "Accept" : "application/json" } } }, { "name" : "pyrest", "taskReferenceName" : "get_client_roles", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak_admin}/clients/${lookup_client.output.body[0].id}/roles", "expect" : [200, 404], "method" : "GET", "headers" : { "Authorization" : "Bearer ${authorize.output.body.access_token}", "Accept" : "application/json" } } }, { "name" : "check_role_existance", "taskReferenceName" : "check_role_existance", "type" : "SWITCH", "inputParameters" :{ "previous_outcome" : "${get_client_roles.output.status}" }, "evaluatorType" : "value-param", "expression" : "previous_outcome", "decisionCases" : { "200" : [ { "name": "INLINE_TASK", "taskReferenceName": "select_role", "type": "INLINE", "inputParameters": { "evaluatorType" : "javascript", "role": "${workflow.input.role}", "roles" : "${get_client_roles.output.body}", "expression": "function f(){for(var i=0; i < $.roles.length;i++){if($.roles[i]['name'] == 'Member') return $.roles[i]}} f()" } }, { "name" : "pyrest", "taskReferenceName" : "look_up_groups", "type" : "SIMPLE", "inputParameters" : { "url" : "${init.input.keycloak_admin}/groups?search=${init.output.result.name}", "headers" : { "Authorization" : "Bearer ${authorize.output.body.access_token}", "Accept" : "application/json" } } }, { "name": "INLINE_TASK", "taskReferenceName": "extract_groups", "type": "INLINE", "inputParameters": { "evaluatorType" : "javascript", "tree" : "${init.output.result.tree}", "groups" : "${look_up_groups.output.body}", "expression": "function selectByPath(groups, path, level, acc){ for (var i=0; i < groups.length; i++) {if (groups[i].name === path[level]) {acc.push(groups[i]); if (level === path.length - 1) return acc;return selectByPath(groups[i].subGroups, path, level+1, acc)}} return []; } function f(){ return { 'groups' : Java.to(selectByPath($.groups, $.tree, 0, []),'java.util.Map[]')}} f()" } }, { "name": "INLINE_TASK", "taskReferenceName": "build_add_to_all_groups_tasks", "type": "INLINE", "inputParameters": { "evaluatorType" : "javascript", "groups" : "${extract_groups.output.result.groups}", "auth" : "Bearer ${authorize.output.body.access_token}", "kc_user_url" : "${init.input.keycloak_admin}/users/${select_user.output.result.id}/groups/", "expression": "inputs={};tasks=[];function f(){for(var i=0;i<$.groups.length;i++)group=$.groups[i],tasks.push({name:'pyrest',type:'SIMPLE',taskReferenceName:'user_to_group_'+i}),inputs['user_to_group_'+i]={ url : $.kc_user_url + group.id, method : 'PUT', headers: { Authorization : $.auth} };return {tasks:Java.to(tasks,'java.util.Map[]'),inputs:inputs};} f();" } }, { "name" : "fork_dynamic", "type" : "FORK_JOIN_DYNAMIC", "taskReferenceName" : "parallel_user_to_groups", "inputParameters" : { "tasks" : "${build_add_to_all_groups_tasks.output.result.tasks}", "inputs" : "${build_add_to_all_groups_tasks.output.result.inputs}" }, "dynamicForkTasksParam": "tasks", "dynamicForkTasksInputParamName": "inputs" }, { "name" : "join", "type" : "JOIN", "taskReferenceName" : "join_parallel_user_to_groups" } ] } } ] }