From 4492e357dd41ae269421481483f61336f0efd047 Mon Sep 17 00:00:00 2001 From: Sanja Date: Fri, 9 Feb 2024 12:52:45 -0800 Subject: [PATCH 1/2] Default to Admin User for Scripts without Assigned Run_as_user_id --- ProcessMaker/ImportExport/Exporters/ScriptExporter.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php index 84a87421cd..f6c75194b3 100644 --- a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php +++ b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php @@ -5,6 +5,7 @@ use ProcessMaker\ImportExport\DependentType; use ProcessMaker\Models\EnvironmentVariable; use ProcessMaker\Models\ScriptCategory; +use ProcessMaker\Models\User; class ScriptExporter extends ExporterBase { @@ -35,7 +36,14 @@ public function import() : bool foreach ($this->getDependents('user', true) as $dependent) { $scriptUser = $dependent->model; - $this->model->run_as_user_id = $scriptUser->id; + if (!is_null($scriptUser->id)) { + // Assign the user ID to run the script + $this->model->run_as_user_id = $scriptUser->id; + } else { + // If the user is not set in the dependent modal, default to the admin user + $adminUser = User::where('is_administrator', true)->first(); + $this->model->run_as_user_id = $adminUser->id; + } } foreach ($this->getDependents('executor', true) as $dependent) { From 0387e9b60ff5f43382f66ad5d591f3553bba5beb Mon Sep 17 00:00:00 2001 From: Sanja Date: Fri, 9 Feb 2024 13:35:01 -0800 Subject: [PATCH 2/2] Optimize code to handle better edge cases --- .../ImportExport/Exporters/ScriptExporter.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php index f6c75194b3..e0113e8424 100644 --- a/ProcessMaker/ImportExport/Exporters/ScriptExporter.php +++ b/ProcessMaker/ImportExport/Exporters/ScriptExporter.php @@ -36,14 +36,9 @@ public function import() : bool foreach ($this->getDependents('user', true) as $dependent) { $scriptUser = $dependent->model; - if (!is_null($scriptUser->id)) { - // Assign the user ID to run the script - $this->model->run_as_user_id = $scriptUser->id; - } else { - // If the user is not set in the dependent modal, default to the admin user - $adminUser = User::where('is_administrator', true)->first(); - $this->model->run_as_user_id = $adminUser->id; - } + // Assign the user ID to run the script + // If the user is not set in the dependent modal, default to the admin user + $this->model->run_as_user_id = $scriptUser?->id ?? User::where('is_administrator', true)->first()->id; } foreach ($this->getDependents('executor', true) as $dependent) {