can(self::CORE_PERMISSION), 403); } private function flushCache(): void { app(PermissionRegistrar::class)->forgetCachedPermissions(); } public function updatedSelectAll($value): void { $this->selected = $value ? Role::pluck('id')->map(fn ($id) => (string) $id)->toArray() : []; } // ── View ───────────────────────────────────────────────────────────────── public function openView(int $id): void { $this->viewingRole = $id; } public function closeView(): void { $this->viewingRole = null; } // ── Delete (single / bulk) ───────────────────────────────────────────────── public function delete(int $id): void { $role = Role::findOrFail($id); if (in_array($role->name, self::PROTECTED_ROLES, true)) { $this->dispatch('notify', "El rol '{$role->name}' está protegido y no se puede borrar."); return; } $role->delete(); $this->selected = array_values(array_diff($this->selected, [(string) $id, $id])); $this->flushCache(); $this->dispatch('notify', 'Rol eliminado'); } public function bulkDelete(): void { $roles = Role::whereIn('id', $this->selected)->get(); $deleted = 0; $skipped = 0; foreach ($roles as $role) { if (in_array($role->name, self::PROTECTED_ROLES, true)) { $skipped++; continue; } $role->delete(); $deleted++; } $this->selected = []; $this->selectAll = false; $this->flushCache(); $msg = "{$deleted} rol(es) eliminados"; if ($skipped) $msg .= " ({$skipped} protegido(s) omitido(s))"; $this->dispatch('notify', $msg); } public function render() { return view('livewire.role-manager', [ 'roles' => Role::with('permissions')->withCount('users')->orderBy('name')->get(), 'viewing' => $this->viewingRole ? Role::with('permissions')->withCount('users')->find($this->viewingRole) : null, ]); } }