2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Actions\Databases;
|
|
|
|
|
|
2026-10-11 23:41:04 -04:00
|
|
|
use Illuminate\Support\Arr;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-10-11 23:41:04 -04:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Contracts\Databases\UpdatesDatabaseHosts;
|
|
|
|
|
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Pterodactyl\Services\Extensions\ExtensionFields;
|
|
|
|
|
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
final readonly class UpdateDatabaseHost implements UpdatesDatabaseHosts
|
|
|
|
|
{
|
2026-10-09 06:26:45 +01:00
|
|
|
public function __construct(private DynamicDatabaseConnection $dynamic, private ExtensionFields $extensions) {}
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-11 07:27:47 -04:00
|
|
|
public function update(DatabaseHost $host, array $input): DatabaseHost
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
2026-10-11 23:41:04 -04:00
|
|
|
$validated = Validator::validate($input, CreateDatabaseHost::rules());
|
|
|
|
|
// Blank strings are null, as they are in a request.
|
|
|
|
|
$validated = array_map(fn (mixed $value): mixed => is_string($value) && blank($value) ? null : $value, $validated);
|
|
|
|
|
|
2026-10-11 07:27:47 -04:00
|
|
|
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
|
|
|
|
|
|
2026-10-11 23:41:04 -04:00
|
|
|
// A host given no node becomes global, and a blank password keeps the stored one.
|
|
|
|
|
$attributes = [...Arr::except($validated, 'password'), 'node_id' => $validated['node_id'] ?? null];
|
|
|
|
|
if (isset($validated['password'])) {
|
|
|
|
|
$attributes['password'] = Crypt::encrypt($validated['password']);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 17:22:14 -04:00
|
|
|
// Confirm access using the new credentials before saving them.
|
2026-10-11 07:27:47 -04:00
|
|
|
$host->fill($attributes);
|
2026-10-08 17:22:14 -04:00
|
|
|
$this->dynamic->set('dynamic', $host);
|
|
|
|
|
DB::connection('dynamic')->select('SELECT 1 FROM dual');
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-09 06:26:45 +01:00
|
|
|
// The host row and its extension values are written together so a failing
|
|
|
|
|
// extension cannot leave a host behind without them.
|
|
|
|
|
return DB::transaction(function () use ($host, $extensions): DatabaseHost {
|
|
|
|
|
$host->save();
|
|
|
|
|
$this->extensions->save($host, $extensions);
|
2026-10-08 17:22:14 -04:00
|
|
|
|
2026-10-09 06:26:45 +01:00
|
|
|
return $host;
|
|
|
|
|
});
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
}
|