From a2c0b68730a615ef8796c41eac237721972db9f4 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:38:20 +0100 Subject: [PATCH 1/5] fix validation --- app/Livewire/Server/New/ByIp.php | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/app/Livewire/Server/New/ByIp.php b/app/Livewire/Server/New/ByIp.php index f80152435..20736160d 100644 --- a/app/Livewire/Server/New/ByIp.php +++ b/app/Livewire/Server/New/ByIp.php @@ -6,64 +6,60 @@ use App\Enums\ProxyTypes; use App\Models\Server; use App\Models\Team; use Illuminate\Support\Collection; +use Livewire\Attributes\Locked; +use Livewire\Attributes\Validate; use Livewire\Component; class ByIp extends Component { + #[Locked] public $private_keys; + #[Locked] public $limit_reached; + #[Validate('nullable|integer', as: 'Private Key')] public ?int $private_key_id = null; + #[Validate('nullable|string|max:255', as: 'Private Key Name')] public $new_private_key_name; + #[Validate('nullable|string', as: 'Private Key Description')] public $new_private_key_description; + #[Validate('nullable|string', as: 'Private Key Value')] public $new_private_key_value; + #[Validate('required|string|max:255', as: 'Name')] public string $name; + #[Validate('nullable|string|max:255', as: 'Description')] public ?string $description = null; + #[Validate('required|string', as: 'IP Address/Domain')] public string $ip; + #[Validate('required|string|max:255', as: 'User')] public string $user = 'root'; + #[Validate('required|integer|between:1,65535', as: 'Port')] public int $port = 22; + #[Validate('required|boolean', as: 'Swarm Manager')] public bool $is_swarm_manager = false; + #[Validate('required|boolean', as: 'Swarm Worker')] public bool $is_swarm_worker = false; + #[Validate('nullable|integer', as: 'Swarm Cluster')] public $selected_swarm_cluster = null; + #[Validate('required|boolean', as: 'Build Server')] public bool $is_build_server = false; + #[Locked] public Collection $swarm_managers; - protected $rules = [ - 'name' => 'required|string', - 'description' => 'nullable|string', - 'ip' => 'required', - 'user' => 'required|string', - 'port' => 'required|integer', - 'is_swarm_manager' => 'required|boolean', - 'is_swarm_worker' => 'required|boolean', - 'is_build_server' => 'required|boolean', - ]; - - protected $validationAttributes = [ - 'name' => 'Name', - 'description' => 'Description', - 'ip' => 'IP Address/Domain', - 'user' => 'User', - 'port' => 'Port', - 'is_swarm_manager' => 'Swarm Manager', - 'is_swarm_worker' => 'Swarm Worker', - 'is_build_server' => 'Build Server', - ]; - public function mount() { $this->name = generate_random_name(); @@ -88,6 +84,12 @@ class ByIp extends Component { $this->validate(); try { + if (Server::where('team_id', currentTeam()->id) + ->where('ip', $this->ip) + ->exists()) { + return $this->dispatch('error', 'This IP/Domain is already in use by another server in your team.'); + } + if (is_null($this->private_key_id)) { return $this->dispatch('error', 'You must select a private key'); } From ca4b91c51f72096582d193b33ba6a4b4e1d4c4b1 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:48:14 +0100 Subject: [PATCH 2/5] Update ByIp.php --- app/Livewire/Server/New/ByIp.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Livewire/Server/New/ByIp.php b/app/Livewire/Server/New/ByIp.php index 20736160d..5f60c5db5 100644 --- a/app/Livewire/Server/New/ByIp.php +++ b/app/Livewire/Server/New/ByIp.php @@ -21,7 +21,7 @@ class ByIp extends Component #[Validate('nullable|integer', as: 'Private Key')] public ?int $private_key_id = null; - #[Validate('nullable|string|max:255', as: 'Private Key Name')] + #[Validate('nullable|string', as: 'Private Key Name')] public $new_private_key_name; #[Validate('nullable|string', as: 'Private Key Description')] @@ -30,16 +30,16 @@ class ByIp extends Component #[Validate('nullable|string', as: 'Private Key Value')] public $new_private_key_value; - #[Validate('required|string|max:255', as: 'Name')] + #[Validate('required|string', as: 'Name')] public string $name; - #[Validate('nullable|string|max:255', as: 'Description')] + #[Validate('nullable|string', as: 'Description')] public ?string $description = null; #[Validate('required|string', as: 'IP Address/Domain')] public string $ip; - #[Validate('required|string|max:255', as: 'User')] + #[Validate('required|string', as: 'User')] public string $user = 'root'; #[Validate('required|integer|between:1,65535', as: 'Port')] From 465dfbdf530bc7b09e62344ca435a5b1189a2eb6 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:48:35 +0100 Subject: [PATCH 3/5] fix check if IP is already in used in this team --- app/Livewire/Server/Show.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/Livewire/Server/Show.php b/app/Livewire/Server/Show.php index 5a1743f96..a5544489d 100644 --- a/app/Livewire/Server/Show.php +++ b/app/Livewire/Server/Show.php @@ -107,6 +107,15 @@ class Show extends Component { if ($toModel) { $this->validate(); + + if (Server::where('team_id', currentTeam()->id) + ->where('ip', $this->ip) + ->where('id', '!=', $this->server->id) + ->exists()) { + $this->ip = $this->server->ip; + throw new \Exception('This IP/Domain is already in use by another server in your team.'); + } + $this->server->name = $this->name; $this->server->description = $this->description; $this->server->ip = $this->ip; From b18d4b95642c462c75ebfbdb502cab93561b909d Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:51:58 +0100 Subject: [PATCH 4/5] dispatch an error message --- app/Livewire/Server/Show.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Livewire/Server/Show.php b/app/Livewire/Server/Show.php index a5544489d..243855e9d 100644 --- a/app/Livewire/Server/Show.php +++ b/app/Livewire/Server/Show.php @@ -113,7 +113,8 @@ class Show extends Component ->where('id', '!=', $this->server->id) ->exists()) { $this->ip = $this->server->ip; - throw new \Exception('This IP/Domain is already in use by another server in your team.'); + + return $this->dispatch('error', 'This IP/Domain is already in use by another server in your team.'); } $this->server->name = $this->name; From 0a36fbddec7e339ee41f1d4065ccd3d8dabe7fda Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:52:54 +0100 Subject: [PATCH 5/5] Revert "dispatch an error message" This reverts commit b18d4b95642c462c75ebfbdb502cab93561b909d. --- app/Livewire/Server/Show.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Livewire/Server/Show.php b/app/Livewire/Server/Show.php index 243855e9d..a5544489d 100644 --- a/app/Livewire/Server/Show.php +++ b/app/Livewire/Server/Show.php @@ -113,8 +113,7 @@ class Show extends Component ->where('id', '!=', $this->server->id) ->exists()) { $this->ip = $this->server->ip; - - return $this->dispatch('error', 'This IP/Domain is already in use by another server in your team.'); + throw new \Exception('This IP/Domain is already in use by another server in your team.'); } $this->server->name = $this->name;