columns(1) ->components([ Section::make('Rolle') ->columns(2) ->schema([ TextInput::make('name')->label('Name')->required()->maxLength(100)->disabled(fn (?Role $record) => $record?->is_system), Textarea::make('description')->label('Beschreibung')->rows(2)->maxLength(500), ]), Section::make('Globale Rechte') ->description('Gelten im ganzen Portal.') ->visible(fn (?Role $record) => ! $record?->isAdministration()) ->schema([ CheckboxList::make('global_abilities') ->hiddenLabel() ->columns(2) ->options(fn () => collect($abilities->global())->map(fn (array $a) => $a['label'])->all()) ->descriptions(fn () => collect($abilities->global())->map(fn (array $a) => $a['group'])->all()) ->bulkToggleable() ->afterStateHydrated(fn (CheckboxList $component, ?Role $record) => $component->state( $record?->grants()->whereNull('resource_type')->pluck('ability')->all() ?? [] )) ->saveRelationshipsUsing(function (Role $record, ?array $state) { $record->grants()->whereNull('resource_type')->where('ability', '!=', '*')->delete(); foreach (array_unique($state ?? []) as $ability) { $record->grants()->create(['ability' => $ability]); } }) ->dehydrated(false), ]), Section::make('Rechte für einzelne Objekte') ->description('Zum Beispiel: Beiträge schreiben nur in der Kategorie „MAV-Info“.') ->visible(fn (?Role $record) => ! $record?->isAdministration() && $abilities->resourceTypes() !== []) ->schema([ Repeater::make('object_grants') ->hiddenLabel() ->columns(3) ->addActionLabel('Recht hinzufügen') ->defaultItems(0) ->schema([ Select::make('ability') ->label('Recht') ->required() ->live() ->options(fn () => collect($abilities->all()) ->filter(fn (array $a) => $a['resource'] !== null) ->map(fn (array $a) => $a['group'].': '.$a['label']) ->all()), Select::make('resource_ids') ->label('Für') ->multiple() ->required() ->options(function (Get $get) use ($abilities) { $type = $abilities->all()[$get('ability')]['resource'] ?? null; $options = $type ? ($abilities->resourceTypes()[$type]['options'])() : []; return ['*' => 'Alle'] + $options; }), Toggle::make('include_children') ->label('Inkl. Unterseiten') ->inline(false) ->default(true) ->visible(fn (Get $get) => $abilities->isHierarchical($abilities->all()[$get('ability')]['resource'] ?? null)), ]) ->afterStateHydrated(fn (Repeater $component, ?Role $record) => $component->state( $record?->grants()->whereNotNull('resource_type')->get() ->groupBy(fn (Grant $g) => $g->ability.'|'.(int) $g->include_children) ->map(fn ($grants) => [ 'ability' => $grants->first()->ability, 'resource_ids' => $grants->map(fn (Grant $g) => $g->resource_id === null ? '*' : (string) $g->resource_id)->values()->all(), 'include_children' => (bool) $grants->first()->include_children, ])->values()->all() ?? [] )) ->saveRelationshipsUsing(function (Role $record, ?array $state) use ($abilities) { $record->grants()->whereNotNull('resource_type')->delete(); foreach ($state ?? [] as $item) { $type = $abilities->all()[$item['ability'] ?? '']['resource'] ?? null; if (! $type) { continue; } $ids = in_array('*', (array) $item['resource_ids'], true) ? [null] : array_map('intval', (array) $item['resource_ids']); $inherit = $abilities->isHierarchical($type) && ($item['include_children'] ?? false); foreach (array_unique($ids) as $id) { $record->grants()->create(['ability' => $item['ability'], 'resource_type' => $type, 'resource_id' => $id, 'include_children' => $inherit && $id !== null]); } } }) ->dehydrated(false), ]), Section::make('Mitglieder') ->description('Manuell zugewiesene Personen. Personen aus Identity-Provider-Gruppen kommen automatisch dazu.') ->schema([ Select::make('members') ->hiddenLabel() ->multiple() ->searchable() ->getSearchResultsUsing(fn (string $search) => User::query() ->where(fn ($q) => $q->where('name', 'like', "%{$search}%")->orWhere('email', 'like', "%{$search}%")) ->orderBy('name')->limit(50)->pluck('name', 'id')->all()) ->getOptionLabelsUsing(fn (array $values) => User::query()->whereIn('id', $values)->pluck('name', 'id')->all()) ->afterStateHydrated(fn (Select $component, ?Role $record) => $component->state( $record?->users()->wherePivot('source', 'manual')->pluck('users.id')->all() ?? [] )) ->rule(fn (?Role $record) => function (string $attribute, mixed $value, \Closure $fail) use ($record) { if ($record?->isAdministration() && ! in_array(auth()->id(), array_map('intval', (array) $value), true)) { $fail('Sie können sich nicht selbst aus der Rolle „Administration“ entfernen.'); } }) ->saveRelationshipsUsing(function (Role $record, ?array $state) { $ids = array_map('intval', $state ?? []); $current = $record->users()->wherePivot('source', 'manual')->pluck('users.id')->all(); foreach (array_diff($current, $ids) as $userId) { $record->users()->wherePivot('source', 'manual')->detach($userId); } foreach (array_diff($ids, $current) as $userId) { $record->users()->attach($userId, ['source' => 'manual', 'created_at' => now()]); } Permissions::flush(); }) ->dehydrated(false), ]), ]); } public static function table(Table $table): Table { return $table ->defaultSort('name') ->modifyQueryUsing(fn ($query) => $query->withCount(['users', 'grants'])) ->columns([ TextColumn::make('name')->label('Name')->searchable()->weight('medium') ->description(fn (Role $record) => $record->description), IconColumn::make('is_system')->label('System')->boolean()->trueIcon(Heroicon::LockClosed)->falseIcon(false), TextColumn::make('users_count')->label('Mitglieder'), TextColumn::make('grants_count')->label('Rechte') ->formatStateUsing(fn (Role $record, $state) => $record->isAdministration() ? 'alle' : $state), ]) ->recordActions([ EditAction::make(), DeleteAction::make()->visible(fn (Role $record) => ! $record->is_system), ]); } public static function getPages(): array { return [ 'index' => ListRoles::route('/'), 'create' => CreateRole::route('/neu'), 'edit' => EditRole::route('/{record}/bearbeiten'), ]; } }