2017-10-19 03:32:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Databases;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
2020-03-15 23:25:29 +00:00
|
|
|
use Pterodactyl\Helpers\Utilities;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
|
|
|
|
|
|
|
class DatabasePasswordService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* DatabasePasswordService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private DynamicDatabaseConnection $dynamic,
|
2022-10-24 04:19:14 +00:00
|
|
|
private Encrypter $encrypter
|
2017-10-19 03:32:19 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a password for a given database.
|
|
|
|
*
|
2019-08-03 21:42:32 +00:00
|
|
|
* @throws \Throwable
|
2017-10-19 03:32:19 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(Database|int $database): string
|
2017-10-19 03:32:19 +00:00
|
|
|
{
|
2020-03-15 23:25:29 +00:00
|
|
|
$password = Utilities::randomStringWithSpecialCharacters(24);
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2019-08-03 21:42:32 +00:00
|
|
|
$this->connection->transaction(function () use ($database, $password) {
|
|
|
|
$this->dynamic->set('dynamic', $database->database_host_id);
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2022-10-24 04:19:14 +00:00
|
|
|
$database->update([
|
2019-08-03 21:42:32 +00:00
|
|
|
'password' => $this->encrypter->encrypt($password),
|
|
|
|
]);
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2022-10-24 04:19:14 +00:00
|
|
|
$database->dropUser($database->username, $database->remote);
|
|
|
|
$database->createUser($database->username, $database->remote, $password, $database->max_connections);
|
|
|
|
$database->assignUserToDatabase($database->database, $database->username, $database->remote);
|
|
|
|
$database->flush();
|
2019-08-03 21:42:32 +00:00
|
|
|
});
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2019-08-03 21:42:32 +00:00
|
|
|
return $password;
|
2017-10-19 03:32:19 +00:00
|
|
|
}
|
|
|
|
}
|