From 52fd7ad57156540545a65e3672581d98ff1209ba Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 31 Jan 2024 13:40:15 +0100 Subject: [PATCH] fix: not able to use other shared envs --- .../Project/Shared/EnvironmentVariable/Add.php | 7 +++++++ .../Project/Shared/EnvironmentVariable/All.php | 14 ++++++++++++++ .../Project/Shared/EnvironmentVariable/Show.php | 12 ++++++++++-- app/Models/EnvironmentVariable.php | 3 +++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php index 78ed3c780..0b6a3897d 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php @@ -32,6 +32,13 @@ class Add extends Component public function submit() { $this->validate(); + if (str($this->value)->startsWith('{{') && str($this->value)->endsWith('}}')) { + $type = str($this->value)->after("{{")->before(".")->value; + if (!collect(['team', 'project', 'environment'])->contains($type)) { + $this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment."); + return; + } + } $this->dispatch('saveKey', [ 'key' => $this->key, 'value' => $this->value, diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 77644519a..0a9e73461 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -71,12 +71,26 @@ class All extends Component continue; } $found->value = $variable; + if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) { + $type = str($found->value)->after("{{")->before(".")->value; + if (!collect(['team', 'project', 'environment'])->contains($type)) { + $this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment."); + return; + } + } $found->save(); continue; } else { $environment = new EnvironmentVariable(); $environment->key = $key; $environment->value = $variable; + if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) { + $type = str($environment->value)->after("{{")->before(".")->value; + if (!collect(['team', 'project', 'environment'])->contains($type)) { + $this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment."); + return; + } + } $environment->is_build_time = false; $environment->is_preview = $isPreview ? true : false; switch ($this->resource->type()) { diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php index f8709afd8..b0c57e252 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -50,7 +50,8 @@ class Show extends Component $this->isLocked = true; } } - public function serialize() { + public function serialize() + { data_forget($this->env, 'real_value'); if ($this->env->getMorphClass() === 'App\Models\SharedEnvironmentVariable') { data_forget($this->env, 'is_build_time'); @@ -80,11 +81,18 @@ class Show extends Component } else { $this->validate(); } + if (str($this->env->value)->startsWith('{{') && str($this->env->value)->endsWith('}}')) { + $type = str($this->env->value)->after("{{")->before(".")->value; + if (!collect(['team', 'project', 'environment'])->contains($type)) { + $this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment."); + return; + } + } $this->serialize(); $this->env->save(); $this->dispatch('success', 'Environment variable updated successfully.'); $this->dispatch('refreshEnvs'); - } catch(\Exception $e) { + } catch (\Exception $e) { return handleError($e); } } diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php index 4ecc6699c..b5ecffaba 100644 --- a/app/Models/EnvironmentVariable.php +++ b/app/Models/EnvironmentVariable.php @@ -100,6 +100,9 @@ class EnvironmentVariable extends Model $variable = Str::after($environment_variable, "{$type}."); $variable = Str::before($variable, '}}'); $variable = Str::of($variable)->trim()->value; + if (!collect(['team', 'project', 'environment'])->contains($type)) { + return $variable; + } if ($type === 'environment') { $id = $resource->environment->id; } else if ($type === 'project') {