misc_pterodactyl-panel/app/Services/Databases/DatabaseManagementService.php

170 lines
5.7 KiB
PHP
Raw Normal View History

2017-07-15 16:52:34 +00:00
<?php
namespace Pterodactyl\Services\Databases;
2017-07-15 16:52:34 +00:00
2019-09-06 04:32:57 +00:00
use Exception;
use Pterodactyl\Models\Server;
2017-10-24 01:12:15 +00:00
use Pterodactyl\Models\Database;
use Pterodactyl\Helpers\Utilities;
use Illuminate\Database\ConnectionInterface;
2017-07-15 16:52:34 +00:00
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
2017-07-15 16:52:34 +00:00
class DatabaseManagementService
2017-07-15 16:52:34 +00:00
{
/**
* @var \Illuminate\Database\ConnectionInterface
2017-07-15 16:52:34 +00:00
*/
private $connection;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
private $dynamic;
2017-07-15 16:52:34 +00:00
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
2017-07-15 16:52:34 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
private $repository;
/**
* Determines if the service should validate the user's ability to create an additional
* database for this server. In almost all cases this should be true, but to keep things
* flexible you can also set it to false and create more databases than the server is
* allocated.
*
* @var bool
*/
protected $validateDatabaseLimit = true;
2017-07-15 16:52:34 +00:00
/**
* CreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
2017-07-15 16:52:34 +00:00
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
2017-07-15 16:52:34 +00:00
*/
public function __construct(
ConnectionInterface $connection,
2017-07-15 16:52:34 +00:00
DynamicDatabaseConnection $dynamic,
DatabaseRepositoryInterface $repository,
Encrypter $encrypter
) {
$this->connection = $connection;
2017-07-15 16:52:34 +00:00
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Set wether or not this class should validate that the server has enough slots
* left before creating the new database.
*
* @param bool $validate
* @return $this
*/
public function setValidateDatabaseLimit(bool $validate): self
{
$this->validateDatabaseLimit = $validate;
return $this;
}
2017-07-15 16:52:34 +00:00
/**
* Create a new database that is linked to a specific host.
*
* @param \Pterodactyl\Models\Server $server
2017-08-22 03:10:48 +00:00
* @param array $data
* @return \Pterodactyl\Models\Database
2017-07-15 16:52:34 +00:00
*
* @throws \Throwable
* @throws \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
2017-07-15 16:52:34 +00:00
*/
public function create(Server $server, array $data)
2017-07-15 16:52:34 +00:00
{
if (! config('pterodactyl.client_features.databases.enabled')) {
throw new DatabaseClientFeatureNotEnabledException;
}
if ($this->validateDatabaseLimit) {
// If the server has a limit assigned and we've already reached that limit, throw back
// an exception and kill the process.
if (! is_null($server->database_limit) && $server->databases()->count() >= $server->database_limit) {
throw new TooManyDatabasesException;
}
}
// Max of 48 characters, including the s123_ that we append to the front.
$truncatedName = substr($data['database'], 0, 48 - strlen("s{$server->id}_"));
$data = array_merge($data, [
'server_id' => $server->id,
'database' => $truncatedName,
'username' => sprintf('u%d_%s', $server->id, str_random(10)),
'password' => $this->encrypter->encrypt(
Utilities::randomStringWithSpecialCharacters(24)
),
]);
$database = null;
2017-07-15 16:52:34 +00:00
try {
return $this->connection->transaction(function () use ($data, &$database) {
$database = $this->repository->createIfNotExists($data);
$this->dynamic->set('dynamic', $data['database_host_id']);
$this->repository->createDatabase($database->database);
$this->repository->createUser(
$database->username, $database->remote, $this->encrypter->decrypt($database->password), $database->max_connections
);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
return $database;
});
} catch (Exception $exception) {
2017-07-15 16:52:34 +00:00
try {
if ($database instanceof Database) {
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
2017-07-15 16:52:34 +00:00
}
} catch (Exception $exception) {
// Do nothing here. We've already encountered an issue before this point so no
// reason to prioritize this error over the initial one.
2017-07-15 16:52:34 +00:00
}
throw $exception;
2017-07-15 16:52:34 +00:00
}
}
/**
* Delete a database from the given host server.
*
* @param \Pterodactyl\Models\Database $database
2017-07-15 16:52:34 +00:00
* @return bool|null
*
* @throws \Exception
2017-07-15 16:52:34 +00:00
*/
public function delete(Database $database)
2017-07-15 16:52:34 +00:00
{
$this->dynamic->set('dynamic', $database->database_host_id);
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
2017-07-15 16:52:34 +00:00
return $database->delete();
2017-07-15 16:52:34 +00:00
}
}