2024-05-21 12:29:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire\Team;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
2024-09-23 17:51:31 +00:00
|
|
|
use Livewire\Component;
|
2024-05-21 12:29:06 +00:00
|
|
|
|
|
|
|
|
class AdminView extends Component
|
|
|
|
|
{
|
|
|
|
|
public $users;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
|
public ?string $search = '';
|
|
|
|
|
|
2024-05-22 12:23:55 +00:00
|
|
|
public bool $lots_of_users = false;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-22 12:23:55 +00:00
|
|
|
private $number_of_users_to_show = 20;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-21 12:29:06 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! isInstanceAdmin()) {
|
2024-05-21 12:29:06 +00:00
|
|
|
return redirect()->route('dashboard');
|
|
|
|
|
}
|
|
|
|
|
$this->getUsers();
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-21 12:29:06 +00:00
|
|
|
public function submitSearch()
|
|
|
|
|
{
|
2024-06-10 20:43:34 +00:00
|
|
|
if ($this->search !== '') {
|
2024-05-21 12:29:06 +00:00
|
|
|
$this->users = User::where(function ($query) {
|
|
|
|
|
$query->where('name', 'like', "%{$this->search}%")
|
|
|
|
|
->orWhere('email', 'like', "%{$this->search}%");
|
|
|
|
|
})->get()->filter(function ($user) {
|
|
|
|
|
return $user->id !== auth()->id();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$this->getUsers();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-21 12:29:06 +00:00
|
|
|
public function getUsers()
|
|
|
|
|
{
|
2024-05-22 12:23:55 +00:00
|
|
|
$users = User::where('id', '!=', auth()->id())->get();
|
|
|
|
|
if ($users->count() > $this->number_of_users_to_show) {
|
|
|
|
|
$this->lots_of_users = true;
|
|
|
|
|
$this->users = $users->take($this->number_of_users_to_show);
|
|
|
|
|
} else {
|
|
|
|
|
$this->lots_of_users = false;
|
|
|
|
|
$this->users = $users;
|
|
|
|
|
}
|
2024-05-21 12:29:06 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-03-11 14:04:45 +00:00
|
|
|
public function delete($id, $password, $selectedActions = [])
|
2024-05-21 12:29:06 +00:00
|
|
|
{
|
2024-11-03 12:43:28 +00:00
|
|
|
if (! isInstanceAdmin()) {
|
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
|
}
|
2025-06-26 10:23:08 +00:00
|
|
|
|
2025-12-12 13:12:02 +00:00
|
|
|
if (! verifyPasswordConfirmation($password, $this)) {
|
2026-03-11 14:04:45 +00:00
|
|
|
return 'The provided password is incorrect.';
|
2024-09-04 19:14:18 +00:00
|
|
|
}
|
2025-06-26 10:23:08 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! auth()->user()->isInstanceAdmin()) {
|
2024-05-22 12:23:55 +00:00
|
|
|
return $this->dispatch('error', 'You are not authorized to delete users');
|
|
|
|
|
}
|
2025-06-26 10:23:08 +00:00
|
|
|
|
2024-05-21 12:29:06 +00:00
|
|
|
$user = User::find($id);
|
2025-06-26 10:23:08 +00:00
|
|
|
if (! $user) {
|
|
|
|
|
return $this->dispatch('error', 'User not found');
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-06-26 10:23:08 +00:00
|
|
|
try {
|
|
|
|
|
$user->delete();
|
|
|
|
|
$this->getUsers();
|
2026-03-11 14:04:45 +00:00
|
|
|
|
|
|
|
|
return true;
|
2025-06-26 10:23:08 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->dispatch('error', $e->getMessage());
|
2024-05-21 12:29:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-21 12:29:06 +00:00
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.team.admin-view');
|
|
|
|
|
}
|
|
|
|
|
}
|