167 lines
5.6 KiB
PHP
167 lines
5.6 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\Locked;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Volt\Component;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
new #[Layout('components.layouts.app'), Title('Rolle bearbeiten')] class extends Component
|
|
{
|
|
#[Locked]
|
|
public int $id;
|
|
|
|
public string $name = '';
|
|
|
|
public array $permissions = [];
|
|
|
|
public string $guardName = 'web';
|
|
|
|
public bool $isSystemRole = false;
|
|
|
|
public function mount(int $id): void
|
|
{
|
|
$this->id = $id;
|
|
|
|
$role = Role::query()
|
|
->with('permissions:id,name,guard_name')
|
|
->findOrFail($id);
|
|
|
|
$this->name = $role->name;
|
|
$this->guardName = $role->guard_name;
|
|
$this->permissions = $role->permissions
|
|
->pluck('name')
|
|
->values()
|
|
->all();
|
|
$this->isSystemRole = in_array($role->name, ['admin', 'editor', 'customer', 'api-only'], true);
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$validated = $this->validate([
|
|
'name' => [
|
|
'required',
|
|
'min:3',
|
|
'max:50',
|
|
Rule::unique('roles', 'name')
|
|
->ignore($this->id)
|
|
->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()->findOrFail($this->id);
|
|
$role->name = $validated['name'];
|
|
$role->save();
|
|
$role->syncPermissions($validated['permissions'] ?? []);
|
|
|
|
session()->flash('success', 'Rolle und Berechtigungen erfolgreich aktualisiert.');
|
|
$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">{{ __('Rolle bearbeiten') }}</flux:heading>
|
|
<flux:subheading>{{ __('ID') }}: {{ $id }} | {{ __('Guard') }}: {{ $guardName }}</flux:subheading>
|
|
</div>
|
|
|
|
@if($isSystemRole)
|
|
<flux:badge color="purple" size="sm">{{ __('Systemrolle') }}</flux:badge>
|
|
@endif
|
|
</div>
|
|
</flux:card>
|
|
|
|
@if($isSystemRole)
|
|
<flux:card>
|
|
<flux:text class="text-sm text-zinc-600 dark:text-zinc-300">
|
|
{{ __('Hinweis: Diese Rolle ist Teil des Basis-Setups. Aenderungen wirken sich direkt auf den Admin-Zugriff aus.') }}
|
|
</flux:text>
|
|
</flux:card>
|
|
@endif
|
|
|
|
<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" />
|
|
<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">
|
|
{{ __('Aenderungen speichern') }}
|
|
</flux:button>
|
|
</div>
|
|
</flux:card>
|
|
</form>
|