Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor

Andras Bacsaife68e45609fd
diff --git a/app/Http/Livewire/Server/Form.php b/app/Http/Livewire/Server/Form.php
index 290c06dd7d..0546c00072 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 b54b98fead..75053cc1aa 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 764c58246e..f3d9090a07 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 0000000000..08ee5e64d3
--- /dev/null
+++ b/app/Policies/ServerPolicy.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Policies;
+
+use App\Models\Server;
+use App\Models\User;
+use Illuminate\Auth\Access\Response;
+
+class ServerPolicy
+{
+ /**
+ * Determine whether the user can view any models.
+ */
+ public function viewAny(User $user): bool
+ {
+ return true;
+ }
+
+ /**
+ * Determine whether the user can view the model.
+ */
+ public function view(User $user, Server $server): bool
+ {
+ return $user->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 f1670723e5..378bfae474 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 44e9452a1c..ed814ffa90 100644
--- a/resources/views/livewire/dashboard.blade.php
+++ b/resources/views/livewire/dashboard.blade.php
@@ -1,6 +1,9 @@
<div>
+ @if (session('error'))
+ <span x-data x-init="$wire.emit('error', '{{ session('error') }}')"/>
+ @endif
<h1>Dashboard</h1>
- <div class="subtitle">Something <x-highlighted text="(more)"/> useful will be here.</div>
+ <div class="subtitle">Something <x-highlighted text="(more)" /> useful will be here.</div>
<div class="w-full rounded stats stats-vertical lg:stats-horizontal">
<div class="stat">
<div class="stat-title">Servers</div>