2018-01-25 22:34:53 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-01-25 22:34:53 -06:00
|
|
|
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
|
|
|
|
|
|
2019-03-02 15:31:25 -08:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Validation\Rule;
|
2018-01-25 22:34:53 -06:00
|
|
|
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;
|
2018-01-25 22:34:53 -06:00
|
|
|
|
|
|
|
|
class StoreServerDatabaseRequest extends ApplicationApiRequest
|
|
|
|
|
{
|
2022-10-14 10:59:20 -06:00
|
|
|
protected ?string $resource = AdminAcl::RESOURCE_SERVER_DATABASES;
|
2018-01-25 22:34:53 -06:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
protected int $permission = AdminAcl::WRITE;
|
2018-01-25 22:34:53 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validation rules for database creation.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @return ValidationRules
|
2018-01-25 22:34:53 -06:00
|
|
|
*/
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
2025-12-20 15:55:13 -08:00
|
|
|
$server = $this->parameter('server', Server::class);
|
2020-10-11 11:59:46 -07:00
|
|
|
|
2018-01-25 22:34:53 -06:00
|
|
|
return [
|
2019-03-02 15:31:25 -08:00
|
|
|
'database' => [
|
|
|
|
|
'required',
|
2020-10-11 11:59:46 -07:00
|
|
|
'alpha_dash',
|
2019-03-02 15:31:25 -08:00
|
|
|
'min:1',
|
2020-10-11 11:59:46 -07:00
|
|
|
'max:48',
|
2026-10-05 18:10:18 -04:00
|
|
|
Rule::unique('databases')->where(function (Builder $query) use ($server): void {
|
2020-10-11 11:59:46 -07:00
|
|
|
$query->where('server_id', $server->id)->where('database', $this->databaseName());
|
2019-03-02 15:31:25 -08:00
|
|
|
}),
|
|
|
|
|
],
|
2026-10-05 18:10:18 -04:00
|
|
|
'remote' => ['required', 'string', 'regex:/^[0-9%.]{1,15}$/'],
|
|
|
|
|
'host' => ['required', 'integer', 'exists:database_hosts,id'],
|
2018-01-25 22:34:53 -06:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
2018-01-25 22:34:53 -06:00
|
|
|
*/
|
2026-10-11 07:34:41 -04:00
|
|
|
public function actionInput(): array
|
2018-01-25 22:34:53 -06:00
|
|
|
{
|
2026-10-11 07:34:41 -04:00
|
|
|
$input = parent::actionInput();
|
|
|
|
|
|
2018-01-25 22:34:53 -06:00
|
|
|
return [
|
2026-10-11 07:34:41 -04:00
|
|
|
'database' => $this->databaseName(),
|
|
|
|
|
'remote' => $input['remote'] ?? null,
|
|
|
|
|
'database_host_id' => $input['host'] ?? null,
|
2018-01-25 22:34:53 -06:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format error messages in a more understandable format for API output.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function attributes(): array
|
2018-01-25 22:34:53 -06:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'host' => 'Database Host Server ID',
|
|
|
|
|
'remote' => 'Remote Connection String',
|
|
|
|
|
'database' => 'Database Name',
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-10-11 11:59:46 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
2020-10-11 11:59:46 -07:00
|
|
|
|
2026-10-11 07:34:41 -04:00
|
|
|
return DatabaseName::generateUnique($this->string('database')->toString(), $server->id);
|
2020-10-11 11:59:46 -07:00
|
|
|
}
|
2018-01-25 22:34:53 -06:00
|
|
|
}
|