mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-14 08:42:38 -04:00
- Check read access on the project or environment for shared variables pages, the canvas, and the sidebar, not only navigation access - Require team-wide read for the server shared variables canvas API - Open the environment variable editor read-only when the value is hidden - Show the integration tokens button only to users who can open it - Show disabled confirmation buttons as disabled, not as danger buttons
186 lines
5.9 KiB
PHP
186 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\SharedVariables\Project;
|
|
|
|
use App\Livewire\Concerns\RemembersCanvasView;
|
|
use App\Models\Project;
|
|
use App\Models\SharedEnvironmentVariable;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Component;
|
|
|
|
class Show extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
use RemembersCanvasView;
|
|
|
|
public Project $project;
|
|
|
|
public string $view = 'normal';
|
|
|
|
public ?string $variables = null;
|
|
|
|
protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];
|
|
|
|
public function saveKey($data)
|
|
{
|
|
try {
|
|
$this->authorize('update', $this->project);
|
|
|
|
$found = $this->project->environment_variables()->where('key', $data['key'])->first();
|
|
if ($found) {
|
|
throw new \Exception('Variable already exists.');
|
|
}
|
|
$this->project->environment_variables()->create([
|
|
'key' => $data['key'],
|
|
'value' => $data['value'],
|
|
'is_multiline' => $data['is_multiline'],
|
|
'is_literal' => $data['is_literal'],
|
|
'comment' => $data['comment'] ?? null,
|
|
'type' => 'project',
|
|
'team_id' => $this->project->team_id,
|
|
]);
|
|
$this->project->refresh();
|
|
$this->getDevView();
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function mount(?string $project_uuid = null)
|
|
{
|
|
$projectUuid = $project_uuid ?? request()->route('project_uuid');
|
|
$teamId = currentTeam()->id;
|
|
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
|
|
if (! $project) {
|
|
return redirect()->route('dashboard');
|
|
}
|
|
$this->authorize('viewSharedVariables', $project);
|
|
$this->project = $project;
|
|
if ($this->prefersCanvasView('project_variables')) {
|
|
return $this->redirectRoute('shared-variables.project.canvas', ['project_uuid' => $project->uuid], navigate: true);
|
|
}
|
|
$this->getDevView();
|
|
}
|
|
|
|
public function switch()
|
|
{
|
|
try {
|
|
$this->authorize('viewSharedVariables', $this->project);
|
|
$this->view = $this->view === 'normal' ? 'dev' : 'normal';
|
|
$this->getDevView();
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function getDevView()
|
|
{
|
|
$this->variables = $this->formatEnvironmentVariables($this->project->environment_variables->sortBy('key'));
|
|
}
|
|
|
|
private function formatEnvironmentVariables($variables)
|
|
{
|
|
$canViewValues = auth()->user()?->isAdminOfTeam($this->project->team_id) ?? false;
|
|
|
|
return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
|
try {
|
|
$this->authorize('update', $this->project);
|
|
$variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
|
|
try {
|
|
$this->handleBulkSubmit($variables);
|
|
$this->getDevView();
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
} finally {
|
|
$this->refreshEnvs();
|
|
}
|
|
}
|
|
|
|
private function handleBulkSubmit(array $variables)
|
|
{
|
|
$changesMade = DB::transaction(function () use ($variables) {
|
|
// Delete removed variables
|
|
$deletedCount = $this->deleteRemovedVariables($variables);
|
|
|
|
// Update or create variables
|
|
$updatedCount = $this->updateOrCreateVariables($variables);
|
|
|
|
return $deletedCount > 0 || $updatedCount > 0;
|
|
});
|
|
|
|
if ($changesMade) {
|
|
$this->dispatch('success', 'Environment variables updated.');
|
|
}
|
|
}
|
|
|
|
private function deleteRemovedVariables($variables)
|
|
{
|
|
$variablesToDelete = $this->project->environment_variables()->whereNotIn('key', array_keys($variables))->get();
|
|
|
|
if ($variablesToDelete->isEmpty()) {
|
|
return 0;
|
|
}
|
|
|
|
$this->project->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
|
|
|
|
return $variablesToDelete->count();
|
|
}
|
|
|
|
private function updateOrCreateVariables($variables)
|
|
{
|
|
$count = 0;
|
|
foreach ($variables as $key => $data) {
|
|
$value = is_array($data) ? ($data['value'] ?? '') : $data;
|
|
|
|
$found = $this->project->environment_variables()->where('key', $key)->first();
|
|
|
|
if ($found) {
|
|
if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
|
|
if ($found->value !== $value) {
|
|
$found->value = $value;
|
|
}
|
|
$found->fill($data['settings']);
|
|
if ($found->isDirty()) {
|
|
$found->save();
|
|
$count++;
|
|
}
|
|
}
|
|
} else {
|
|
$this->project->environment_variables()->create([
|
|
'key' => $key,
|
|
'value' => $value,
|
|
'is_multiline' => false,
|
|
'is_literal' => false,
|
|
...$data['settings'],
|
|
'type' => 'project',
|
|
'team_id' => $this->project->team_id,
|
|
]);
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
public function refreshEnvs()
|
|
{
|
|
$this->project->refresh();
|
|
$this->getDevView();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.shared-variables.project.show');
|
|
}
|
|
}
|