Files

83 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
use Illuminate\Database\Query\Builder;
2026-10-05 18:10:18 -04:00
use Illuminate\Validation\Rule;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Support\DatabaseName;
class StoreServerDatabaseRequest extends ApplicationApiRequest
{
2022-10-14 10:59:20 -06:00
protected ?string $resource = AdminAcl::RESOURCE_SERVER_DATABASES;
2022-10-14 10:59:20 -06:00
protected int $permission = AdminAcl::WRITE;
/**
* Validation rules for database creation.
2026-10-05 18:10:18 -04:00
*
* @return ValidationRules
*/
public function rules(): array
{
$server = $this->parameter('server', Server::class);
return [
'database' => [
'required',
'alpha_dash',
'min:1',
'max:48',
2026-10-05 18:10:18 -04:00
Rule::unique('databases')->where(function (Builder $query) use ($server): void {
$query->where('server_id', $server->id)->where('database', $this->databaseName());
}),
],
2026-10-05 18:10:18 -04:00
'remote' => ['required', 'string', 'regex:/^[0-9%.]{1,15}$/'],
'host' => ['required', 'integer', 'exists:database_hosts,id'],
];
}
/**
2026-10-11 07:34:41 -04:00
* The validated input under the action's field names: the database name gets its
* "s{server_id}_" prefix, and host is the action's database_host_id.
2026-10-05 18:10:18 -04:00
*
2026-10-11 19:54:55 -04:00
* @return array<string, mixed>
*/
2026-10-11 07:34:41 -04:00
public function actionInput(): array
{
2026-10-11 07:34:41 -04:00
$input = parent::actionInput();
return [
2026-10-11 07:34:41 -04:00
'database' => $this->databaseName(),
'remote' => $input['remote'] ?? null,
'database_host_id' => $input['host'] ?? null,
];
}
/**
* Format error messages in a more understandable format for API output.
*/
2022-10-14 10:59:20 -06:00
public function attributes(): array
{
return [
'host' => 'Database Host Server ID',
'remote' => 'Remote Connection String',
'database' => 'Database Name',
];
}
/**
* Returns the database name in the expected format.
*/
public function databaseName(): string
{
2026-10-05 18:10:18 -04:00
$server = $this->parameter('server', Server::class);
2026-10-11 07:34:41 -04:00
return DatabaseName::generateUnique($this->string('database')->toString(), $server->id);
}
}