/ app / Http / Livewire / Users / Remove.php
Remove.php
 1  <?php
 2  
 3  namespace App\Http\Livewire\Users;
 4  
 5  use App\Http\Services\UserService;
 6  use Exception;
 7  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 8  use Livewire\Component;
 9  use WireUi\Traits\Actions;
10  
11  class Remove extends Component
12  {
13      use AuthorizesRequests, Actions;
14  
15      public int $entityId = 0;
16  
17      public function mount()
18      {
19          $this->authorize('can_write_users');
20      }
21  
22      public function remove()
23      {
24          $this->authorize('can_write_users');
25          $currentUserId = auth()->user()->id;
26          try {
27              UserService::remove($this->entityId);
28  
29              // UI Trick for delaying spinner
30              if (config('app.env') !== 'testing') {
31                  sleep(1);
32              }
33  
34              $this->emitUp('closePanel');
35              $this->emit('userRemoved');
36  
37              $this->notification()->success(
38                  __('users.notifications.remove.success.title'),
39                  __('users.notifications.remove.success.message')
40              );
41  
42              if ($this->entityId === $currentUserId) {
43                  auth()->logout(); //if the user deletes himself, the session gets closed
44                  return redirect(route('login'));
45              }
46          } catch (Exception $e) {
47              $this->notification()->error(
48                  'Error',
49                  $e->getMessage()
50              );
51          }
52      }
53  
54      public function render()
55      {
56          return view('livewire.users.remove');
57      }
58  }