mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 12:25:44 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
134 lines
4.3 KiB
PHP
134 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Http\Requests\Api\Admin\Nodes;
|
|
|
|
use Closure;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Validator;
|
|
use Pterodactyl\Enum\Permissions;
|
|
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
|
|
use Pterodactyl\Rules\Fqdn;
|
|
use Pterodactyl\Support\JsonValueGuard;
|
|
use Pterodactyl\Support\ValidationRuleSubset;
|
|
use Pterodactyl\Validation\NodeRules;
|
|
|
|
class StoreNodeRequest extends AdminApiRequest
|
|
{
|
|
public function permissions(): array
|
|
{
|
|
return [Permissions::AdminNodesCreate];
|
|
}
|
|
|
|
/**
|
|
* Validation rules for creating a node.
|
|
*
|
|
* @param NormalizedValidationRules|null $rules
|
|
* @return ValidationRules
|
|
*/
|
|
public function rules(?array $rules = null): array
|
|
{
|
|
$rules ??= NodeRules::rules();
|
|
$rules['fqdn'][] = Fqdn::make('scheme');
|
|
|
|
$selected = ValidationRuleSubset::select($rules, [
|
|
'public',
|
|
'name',
|
|
'description',
|
|
'location_id',
|
|
'fqdn',
|
|
'scheme',
|
|
'behind_proxy',
|
|
'maintenance_mode',
|
|
'memory',
|
|
'memory_overallocate',
|
|
'disk',
|
|
'disk_overallocate',
|
|
'upload_size',
|
|
'daemonListen',
|
|
'daemonSFTP',
|
|
'daemonBase',
|
|
]);
|
|
|
|
$normalized = [];
|
|
foreach ($selected as $key => $value) {
|
|
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
|
|
$normalized[Str::snake($key)] = $value;
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
|
|
/**
|
|
* @return list<Closure(Validator): void>
|
|
*/
|
|
public function after(): array
|
|
{
|
|
return [function (Validator $validator): void {
|
|
if ($this->isSecure() && $this->input('scheme') === 'http') {
|
|
$validator->errors()->add(
|
|
'scheme',
|
|
'HTTP node connections cannot be used when the Panel is loaded over HTTPS.'
|
|
);
|
|
}
|
|
}];
|
|
}
|
|
|
|
/** Rename fields to be more clear in error messages. */
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'daemon_base' => 'Daemon Base Path',
|
|
'upload_size' => 'File Upload Size Limit',
|
|
'location_id' => 'Location',
|
|
'public' => 'Node Visibility',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Remap snake_case keys back to the daemon* keys the services expect.
|
|
*
|
|
* @return NodeCreationData
|
|
*/
|
|
public function payload(): array
|
|
{
|
|
$data = parent::validated();
|
|
|
|
$daemonBase = Arr::get($data, 'daemon_base');
|
|
|
|
$response = [
|
|
'name' => JsonValueGuard::string(Arr::get($data, 'name')),
|
|
'location_id' => JsonValueGuard::integer(Arr::get($data, 'location_id')),
|
|
'fqdn' => JsonValueGuard::string(Arr::get($data, 'fqdn')),
|
|
'scheme' => JsonValueGuard::string(Arr::get($data, 'scheme')),
|
|
'memory' => JsonValueGuard::integer(Arr::get($data, 'memory')),
|
|
'memory_overallocate' => JsonValueGuard::integer(Arr::get($data, 'memory_overallocate')),
|
|
'disk' => JsonValueGuard::integer(Arr::get($data, 'disk')),
|
|
'disk_overallocate' => JsonValueGuard::integer(Arr::get($data, 'disk_overallocate')),
|
|
'daemonListen' => JsonValueGuard::integer(Arr::get($data, 'daemon_listen')),
|
|
'daemonSFTP' => JsonValueGuard::integer(Arr::get($data, 'daemon_sftp')),
|
|
'daemonBase' => $daemonBase !== null && $daemonBase !== '' ? JsonValueGuard::string($daemonBase) : '/var/lib/pterodactyl/volumes',
|
|
];
|
|
|
|
if (array_key_exists('description', $data)) {
|
|
$description = Arr::get($data, 'description');
|
|
$response['description'] = $description !== null && $description !== '' ? JsonValueGuard::string($description) : null;
|
|
}
|
|
|
|
foreach (['public', 'behind_proxy', 'maintenance_mode'] as $boolean) {
|
|
if (array_key_exists($boolean, $data)) {
|
|
$response[$boolean] = JsonValueGuard::boolean(Arr::get($data, $boolean));
|
|
}
|
|
}
|
|
|
|
$uploadSize = Arr::get($data, 'upload_size');
|
|
if ($uploadSize !== null && $uploadSize !== '') {
|
|
$response['upload_size'] = JsonValueGuard::integer($uploadSize);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|