PteroTheme/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php

87 lines
2.3 KiB
PHP

<?php
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Validation\Rule;
use Illuminate\Database\Query\Builder;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class StoreServerDatabaseRequest extends ApplicationApiRequest
{
/**
* @var string
*/
protected $resource = AdminAcl::RESOURCE_SERVER_DATABASES;
/**
* @var int
*/
protected $permission = AdminAcl::WRITE;
/**
* Validation rules for database creation.
*/
public function rules(): array
{
$server = $this->route()->parameter('server');
return [
'database' => [
'required',
'alpha_dash',
'min:1',
'max:48',
Rule::unique('databases')->where(function (Builder $query) use ($server) {
$query->where('server_id', $server->id)->where('database', $this->databaseName());
}),
],
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
'host' => 'required|integer|exists:database_hosts,id',
];
}
/**
* Return data formatted in the correct format for the service to consume.
*
* @return array
*/
public function validated()
{
return [
'database' => $this->input('database'),
'remote' => $this->input('remote'),
'database_host_id' => $this->input('host'),
];
}
/**
* Format error messages in a more understandable format for API output.
*
* @return array
*/
public function attributes()
{
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
{
$server = $this->route()->parameter('server');
Assert::isInstanceOf($server, Server::class);
return DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id);
}
}