Replace all uses of `forceFill`, `forceCreate`, and `forceFill` with their non-force equivalents across models, actions, controllers, and Livewire components. Add explicit `$fillable` arrays to all affected Eloquent models to enforce mass assignment protection. Add ModelFillableCreationTest and ModelFillableRegressionTest to verify that model creation respects fillable constraints and prevent regressions.
103 lines
3.2 KiB
PHP
103 lines
3.2 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;
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
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;
|
|
|
|
#[Validate(['required', 'boolean'])]
|
|
public bool $isSwarm = false;
|
|
|
|
public function mount(?string $server_id = null)
|
|
{
|
|
$this->network = new Cuid2;
|
|
$this->servers = Server::isUsable()->get();
|
|
if ($server_id) {
|
|
$foundServer = $this->servers->find($server_id) ?: $this->servers->first();
|
|
if (! $foundServer) {
|
|
throw new \Exception('Server not found.');
|
|
}
|
|
$this->selectedServer = $foundServer;
|
|
$this->serverId = $this->selectedServer->id;
|
|
} else {
|
|
$foundServer = $this->servers->first();
|
|
if (! $foundServer) {
|
|
throw new \Exception('Server not found.');
|
|
}
|
|
$this->selectedServer = $foundServer;
|
|
$this->serverId = $this->selectedServer->id;
|
|
}
|
|
$this->generateName();
|
|
}
|
|
|
|
public function updatedServerId()
|
|
{
|
|
$this->selectedServer = $this->servers->find($this->serverId);
|
|
$this->generateName();
|
|
}
|
|
|
|
public function generateName()
|
|
{
|
|
$name = data_get($this->selectedServer, 'name', new Cuid2);
|
|
$this->name = str("{$name}-{$this->network}")->kebab();
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
try {
|
|
$this->authorize('create', StandaloneDocker::class);
|
|
$this->validate();
|
|
if ($this->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,
|
|
]);
|
|
}
|
|
}
|
|
redirectRoute($this, 'destination.show', [$docker->uuid]);
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
}
|