Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LegacySettingsMigrations #941

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/Classes/LegacySettingsMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Classes;

use Exception;
use Illuminate\Support\Facades\DB;
use Spatie\LaravelSettings\Migrations\SettingsMigration;


abstract class LegacySettingsMigration extends SettingsMigration
{
public function getNewValue(string $name, string $group)
{
$new_value = DB::table('settings')->where([['group', '=', $group], ['name', '=', $name]])->get(['payload'])->first();

if (is_null($new_value) || is_null($new_value->payload)) {
return null;
}

// Some keys returns '""' as a value.
if ($new_value->payload === '""') {
return null;
}


// remove the quotes from the string
if (substr($new_value->payload, 0, 1) === '"' && substr($new_value->payload, -1) === '"') {
return substr($new_value->payload, 1, -1);
}

return $new_value->payload;
}

/**
* Get the old value from the settings_old table.
* @param string $key The key to get the value from table.
* @param int|string|bool|null $default The default value to return if the value is null. If value is not nullable, a default must be provided.
*/
public function getOldValue(string $key, int|string|bool|null $default = null)
{
$old_value = DB::table('settings_old')->where('key', '=', $key)->get(['value', 'type'])->first();

if (is_null($old_value) || is_null($old_value->value)) {
return $default;
}

switch ($old_value->type) {
case 'string':
case 'text':
// Edgecase: The value is a boolean, but it's stored as a string.
if ($old_value->value === "false" || $old_value->value === "true") {
return filter_var($old_value->value, FILTER_VALIDATE_BOOL);
}
return $old_value->value;
case 'boolean':
return filter_var($old_value->value, FILTER_VALIDATE_BOOL);
case 'integer':
return filter_var($old_value->value, FILTER_VALIDATE_INT);
default:
throw new Exception("Unknown type: {$old_value->type}");
}
}
}
3 changes: 0 additions & 3 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use App\Models\Pterodactyl\Node;
use App\Models\Product;
use App\Models\Server;
use App\Models\User;
use App\Models\Settings;
use App\Notifications\ServerCreationError;
use Carbon\Carbon;
use App\Settings\UserSettings;
Expand All @@ -18,7 +16,6 @@
use App\Classes\PterodactylClient;
use App\Settings\GeneralSettings;
use Exception;
use GuzzleHttp\Promise\Create;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Client\Response;
use Illuminate\Http\RedirectResponse;
Expand Down
28 changes: 15 additions & 13 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Providers;

use App\Extensions\PaymentGateways\PayPal\PayPalSettings;
use App\Models\UsefulLink;
use App\Settings\GeneralSettings;
use App\Settings\MailSettings;
Expand Down Expand Up @@ -89,20 +88,23 @@ public function boot()
Log::error("Couldnt find useful_links. Probably the installation is not completet. " . $e);
}

$generalSettings = $this->app->make(GeneralSettings::class);
if (!file_exists(base_path('themes') . "/" . $generalSettings->theme)) {
$generalSettings->theme = "default";
}

if ($generalSettings->theme && $generalSettings->theme !== config('theme.active')) {
Theme::set($generalSettings->theme, "default");
} else {
Theme::set("default", "default");
}

try {
$generalSettings = $this->app->make(GeneralSettings::class);
if (!file_exists(base_path('themes') . "/" . $generalSettings->theme)) {
$generalSettings->theme = "default";
}

$settings = $this->app->make(MailSettings::class);
$settings->setConfig();
if ($generalSettings->theme && $generalSettings->theme !== config('theme.active')) {
Theme::set($generalSettings->theme, "default");
} else {
Theme::set("default", "default");
}

$settings = $this->app->make(MailSettings::class);
$settings->setConfig();
} catch (Exception $e) {
Log::error("Couldnt load Settings. Probably the installation is not completet. " . $e);
}
}
}
2 changes: 0 additions & 2 deletions app/Settings/DiscordSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public static function group(): string
return 'discord';
}



