mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 11:55:51 -05:00
## Problem
Extensions can render content before and after most admin pages, but not
inside the forms on them. The only exception was
`panel.users.detail.form`, and it could only re-render or validate the
user's existing fields. An extension had no way to add a field of its
own, because the Panel's save requests had nowhere to carry the value
and nothing loaded it back.
## Changes
### Slots inside every admin form
The create and edit forms for users, nodes, servers, eggs, locations,
mounts and database hosts each render a slot inside the form, such as
`panel.servers.create.form` and `panel.servers.detail.details.form`. The
slot receives the native `form`, the form's `kind` (`admin.server`), and
`mode` (`'create'` or `'edit'`). Edit slots also receive the saved
`resource`. Contributed `form.AppField` controls share the form's
validation, pending state and submit action.
### Extension-owned fields
An extension registers fields in its provider:
```php
$this->registerFormFields('admin.user', ['plan' => ['required', 'in:free,pro']]);
$this->registerFormFields(
'admin.node',
['region' => ['required', 'string', 'max:32']],
fn (Node $node): array => ['region' => NodeRegion::for($node)],
fn (Node $node, array $values) => NodeRegion::store($node, $values['region']),
);
```
The 14 create and update endpoints accept an optional `extensions: {
[extensionId]: { [field]: value } }` body field. Each extension's rules
are applied as `extensions.{id}.{field}`, and only when the request
includes that extension's values. The extension's `save` callback runs
in the same database transaction as the core change, so a failure rolls
both back.
Server creation is the exception. Wings reads the new server while
provisioning it, so extension values are saved after provisioning
succeeds, outside the transaction.
For Node updates when Wings is unreachable. `UpdateNode` saves the node
and then throws `ConfigurationNotPersistedException` as a warning. The
node controller defers that exception until the extension values are
saved, so the node update is no longer rolled back.
`GET /api/admin/extensions/forms/{form}/{id}` returns every extension's
current values. It requires the resource's read permission. Edit pages
fetch it before rendering the form, but only when an extension has
registered fields for that form; the bootstrap payload now lists them
per extension in `forms`.
For users and servers the callbacks are optional, and values are stored
in the extension's scoped settings (`settings()->forUser($user)`,
`forServer($server)`). Values are limited to strings, numbers, booleans,
`null`, or lists of strings and numbers, which keeps the OpenAPI schema
and generated types concrete.
A `load` callback that throws, or returns an invalid value, is recorded
against the extension in the `form` phase. The frontend then hides that
extension's slot contributions on the edit page, so a save cannot
overwrite its stored values with defaults. Fields of a disabled or
failed extension are neither validated, saved nor loaded.
---------
Co-authored-by: robert dennis <31261583+robertdrakedennis@users.noreply.github.com>
138 lines
6.5 KiB
PHP
138 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Locations;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Response;
|
|
use Knuckles\Scribe\Attributes\Endpoint;
|
|
use Knuckles\Scribe\Attributes\Group;
|
|
use Knuckles\Scribe\Attributes\QueryParam;
|
|
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
|
|
use Knuckles\Scribe\Attributes\Subgroup;
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
use Pterodactyl\Contracts\Locations\CreatesLocations;
|
|
use Pterodactyl\Contracts\Locations\DeletesLocations;
|
|
use Pterodactyl\Contracts\Locations\UpdatesLocations;
|
|
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException;
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ExtensionFieldsParam;
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
|
|
use Pterodactyl\Facades\Fractal;
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest;
|
|
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
|
|
use Pterodactyl\Models\Location;
|
|
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
#[Group('Application API', 'Root administrator endpoints for managing panel resources using application API tokens.')]
|
|
#[Subgroup('Locations', 'Create, update, retrieve, and delete physical or logical deployment locations.')]
|
|
class LocationController extends ApplicationApiController
|
|
{
|
|
private const array ACTIVE_NODES_ERROR = [
|
|
'errors' => [
|
|
[
|
|
'code' => 'HasActiveNodesException',
|
|
'status' => '400',
|
|
'detail' => 'Cannot delete a location that has active nodes attached to it.',
|
|
],
|
|
],
|
|
];
|
|
|
|
/**
|
|
* Return all the locations currently registered on the Panel.
|
|
*
|
|
* @return ApiPayload
|
|
*/
|
|
#[Endpoint('List locations', 'Returns a paginated list of all locations registered on the panel.')]
|
|
#[QueryParam('per_page', 'integer', 'Number of locations to return per page, up to 100.', required: false, example: 50)]
|
|
#[QueryParam('filter[short]', 'string', 'Filter locations by short identifier.', required: false, example: 'us-east')]
|
|
#[QueryParam('filter[long]', 'string', 'Filter locations by description.', required: false, example: 'US East Datacenter')]
|
|
#[QueryParam('sort', 'string', 'Sort locations by id. Prefix with "-" for descending order.', required: false, example: 'id', enum: ['id', '-id'])]
|
|
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "nodes" and "servers" when the key can read those resources.', required: false, example: 'nodes,servers', enum: ['nodes', 'servers', 'nodes,servers'])]
|
|
#[ResponseFromTransformer(LocationTransformer::class, Location::class, collection: true, resourceKey: 'location', paginate: [IlluminatePaginatorAdapter::class, 50])]
|
|
public function index(GetLocationsRequest $request): array
|
|
{
|
|
$locations = QueryBuilder::for(Location::query())
|
|
->allowedFilters(['short', 'long'])
|
|
->allowedSorts(['id'])
|
|
->paginate($request->perPage());
|
|
|
|
return Fractal::collection($locations)
|
|
->transformWith($this->getTransformer(LocationTransformer::class))
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Return a single location.
|
|
*
|
|
* @return ApiPayload
|
|
*/
|
|
#[Endpoint('Get location', 'Returns a single location by internal numeric ID.')]
|
|
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "nodes" and "servers" when the key can read those resources.', required: false, example: 'nodes,servers', enum: ['nodes', 'servers', 'nodes,servers'])]
|
|
#[ResponseFromTransformer(LocationTransformer::class, Location::class, resourceKey: 'location')]
|
|
public function view(GetLocationRequest $request, Location $location): array
|
|
{
|
|
return Fractal::item($location)
|
|
->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields())
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Store a new location on the Panel and return an HTTP/201 response code with the
|
|
* new location attached.
|
|
*/
|
|
#[Endpoint('Create location', 'Creates a new location and returns the created resource.')]
|
|
#[ResponseFromTransformer(LocationTransformer::class, Location::class, status: 201, description: 'Location created.', resourceKey: 'location', meta: ['resource' => 'https://panel.example.test/api/application/locations/1'])]
|
|
#[ExtensionFieldsParam]
|
|
public function store(StoreLocationRequest $request, CreatesLocations $locations): JsonResponse
|
|
{
|
|
$location = $locations->create($request->payload());
|
|
|
|
return Fractal::item($location)
|
|
->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields())
|
|
->addMeta([
|
|
'resource' => route('api.application.locations.view', [
|
|
'location' => $location->id,
|
|
]),
|
|
])
|
|
->respond(201);
|
|
}
|
|
|
|
/**
|
|
* Update a location on the Panel and return the updated record to the user.
|
|
*
|
|
* @return ApiPayload
|
|
*/
|
|
#[Endpoint('Update location', 'Updates an existing location by internal numeric ID.')]
|
|
#[ResponseFromTransformer(LocationTransformer::class, Location::class, resourceKey: 'location')]
|
|
#[ExtensionFieldsParam]
|
|
public function update(UpdateLocationRequest $request, UpdatesLocations $locations, Location $location): array
|
|
{
|
|
$location = $locations->update($location, $request->payload());
|
|
|
|
return Fractal::item($location)
|
|
->transformWith($this->getTransformer(LocationTransformer::class)->withExtensionFields())
|
|
->toResponseArray();
|
|
}
|
|
|
|
/**
|
|
* Delete a location from the Panel.
|
|
*
|
|
* @throws HasActiveNodesException
|
|
*/
|
|
#[Endpoint('Delete location', 'Deletes a location by internal numeric ID.')]
|
|
#[ScribeResponse(status: 204, description: 'Location deleted.')]
|
|
#[ScribeResponse(self::ACTIVE_NODES_ERROR, status: 400, description: 'The location has active nodes attached.')]
|
|
public function delete(DeleteLocationRequest $request, DeletesLocations $locations, Location $location): Response
|
|
{
|
|
$locations->delete($location);
|
|
|
|
return response('', 204);
|
|
}
|
|
}
|