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>
115 lines
3.3 KiB
PHP
115 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Services\Extensions;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Routing\RouteCollection;
|
|
use Illuminate\Routing\Router;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Throwable;
|
|
|
|
/** Stages the provider helpers until Laravel has successfully booted the provider. */
|
|
final class ExtensionRegistration
|
|
{
|
|
/** @var list<Closure(): void>|null */
|
|
private ?array $pending = null;
|
|
|
|
private bool $active = true;
|
|
|
|
public function __construct(
|
|
private readonly Application $app,
|
|
private readonly Router $router,
|
|
private readonly ExtensionSettingsRegistry $settings,
|
|
private readonly ExtensionPermissionRegistry $permissions,
|
|
private readonly ExtensionActionDecorators $actions,
|
|
private readonly ExtensionConsoleRegistry $console,
|
|
private readonly ExtensionHeadTags $headTags,
|
|
private readonly ExtensionFieldRegistry $fields,
|
|
) {}
|
|
|
|
public function begin(): void
|
|
{
|
|
$this->pending = [];
|
|
$this->active = false;
|
|
}
|
|
|
|
/** @param Closure(): void $registration */
|
|
public function defer(Closure $registration): void
|
|
{
|
|
if ($this->pending === null) {
|
|
$registration();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->pending[] = $registration;
|
|
}
|
|
|
|
public function commit(string $identifier): void
|
|
{
|
|
if ($this->pending === null) {
|
|
return;
|
|
}
|
|
|
|
$definition = $this->settings->get($identifier);
|
|
$permissions = $this->permissions->all()[ExtensionPermissionRegistry::group($identifier)] ?? null;
|
|
$actions = $this->actions->snapshot();
|
|
$console = $this->console->snapshot();
|
|
$headTags = $this->headTags->snapshot();
|
|
$fields = $this->fields->snapshot();
|
|
$router = new ExtensionStagingRouter($this->router);
|
|
|
|
Route::swap($router);
|
|
$registrations = $this->pending;
|
|
$this->pending = null;
|
|
try {
|
|
foreach ($registrations as $registration) {
|
|
$registration();
|
|
}
|
|
|
|
$router->apply();
|
|
$this->active = true;
|
|
} catch (Throwable $throwable) {
|
|
$this->settings->unregister($identifier);
|
|
if ($definition instanceof ExtensionSettingsDefinition) {
|
|
$this->settings->register($identifier, $definition);
|
|
}
|
|
|
|
$this->permissions->unregister($identifier);
|
|
if ($permissions !== null) {
|
|
$this->permissions->register($identifier, $permissions['description'], $permissions['keys']);
|
|
}
|
|
|
|
$this->actions->restore($actions);
|
|
$this->console->restore($console);
|
|
$this->headTags->restore($headTags);
|
|
$this->fields->restore($fields);
|
|
|
|
throw $throwable;
|
|
} finally {
|
|
Route::swap($this->router);
|
|
$routes = $this->router->getRoutes();
|
|
if ($routes instanceof RouteCollection) {
|
|
$this->router->setRoutes($routes);
|
|
}
|
|
|
|
$this->app->instance('routes', $routes);
|
|
$this->pending = null;
|
|
}
|
|
}
|
|
|
|
public function discard(): void
|
|
{
|
|
$this->pending = null;
|
|
$this->active = false;
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->active;
|
|
}
|
|
}
|