refactor
This commit is contained in:
parent
291b9a84ef
commit
fe68e45609
@ -4,10 +4,12 @@
|
||||
|
||||
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 @@ public function validateServer()
|
||||
|
||||
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();
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -33,6 +33,9 @@ protected static function booted()
|
||||
|
||||
});
|
||||
static::deleting(function ($server) {
|
||||
$server->destinations()->each(function ($destination) {
|
||||
$destination->delete();
|
||||
});
|
||||
$server->settings()->delete();
|
||||
});
|
||||
}
|
||||
@ -70,8 +73,6 @@ static public function destinationsByServer(string $server_id)
|
||||
return $standaloneDocker->concat($swarmDocker);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function settings()
|
||||
{
|
||||
return $this->hasOne(ServerSetting::class);
|
||||
@ -84,12 +85,20 @@ public function scopeWithProxy(): Builder
|
||||
|
||||
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) {
|
||||
|
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
|
||||
{
|
||||
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) {
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user