Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
139 lines
5.7 KiB
PHP
139 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Models\Role;
|
||
use App\Plugins\PluginAccess;
|
||
use App\Plugins\PluginInstaller;
|
||
use App\Plugins\PluginManager;
|
||
use App\Plugins\PluginManifest;
|
||
use App\Support\Audit;
|
||
use BackedEnum;
|
||
use Filament\Actions\Action;
|
||
use Filament\Forms\Components\Select;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Pages\Page;
|
||
use Filament\Support\Icons\Heroicon;
|
||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||
use Livewire\WithFileUploads;
|
||
use Throwable;
|
||
use UnitEnum;
|
||
|
||
class Plugins extends Page
|
||
{
|
||
use WithFileUploads;
|
||
|
||
protected string $view = 'filament.pages.plugins';
|
||
|
||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedPuzzlePiece;
|
||
|
||
protected static string|UnitEnum|null $navigationGroup = 'System';
|
||
|
||
protected static ?int $navigationSort = 5;
|
||
|
||
protected static ?string $title = 'Plugins';
|
||
|
||
protected static ?string $slug = 'plugins';
|
||
|
||
/** @var TemporaryUploadedFile|null */
|
||
public $archive = null;
|
||
|
||
public static function canAccess(): bool
|
||
{
|
||
return auth()->user()?->can('plugins.manage') ?? false;
|
||
}
|
||
|
||
/** Plugins ohne eigenen Bereich im Portal – für sie gibt es keinen „Zugang“. */
|
||
private const WITHOUT_ACCESS = ['ldap', 'oidc', 'saml', 'wordpress-import'];
|
||
|
||
public function accessAction(): Action
|
||
{
|
||
return Action::make('access')
|
||
->label('Zugang')
|
||
->icon(Heroicon::OutlinedNoSymbol)
|
||
->color('gray')
|
||
->size('sm')
|
||
->modalHeading(fn (array $arguments) => 'Zugang: '.(app(PluginManager::class)->find($arguments['slug'] ?? '')?->name ?? ''))
|
||
->modalDescription('Personen mit einer dieser Rollen sehen den Bereich nicht: kein Menüpunkt, keine Seiten, keine Kacheln auf der Startseite, keine Suchtreffer. Wer Plugins verwalten darf, ist nie ausgeschlossen.')
|
||
->fillForm(fn (array $arguments) => ['roles' => array_map('strval', app(PluginAccess::class)->excludedRoles($arguments['slug'] ?? ''))])
|
||
->schema([
|
||
Select::make('roles')
|
||
->label('Ausgeschlossene Rollen')
|
||
->multiple()
|
||
->searchable()
|
||
->options(fn () => Role::query()->orderBy('name')->pluck('name', 'id')->mapWithKeys(fn ($name, $id) => [(string) $id => $name])->all()),
|
||
])
|
||
->modalSubmitActionLabel('Speichern')
|
||
->action(function (array $data, array $arguments) {
|
||
$slug = (string) ($arguments['slug'] ?? '');
|
||
abort_unless(app(PluginManager::class)->find($slug), 404);
|
||
app(PluginAccess::class)->exclude($slug, $data['roles'] ?? []);
|
||
Audit::log('plugin.access', ['plugin' => $slug, 'excluded_roles' => array_map('intval', $data['roles'] ?? [])]);
|
||
Notification::make()->success()->title('Zugang gespeichert')->send();
|
||
});
|
||
}
|
||
|
||
public function activate(string $slug, PluginManager $plugins): void
|
||
{
|
||
$this->run(fn () => $plugins->activate($slug), 'Plugin aktiviert', 'Die Änderungen sind ab dem nächsten Seitenaufruf sichtbar.');
|
||
}
|
||
|
||
public function deactivate(string $slug, PluginManager $plugins): void
|
||
{
|
||
$this->run(fn () => $plugins->deactivate($slug), 'Plugin deaktiviert', 'Die Daten bleiben erhalten und stehen nach erneuter Aktivierung wieder zur Verfügung.');
|
||
}
|
||
|
||
public function delete(string $slug, bool $removeData, PluginManager $plugins): void
|
||
{
|
||
$this->run(fn () => $plugins->delete($slug, $removeData), 'Plugin gelöscht', $removeData ? 'Das Plugin und seine Daten wurden entfernt.' : 'Die Daten des Plugins sind noch in der Datenbank.');
|
||
}
|
||
|
||
public function installArchive(PluginInstaller $installer): void
|
||
{
|
||
$this->validate(['archive' => ['required', 'file', 'mimes:zip', 'max:51200']], attributes: ['archive' => 'ZIP-Archiv']);
|
||
|
||
try {
|
||
$manifest = $installer->install($this->archive->getRealPath());
|
||
$this->archive = null;
|
||
|
||
Notification::make()
|
||
->success()
|
||
->title("„{$manifest->name}“ {$manifest->version} installiert")
|
||
->body('Das Plugin kann jetzt aktiviert werden.')
|
||
->send();
|
||
} catch (Throwable $e) {
|
||
$this->addError('archive', $e->getMessage());
|
||
}
|
||
}
|
||
|
||
private function run(callable $action, string $title, string $body): void
|
||
{
|
||
try {
|
||
$action();
|
||
Notification::make()->success()->title($title)->body($body)->send();
|
||
$this->redirect(static::getUrl());
|
||
} catch (Throwable $e) {
|
||
Notification::make()->danger()->title('Nicht möglich')->body($e->getMessage())->send();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function getViewData(): array
|
||
{
|
||
$plugins = app(PluginManager::class);
|
||
|
||
return [
|
||
'plugins' => collect($plugins->all())->map(fn (PluginManifest $m) => [
|
||
'manifest' => $m,
|
||
'enabled' => $plugins->isEnabled($m->slug),
|
||
'problem' => $plugins->problem($m->slug) ?? (($issues = $m->incompatibilities()) ? implode(', ', $issues) : null),
|
||
'dependents' => $plugins->dependents($m->slug),
|
||
'accessible' => ! in_array($m->slug, self::WITHOUT_ACCESS, true),
|
||
'excluded' => ($ids = app(PluginAccess::class)->excludedRoles($m->slug)) ? Role::query()->whereIn('id', $ids)->orderBy('name')->pluck('name')->all() : [],
|
||
])->sortBy(fn (array $p) => [$p['manifest']->core ? 0 : 1, $p['manifest']->name])->values(),
|
||
'uploadsAllowed' => PluginInstaller::uploadsAllowed(),
|
||
];
|
||
}
|
||
}
|