/**
* Summary of validations array
* @return array<string, string>
Expand Down
7 changes: 6 additions & 1 deletion app/Settings/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public static function group(): string
return 'mail';
}


public static function encrypted(): array
{
return [
'mail_password',
];
}

public function setConfig()
{
Expand Down
8 changes: 7 additions & 1 deletion app/Settings/PterodactylSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public static function group(): string
return 'pterodactyl';
}


public static function encrypted(): array
{
return [
'admin_token',
'user_token',
];
}

/**
* Get url with ensured ending backslash
Expand Down
2 changes: 1 addition & 1 deletion config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
UserSettings::class,
WebsiteSettings::class,
TicketSettings::class,
CouponSettings::class,
CouponSettings::class,
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function up()
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->decimal('price', ['11', '2'])->change();
$table->decimal('credits', ['11', '2'])->change();
});
}
}
102 changes: 31 additions & 71 deletions database/settings/2023_02_01_164731_create_general_settings.php
Original file line number Diff line number Diff line change
@@ -1,139 +1,99 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;
use App\Classes\LegacySettingsMigration;
use Illuminate\Support\Facades\DB;

class CreateGeneralSettings extends SettingsMigration
class CreateGeneralSettings extends LegacySettingsMigration
{
public function up(): void
{
$table_exists = DB::table('settings_old')->exists();

// Get the user-set configuration values from the old table.
$this->migrator->add('general.store_enabled', true);
$this->migrator->add('general.credits_display_name', $table_exists ? $this->getOldValue('SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME') : 'Credits');
$this->migrator->add('general.credits_display_name', $table_exists ? $this->getOldValue('SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME', 'Credits') : 'Credits');
$this->migrator->add('general.recaptcha_site_key', $table_exists ? $this->getOldValue("SETTINGS::RECAPTCHA:SITE_KEY") : env('RECAPTCHA_SITE_KEY', '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'));
$this->migrator->add('general.recaptcha_secret_key', $table_exists ? $this->getOldValue("SETTINGS::RECAPTCHA:SECRET_KEY") : env('RECAPTCHA_SECRET_KEY', '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe'));
$this->migrator->add('general.recaptcha_enabled', $table_exists ? $this->getOldValue("SETTINGS::RECAPTCHA:ENABLED") : true);
$this->migrator->add('general.phpmyadmin_url', $table_exists ? $this->getOldValue("SETTINGS::MISC:PHPMYADMIN:URL") : env('PHPMYADMIN_URL', ''));
$this->migrator->add('general.alert_enabled', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_ENABLED") : false);
$this->migrator->add('general.alert_type', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_TYPE") : 'dark');
$this->migrator->add('general.alert_message', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_MESSAGE") : '');
$this->migrator->add('general.theme', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:THEME") : 'default');
$this->migrator->add('general.recaptcha_enabled', $table_exists ? $this->getOldValue("SETTINGS::RECAPTCHA:ENABLED", false) : false);
$this->migrator->add('general.phpmyadmin_url', $table_exists ? $this->getOldValue("SETTINGS::MISC:PHPMYADMIN:URL") : env('PHPMYADMIN_URL'));
$this->migrator->add('general.alert_enabled', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_ENABLED", false) : false);
$this->migrator->add('general.alert_type', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_TYPE", 'dark') : 'dark');
$this->migrator->add('general.alert_message', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:ALERT_MESSAGE") : null);
$this->migrator->add('general.theme', $table_exists ? $this->getOldValue("SETTINGS::SYSTEM:THEME", 'default') : 'default');
}

public function down(): void
{
DB::table('settings_old')->insert([
[
'key' => 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME',
'value' => $this->getNewValue('credits_display_name'),
'value' => $this->getNewValue('credits_display_name', 'general'),
'type' => 'string',
'description' => 'The name of the credits on the panel.'
],
[
'key' => 'SETTINGS::SYSTEM:ALERT_ENABLED',
'value' => $this->getNewValue('alert_enabled'),
'value' => $this->getNewValue('alert_enabled', 'general'),
'type' => 'boolean',
'description' => 'Enable the alert at the top of the panel.'
],
[
'key' => 'SETTINGS::SYSTEM:ALERT_TYPE',
'value' => $this->getNewValue('alert_type'),
'value' => $this->getNewValue('alert_type', 'general'),
'type' => 'string',
'description' => 'The type of alert to display.'
],
[
'key' => 'SETTINGS::SYSTEM:ALERT_MESSAGE',
'value' => $this->getNewValue('alert_message'),
'value' => $this->getNewValue('alert_message', 'general'),
'type' => 'text',
'description' => 'The message to display in the alert.'
],
[
'key' => 'SETTINGS::SYSTEM:THEME',
'value' => $this->getNewValue('theme'),
'value' => $this->getNewValue('theme', 'general'),
'type' => 'string',
'description' => 'The theme to use for the panel.'

],
[
'key' => 'SETTINGS::RECAPTCHA:SITE_KEY',
'value' => $this->getNewValue('recaptcha_site_key'),
'value' => $this->getNewValue('recaptcha_site_key', 'general'),
'type' => 'string',
'description' => 'The site key for reCAPTCHA.'
],
[
'key' => 'SETTINGS::RECAPTCHA:SECRET_KEY',
'value' => $this->getNewValue('recaptcha_secret_key'),
'value' => $this->getNewValue('recaptcha_secret_key', 'general'),
'type' => 'string',
'description' => 'The secret key for reCAPTCHA.'
],
[
'key' => 'SETTINGS::RECAPTCHA:ENABLED',
'value' => $this->getNewValue('recaptcha_enabled'),
'value' => $this->getNewValue('recaptcha_enabled', 'general'),
'type' => 'boolean',
'description' => 'Enable reCAPTCHA on the panel.'
],
[
'key' => 'SETTINGS::MISC:PHPMYADMIN:URL',
'value' => $this->getNewValue('phpmyadmin_url'),
'value' => $this->getNewValue('phpmyadmin_url', 'general'),
'type' => 'string',
'description' => 'The URL to your phpMyAdmin installation.'
],
]);

$this->migrator->delete('general.store_enabled');
$this->migrator->delete('general.credits_display_name');
$this->migrator->delete('general.recaptcha_site_key');
$this->migrator->delete('general.recaptcha_secret_key');
$this->migrator->delete('general.recaptcha_enabled');
$this->migrator->delete('general.phpmyadmin_url');
$this->migrator->delete('general.alert_enabled');
$this->migrator->delete('general.alert_type');
$this->migrator->delete('general.alert_message');
$this->migrator->delete('general.theme');
}

public function getNewValue(string $name)
{
$new_value = DB::table('settings')->where([['group', '=', 'general'], ['name', '=', $name]])->get(['payload'])->first();

// Some keys returns '""' as a value.
if ($new_value->payload === '""') {
return null;
}

// remove the quotes from the string
if (substr($new_value->payload, 0, 1) === '"' && substr($new_value->payload, -1) === '"') {
return substr($new_value->payload, 1, -1);
}

return $new_value->payload;
}

public function getOldValue(string $key)
{
// Always get the first value of the key.
$old_value = DB::table('settings_old')->where('key', '=', $key)->get(['value', 'type'])->first();

// Handle the old values to return without it being a string in all cases.
if ($old_value->type === "string" || $old_value->type === "text") {
if (is_null($old_value->value)) {
return '';
}

// Some values have the type string, but their values are boolean.
if ($old_value->value === "false" || $old_value->value === "true") {
return filter_var($old_value->value, FILTER_VALIDATE_BOOL);
}

return $old_value->value;
}

if ($old_value->type === "boolean") {
return filter_var($old_value->value, FILTER_VALIDATE_BOOL);
try {
$this->migrator->delete('general.store_enabled');
$this->migrator->delete('general.credits_display_name');
$this->migrator->delete('general.recaptcha_site_key');
$this->migrator->delete('general.recaptcha_secret_key');
$this->migrator->delete('general.recaptcha_enabled');
$this->migrator->delete('general.phpmyadmin_url');
$this->migrator->delete('general.alert_enabled');
$this->migrator->delete('general.alert_type');
$this->migrator->delete('general.alert_message');
$this->migrator->delete('general.theme');
} catch (Exception $e) {
// Do nothing
}

return filter_var($old_value->value, FILTER_VALIDATE_INT);
}
}
Loading