Files
coolify/app/Livewire/Destination/New/Docker.php
T
Andras Bacsai 88efc0716e fix: enforce resource team isolation and preserve ClickHouse data
Scope resource permissions and related records to the owning team across
team switches, cloning, deployments, and resource operations.

Merge ClickHouse volume mounts, preserve anonymous data volumes on stop,
and require adopting current data before starting or restarting.

Fix file storage deletion checks and add regression coverage.
2026-10-05 14:23:04 +02:00

115 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Destination\New;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
class Docker extends Component
{
use AuthorizesRequests;
#[Locked]
public $servers;
#[Locked]
public Server $selectedServer;
#[Validate(['required', 'string'])]
public string $name;
#[Validate(['required', 'string', 'max:255', 'regex:/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/'])]
public string $network;
#[Validate(['required', 'string'])]
public string $serverId;
public function mount(?string $server_id = null): void
{
$this->network = new_public_id();
$this->servers = Server::isUsable()->get();
if (filled($server_id)) {
$this->selectedServer = Server::ownedByCurrentTeam()->whereKey($server_id)->firstOrFail();
if (! $this->servers->contains('id', $this->selectedServer->id)) {
$this->servers->push($this->selectedServer);
}
$this->serverId = (string) $this->selectedServer->id;
} else {
$foundServer = $this->servers->first();
if (! $foundServer) {
throw new \Exception('Server not found.');
}
$this->selectedServer = $foundServer;
$this->serverId = (string) $this->selectedServer->id;
}
$this->generateName();
}
public function updatedServerId(): void
{
$this->selectedServer = $this->servers->find($this->serverId);
if (! $this->selectedServer) {
throw new \Exception('Server not found.');
}
$this->generateName();
}
public function generateName(): void
{
$name = data_get($this->selectedServer, 'name', new_public_id());
$this->name = str("{$name}-{$this->network}")->kebab();
}
public function submit(): mixed
{
try {
$isSwarm = $this->selectedServer->isSwarm();
$this->authorize('update', $this->selectedServer);
$this->validate();
if ($isSwarm) {
$found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first();
if ($found) {
throw new \Exception('Network already added to this server.');
} else {
$docker = SwarmDocker::create([
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->selectedServer->id,
]);
}
} else {
$found = $this->selectedServer->standaloneDockers()->where('network', $this->network)->first();
if ($found) {
throw new \Exception('Network already added to this server.');
} else {
$docker = StandaloneDocker::create([
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->selectedServer->id,
]);
}
}
auditLog('ui.destination.created', [
'team_id' => $this->selectedServer->team_id,
'destination_uuid' => $docker->uuid,
'destination_name' => $docker->name,
'destination_type' => $isSwarm ? 'swarm' : 'standalone',
'server_uuid' => $this->selectedServer->uuid,
]);
return redirectRoute($this, 'destination.show', [$docker->uuid]);
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
}