mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 20:35:50 -05:00
boolean inputs
This commit is contained in:
@@ -16,7 +16,6 @@ use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
|
||||
use Pterodactyl\Http\Requests\Api\Admin\Eggs\GetEggScriptRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Admin\Eggs\UpdateEggScriptRequest;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use Pterodactyl\Transformers\Api\Admin\EggTransformer;
|
||||
|
||||
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
|
||||
@@ -58,10 +57,7 @@ class EggScriptController extends AdminApiController
|
||||
#[ScribeResponse(self::INVALID_COPY_FROM_ERROR, status: 400, description: 'The selected copy source is invalid.')]
|
||||
public function update(UpdateEggScriptRequest $request, UpdatesEggInstallScripts $scripts, Egg $egg): array
|
||||
{
|
||||
|
||||
$data = $request->validated();
|
||||
JsonValueGuard::assertPayload9($data);
|
||||
$egg = $scripts->update($egg, $data);
|
||||
$egg = $scripts->update($egg, $request->payload());
|
||||
|
||||
Activity::event('admin:egg.scripts')
|
||||
->subject($egg)
|
||||
|
||||
@@ -387,11 +387,14 @@ class FileController extends ClientApiController
|
||||
{
|
||||
$url = JsonValueGuard::string($request->validated('url'));
|
||||
$directory = JsonValueGuard::nullableString($request->validated('directory'));
|
||||
$operation->pull($server,
|
||||
$url,
|
||||
$directory,
|
||||
JsonValueGuard::jsonArray($request->safe()->only(['filename', 'use_header', 'foreground']))
|
||||
);
|
||||
$options = $request->safe()->only(['filename']);
|
||||
foreach (['use_header', 'foreground'] as $flag) {
|
||||
if ($request->safe()->has($flag)) {
|
||||
$options[$flag] = $request->boolean($flag);
|
||||
}
|
||||
}
|
||||
|
||||
$operation->pull($server, $url, $directory, JsonValueGuard::jsonArray($options));
|
||||
|
||||
Activity::event('server:file.pull')
|
||||
->property('directory', $directory)
|
||||
|
||||
@@ -11,7 +11,6 @@ use Pterodactyl\Contracts\Servers\RecordsServerInstallResults;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;
|
||||
use Pterodactyl\Services\Servers\ServerInstallService;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
class ServerInstallController extends Controller
|
||||
{
|
||||
@@ -28,14 +27,7 @@ class ServerInstallController extends Controller
|
||||
*/
|
||||
public function store(InstallationDataRequest $request, RecordsServerInstallResults $installResult, string $uuid): JsonResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
|
||||
$installResult->record(
|
||||
$request,
|
||||
$uuid,
|
||||
JsonValueGuard::boolean($validated['successful'] ?? false),
|
||||
JsonValueGuard::boolean($validated['reinstall'] ?? false),
|
||||
);
|
||||
$installResult->record($request, $uuid, $request->boolean('successful'), $request->boolean('reinstall'));
|
||||
|
||||
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Pterodactyl\Http\Requests\Api\Admin\Eggs;
|
||||
|
||||
use Pterodactyl\Enum\Permissions;
|
||||
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
class UpdateEggScriptRequest extends AdminApiRequest
|
||||
{
|
||||
@@ -29,4 +30,18 @@ class UpdateEggScriptRequest extends AdminApiRequest
|
||||
'copy_script_from' => ['sometimes', 'nullable', 'numeric'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ModelAttributes
|
||||
*/
|
||||
public function payload(): array
|
||||
{
|
||||
$data = $this->validated();
|
||||
JsonValueGuard::assertPayload9($data);
|
||||
if (array_key_exists('script_is_privileged', $data)) {
|
||||
$data['script_is_privileged'] = $this->boolean('script_is_privileged');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ class StoreNodeRequest extends AdminApiRequest
|
||||
|
||||
foreach (['public', 'behind_proxy', 'maintenance_mode'] as $boolean) {
|
||||
if (array_key_exists($boolean, $data)) {
|
||||
$response[$boolean] = JsonValueGuard::boolean(Arr::get($data, $boolean));
|
||||
$response[$boolean] = $this->boolean($boolean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ class StoreUserRequest extends AdminApiRequest
|
||||
}
|
||||
|
||||
if (array_key_exists('root_admin', $validated)) {
|
||||
$data['root_admin'] = JsonValueGuard::boolean(Arr::get($validated, 'root_admin'));
|
||||
$data['root_admin'] = $this->boolean('root_admin');
|
||||
}
|
||||
|
||||
$language = Arr::get($validated, 'preferences.language');
|
||||
|
||||
@@ -105,7 +105,7 @@ class StoreNodeRequest extends ApplicationApiRequest
|
||||
|
||||
foreach (['public', 'behind_proxy', 'maintenance_mode'] as $boolean) {
|
||||
if (array_key_exists($boolean, $data)) {
|
||||
$response[$boolean] = JsonValueGuard::boolean(Arr::get($data, $boolean));
|
||||
$response[$boolean] = $this->boolean($boolean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class StoreUserRequest extends ApplicationApiRequest
|
||||
}
|
||||
|
||||
if (array_key_exists('root_admin', $validated)) {
|
||||
$data['root_admin'] = JsonValueGuard::boolean(Arr::get($validated, 'root_admin'));
|
||||
$data['root_admin'] = $this->boolean('root_admin');
|
||||
}
|
||||
|
||||
$language = Arr::get($validated, 'language');
|
||||
|
||||
@@ -4,11 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Backups;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
|
||||
use Pterodactyl\Enum\Permissions;
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
class RestoreBackupRequest extends ClientApiRequest implements ClientPermissionsRequest
|
||||
{
|
||||
@@ -30,10 +28,8 @@ class RestoreBackupRequest extends ClientApiRequest implements ClientPermissions
|
||||
*/
|
||||
public function payload(): array
|
||||
{
|
||||
$data = parent::validated();
|
||||
|
||||
return [
|
||||
'truncate' => JsonValueGuard::boolean(Arr::get($data, 'truncate')),
|
||||
'truncate' => $this->boolean('truncate'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ class StoreBackupRequest extends ClientApiRequest implements ClientPermissionsRe
|
||||
|
||||
return [
|
||||
'name' => JsonValueGuard::nullableString(Arr::get($data, 'name')),
|
||||
'is_locked' => JsonValueGuard::boolean(Arr::get($data, 'is_locked', false)),
|
||||
'is_locked' => $this->boolean('is_locked'),
|
||||
'ignored' => JsonValueGuard::nullableString(Arr::get($data, 'ignored')),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -44,13 +44,13 @@ class StoreScheduleRequest extends ViewScheduleRequest
|
||||
|
||||
return [
|
||||
'name' => JsonValueGuard::string(Arr::get($data, 'name')),
|
||||
'is_active' => array_key_exists('is_active', $data) && JsonValueGuard::boolean(Arr::get($data, 'is_active')),
|
||||
'is_active' => $this->boolean('is_active'),
|
||||
'minute' => JsonValueGuard::string(Arr::get($data, 'minute')),
|
||||
'hour' => JsonValueGuard::string(Arr::get($data, 'hour')),
|
||||
'day_of_month' => JsonValueGuard::string(Arr::get($data, 'day_of_month')),
|
||||
'month' => JsonValueGuard::string(Arr::get($data, 'month')),
|
||||
'day_of_week' => JsonValueGuard::string(Arr::get($data, 'day_of_week')),
|
||||
'only_when_online' => JsonValueGuard::boolean(Arr::get($data, 'only_when_online', false)),
|
||||
'only_when_online' => $this->boolean('only_when_online'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ class StoreTaskRequest extends ViewScheduleRequest
|
||||
'payload' => JsonValueGuard::nullableScalarString(Arr::get($data, 'payload')) ?? '',
|
||||
'time_offset' => JsonValueGuard::integer(Arr::get($data, 'time_offset')),
|
||||
'sequence_id' => $sequenceId === null || $sequenceId === '' ? null : JsonValueGuard::integer($sequenceId),
|
||||
'continue_on_failure' => JsonValueGuard::boolean(Arr::get($data, 'continue_on_failure', false)),
|
||||
'continue_on_failure' => $this->boolean('continue_on_failure'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ declare(strict_types=1);
|
||||
namespace Pterodactyl\Http\Requests\Api\Remote;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
class ReportBackupRestoreRequest extends FormRequest
|
||||
{
|
||||
@@ -30,10 +28,8 @@ class ReportBackupRestoreRequest extends FormRequest
|
||||
*/
|
||||
public function payload(): array
|
||||
{
|
||||
$data = parent::validated();
|
||||
|
||||
return [
|
||||
'successful' => JsonValueGuard::boolean(Arr::get($data, 'successful')),
|
||||
'successful' => $this->boolean('successful'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,13 @@ test('create node', function (): void {
|
||||
$node = Node::query()->where('name', 'NewNode')->first();
|
||||
$response->assertJson(['object' => 'node', 'attributes' => ['id' => $node->id, 'uuid' => $node->uuid, 'name' => $node->name, 'fqdn' => $node->fqdn, 'location_id' => $node->location_id, 'memory' => $node->memory, 'disk' => $node->disk], 'meta' => ['resource' => route('api.admin.nodes.view', ['node' => $node->id])]]);
|
||||
});
|
||||
test('create node accepts integer and string flags', function (): void {
|
||||
$location = Location::factory()->create();
|
||||
$this->postJson(route('api.admin.nodes.store'), ['name' => 'FlagNode', 'location_id' => $location->id, 'public' => '1', 'fqdn' => 'localhost', 'scheme' => 'https', 'behind_proxy' => 0, 'maintenance_mode' => 1, 'memory' => 2048, 'memory_overallocate' => 0, 'disk' => 20480, 'disk_overallocate' => 0, 'daemon_listen' => 8080, 'daemon_sftp' => 2022])
|
||||
->assertStatus(Response::HTTP_CREATED);
|
||||
|
||||
$this->assertDatabaseHas('nodes', ['name' => 'FlagNode', 'public' => true, 'behind_proxy' => false, 'maintenance_mode' => true]);
|
||||
});
|
||||
test('create node rejects ip address when using https', function (): void {
|
||||
$location = Location::factory()->create();
|
||||
$response = $this->postJson(route('api.admin.nodes.store'), ['name' => 'IpNode', 'description' => 'A node with an invalid HTTPS address.', 'location_id' => $location->id, 'public' => true, 'fqdn' => '127.0.0.1', 'scheme' => 'https', 'behind_proxy' => false, 'maintenance_mode' => false, 'memory' => 2048, 'memory_overallocate' => 0, 'disk' => 20480, 'disk_overallocate' => 0, 'upload_size' => 100, 'daemon_listen' => 8080, 'daemon_sftp' => 2022, 'daemon_base' => '/var/lib/pterodactyl/volumes']);
|
||||
|
||||
@@ -97,6 +97,10 @@ test('a user can be created with an eight character password', function (): void
|
||||
$this->postJson('/api/application/users', ['username' => 'okuser', 'email' => 'ok@example.com', 'first_name' => 'Ok', 'last_name' => 'Password', 'password' => 'abcdefgh'])->assertCreated();
|
||||
expect(Hash::check('abcdefgh', User::query()->where('username', 'okuser')->firstOrFail()->password))->toBeTrue();
|
||||
});
|
||||
test('root admin status accepts integer and string flags', function (int|string $flag, bool $expected): void {
|
||||
$this->postJson('/api/application/users', ['username' => 'flaguser', 'email' => 'flag@example.com', 'first_name' => 'Flag', 'last_name' => 'User', 'root_admin' => $flag])->assertCreated();
|
||||
expect(User::query()->where('username', 'flaguser')->firstOrFail()->root_admin)->toBe($expected);
|
||||
})->with([[1, true], ['0', false]]);
|
||||
test('a user email must not start with a dash', function (string $method, string $suffix): void {
|
||||
$user = User::factory()->create();
|
||||
$this->{$method}('/api/application/users'.str_replace('{user}', (string) $user->id, $suffix), ['username' => 'dashuser', 'email' => '-x@example.com', 'first_name' => 'Dash', 'last_name' => 'User'])
|
||||
|
||||
@@ -43,6 +43,15 @@ test('backup can be created without a name', function (): void {
|
||||
expect($response->json('attributes.name'))->toStartWith('Backup at ');
|
||||
$fake->assertBackedUp($response->json('attributes.uuid'));
|
||||
});
|
||||
test('backup lock accepts null and every value the boolean rule allows', function (bool|int|string|null $locked, bool $expected): void {
|
||||
$user = User::factory()->create();
|
||||
$server = $this->createServerModel(['owner_id' => $user->id, 'backup_limit' => 3]);
|
||||
new FakeDaemonBackup;
|
||||
|
||||
$this->actingAs($user)->postJson($this->link($server, '/backups'), ['name' => 'Flagged', 'is_locked' => $locked])
|
||||
->assertOk()
|
||||
->assertJsonPath('attributes.is_locked', $expected);
|
||||
})->with([[null, false], [1, true], ['1', true], [0, false]]);
|
||||
test('backup cannot be created past the server limit', function () {
|
||||
$user = User::factory()->create();
|
||||
$server = $this->createServerModel(['owner_id' => $user->id, 'backup_limit' => 1]);
|
||||
@@ -67,7 +76,8 @@ test('backup can be restored', function () {
|
||||
[$user, $server] = $this->generateTestAccount([Permissions::BackupRestore->value]);
|
||||
$backup = Backup::factory()->create(['server_id' => $server->id, 'completed_at' => now(), 'is_successful' => true]);
|
||||
$fake = new FakeDaemonBackup;
|
||||
$this->actingAs($user)->postJson($this->link($backup, '/restore'), ['truncate' => false])->assertNoContent();
|
||||
// A form-encoded "0" is as valid as false.
|
||||
$this->actingAs($user)->post($this->link($backup, '/restore'), ['truncate' => '0'], ['Accept' => 'application/json'])->assertNoContent();
|
||||
// The server is placed into a restoring state while Wings works.
|
||||
expect($server->refresh()->status)->toBe('restoring_backup');
|
||||
$fake->assertRestored($backup->uuid);
|
||||
|
||||
@@ -47,6 +47,15 @@ test('schedule can be created for server', function (array $permissions): void {
|
||||
]);
|
||||
$response->assertJsonCount(0, 'attributes.relationships.tasks.data');
|
||||
})->with('permissionsDataProvider');
|
||||
test('schedule flags accept every value the boolean rule allows', function (bool|int|string $active, bool $expected): void {
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
$id = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/schedules", ['name' => 'Flags', 'is_active' => $active, 'only_when_online' => $active, 'minute' => '0', 'hour' => '*', 'day_of_week' => '*', 'month' => '*', 'day_of_month' => '*'])
|
||||
->assertOk()
|
||||
->json('attributes.id');
|
||||
|
||||
$schedule = Schedule::query()->findOrFail($id);
|
||||
expect($schedule->is_active)->toBe($expected)->and($schedule->only_when_online)->toBe($expected);
|
||||
})->with([[1, true], ['1', true], [0, false], ['0', false], [true, true]]);
|
||||
test('schedule validation rules', function (): void {
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
$response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/schedules", []);
|
||||
|
||||
Reference in New Issue
Block a user