Files
pterodactyl-panel/app/Actions/Databases/RotateDatabasePassword.php
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

42 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Databases;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Pterodactyl\Contracts\Databases\RotatesDatabasePasswords;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Helpers\Utilities;
use Pterodactyl\Models\Database;
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Throwable;
final readonly class RotateDatabasePassword implements RotatesDatabasePasswords
{
public function __construct(
private DynamicDatabaseConnection $dynamic,
private DatabaseHostGateway $gateway,
) {}
/** @throws Throwable */
public function rotate(Database $database): string
{
$password = Utilities::randomStringWithSpecialCharacters(24);
DB::transaction(function () use ($database, $password): void {
$database->newQuery()->whereKey($database->getKey())->lockForUpdate()->firstOrFail();
$database->update(['password' => Crypt::encrypt($password)]);
$this->dynamic->set('dynamic', $database->database_host_id);
$this->gateway->dropUser($database->username, $database->remote);
$this->gateway->createUser($database->username, $database->remote, $password, $database->max_connections);
$this->gateway->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->gateway->flush();
});
return $password;
}
}