mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 04:15:52 -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>
126 lines
5.2 KiB
PHP
126 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Providers;
|
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Console\Application as Artisan;
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
use Illuminate\Foundation\Console\AboutCommand;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Exceptions;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Pterodactyl\Models\User;
|
|
use Pterodactyl\Services\Extensions\ExtensionActionDecorators;
|
|
use Pterodactyl\Services\Extensions\ExtensionAssetPublisher;
|
|
use Pterodactyl\Services\Extensions\ExtensionConsoleRegistry;
|
|
use Pterodactyl\Services\Extensions\ExtensionFailureAttributor;
|
|
use Pterodactyl\Services\Extensions\ExtensionFieldRegistry;
|
|
use Pterodactyl\Services\Extensions\ExtensionHeadTags;
|
|
use Pterodactyl\Services\Extensions\ExtensionLock;
|
|
use Pterodactyl\Services\Extensions\ExtensionManifestValidator;
|
|
use Pterodactyl\Services\Extensions\ExtensionPermissionRegistry;
|
|
use Pterodactyl\Services\Extensions\ExtensionProviderLoader;
|
|
use Pterodactyl\Services\Extensions\ExtensionRepository;
|
|
use Pterodactyl\Services\Extensions\ExtensionSettingsRegistry;
|
|
use Pterodactyl\Support\JsonValueGuard;
|
|
use Throwable;
|
|
|
|
class ExtensionServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
$this->app->singleton(ExtensionManifestValidator::class);
|
|
$this->app->singleton(ExtensionAssetPublisher::class);
|
|
$this->app->singleton(ExtensionSettingsRegistry::class);
|
|
$this->app->singleton(ExtensionPermissionRegistry::class);
|
|
$this->app->singleton(ExtensionFieldRegistry::class);
|
|
$this->app->singleton(ExtensionRepository::class);
|
|
$this->app->singleton(ExtensionLock::class);
|
|
$this->app->singleton(ExtensionProviderLoader::class);
|
|
$this->app->singleton(ExtensionActionDecorators::class);
|
|
$this->app->singleton(ExtensionConsoleRegistry::class);
|
|
$this->app->singleton(ExtensionHeadTags::class);
|
|
$this->app->singleton(ExtensionFailureAttributor::class);
|
|
|
|
if (! config('extensions.enabled')) {
|
|
return;
|
|
}
|
|
|
|
// Registered during the provider registration phase, then executed after
|
|
// the core app has booted. Application::register() immediately runs
|
|
// Laravel's normal provider boot path inside the loader's failure boundary.
|
|
$this->app->booted(fn () => $this->app->make(ExtensionProviderLoader::class)->registerProviders($this->app->make(ExtensionRepository::class)->enabled()));
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
// Root path routes of extensions are public, so they are throttled per client.
|
|
RateLimiter::for('extensions.root', function (Request $request): Limit {
|
|
$user = $request->user();
|
|
|
|
return Limit::perMinute(JsonValueGuard::integer(config('extensions.root_routes_per_minute')))
|
|
->by($user instanceof User ? $user->uuid : $request->ip());
|
|
});
|
|
|
|
AboutCommand::add('Extensions', fn (): array => $this->about());
|
|
|
|
if (! config('extensions.enabled')) {
|
|
return;
|
|
}
|
|
|
|
// The console application starts after every extension provider has been
|
|
// committed, so the registry only ever hands over what booted successfully. Its
|
|
// commands are added by Pterodactyl\Console\Kernel once the panel's own exist; the
|
|
// scheduler is wired the way core's own schedule is (bootstrap/app.php).
|
|
Artisan::starting(static function (Artisan $artisan): void {
|
|
$app = $artisan->getLaravel();
|
|
$registry = $app->make(ExtensionConsoleRegistry::class);
|
|
|
|
$app->afterResolving(Schedule::class, static fn (Schedule $schedule) => $registry->schedule($schedule));
|
|
if ($app->resolved(Schedule::class)) {
|
|
$registry->schedule($app->make(Schedule::class));
|
|
}
|
|
});
|
|
|
|
Exceptions::reportable(function (Throwable $throwable): void {
|
|
$this->app->make(ExtensionFailureAttributor::class)->attribute($throwable);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The `php artisan about` rows for extensions: each installed one with its version and
|
|
* state, the way Laravel packages report themselves there.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
private function about(): array
|
|
{
|
|
if (! config('extensions.enabled')) {
|
|
return ['Status' => 'OFF'];
|
|
}
|
|
|
|
$extensions = $this->app->make(ExtensionRepository::class);
|
|
$records = $extensions->records();
|
|
$rows = [];
|
|
foreach ($extensions->discovered() as $manifest) {
|
|
$record = $records->get($manifest->id);
|
|
$state = match (true) {
|
|
$record === null => 'not registered',
|
|
$record->error !== null => $record->enabled ? 'enabled, failing' : 'disabled, failing',
|
|
$record->enabled => 'enabled',
|
|
default => 'disabled',
|
|
};
|
|
$rows[$manifest->id] = sprintf('%s (%s)', $manifest->version, $state);
|
|
}
|
|
|
|
foreach (array_keys($extensions->discoveryErrors()) as $directory) {
|
|
$rows[$directory] = 'invalid manifest';
|
|
}
|
|
|
|
return $rows === [] ? ['Installed' => 'none'] : $rows;
|
|
}
|
|
}
|