mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-03 21:05:45 -05:00
location actions
This commit is contained in:
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace Pterodactyl\Actions\Locations;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Pterodactyl\Contracts\Locations\CreatesLocations;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Services\Extensions\ExtensionFields;
|
||||
@@ -15,17 +17,45 @@ final readonly class CreateLocation implements CreatesLocations
|
||||
public function __construct(private ExtensionFields $extensions) {}
|
||||
|
||||
/**
|
||||
* Create a new location from validated attributes.
|
||||
* A location's fields. Pass the location being updated so its own short code is not
|
||||
* taken for a duplicate.
|
||||
*
|
||||
* @param LocationCreationData $data
|
||||
* @return NormalizedValidationRules
|
||||
*/
|
||||
public function create(array $data): Location
|
||||
public static function rules(?Location $location = null): array
|
||||
{
|
||||
$extensions = ValidatedExtensionValues::of($data['extensions'] ?? null);
|
||||
unset($data['extensions']);
|
||||
return [
|
||||
'short' => ['required', 'string', 'between:1,60', Rule::unique('locations', 'short')->ignore($location)],
|
||||
'long' => ['string', 'nullable', 'between:1,191'],
|
||||
];
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($data, $extensions): Location {
|
||||
$location = Location::query()->create($data);
|
||||
/**
|
||||
* Validates location input and returns the model attributes it describes.
|
||||
*
|
||||
* @param ActionInput $input
|
||||
* @return array{short: string, long: string|null}
|
||||
*/
|
||||
public static function attributes(array $input, ?Location $location = null): array
|
||||
{
|
||||
$input = Validator::make($input, self::rules($location))->safe();
|
||||
|
||||
return [
|
||||
'short' => $input->string('short')->toString(),
|
||||
'long' => $input->filled('long') ? $input->string('long')->toString() : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActionInput $input
|
||||
*/
|
||||
public function create(array $input): Location
|
||||
{
|
||||
$attributes = self::attributes($input);
|
||||
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
|
||||
|
||||
return DB::transaction(function () use ($attributes, $extensions): Location {
|
||||
$location = Location::query()->create($attributes);
|
||||
$this->extensions->save($location, $extensions);
|
||||
|
||||
return $location;
|
||||
|
||||
@@ -15,17 +15,15 @@ final readonly class UpdateLocation implements UpdatesLocations
|
||||
public function __construct(private ExtensionFields $extensions) {}
|
||||
|
||||
/**
|
||||
* Update an existing location from validated attributes.
|
||||
*
|
||||
* @param LocationUpdateData $data
|
||||
* @param ActionInput $input
|
||||
*/
|
||||
public function update(Location $location, array $data): Location
|
||||
public function update(Location $location, array $input): Location
|
||||
{
|
||||
$extensions = ValidatedExtensionValues::of($data['extensions'] ?? null);
|
||||
unset($data['extensions']);
|
||||
$attributes = CreateLocation::attributes($input, $location);
|
||||
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
|
||||
|
||||
DB::transaction(function () use ($location, $data, $extensions): void {
|
||||
$location->update($data);
|
||||
DB::transaction(function () use ($location, $attributes, $extensions): void {
|
||||
$location->update($attributes);
|
||||
$this->extensions->save($location, $extensions);
|
||||
});
|
||||
|
||||
|
||||
@@ -7,10 +7,9 @@ namespace Pterodactyl\Console\Commands\Location;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Pterodactyl\Contracts\Locations\CreatesLocations;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use Pterodactyl\Validation\LocationRules;
|
||||
|
||||
#[Description('Creates a new location on the system via the CLI.')]
|
||||
#[Signature('p:location:make
|
||||
@@ -26,19 +25,19 @@ class MakeLocationCommand extends Command
|
||||
$short = $this->option('short') ?? $this->ask(trans('command/messages.location.ask_short'));
|
||||
$long = $this->option('long') ?? $this->ask(trans('command/messages.location.ask_long'));
|
||||
|
||||
$validator = Validator::make(['short' => $short, 'long' => $long], LocationRules::rules());
|
||||
if ($validator->fails()) {
|
||||
foreach ($validator->errors()->all() as $error) {
|
||||
try {
|
||||
$location = $locations->create([
|
||||
'short' => JsonValueGuard::nullableString($short),
|
||||
'long' => JsonValueGuard::nullableString($long),
|
||||
]);
|
||||
} catch (ValidationException $validationException) {
|
||||
foreach ($validationException->validator->errors()->all() as $error) {
|
||||
$this->error($error);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$location = $locations->create([
|
||||
'short' => JsonValueGuard::string($short),
|
||||
'long' => JsonValueGuard::nullableString($long),
|
||||
]);
|
||||
$this->line(trans('command/messages.location.created', [
|
||||
'name' => $location->short,
|
||||
'id' => $location->id,
|
||||
|
||||
@@ -9,9 +9,10 @@ use Pterodactyl\Models\Location;
|
||||
interface CreatesLocations
|
||||
{
|
||||
/**
|
||||
* Create a new location from validated attributes.
|
||||
* Create a new location. The input is validated here, so it can come straight from any caller;
|
||||
* extension field values go under `extensions` as ValidatedExtensionValues.
|
||||
*
|
||||
* @param LocationCreationData $data
|
||||
* @param ActionInput $input short and long
|
||||
*/
|
||||
public function create(array $data): Location;
|
||||
public function create(array $input): Location;
|
||||
}
|
||||
|
||||
@@ -9,9 +9,10 @@ use Pterodactyl\Models\Location;
|
||||
interface UpdatesLocations
|
||||
{
|
||||
/**
|
||||
* Update an existing location from validated attributes.
|
||||
* Update an existing location. The input is validated here, so it can come straight from any caller;
|
||||
* extension field values go under `extensions` as ValidatedExtensionValues.
|
||||
*
|
||||
* @param LocationUpdateData $data
|
||||
* @param ActionInput $input short and long
|
||||
*/
|
||||
public function update(Location $location, array $data): Location;
|
||||
public function update(Location $location, array $input): Location;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ class LocationController extends AdminApiController
|
||||
#[ExtensionFieldsParam]
|
||||
public function store(StoreLocationRequest $request, CreatesLocations $locations): JsonResponse
|
||||
{
|
||||
$location = $locations->create($request->payload());
|
||||
$location = $locations->create($request->actionInput());
|
||||
|
||||
Activity::event('admin:location.create')
|
||||
->subject($location)
|
||||
@@ -116,7 +116,7 @@ class LocationController extends AdminApiController
|
||||
#[ExtensionFieldsParam]
|
||||
public function update(UpdateLocationRequest $request, UpdatesLocations $locations, Location $location): array
|
||||
{
|
||||
$location = $locations->update($location, $request->payload());
|
||||
$location = $locations->update($location, $request->actionInput());
|
||||
|
||||
Activity::event('admin:location.update')
|
||||
->subject($location)
|
||||
|
||||
@@ -91,7 +91,7 @@ class LocationController extends ApplicationApiController
|
||||
#[ExtensionFieldsParam]
|
||||
public function store(StoreLocationRequest $request, CreatesLocations $locations): JsonResponse
|
||||
{
|
||||
$location = $locations->create($request->payload());
|
||||
$location = $locations->create($request->actionInput());
|
||||
|
||||
return Fractal::item($location)
|
||||
->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields())
|
||||
@@ -113,7 +113,7 @@ class LocationController extends ApplicationApiController
|
||||
#[ExtensionFieldsParam]
|
||||
public function update(UpdateLocationRequest $request, UpdatesLocations $locations, Location $location): array
|
||||
{
|
||||
$location = $locations->update($location, $request->payload());
|
||||
$location = $locations->update($location, $request->actionInput());
|
||||
|
||||
return Fractal::item($location)
|
||||
->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields())
|
||||
|
||||
@@ -5,11 +5,11 @@ declare(strict_types=1);
|
||||
namespace Pterodactyl\Http\Requests\Api\Admin\Locations;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Pterodactyl\Actions\Locations\CreateLocation;
|
||||
use Pterodactyl\Enum\Permissions;
|
||||
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
|
||||
use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Validation\LocationRules;
|
||||
|
||||
class StoreLocationRequest extends AdminApiRequest
|
||||
{
|
||||
@@ -27,7 +27,7 @@ class StoreLocationRequest extends AdminApiRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return LocationRules::rules();
|
||||
return CreateLocation::rules();
|
||||
}
|
||||
|
||||
/** Rename fields to be more clear in error messages. */
|
||||
@@ -39,16 +39,6 @@ class StoreLocationRequest extends AdminApiRequest
|
||||
];
|
||||
}
|
||||
|
||||
/** @return LocationCreationData */
|
||||
public function payload(): array
|
||||
{
|
||||
return [
|
||||
'short' => $this->string('short')->toString(),
|
||||
'long' => $this->filled('long') ? $this->string('long')->toString() : null,
|
||||
'extensions' => $this->extensionValues(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -4,9 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Admin\Locations;
|
||||
|
||||
use Pterodactyl\Actions\Locations\CreateLocation;
|
||||
use Pterodactyl\Enum\Permissions;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Validation\LocationRules;
|
||||
|
||||
class UpdateLocationRequest extends StoreLocationRequest
|
||||
{
|
||||
@@ -22,7 +22,7 @@ class UpdateLocationRequest extends StoreLocationRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return LocationRules::rules($this->parameter('location', Location::class));
|
||||
return CreateLocation::rules($this->parameter('location', Location::class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,11 +5,11 @@ declare(strict_types=1);
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Locations;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Pterodactyl\Actions\Locations\CreateLocation;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Validation\LocationRules;
|
||||
|
||||
class StoreLocationRequest extends ApplicationApiRequest
|
||||
{
|
||||
@@ -26,7 +26,7 @@ class StoreLocationRequest extends ApplicationApiRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return LocationRules::rules();
|
||||
return CreateLocation::rules();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,16 +40,6 @@ class StoreLocationRequest extends ApplicationApiRequest
|
||||
];
|
||||
}
|
||||
|
||||
/** @return LocationCreationData */
|
||||
public function payload(): array
|
||||
{
|
||||
return [
|
||||
'short' => $this->string('short')->toString(),
|
||||
'long' => $this->filled('long') ? $this->string('long')->toString() : null,
|
||||
'extensions' => $this->extensionValues(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -4,8 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Locations;
|
||||
|
||||
use Pterodactyl\Actions\Locations\CreateLocation;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Validation\LocationRules;
|
||||
|
||||
class UpdateLocationRequest extends StoreLocationRequest
|
||||
{
|
||||
@@ -16,7 +16,7 @@ class UpdateLocationRequest extends StoreLocationRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return LocationRules::rules($this->parameter('location', Location::class));
|
||||
return CreateLocation::rules($this->parameter('location', Location::class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,6 +37,16 @@ trait ValidatesExtensionFields
|
||||
return $this->extensionValues ?? ValidatedExtensionValues::none();
|
||||
}
|
||||
|
||||
/**
|
||||
* The validated input, with the validated extension field values under `extensions`.
|
||||
*
|
||||
* @return ActionInput
|
||||
*/
|
||||
public function actionInput(): array
|
||||
{
|
||||
return [...parent::actionInput(), 'extensions' => $this->extensionValues()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses extension values the signed-in user may not change before anything is
|
||||
* validated, then validates each extension's values once the request's own rules ran,
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Validation;
|
||||
|
||||
use Illuminate\Validation\Rule;
|
||||
use Pterodactyl\Models\Location;
|
||||
|
||||
final class LocationRules
|
||||
{
|
||||
/**
|
||||
* Validation rules for the location requests and p:location:make.
|
||||
*
|
||||
* @param Location|null $ignore the location being updated
|
||||
* @return NormalizedValidationRules
|
||||
*/
|
||||
public static function rules(?Location $ignore = null): array
|
||||
{
|
||||
return [
|
||||
'short' => ['required', 'string', 'between:1,60', Rule::unique('locations', 'short')->ignore($ignore)],
|
||||
'long' => ['string', 'nullable', 'between:1,191'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -47,7 +47,7 @@ parameters:
|
||||
ApiKeyCreationData: 'array{memo: string, user_id: int, allowed_ips?: list<string>}'
|
||||
ValidationData: 'array<string, JsonValue>'
|
||||
# Input an action validates itself, from a request or from any other caller.
|
||||
ActionInput: 'array<string, ApiValue9>'
|
||||
ActionInput: 'array<string, ApiValue9|\Pterodactyl\Services\Extensions\ValidatedExtensionValues>'
|
||||
SoftwareVersionData: 'array{panel?: string, wings?: string, discord?: string, donations?: string}'
|
||||
ServerBuildModificationData: 'array{oom_disabled?: bool, memory?: int, swap?: int, io?: int, cpu?: int, threads?: string|null, disk?: int, allocation_id?: int, database_limit?: int|null, allocation_limit?: int|null, backup_limit?: int|null, add_allocations?: list<int>, remove_allocations?: list<int>}'
|
||||
ServerStartupModificationData: 'array{environment?: array<string, ApiScalar>, egg_id?: int, startup?: string, skip_scripts?: bool, docker_image?: string}'
|
||||
@@ -90,8 +90,6 @@ parameters:
|
||||
DatabaseHostUpdateData: 'array{name: string, host: string, port: int, username: string, password?: string|null, node_id: int|null, extensions?: \Pterodactyl\Services\Extensions\ValidatedExtensionValues}'
|
||||
EggVariableData: 'array{name: string, description: string, env_variable: string, default_value: string, user_viewable: bool, user_editable: bool, rules: string}'
|
||||
MultipartUploadPart: 'array{etag: string, part_number: int}'
|
||||
LocationCreationData: 'array{short: string, long?: string|null, extensions?: \Pterodactyl\Services\Extensions\ValidatedExtensionValues}'
|
||||
LocationUpdateData: 'array{short?: string, long?: string|null, extensions?: \Pterodactyl\Services\Extensions\ValidatedExtensionValues}'
|
||||
MountData: 'array{name: string, description?: string|null, source: string, target: string, read_only?: bool, user_mountable?: bool, extensions?: \Pterodactyl\Services\Extensions\ValidatedExtensionValues}'
|
||||
TagData: 'array{name: string, slug: string, color?: string|null}'
|
||||
ApiKeyUpdateData: 'array{memo: string|null}'
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Tests\Pest\Integration\Actions\Locations\CreateLocationTest;
|
||||
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Contracts\Locations\CreatesLocations;
|
||||
use Pterodactyl\Contracts\Locations\UpdatesLocations;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
||||
|
||||
uses(IntegrationTestCase::class, DatabaseTransactions::class);
|
||||
|
||||
test('locations created or updated without the http api meet the same rules', function (): void {
|
||||
$locations = $this->app->make(CreatesLocations::class);
|
||||
$taken = Location::factory()->create(['short' => 'taken']);
|
||||
|
||||
expect(fn () => $locations->create(['short' => 'taken']))->toThrow(ValidationException::class)
|
||||
->and(fn () => $locations->create(['short' => str_repeat('a', 61)]))->toThrow(ValidationException::class);
|
||||
|
||||
$location = $locations->create(['short' => 'fresh', 'long' => '']);
|
||||
expect($location->long)->toBeNull();
|
||||
|
||||
// A location keeps its own short code when it is updated.
|
||||
$this->app->make(UpdatesLocations::class)->update($taken, ['short' => 'taken', 'long' => 'Still here']);
|
||||
expect($taken->refresh()->long)->toBe('Still here');
|
||||
});
|
||||
|
||||
test('extension field values that no request validated are refused', function (): void {
|
||||
expect(fn () => $this->app->make(CreatesLocations::class)->create(['short' => 'ext', 'extensions' => ['billing' => ['plan' => 'pro']]]))
|
||||
->toThrow(InvalidArgumentException::class, 'must come from a request that validated them');
|
||||
});
|
||||
Reference in New Issue
Block a user