refactor
This commit is contained in:
parent
291b9a84ef
commit
fe68e45609
@ -4,10 +4,12 @@ namespace App\Http\Livewire\Server;
|
|||||||
|
|
||||||
use App\Actions\Server\InstallDocker;
|
use App\Actions\Server\InstallDocker;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class Form extends Component
|
class Form extends Component
|
||||||
{
|
{
|
||||||
|
use AuthorizesRequests;
|
||||||
public Server $server;
|
public Server $server;
|
||||||
public $uptime;
|
public $uptime;
|
||||||
public $dockerVersion;
|
public $dockerVersion;
|
||||||
@ -64,14 +66,20 @@ class Form extends Component
|
|||||||
|
|
||||||
public function delete()
|
public function delete()
|
||||||
{
|
{
|
||||||
if (!$this->server->isEmpty()) {
|
try {
|
||||||
$this->emit('error', 'Server has defined resources. Please delete them first.');
|
$this->authorize('delete', $this->server);
|
||||||
return;
|
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()
|
public function submit()
|
||||||
{
|
{
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
@ -3,14 +3,20 @@
|
|||||||
namespace App\Http\Livewire\Server;
|
namespace App\Http\Livewire\Server;
|
||||||
|
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class Show extends Component
|
class Show extends Component
|
||||||
{
|
{
|
||||||
|
use AuthorizesRequests;
|
||||||
public ?Server $server = null;
|
public ?Server $server = null;
|
||||||
public function mount()
|
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()
|
public function render()
|
||||||
{
|
{
|
||||||
|
@ -33,6 +33,9 @@ class Server extends BaseModel
|
|||||||
|
|
||||||
});
|
});
|
||||||
static::deleting(function ($server) {
|
static::deleting(function ($server) {
|
||||||
|
$server->destinations()->each(function ($destination) {
|
||||||
|
$destination->delete();
|
||||||
|
});
|
||||||
$server->settings()->delete();
|
$server->settings()->delete();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -70,8 +73,6 @@ class Server extends BaseModel
|
|||||||
return $standaloneDocker->concat($swarmDocker);
|
return $standaloneDocker->concat($swarmDocker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function settings()
|
public function settings()
|
||||||
{
|
{
|
||||||
return $this->hasOne(ServerSetting::class);
|
return $this->hasOne(ServerSetting::class);
|
||||||
@ -84,12 +85,20 @@ class Server extends BaseModel
|
|||||||
|
|
||||||
public function isEmpty()
|
public function isEmpty()
|
||||||
{
|
{
|
||||||
if ($this->applications()->count() === 0) {
|
$applications = $this->applications()->count() === 0;
|
||||||
|
$databases = $this->databases()->count() === 0;
|
||||||
|
if ($applications && $databases) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function databases() {
|
||||||
|
return $this->destinations()->map(function ($standaloneDocker) {
|
||||||
|
$postgresqls = $standaloneDocker->postgresqls;
|
||||||
|
return $postgresqls?->concat([]) ?? collect([]);
|
||||||
|
})->flatten();
|
||||||
|
}
|
||||||
public function applications()
|
public function applications()
|
||||||
{
|
{
|
||||||
return $this->destinations()->map(function ($standaloneDocker) {
|
return $this->destinations()->map(function ($standaloneDocker) {
|
||||||
|
66
app/Policies/ServerPolicy.php
Normal file
66
app/Policies/ServerPolicy.php
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -58,6 +58,7 @@ function refreshSession(): void
|
|||||||
function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
|
function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
ray($err);
|
||||||
ray('ERROR OCCURRED: ' . $err->getMessage());
|
ray('ERROR OCCURRED: ' . $err->getMessage());
|
||||||
if ($err instanceof QueryException) {
|
if ($err instanceof QueryException) {
|
||||||
if ($err->errorInfo[0] === '23505') {
|
if ($err->errorInfo[0] === '23505') {
|
||||||
@ -70,6 +71,9 @@ function general_error_handler(Throwable | null $err = null, $that = null, $isJs
|
|||||||
} elseif ($err instanceof TooManyRequestsException) {
|
} elseif ($err instanceof TooManyRequestsException) {
|
||||||
throw new Exception($customErrorMessage ?? "Too many requests. Please try again in {$err->secondsUntilAvailable} seconds.");
|
throw new Exception($customErrorMessage ?? "Too many requests. Please try again in {$err->secondsUntilAvailable} seconds.");
|
||||||
} else {
|
} else {
|
||||||
|
if ($err->getMessage() === 'This action is unauthorized.') {
|
||||||
|
return redirect()->route('dashboard')->with('error', $customErrorMessage ?? $err->getMessage());
|
||||||
|
}
|
||||||
throw new Exception($customErrorMessage ?? $err->getMessage());
|
throw new Exception($customErrorMessage ?? $err->getMessage());
|
||||||
}
|
}
|
||||||
} catch (Throwable $error) {
|
} catch (Throwable $error) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<div>
|
<div>
|
||||||
|
@if (session('error'))
|
||||||
|
<span x-data x-init="$wire.emit('error', '{{ session('error') }}')"/>
|
||||||
|
@endif
|
||||||
<h1>Dashboard</h1>
|
<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="w-full rounded stats stats-vertical lg:stats-horizontal">
|
||||||
<div class="stat">
|
<div class="stat">
|
||||||
<div class="stat-title">Servers</div>
|
<div class="stat-title">Servers</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user