2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Actions\Databases;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-10-11 07:34:41 -04:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Pterodactyl\Contracts\Databases\CreatesDatabases;
|
|
|
|
|
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
|
|
|
|
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
|
|
|
|
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
|
|
|
|
|
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
|
|
|
|
use Pterodactyl\Helpers\Utilities;
|
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
|
use Pterodactyl\Services\Databases\DatabaseHostGateway;
|
|
|
|
|
use Pterodactyl\Support\JsonValueGuard;
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
final class CreateDatabase implements CreatesDatabases
|
|
|
|
|
{
|
2026-10-11 07:34:41 -04:00
|
|
|
/**
|
|
|
|
|
* Rules every database's remote connection pattern must pass: the hosts its user may
|
|
|
|
|
* connect from, such as `%` or `192.168.1.%`.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
public const array REMOTE = ['required', 'string', 'regex:/^[\w\-\/.%:]+$/'];
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @see \Pterodactyl\Support\DatabaseName::generateUnique() */
|
|
|
|
|
private const string MATCH_NAME_REGEX = '/^(s[\d]+_)(.*)$/';
|
|
|
|
|
|
|
|
|
|
private bool $validateDatabaseLimit = true;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly DynamicDatabaseConnection $dynamic,
|
|
|
|
|
private readonly DatabaseHostGateway $gateway,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-10-11 07:34:41 -04:00
|
|
|
/**
|
|
|
|
|
* A database's fields. The name is the full one, already prefixed with "s{server_id}_" by
|
|
|
|
|
* DatabaseName::generateUnique().
|
|
|
|
|
*
|
|
|
|
|
* @return NormalizedValidationRules
|
|
|
|
|
*/
|
|
|
|
|
public static function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'database' => ['required', 'string', 'alpha_dash', 'between:3,48', 'regex:'.self::MATCH_NAME_REGEX],
|
|
|
|
|
'remote' => self::REMOTE,
|
|
|
|
|
'database_host_id' => ['required', 'integer', 'exists:database_hosts,id'],
|
|
|
|
|
'max_connections' => ['nullable', 'integer', 'min:0'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validates database input and returns the model attributes it describes. The connection
|
|
|
|
|
* limit is left out when it is not given, so the database takes the column default; a
|
|
|
|
|
* blank one is stored as null. Null and 0 both leave the user's connections unlimited.
|
|
|
|
|
*
|
|
|
|
|
* @param ActionInput $input
|
|
|
|
|
* @return DatabaseCreationData
|
|
|
|
|
*/
|
|
|
|
|
public static function attributes(array $input): array
|
|
|
|
|
{
|
|
|
|
|
$input = Validator::make($input, self::rules())->safe();
|
|
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
|
'database' => $input->string('database')->toString(),
|
|
|
|
|
'remote' => $input->string('remote')->toString(),
|
|
|
|
|
'database_host_id' => $input->integer('database_host_id'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($input->has('max_connections')) {
|
|
|
|
|
$attributes['max_connections'] = $input->filled('max_connections') ? $input->integer('max_connections') : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $attributes;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public function setValidateDatabaseLimit(bool $validate): CreatesDatabases
|
|
|
|
|
{
|
|
|
|
|
$this->validateDatabaseLimit = $validate;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 07:34:41 -04:00
|
|
|
/**
|
|
|
|
|
* @param ActionInput $input
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Throwable
|
|
|
|
|
* @throws TooManyDatabasesException
|
|
|
|
|
* @throws DatabaseClientFeatureNotEnabledException
|
|
|
|
|
*/
|
2026-10-11 07:34:41 -04:00
|
|
|
public function create(Server $server, array $input): Database
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
|
|
|
|
throw_unless(config('pterodactyl.client_features.databases.enabled'), DatabaseClientFeatureNotEnabledException::class);
|
|
|
|
|
|
|
|
|
|
if ($this->validateDatabaseLimit) {
|
|
|
|
|
throw_if(($server->database_limit) !== null && $server->databases()->count() >= $server->database_limit, TooManyDatabasesException::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2026-10-11 07:34:41 -04:00
|
|
|
...self::attributes($input),
|
2026-10-05 18:10:18 -04:00
|
|
|
'server_id' => $server->id,
|
|
|
|
|
'username' => sprintf('u%d_%s', $server->id, Str::random(10)),
|
|
|
|
|
'password' => Crypt::encrypt(Utilities::randomStringWithSpecialCharacters(24)),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$database = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return DB::transaction(function () use ($data, &$database): Database {
|
|
|
|
|
$this->dynamic->set('dynamic', $data['database_host_id']);
|
|
|
|
|
$database = $this->createModel($data);
|
|
|
|
|
$this->gateway->createDatabase($database->database);
|
|
|
|
|
$this->gateway->createUser(
|
|
|
|
|
$database->username,
|
|
|
|
|
$database->remote,
|
|
|
|
|
JsonValueGuard::string(Crypt::decrypt($database->password)),
|
|
|
|
|
$database->max_connections
|
|
|
|
|
);
|
|
|
|
|
$this->gateway->assignUserToDatabase($database->database, $database->username, $database->remote);
|
|
|
|
|
$this->gateway->flush();
|
|
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
|
});
|
|
|
|
|
} catch (Throwable $throwable) {
|
|
|
|
|
// The model row may have been created before provisioning failed, so clean up any host state that exists.
|
|
|
|
|
if ($database instanceof Database) {
|
|
|
|
|
rescue(fn (): bool => $this->gateway->dropDatabase($database->database));
|
|
|
|
|
rescue(fn (): bool => $this->gateway->dropUser($database->username, $database->remote));
|
|
|
|
|
rescue(fn (): bool => $this->gateway->flush());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw $throwable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param DatabaseModelCreationData $data */
|
|
|
|
|
private function createModel(array $data): Database
|
|
|
|
|
{
|
|
|
|
|
$exists = Database::query()->where('server_id', $data['server_id'])
|
|
|
|
|
->where('database', $data['database'])
|
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
|
|
throw_if($exists, DuplicateDatabaseNameException::class, 'A database with that name already exists for this server.');
|
|
|
|
|
|
|
|
|
|
$database = (new Database)->forceFill($data);
|
|
|
|
|
$database->saveOrFail();
|
|
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
|
}
|
|
|
|
|
}
|