Files

85 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Http\Requests\Api\Admin\Servers\Databases;
use Illuminate\Database\Query\Builder;
use Illuminate\Validation\Rule;
2026-10-11 07:34:41 -04:00
use Pterodactyl\Actions\Databases\CreateDatabase;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Enum\Permissions;
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\DatabaseName;
class StoreDatabaseRequest extends AdminApiRequest
{
public function permissions(): array
{
return [Permissions::AdminServerDatabasesCreate];
}
/**
2026-10-11 07:34:41 -04:00
* Validation rules for creating a server database. The name is the one before the
* "s{server_id}_" prefix, and the remote is held to an IP address pattern.
2026-10-05 18:10:18 -04:00
*
* @return ValidationRules
*/
public function rules(): array
{
$server = $this->parameter('server', Server::class);
2026-10-11 07:34:41 -04:00
$rules = CreateDatabase::rules();
2026-10-05 18:10:18 -04:00
return [
'database' => [
'required',
'alpha_dash',
'min:1',
'max:48',
Rule::unique('databases')->where(function (Builder $query) use ($server): void {
$query->where('server_id', $server->id)->where('database', $this->databaseName());
}),
],
'remote' => ['required', 'string', 'regex:/^[0-9%.]{1,15}$/'],
2026-10-11 07:34:41 -04:00
'database_host_id' => $rules['database_host_id'],
'max_connections' => $rules['max_connections'],
2026-10-05 18:10:18 -04:00
];
}
/**
2026-10-11 07:34:41 -04:00
* The validated input, with the database name prefixed for the action. A database
* created without a connection limit gets none.
2026-10-05 18:10:18 -04:00
*
2026-10-11 19:54:55 -04:00
* @return array<string, mixed>
2026-10-05 18:10:18 -04:00
*/
2026-10-11 07:34:41 -04:00
public function actionInput(): array
2026-10-05 18:10:18 -04:00
{
2026-10-11 07:34:41 -04:00
$input = parent::actionInput();
2026-10-05 18:10:18 -04:00
return [
2026-10-11 07:34:41 -04:00
...$input,
'database' => $this->databaseName(),
'max_connections' => $input['max_connections'] ?? null,
2026-10-05 18:10:18 -04:00
];
}
/** Rename fields to be more clear in error messages. */
public function attributes(): array
{
return [
'database_host_id' => 'Database Host Server ID',
'remote' => 'Remote Connection String',
'database' => 'Database Name',
'max_connections' => 'Max Connections',
];
}
2026-10-11 07:34:41 -04:00
/** The unique, prefixed database name used by the uniqueness rule and the action. */
2026-10-05 18:10:18 -04:00
public function databaseName(): string
{
$server = $this->parameter('server', Server::class);
2026-10-11 07:34:41 -04:00
return DatabaseName::generateUnique($this->string('database')->toString(), $server->id);
2026-10-05 18:10:18 -04:00
}
}