feat: add database transactions and component-level authorization to shared variables

This commit is contained in:
Aditya Tripathi 2025-11-03 20:54:34 +00:00
parent 2e6b8a69f8
commit 28cb561c04
6 changed files with 66 additions and 75 deletions

View file

@ -5,6 +5,7 @@
use App\Models\Application;
use App\Models\Project;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Show extends Component
@ -98,23 +99,26 @@ public function submit()
private function handleBulkSubmit()
{
$variables = parseEnvFormatToArray($this->variables);
$changesMade = false;
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
DB::transaction(function () use ($variables) {
$changesMade = false;
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
});
}
private function deleteRemovedVariables($variables)

View file

@ -4,6 +4,7 @@
use App\Models\Project;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Show extends Component
@ -95,23 +96,26 @@ public function submit()
private function handleBulkSubmit()
{
$variables = parseEnvFormatToArray($this->variables);
$changesMade = false;
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
DB::transaction(function () use ($variables) {
$changesMade = false;
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
});
}
private function deleteRemovedVariables($variables)

View file

@ -4,6 +4,7 @@
use App\Models\Team;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Index extends Component
@ -89,23 +90,26 @@ public function submit()
private function handleBulkSubmit()
{
$variables = parseEnvFormatToArray($this->variables);
$changesMade = false;
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
DB::transaction(function () use ($variables) {
$changesMade = false;
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
// Delete removed variables
$deletedCount = $this->deleteRemovedVariables($variables);
if ($deletedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
// Update or create variables
$updatedCount = $this->updateOrCreateVariables($variables);
if ($updatedCount > 0) {
$changesMade = true;
}
if ($changesMade) {
$this->dispatch('success', 'Environment variables updated.');
}
});
}
private function deleteRemovedVariables($variables)

View file

@ -9,9 +9,7 @@
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
@endcan
@can('update', $environment)
<x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
@endcan
<x-forms.button canGate="update" :canResource="$environment" wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div>
<div class="flex items-center gap-1 subtitle">You can use these variables anywhere with <span
class="dark:text-warning text-coollabs">@{{ environment.VARIABLENAME }}</span><x-helper
@ -28,14 +26,9 @@ class="dark:text-warning text-coollabs">@{{ environment.VARIABLENAME }}</span><x
</div>
@else
<form wire:submit='submit' class="flex flex-col gap-2">
@can('update', $environment)
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Environment Shared Variables"></x-forms.textarea>
<x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
@else
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Environment Shared Variables" disabled></x-forms.textarea>
@endcan
<x-forms.textarea canGate="update" :canResource="$environment" rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Environment Shared Variables"></x-forms.textarea>
<x-forms.button canGate="update" :canResource="$environment" type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
</form>
@endif
</div>

View file

@ -9,9 +9,7 @@
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
@endcan
@can('update', $project)
<x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
@endcan
<x-forms.button canGate="update" :canResource="$project" wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div>
<div class="flex flex-wrap gap-1 subtitle">
<div>You can use these variables anywhere with</div>
@ -30,14 +28,9 @@
</div>
@else
<form wire:submit='submit' class="flex flex-col gap-2">
@can('update', $project)
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Project Shared Variables"></x-forms.textarea>
<x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
@else
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Project Shared Variables" disabled></x-forms.textarea>
@endcan
<x-forms.textarea canGate="update" :canResource="$project" rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Project Shared Variables"></x-forms.textarea>
<x-forms.button canGate="update" :canResource="$project" type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
</form>
@endif
</div>

View file

@ -9,9 +9,7 @@
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
@endcan
@can('update', $team)
<x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
@endcan
<x-forms.button canGate="update" :canResource="$team" wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div>
<div class="flex items-center gap-1 subtitle">You can use these variables anywhere with <span
class="dark:text-warning text-coollabs">@{{ team.VARIABLENAME }}</span> <x-helper
@ -29,14 +27,9 @@ class="dark:text-warning text-coollabs">@{{ team.VARIABLENAME }}</span> <x-helpe
</div>
@else
<form wire:submit='submit' class="flex flex-col gap-2">
@can('update', $team)
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Team Shared Variables"></x-forms.textarea>
<x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
@else
<x-forms.textarea rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Team Shared Variables" disabled></x-forms.textarea>
@endcan
<x-forms.textarea canGate="update" :canResource="$team" rows="20" class="whitespace-pre-wrap" id="variables" wire:model="variables"
label="Team Shared Variables"></x-forms.textarea>
<x-forms.button canGate="update" :canResource="$team" type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
</form>
@endif
</div>