Index.php
1 <?php 2 3 namespace App\Http\Livewire\Users; 4 5 use App\Http\Services\GpgService; 6 use App\Http\Services\UserService; 7 use App\Models\Gpg; 8 use App\Models\Role; 9 use App\Models\User; 10 use App\Traits\WithPerPagePagination; 11 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 12 use Livewire\Component; 13 use Livewire\Redirector; 14 use Livewire\WithPagination; 15 use WireUi\Traits\Actions; 16 17 class Index extends Component 18 { 19 use WithPagination, WithPerPagePagination, AuthorizesRequests, Actions; 20 21 public $readyToLoad = false; 22 23 public array $filters = [ 24 'created_at_since' => null, 25 'created_at_to' => null, 26 'role_ids' => [], 27 'withTrashed' => false, 28 ]; 29 30 public bool $showInternalUsers = true; 31 32 public string $search = ''; 33 34 public string $sortField = 'name'; 35 36 public string $sortDirection = 'asc'; 37 38 protected $rules = [ 39 'filters.created_at_since' => 'nullable|date', 40 'filters.created_at_to' => 'nullable|date|after:filters.created_at_since', 41 'filters.role_ids' => 'nullable|array', 42 'filters.role_ids.*' => 'nullable|exists:roles,id', 43 'filters.withTrashed' => 'nullable|boolean', 44 ]; 45 46 public function mount() 47 { 48 $this->authorize('can_read_users'); 49 } 50 51 public function fetchData() 52 { 53 $this->readyToLoad = true; 54 } 55 56 public function updated($propertyName) 57 { 58 $this->validateOnly($propertyName); 59 } 60 61 protected function getListeners() 62 { 63 return [ 64 'refresh' => '$refresh', 65 'userRestored' => '$refresh', 66 'userRemoved' => '$refresh', 67 ]; 68 } 69 70 public function updatingSearch(): void 71 { 72 $this->resetPage(); 73 } 74 75 public function updatedFilters(): void 76 { 77 $this->getUsersProperty(); 78 } 79 80 public function sortBy(string $field): void 81 { 82 $this->sortField === $field 83 ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc' 84 : $this->sortDirection = 'asc'; 85 86 $this->sortField = $field; 87 } 88 89 public function getRowsQueryProperty() 90 { 91 return UserService::searchUsers($this->showInternalUsers, $this->search, $this->sortField, $this->sortDirection, $this->filters); 92 } 93 94 public function getUsersProperty() 95 { 96 return $this->readyToLoad ? $this->applyPagination($this->rowsQuery) : []; 97 } 98 99 public function getRolesProperty() 100 { 101 return Role::all()->map(function (Role $role) { 102 $role->name = __('roles.' . $role->name); 103 return $role; 104 }); 105 } 106 107 public function viewRow(int $rowId): Redirector|bool 108 { 109 if (User::withTrashed()->find($rowId)->trashed()) { 110 return false; 111 } 112 113 return redirect()->to(route('users.view', ['user' => $rowId])); 114 } 115 116 public function render() 117 { 118 return view('livewire.users.index') 119 ->layoutData([ 120 'title' => __('users.title'), 121 'entity' => null, 122 'breadcrumb' => 'users.index', 123 ]); 124 } 125 126 public function generateGpgs() 127 { 128 $this->dialog()->confirm([ 129 'title' => __('users.dialogs.generate_gpgs.title'), 130 'description' => __('users.dialogs.generate_gpgs.message'), 131 'acceptLabel' => __('users.dialogs.generate_gpgs.buttons.confirm'), 132 'rejectLabel' => __('users.dialogs.generate_gpgs.buttons.cancel'), 133 'method' => 'generateAllGpg' 134 ]); 135 } 136 137 public function generateAllGpg(GpgService $gpgService) 138 { 139 $this->authorize('can_write_users'); 140 $gpgService->generateAllUsersKeys(); 141 $this->notification()->success( 142 __('users.notifications.generate_all_gpg.success.title'), 143 __('users.notifications.generate_all_gpg.success.message') 144 ); 145 } 146 }