Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 13m13s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 13m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 9m41s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 10m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 12m16s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m54s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 11m10s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 47s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 10m38s
Admin-Seite je Rolle: Bereiche direkt ein-/ausblenden, Menü, Schnellzugriffe, Rechte und „Als … ansehen“. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
160 lines
6.4 KiB
PHP
160 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Models\Role;
|
||
use App\Models\User;
|
||
use App\Plugins\PluginAccess;
|
||
use App\Plugins\PluginManager;
|
||
use App\Plugins\PluginManifest;
|
||
use App\Support\Abilities;
|
||
use App\Support\Audit;
|
||
use App\Support\Impersonation;
|
||
use App\Support\Navigation;
|
||
use App\Support\QuickLinks;
|
||
use App\Support\Settings;
|
||
use BackedEnum;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Pages\Page;
|
||
use Filament\Support\Icons\Heroicon;
|
||
use UnitEnum;
|
||
|
||
/**
|
||
* „Was sieht wer?“ – eine Rolle (z. B. Klienten) auf einen Blick: welche Bereiche sie
|
||
* sieht (hier direkt umschaltbar), welche Menüpunkte und Schnellzugriffe, welche Rechte –
|
||
* und per Klick das Portal mit den Augen einer Person dieser Rolle.
|
||
*/
|
||
class RoleView extends Page
|
||
{
|
||
protected string $view = 'filament.pages.role-view';
|
||
|
||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedEye;
|
||
|
||
protected static string|UnitEnum|null $navigationGroup = 'Benutzer & Rechte';
|
||
|
||
protected static ?int $navigationSort = 4;
|
||
|
||
protected static ?string $title = 'Was sieht wer?';
|
||
|
||
protected static ?string $slug = 'was-sieht-wer';
|
||
|
||
/** Anmeldeverfahren und Importe lassen sich nicht je Rolle sperren */
|
||
private const WITHOUT_ACCESS = ['ldap', 'oidc', 'saml', 'wordpress-import'];
|
||
|
||
public ?int $roleId = null;
|
||
|
||
public static function canAccess(): bool
|
||
{
|
||
return auth()->user()?->can('roles.manage') ?? false;
|
||
}
|
||
|
||
public function mount(): void
|
||
{
|
||
$this->roleId = (int) request()->query('rolle') ?: Role::query()->where('is_system', false)->orderBy('name')->value('id');
|
||
}
|
||
|
||
public function selectRole(int $id): void
|
||
{
|
||
$this->roleId = $id;
|
||
}
|
||
|
||
/** Bereich für die gewählte Rolle ein- oder ausblenden */
|
||
public function toggleArea(string $slug): void
|
||
{
|
||
abort_unless(static::canAccess() && auth()->user()->can('plugins.manage'), 403);
|
||
$role = Role::query()->findOrFail($this->roleId);
|
||
abort_if($role->isAdministration() || in_array($slug, self::WITHOUT_ACCESS, true), 422);
|
||
|
||
$access = app(PluginAccess::class);
|
||
$excluded = $access->excludedRoles($slug);
|
||
$hide = ! in_array($role->id, $excluded, true);
|
||
$roles = $hide ? [...$excluded, $role->id] : array_values(array_diff($excluded, [$role->id]));
|
||
|
||
$access->exclude($slug, $roles);
|
||
Audit::log('plugin.access', ['plugin' => $slug, 'excluded_roles' => $roles]);
|
||
|
||
Notification::make()->success()
|
||
->title($hide ? 'Für „'.$role->name.'“ ausgeblendet' : 'Für „'.$role->name.'“ sichtbar')
|
||
->body($hide ? 'Menüpunkt, Seiten, Startseiten-Kacheln, Suchtreffer, Feed und Aufgaben dieses Bereichs entfallen für die Rolle.' : null)
|
||
->send();
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function getViewData(): array
|
||
{
|
||
$roles = Role::query()->withCount('users')->orderByDesc('is_system')->orderBy('name')->get();
|
||
$role = $roles->firstWhere('id', $this->roleId);
|
||
|
||
if (! $role) {
|
||
return ['roles' => $roles, 'role' => null];
|
||
}
|
||
|
||
$plugins = app(PluginManager::class);
|
||
$access = app(PluginAccess::class);
|
||
$abilities = app(Abilities::class)->all();
|
||
$modules = app(Navigation::class)->modules();
|
||
$menu = app(Settings::class)->get(Navigation::MENU_SETTING);
|
||
$visibleFor = fn (array $roles) => $roles === [] || in_array($role->id, array_map('intval', $roles), true);
|
||
|
||
$grants = $role->grants()->get();
|
||
|
||
return [
|
||
'roles' => $roles,
|
||
'role' => $role,
|
||
'isAdministration' => $role->isAdministration(),
|
||
'canToggle' => auth()->user()->can('plugins.manage') && ! $role->isAdministration(),
|
||
'areas' => collect($plugins->all())
|
||
->filter(fn (PluginManifest $m) => $plugins->isEnabled($m->slug) && ! in_array($m->slug, self::WITHOUT_ACCESS, true))
|
||
->map(fn (PluginManifest $m) => [
|
||
'slug' => $m->slug,
|
||
'name' => $m->name,
|
||
'core' => $m->core,
|
||
'visible' => ! in_array($role->id, $access->excludedRoles($m->slug), true),
|
||
])->sortBy('name')->values(),
|
||
'menu' => is_array($menu) && $menu !== [] ? collect($menu)->map(fn (array $item) => [
|
||
'label' => $this->menuLabel($item, $modules),
|
||
'visible' => $visibleFor((array) ($item['roles'] ?? []))
|
||
&& ! in_array($role->id, $access->excludedRoles((string) ($item['module'] ?? '')), true),
|
||
'restricted' => ($item['roles'] ?? []) !== [],
|
||
])->all() : null,
|
||
'quickLinks' => collect(app(QuickLinks::class)->all())->map(fn (array $link) => [
|
||
'label' => $link['label'],
|
||
'visible' => $visibleFor($link['roles']),
|
||
'restricted' => $link['roles'] !== [],
|
||
])->all(),
|
||
'grants' => $grants->map(fn ($grant) => [
|
||
'label' => $grant->ability === '*' ? 'Vollzugriff' : ($abilities[$grant->ability]['label'] ?? $grant->ability),
|
||
'scope' => $grant->resource_type ? 'für einzelne Objekte' : null,
|
||
])->unique('label')->values(),
|
||
'preview' => $this->previewPerson($role),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $item
|
||
* @param array<string, string> $modules
|
||
*/
|
||
private function menuLabel(array $item, array $modules): string
|
||
{
|
||
$type = (string) ($item['type'] ?? '');
|
||
|
||
return match (true) {
|
||
filled($item['label'] ?? null) => (string) $item['label'],
|
||
$type === 'module' => $modules[$item['module'] ?? ''] ?? (string) ($item['module'] ?? 'Bereich'),
|
||
in_array($type, ['link', 'group'], true) => $type === 'link' ? 'Link' : 'Gruppe',
|
||
default => (string) (app(Navigation::class)->sourceOptions($type)[$item['target'] ?? ''] ?? $type),
|
||
};
|
||
}
|
||
|
||
/** Eine Person dieser Rolle, als die die Administration das Portal ansehen darf */
|
||
private function previewPerson(Role $role): ?User
|
||
{
|
||
$me = auth()->user();
|
||
$impersonation = app(Impersonation::class);
|
||
|
||
return $role->users()->where('is_active', true)->whereKeyNot($me->getKey())->orderBy('users.id')->limit(20)->get()
|
||
->first(fn (User $user) => $impersonation->canImpersonate($me, $user));
|
||
}
|
||
}
|