From fe68e45609fd24b3b38c0a51aea9c6f33dfecda4 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 29 Aug 2023 15:51:30 +0200 Subject: [PATCH] refactor --- app/Http/Livewire/Server/Form.php | 20 ++++-- app/Http/Livewire/Server/Show.php | 8 ++- app/Models/Server.php | 15 ++++- app/Policies/ServerPolicy.php | 66 ++++++++++++++++++++ bootstrap/helpers/shared.php | 4 ++ resources/views/livewire/dashboard.blade.php | 5 +- 6 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 app/Policies/ServerPolicy.php diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php index 290c06dd7..0546c0007 100644 --- a/app/Http/Livewire/Server/Form.php +++ b/app/Http/Livewire/Server/Form.php @@ -4,10 +4,12 @@ namespace App\Http\Livewire\Server; use App\Actions\Server\InstallDocker; use App\Models\Server; +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Livewire\Component; class Form extends Component { + use AuthorizesRequests; public Server $server; public $uptime; public $dockerVersion; @@ -64,14 +66,20 @@ class Form extends Component public function delete() { - if (!$this->server->isEmpty()) { - $this->emit('error', 'Server has defined resources. Please delete them first.'); - return; + try { + $this->authorize('delete', $this->server); + if (!$this->server->isEmpty()) { + $this->emit('error', 'Server has defined resources. Please delete them first.'); + return; + } + $this->server->delete(); + return redirect()->route('server.all'); + } catch (\Exception $e) { + return general_error_handler(err: $e, that: $this); } - $this->server->delete(); - redirect()->route('server.all'); - } + + } public function submit() { $this->validate(); diff --git a/app/Http/Livewire/Server/Show.php b/app/Http/Livewire/Server/Show.php index b54b98fea..75053cc1a 100644 --- a/app/Http/Livewire/Server/Show.php +++ b/app/Http/Livewire/Server/Show.php @@ -3,14 +3,20 @@ namespace App\Http\Livewire\Server; use App\Models\Server; +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Livewire\Component; class Show extends Component { + use AuthorizesRequests; public ?Server $server = null; public function mount() { - $this->server = Server::ownedByCurrentTeam(['name', 'description', 'ip', 'port', 'user', 'proxy'])->whereUuid(request()->server_uuid)->firstOrFail(); + try { + $this->server = Server::ownedByCurrentTeam(['name', 'description', 'ip', 'port', 'user', 'proxy'])->whereUuid(request()->server_uuid)->firstOrFail(); + } catch (\Throwable $e) { + return general_error_handler(err: $e, that: $this); + } } public function render() { diff --git a/app/Models/Server.php b/app/Models/Server.php index 764c58246..f3d9090a0 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -33,6 +33,9 @@ class Server extends BaseModel }); static::deleting(function ($server) { + $server->destinations()->each(function ($destination) { + $destination->delete(); + }); $server->settings()->delete(); }); } @@ -70,8 +73,6 @@ class Server extends BaseModel return $standaloneDocker->concat($swarmDocker); } - - public function settings() { return $this->hasOne(ServerSetting::class); @@ -84,12 +85,20 @@ class Server extends BaseModel public function isEmpty() { - if ($this->applications()->count() === 0) { + $applications = $this->applications()->count() === 0; + $databases = $this->databases()->count() === 0; + if ($applications && $databases) { return true; } return false; } + public function databases() { + return $this->destinations()->map(function ($standaloneDocker) { + $postgresqls = $standaloneDocker->postgresqls; + return $postgresqls?->concat([]) ?? collect([]); + })->flatten(); + } public function applications() { return $this->destinations()->map(function ($standaloneDocker) { diff --git a/app/Policies/ServerPolicy.php b/app/Policies/ServerPolicy.php new file mode 100644 index 000000000..08ee5e64d --- /dev/null +++ b/app/Policies/ServerPolicy.php @@ -0,0 +1,66 @@ +teams()->get()->firstWhere('id', $server->team_id) !== null; + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return true; + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, Server $server): bool + { + return $user->teams()->get()->firstWhere('id', $server->team_id) !== null; + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, Server $server): bool + { + return $user->teams()->get()->firstWhere('id', $server->team_id) !== null; + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, Server $server): bool + { + return false; + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, Server $server): bool + { + return false; + } +} diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index f1670723e..378bfae47 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -58,6 +58,7 @@ function refreshSession(): void function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed { try { + ray($err); ray('ERROR OCCURRED: ' . $err->getMessage()); if ($err instanceof QueryException) { if ($err->errorInfo[0] === '23505') { @@ -70,6 +71,9 @@ function general_error_handler(Throwable | null $err = null, $that = null, $isJs } elseif ($err instanceof TooManyRequestsException) { throw new Exception($customErrorMessage ?? "Too many requests. Please try again in {$err->secondsUntilAvailable} seconds."); } else { + if ($err->getMessage() === 'This action is unauthorized.') { + return redirect()->route('dashboard')->with('error', $customErrorMessage ?? $err->getMessage()); + } throw new Exception($customErrorMessage ?? $err->getMessage()); } } catch (Throwable $error) { diff --git a/resources/views/livewire/dashboard.blade.php b/resources/views/livewire/dashboard.blade.php index 44e9452a1..ed814ffa9 100644 --- a/resources/views/livewire/dashboard.blade.php +++ b/resources/views/livewire/dashboard.blade.php @@ -1,6 +1,9 @@
+ @if (session('error')) + + @endif

Dashboard

-
Something useful will be here.
+
Something useful will be here.
Servers