migrate settings from legacy model binding

This commit is contained in:
Andras Bacsai
2024-10-28 10:10:27 +01:00
parent 3cb6ba1a9d
commit 3d58b92a33
2 changed files with 104 additions and 81 deletions

View File

@@ -7,70 +7,93 @@ use App\Models\InstanceSettings;
use App\Models\Server; use App\Models\Server;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
class Index extends Component class Index extends Component
{ {
public InstanceSettings $settings; public InstanceSettings $settings;
public bool $do_not_track;
public bool $is_auto_update_enabled;
public bool $is_registration_enabled;
public bool $is_dns_validation_enabled;
public bool $is_api_enabled;
public string $auto_update_frequency;
public string $update_check_frequency;
public $timezones;
public bool $disable_two_step_confirmation;
protected string $dynamic_config_path = '/data/coolify/proxy/dynamic';
protected Server $server; protected Server $server;
protected $rules = [ #[Locked]
'settings.fqdn' => 'nullable', public $timezones;
'settings.resale_license' => 'nullable',
'settings.public_port_min' => 'required',
'settings.public_port_max' => 'required',
'settings.custom_dns_servers' => 'nullable',
'settings.instance_name' => 'nullable',
'settings.allowed_ips' => 'nullable',
'settings.is_auto_update_enabled' => 'boolean',
'settings.public_ipv4' => 'nullable',
'settings.public_ipv6' => 'nullable',
'auto_update_frequency' => 'string',
'update_check_frequency' => 'string',
'settings.instance_timezone' => 'required|string|timezone',
];
protected $validationAttributes = [ #[Rule('boolean')]
'settings.fqdn' => 'FQDN', public bool $is_auto_update_enabled;
'settings.resale_license' => 'Resale License',
'settings.public_port_min' => 'Public port min', #[Rule('nullable|string|max:255')]
'settings.public_port_max' => 'Public port max', public ?string $fqdn = null;
'settings.custom_dns_servers' => 'Custom DNS servers',
'settings.allowed_ips' => 'Allowed IPs', #[Rule('nullable|string|max:255')]
'settings.is_auto_update_enabled' => 'Auto Update Enabled', public ?string $resale_license = null;
'settings.public_ipv4' => 'IPv4',
'settings.public_ipv6' => 'IPv6', #[Rule('required|integer|min:1025|max:65535')]
'auto_update_frequency' => 'Auto Update Frequency', public int $public_port_min;
'update_check_frequency' => 'Update Check Frequency',
'settings.instance_timezone' => 'Instance Timezone', #[Rule('required|integer|min:1025|max:65535')]
]; public int $public_port_max;
#[Rule('nullable|string')]
public ?string $custom_dns_servers = null;
#[Rule('nullable|string|max:255')]
public ?string $instance_name = null;
#[Rule('nullable|string')]
public ?string $allowed_ips = null;
#[Rule('nullable|string')]
public ?string $public_ipv4 = null;
#[Rule('nullable|string')]
public ?string $public_ipv6 = null;
#[Rule('string')]
public string $auto_update_frequency;
#[Rule('string')]
public string $update_check_frequency;
#[Rule('required|string|timezone')]
public string $instance_timezone;
#[Rule('boolean')]
public bool $do_not_track;
#[Rule('boolean')]
public bool $is_registration_enabled;
#[Rule('boolean')]
public bool $is_dns_validation_enabled;
#[Rule('boolean')]
public bool $is_api_enabled;
#[Rule('boolean')]
public bool $disable_two_step_confirmation;
public function render()
{
return view('livewire.settings.index');
}
public function mount() public function mount()
{ {
if (isInstanceAdmin()) { if (! isInstanceAdmin()) {
return redirect()->route('dashboard');
} else {
$this->settings = instanceSettings(); $this->settings = instanceSettings();
loggy($this->settings); $this->fqdn = $this->settings->fqdn;
$this->resale_license = $this->settings->resale_license;
$this->public_port_min = $this->settings->public_port_min;
$this->public_port_max = $this->settings->public_port_max;
$this->custom_dns_servers = $this->settings->custom_dns_servers;
$this->instance_name = $this->settings->instance_name;
$this->allowed_ips = $this->settings->allowed_ips;
$this->public_ipv4 = $this->settings->public_ipv4;
$this->public_ipv6 = $this->settings->public_ipv6;
$this->do_not_track = $this->settings->do_not_track; $this->do_not_track = $this->settings->do_not_track;
$this->is_auto_update_enabled = $this->settings->is_auto_update_enabled; $this->is_auto_update_enabled = $this->settings->is_auto_update_enabled;
$this->is_registration_enabled = $this->settings->is_registration_enabled; $this->is_registration_enabled = $this->settings->is_registration_enabled;
@@ -79,14 +102,22 @@ class Index extends Component
$this->auto_update_frequency = $this->settings->auto_update_frequency; $this->auto_update_frequency = $this->settings->auto_update_frequency;
$this->update_check_frequency = $this->settings->update_check_frequency; $this->update_check_frequency = $this->settings->update_check_frequency;
$this->timezones = collect(timezone_identifiers_list())->sort()->values()->toArray(); $this->timezones = collect(timezone_identifiers_list())->sort()->values()->toArray();
$this->instance_timezone = $this->settings->instance_timezone;
$this->disable_two_step_confirmation = $this->settings->disable_two_step_confirmation; $this->disable_two_step_confirmation = $this->settings->disable_two_step_confirmation;
} else {
return redirect()->route('dashboard');
} }
} }
public function instantSave() public function instantSave($isSave = true)
{ {
$this->settings->fqdn = $this->fqdn;
$this->settings->resale_license = $this->resale_license;
$this->settings->public_port_min = $this->public_port_min;
$this->settings->public_port_max = $this->public_port_max;
$this->settings->custom_dns_servers = $this->custom_dns_servers;
$this->settings->instance_name = $this->instance_name;
$this->settings->allowed_ips = $this->allowed_ips;
$this->settings->public_ipv4 = $this->public_ipv4;
$this->settings->public_ipv6 = $this->public_ipv6;
$this->settings->do_not_track = $this->do_not_track; $this->settings->do_not_track = $this->do_not_track;
$this->settings->is_auto_update_enabled = $this->is_auto_update_enabled; $this->settings->is_auto_update_enabled = $this->is_auto_update_enabled;
$this->settings->is_registration_enabled = $this->is_registration_enabled; $this->settings->is_registration_enabled = $this->is_registration_enabled;
@@ -95,9 +126,12 @@ class Index extends Component
$this->settings->auto_update_frequency = $this->auto_update_frequency; $this->settings->auto_update_frequency = $this->auto_update_frequency;
$this->settings->update_check_frequency = $this->update_check_frequency; $this->settings->update_check_frequency = $this->update_check_frequency;
$this->settings->disable_two_step_confirmation = $this->disable_two_step_confirmation; $this->settings->disable_two_step_confirmation = $this->disable_two_step_confirmation;
$this->settings->instance_timezone = $this->instance_timezone;
if ($isSave) {
$this->settings->save(); $this->settings->save();
$this->dispatch('success', 'Settings updated!'); $this->dispatch('success', 'Settings updated!');
} }
}
public function submit() public function submit()
{ {
@@ -153,13 +187,8 @@ class Index extends Component
$this->settings->allowed_ips = $this->settings->allowed_ips->unique(); $this->settings->allowed_ips = $this->settings->allowed_ips->unique();
$this->settings->allowed_ips = $this->settings->allowed_ips->implode(','); $this->settings->allowed_ips = $this->settings->allowed_ips->implode(',');
$this->settings->do_not_track = $this->do_not_track; $this->instantSave(isSave: false);
$this->settings->is_auto_update_enabled = $this->is_auto_update_enabled;
$this->settings->is_registration_enabled = $this->is_registration_enabled;
$this->settings->is_dns_validation_enabled = $this->is_dns_validation_enabled;
$this->settings->is_api_enabled = $this->is_api_enabled;
$this->settings->auto_update_frequency = $this->auto_update_frequency;
$this->settings->update_check_frequency = $this->update_check_frequency;
$this->settings->save(); $this->settings->save();
$this->server->setupDynamicProxyConfiguration(); $this->server->setupDynamicProxyConfiguration();
if (! $error_show) { if (! $error_show) {
@@ -182,11 +211,6 @@ class Index extends Component
} }
} }
public function render()
{
return view('livewire.settings.index');
}
public function toggleTwoStepConfirmation($password) public function toggleTwoStepConfirmation($password)
{ {
if (! Hash::check($password, Auth::user()->password)) { if (! Hash::check($password, Auth::user()->password)) {
@@ -195,9 +219,8 @@ class Index extends Component
return; return;
} }
$this->settings->disable_two_step_confirmation = true; $this->settings->disable_two_step_confirmation = $this->disable_two_step_confirmation = true;
$this->settings->save(); $this->settings->save();
$this->disable_two_step_confirmation = true;
$this->dispatch('success', 'Two step confirmation has been disabled.'); $this->dispatch('success', 'Two step confirmation has been disabled.');
} }
} }

