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;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
|
|
|
|
|
|
|
class DatabasePasswordService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
|
|
|
|
*/
|
|
|
|
private $dynamic;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabasePasswordService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-10-19 03:32:19 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
|
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-10-19 03:32:19 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConnectionInterface $connection,
|
|
|
|
DatabaseRepositoryInterface $repository,
|
|
|
|
DynamicDatabaseConnection $dynamic,
|
|
|
|
Encrypter $encrypter
|
|
|
|
) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->dynamic = $dynamic;
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a password for a given database.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Database|int $database
|
2019-08-03 21:42:32 +00:00
|
|
|
* @return string
|
2017-10-19 03:32:19 +00:00
|
|
|
*
|
2019-08-03 21:42:32 +00:00
|
|
|
* @throws \Throwable
|
2017-10-19 03:32:19 +00:00
|
|
|
*/
|
2019-08-03 21:42:32 +00:00
|
|
|
public function handle(Database $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
|
|
|
|
2019-08-03 21:42:32 +00:00
|
|
|
$this->repository->withoutFreshModel()->update($database->id, [
|
|
|
|
'password' => $this->encrypter->encrypt($password),
|
|
|
|
]);
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2019-08-03 21:42:32 +00:00
|
|
|
$this->repository->dropUser($database->username, $database->remote);
|
2020-04-22 10:00:04 +00:00
|
|
|
$this->repository->createUser($database->username, $database->remote, $password, $database->max_connections);
|
2019-08-03 21:42:32 +00:00
|
|
|
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
|
|
|
|
$this->repository->flush();
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
}
|