permissions = Permission::all(); $this->canCreate = Auth::user()?->can('role-edit') ?? false; $this->canEdit = Auth::user()?->can('role-edit') ?? false; $this->canDelect = Auth::user()?->can('role-delete') ?? false; } public function openCreateRoleModal() { $this->resetFields(); $this->showCreateModal = true; } public function openEditRoleModal($id) { $role = Role::findOrFail($id); $this->roleId = $role->id; $this->name = $role->name; $this->selectedPermissions = $role->permissions()->pluck('id')->toArray(); $this->showCreateModal = true; } public function save() { $this->validate([ 'name' => 'required|string|max:255', 'selectedPermissions' => 'array', ]); if ($this->roleId) { if ($this->canEdit) { $role = Role::findOrFail($this->roleId); $role->update(['name' => $this->name]); $role->syncPermissions($this->selectedPermissions); $this->notification()->send([ 'icon' => 'success', 'title' => '成功', 'description' => '角色已更新', ]); } } else { if ($this->canCreate) { $role = Role::create(['name' => $this->name]); $role->syncPermissions($this->selectedPermissions); $this->notification()->send([ 'icon' => 'success', 'title' => '成功', 'description' => '角色已新增', ]); } } $this->resetFields(); $this->showCreateModal = false; $this->dispatch('pg:eventRefresh-role-table'); } public function deleteRole($id) { if ($this->canDelect) { Role::findOrFail($id)->delete(); $this->notification()->send([ 'icon' => 'success', 'title' => '成功', 'description' => '角色已刪除', ]); $this->dispatch('pg:eventRefresh-role-table'); } } public function resetFields() { $this->name = ''; $this->selectedPermissions = []; $this->roleId = null; } public function render() { return view('livewire.admin.role-form'); } }