View File

@@ -16,10 +16,10 @@
<h4 class="pt-6">Instance Settings</h4> <h4 class="pt-6">Instance Settings</h4>
<div class="flex flex-wrap items-end gap-2"> <div class="flex flex-wrap items-end gap-2">
<div class="flex gap-2 md:flex-row flex-col w-full"> <div class="flex gap-2 md:flex-row flex-col w-full">
<x-forms.input id="settings.fqdn" label="Instance's Domain" <x-forms.input id="fqdn" label="Instance's Domain"
helper="Enter the full domain name (FQDN) of the instance, including 'https://' if you want to secure the dashboard with HTTPS. Setting this will make the dashboard accessible via this domain, secured by HTTPS, instead of just the IP address." helper="Enter the full domain name (FQDN) of the instance, including 'https://' if you want to secure the dashboard with HTTPS. Setting this will make the dashboard accessible via this domain, secured by HTTPS, instead of just the IP address."
placeholder="https://coolify.yourdomain.com" /> placeholder="https://coolify.yourdomain.com" />
<x-forms.input id="settings.instance_name" label="Instance's Name" placeholder="Coolify" /> <x-forms.input id="instance_name" label="Instance's Name" placeholder="Coolify" />
<div class="w-full" x-data="{ <div class="w-full" x-data="{
open: false, open: false,
search: '{{ $settings->instance_timezone ?: '' }}', search: '{{ $settings->instance_timezone ?: '' }}',
@@ -34,7 +34,7 @@
} }
}"> }">
<div class="flex items-center mb-1"> <div class="flex items-center mb-1">
<label for="settings.instance_timezone">Instance <label for="instance_timezone">Instance
Timezone</label> Timezone</label>
<x-helper class="ml-2" <x-helper class="ml-2"
helper="Timezone for the Coolify instance. This is used for the update check and automatic update frequency." /> helper="Timezone for the Coolify instance. This is used for the update check and automatic update frequency." />
@@ -46,7 +46,7 @@
wire:dirty.class="dark:focus:ring-warning dark:ring-warning" x-model="search" wire:dirty.class="dark:focus:ring-warning dark:ring-warning" x-model="search"
@focus="open = true" @click.away="open = false" @input="open = true" @focus="open = true" @click.away="open = false" @input="open = true"
class="w-full input " :placeholder="placeholder" class="w-full input " :placeholder="placeholder"
wire:model.debounce.300ms="settings.instance_timezone"> wire:model.debounce.300ms="instance_timezone">
<svg class="absolute right-0 w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" <svg class="absolute right-0 w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg"
fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
@click="open = true"> @click="open = true">
@@ -59,7 +59,7 @@
<template <template
x-for="timezone in timezones.filter(tz => tz.toLowerCase().includes(search.toLowerCase()))" x-for="timezone in timezones.filter(tz => tz.toLowerCase().includes(search.toLowerCase()))"
:key="timezone"> :key="timezone">
<div @click="search = timezone; open = false; $wire.set('settings.instance_timezone', timezone)" <div @click="search = timezone; open = false; $wire.set('instance_timezone', timezone)"
class="px-4 py-2 cursor-pointer hover:bg-gray-100 dark:hover:bg-coolgray-300 text-gray-800 dark:text-gray-200" class="px-4 py-2 cursor-pointer hover:bg-gray-100 dark:hover:bg-coolgray-300 text-gray-800 dark:text-gray-200"
x-text="timezone"></div> x-text="timezone"></div>
</template> </template>
@@ -69,10 +69,10 @@
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<x-forms.input id="settings.public_ipv4" type="password" label="Instance's IPv4" <x-forms.input id="public_ipv4" type="password" label="Instance's IPv4"
helper="Enter the IPv4 address of the instance.<br><br>It is useful if you have several IPv4 addresses and Coolify could not detect the correct one." helper="Enter the IPv4 address of the instance.<br><br>It is useful if you have several IPv4 addresses and Coolify could not detect the correct one."
placeholder="1.2.3.4" /> placeholder="1.2.3.4" />
<x-forms.input id="settings.public_ipv6" type="password" label="Instance's IPv6" <x-forms.input id="public_ipv6" type="password" label="Instance's IPv6"
helper="Enter the IPv6 address of the instance.<br><br>It is useful if you have several IPv6 addresses and Coolify could not detect the correct one." helper="Enter the IPv6 address of the instance.<br><br>It is useful if you have several IPv6 addresses and Coolify could not detect the correct one."
placeholder="2001:db8::1" /> placeholder="2001:db8::1" />
</div> </div>
@@ -80,14 +80,14 @@
<div class="md:w-96"> <div class="md:w-96">
<x-forms.checkbox instantSave id="is_dns_validation_enabled" label="Enabled" /> <x-forms.checkbox instantSave id="is_dns_validation_enabled" label="Enabled" />
</div> </div>
<x-forms.input id="settings.custom_dns_servers" label="DNS Servers" <x-forms.input id="custom_dns_servers" label="DNS Servers"
helper="DNS servers to validate FQDNs against. A comma separated list of DNS servers." helper="DNS servers to validate FQDNs against. A comma separated list of DNS servers."
placeholder="1.1.1.1,8.8.8.8" /> placeholder="1.1.1.1,8.8.8.8" />
</div> </div>
{{-- <div class="flex gap-2 "> {{-- <div class="flex gap-2 ">
<x-forms.input type="number" id="settings.public_port_min" label="Public Port Min" /> <x-forms.input type="number" id="public_port_min" label="Public Port Min" />
<x-forms.input type="number" id="settings.public_port_max" label="Public Port Max" /> <x-forms.input type="number" id="public_port_max" label="Public Port Max" />
</div> --}} </div> --}}
</div> </div>
@@ -95,7 +95,7 @@
<div class="md:w-96 pb-2"> <div class="md:w-96 pb-2">
<x-forms.checkbox instantSave id="is_api_enabled" label="Enabled" /> <x-forms.checkbox instantSave id="is_api_enabled" label="Enabled" />
</div> </div>
<x-forms.input id="settings.allowed_ips" label="Allowed IPs" <x-forms.input id="allowed_ips" label="Allowed IPs"
helper="Allowed IP lists for the API. A comma separated list of IPs. Empty means you allow from everywhere." helper="Allowed IP lists for the API. A comma separated list of IPs. Empty means you allow from everywhere."
placeholder="1.1.1.1,8.8.8.8" /> placeholder="1.1.1.1,8.8.8.8" />