presseportale/resources/views/livewire/admin/roles/create.blade.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

134 lines
4.7 KiB
PHP

<?php
use App\Services\Admin\AdminPerformanceCache;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
new #[Layout('components.layouts.app'), Title('Neue Rolle')] class extends Component
{
public string $name = '';
public array $permissions = [];
public string $guardName = 'web';
public function save(): void
{
$validated = $this->validate([
'name' => [
'required',
'min:3',
'max:50',
Rule::unique('roles', 'name')
->where(fn (Builder $query) => $query->where('guard_name', $this->guardName)),
],
'permissions' => ['array'],
'permissions.*' => [
'string',
Rule::exists('permissions', 'name')
->where(fn (Builder $query) => $query->where('guard_name', $this->guardName)),
],
]);
$role = Role::query()->create([
'name' => $validated['name'],
'guard_name' => $this->guardName,
]);
$role->syncPermissions($validated['permissions'] ?? []);
session()->flash('success', 'Rolle erfolgreich erstellt.');
$this->redirect(route('admin.roles.index'), navigate: true);
}
public function with(): array
{
return [
'permissionGroups' => $this->permissionGroups(),
];
}
private function permissionGroups(): Collection
{
$cache = app(AdminPerformanceCache::class);
return $cache->remember($cache->permissionGroupsKey($this->guardName), AdminPerformanceCache::OptionsTtl, fn () => Permission::query()
->where('guard_name', $this->guardName)
->orderBy('name')
->get(['name'])
->groupBy(function (Permission $permission): string {
$prefix = Str::contains($permission->name, ':')
? Str::before($permission->name, ':')
: $permission->name;
return Str::headline(str_replace(['-', '_'], ' ', $prefix));
})
->map(fn ($group) => $group->values())
->sortKeys());
}
}; ?>
<form wire:submit="save" class="space-y-6">
<flux:card>
<div class="flex items-center justify-between gap-4">
<div>
<flux:heading size="lg">{{ __('Neue Rolle') }}</flux:heading>
<flux:subheading>{{ __('Guard') }}: {{ $guardName }}</flux:subheading>
</div>
</div>
</flux:card>
<flux:card>
<flux:heading size="lg" class="mb-4">{{ __('Basis-Informationen') }}</flux:heading>
<flux:field>
<flux:label>{{ __('Technischer Name') }} <span class="text-red-500">*</span></flux:label>
<flux:input wire:model="name" placeholder="{{ __('z.B. editor') }}" />
<flux:description>{{ __('Kleinbuchstaben, keine Leerzeichen. Wird intern verwendet.') }}</flux:description>
<flux:error name="name" />
</flux:field>
</flux:card>
<flux:card>
<flux:heading size="lg" class="mb-4">{{ __('Berechtigungen') }}</flux:heading>
<div class="space-y-6">
@forelse($permissionGroups as $groupName => $permissionsInGroup)
<div>
<flux:heading size="md" class="mb-3">{{ $groupName }}</flux:heading>
<div class="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">
@foreach($permissionsInGroup as $permission)
<flux:checkbox
wire:model="permissions"
value="{{ $permission['name'] }}"
label="{{ $permission['name'] }}"
/>
@endforeach
</div>
</div>
@empty
<flux:text class="text-sm text-zinc-500">{{ __('Keine Berechtigungen fuer diesen Guard vorhanden.') }}</flux:text>
@endforelse
</div>
<flux:error name="permissions" class="mt-4" />
</flux:card>
<flux:card>
<div class="flex justify-end gap-3">
<flux:button variant="ghost" href="{{ route('admin.roles.index') }}" wire:navigate>
{{ __('Abbrechen') }}
</flux:button>
<flux:button type="submit" variant="primary">
{{ __('Rolle erstellen') }}
</flux:button>
</div>
</flux:card>
</form>