Prevent creating a new database via the application API if server is at its limit; closes #2129
This commit is contained in:
parent
910a48ee8a
commit
b78aa180ea
6 changed files with 116 additions and 76 deletions
|
@ -6,9 +6,7 @@ use Pterodactyl\Models\Server;
|
|||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
|
||||
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
|
||||
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
||||
|
||||
class DeployServerDatabaseService
|
||||
{
|
||||
|
@ -49,20 +47,12 @@ class DeployServerDatabaseService
|
|||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Database
|
||||
*
|
||||
* @throws \Throwable
|
||||
* @throws \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle(Server $server, array $data): Database
|
||||
{
|
||||
if (! config('pterodactyl.client_features.databases.enabled')) {
|
||||
throw new DatabaseClientFeatureNotEnabledException;
|
||||
}
|
||||
|
||||
$databases = $this->repository->findCountWhere([['server_id', '=', $server->id]]);
|
||||
if (! is_null($server->database_limit) && $databases >= $server->database_limit) {
|
||||
throw new TooManyDatabasesException;
|
||||
}
|
||||
|
||||
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
|
||||
$hosts = $this->databaseHostRepository->setColumns(['id'])->findWhere([
|
||||
['node_id', '=', $server->node_id],
|
||||
|
@ -81,7 +71,7 @@ class DeployServerDatabaseService
|
|||
|
||||
$host = $hosts->random();
|
||||
|
||||
return $this->managementService->create($server->id, [
|
||||
return $this->managementService->create($server, [
|
||||
'database_host_id' => $host->id,
|
||||
'database' => array_get($data, 'database'),
|
||||
'remote' => array_get($data, 'remote'),
|
||||
|
|
Reference in a new issue