lasthourcloud/app/Livewire/Team/AdminView.php

142 lines
4.6 KiB
PHP
Raw Normal View History

2024-05-21 12:29:06 +00:00
<?php
namespace App\Livewire\Team;
use App\Models\Team;
use App\Models\User;
use Livewire\Component;
class AdminView extends Component
{
public $users;
2024-06-10 20:43:34 +00:00
public ?string $search = '';
public bool $lots_of_users = false;
2024-06-10 20:43:34 +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()
{
$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
2024-05-21 12:29:06 +00:00
private function finalizeDeletion(User $user, Team $team)
{
$servers = $team->servers;
foreach ($servers as $server) {
$resources = $server->definedResources();
foreach ($resources as $resource) {
2024-06-10 20:43:34 +00:00
ray('Deleting resource: '.$resource->name);
2024-05-21 12:29:06 +00:00
$resource->forceDelete();
}
2024-06-10 20:43:34 +00:00
ray('Deleting server: '.$server->name);
2024-05-21 12:29:06 +00:00
$server->forceDelete();
}
$projects = $team->projects;
foreach ($projects as $project) {
2024-06-10 20:43:34 +00:00
ray('Deleting project: '.$project->name);
2024-05-21 12:29:06 +00:00
$project->forceDelete();
}
$team->members()->detach($user->id);
2024-06-10 20:43:34 +00:00
ray('Deleting team: '.$team->name);
2024-05-21 12:29:06 +00:00
$team->delete();
}
2024-06-10 20:43:34 +00:00
2024-05-21 12:29:06 +00:00
public function delete($id)
{
2024-06-10 20:43:34 +00:00
if (! auth()->user()->isInstanceAdmin()) {
return $this->dispatch('error', 'You are not authorized to delete users');
}
2024-05-21 12:29:06 +00:00
$user = User::find($id);
$teams = $user->teams;
foreach ($teams as $team) {
ray($team->name);
$user_alone_in_team = $team->members->count() === 1;
if ($team->id === 0) {
if ($user_alone_in_team) {
ray('user is alone in the root team, do nothing');
2024-06-10 20:43:34 +00:00
2024-05-21 12:29:06 +00:00
return $this->dispatch('error', 'User is alone in the root team, cannot delete');
}
}
if ($user_alone_in_team) {
ray('user is alone in the team');
$this->finalizeDeletion($user, $team);
2024-06-10 20:43:34 +00:00
2024-05-21 12:29:06 +00:00
continue;
}
ray('user is not alone in the team');
if ($user->isOwner()) {
$found_other_owner_or_admin = $team->members->filter(function ($member) {
return $member->pivot->role === 'owner' || $member->pivot->role === 'admin';
})->where('id', '!=', $user->id)->first();
if ($found_other_owner_or_admin) {
ray('found other owner or admin');
$team->members()->detach($user->id);
2024-06-10 20:43:34 +00:00
2024-05-21 12:29:06 +00:00
continue;
} else {
$found_other_member_who_is_not_owner = $team->members->filter(function ($member) {
return $member->pivot->role === 'member';
})->first();
if ($found_other_member_who_is_not_owner) {
ray('found other member who is not owner');
$found_other_member_who_is_not_owner->pivot->role = 'owner';
$found_other_member_who_is_not_owner->pivot->save();
$team->members()->detach($user->id);
} else {
// This should never happen as if the user is the only member in the team, the team should be deleted already.
ray('found no other member who is not owner');
$this->finalizeDeletion($user, $team);
}
2024-06-10 20:43:34 +00:00
2024-05-21 12:29:06 +00:00
continue;
}
} else {
ray('user is not owner');
$team->members()->detach($user->id);
}
}
2024-06-10 20:43:34 +00:00
ray('Deleting user: '.$user->name);
2024-05-21 12:29:06 +00:00
$user->delete();
$this->getUsers();
}
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');
}
}