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 */ 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 $item * @param array $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)); } }