|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire\Administration; |
| 4 | + |
| 5 | +use App\Core\CrudDialogHelper; |
| 6 | +use Filament\Forms\Components\CheckboxList; |
| 7 | +use Filament\Forms\Components\TextInput; |
| 8 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 9 | +use Filament\Forms\Contracts\HasForms; |
| 10 | +use Filament\Notifications\Notification; |
| 11 | +use Illuminate\Support\HtmlString; |
| 12 | +use Livewire\Component; |
| 13 | +use Spatie\Permission\Models\Permission; |
| 14 | +use Spatie\Permission\Models\Role; |
| 15 | + |
| 16 | +class RolesDialog extends Component implements HasForms |
| 17 | +{ |
| 18 | + use InteractsWithForms; |
| 19 | + use CrudDialogHelper; |
| 20 | + |
| 21 | + public Role $role; |
| 22 | + |
| 23 | + protected $listeners = ['doDeleteRole', 'cancelDeleteRole']; |
| 24 | + |
| 25 | + public array $permissions; |
| 26 | + |
| 27 | + public function mount(): void |
| 28 | + { |
| 29 | + $this->form->fill([ |
| 30 | + 'name' => $this->role->name, |
| 31 | + 'permissions' => $this->role->permissions->pluck('id')->toArray(), |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public function render() |
| 37 | + { |
| 38 | + return view('livewire.administration.roles-dialog'); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Form schema definition |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected function getFormSchema(): array |
| 47 | + { |
| 48 | + return [ |
| 49 | + TextInput::make('name') |
| 50 | + ->label(__('Role name')) |
| 51 | + ->maxLength(255) |
| 52 | + ->required(), |
| 53 | + |
| 54 | + CheckboxList::make('permissions') |
| 55 | + ->label(__('Permissions')) |
| 56 | + ->hint(new HtmlString(' |
| 57 | + <div class="w-full flex items-center gap-2"> |
| 58 | + <button type="button" |
| 59 | + class="text-xs text-primary-500 hover:text-primary-600 hover:underline" |
| 60 | + wire:click="assignAllPermissions"> |
| 61 | + ' . __('Assign all permissions') . ' |
| 62 | + </button> |
| 63 | + <span class="text-xs text-gray-300">|</span> |
| 64 | + <button type="button" |
| 65 | + class="text-xs text-primary-500 hover:text-primary-600 hover:underline" |
| 66 | + wire:click="removeAllPermissions"> |
| 67 | + ' . __('Remove all permissions') . ' |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + ')) |
| 71 | + ->columns(3) |
| 72 | + ->options(Permission::orderBy('name')->get()->pluck('name', 'id')->toArray()) |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Assign all permissions |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function assignAllPermissions(): void |
| 82 | + { |
| 83 | + $this->permissions = Permission::all()->pluck('id')->toArray(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Remove all assigned permissions |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function removeAllPermissions(): void |
| 92 | + { |
| 93 | + $this->permissions = []; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create / Update the role |
| 98 | + * |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function save(): void |
| 102 | + { |
| 103 | + $data = $this->form->getState(); |
| 104 | + if (!$this->role?->id) { |
| 105 | + $role = Role::create(['name' => $data['name']]); |
| 106 | + $role->syncPermissions($this->permissions); |
| 107 | + Notification::make() |
| 108 | + ->success() |
| 109 | + ->title(__('Role created')) |
| 110 | + ->body(__('The user role has been created')) |
| 111 | + ->send(); |
| 112 | + } else { |
| 113 | + $this->role->name = $data['name']; |
| 114 | + $this->role->save(); |
| 115 | + $this->role->syncPermissions($this->permissions); |
| 116 | + Notification::make() |
| 117 | + ->success() |
| 118 | + ->title(__('Role updated')) |
| 119 | + ->body(__('The role\'s details has been updated')) |
| 120 | + ->send(); |
| 121 | + } |
| 122 | + $this->emit('roleSaved'); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Delete an existing role |
| 127 | + * |
| 128 | + * @return void |
| 129 | + */ |
| 130 | + public function doDeleteRole(): void |
| 131 | + { |
| 132 | + $this->role->delete(); |
| 133 | + $this->deleteConfirmationOpened = false; |
| 134 | + $this->emit('roleDeleted'); |
| 135 | + Notification::make() |
| 136 | + ->success() |
| 137 | + ->title(__('Role deleted')) |
| 138 | + ->body(__('The role has been deleted')) |
| 139 | + ->send(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Cancel the deletion of a role |
| 144 | + * |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function cancelDeleteRole(): void |
| 148 | + { |
| 149 | + $this->deleteConfirmationOpened = false; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Show the delete role confirmation dialog |
| 154 | + * |
| 155 | + * @return void |
| 156 | + * @throws \Exception |
| 157 | + */ |
| 158 | + public function deleteRole(): void |
| 159 | + { |
| 160 | + $this->deleteConfirmation( |
| 161 | + __('Role deletion'), |
| 162 | + __('Are you sure you want to delete this role?'), |
| 163 | + 'doDeleteRole', |
| 164 | + 'cancelDeleteRole' |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments