coolify/app/Livewire/Project/Shared/Danger.php

143 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2023-05-18 11:26:35 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Shared;
2023-05-18 11:26:35 +00:00
use App\Jobs\DeleteResourceJob;
2024-08-29 16:01:16 +00:00
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2023-05-18 11:26:35 +00:00
use Livewire\Component;
2023-07-13 11:16:24 +00:00
use Visus\Cuid2\Cuid2;
2023-05-18 11:26:35 +00:00
class Danger extends Component
{
use AuthorizesRequests;
2023-08-07 20:14:21 +00:00
public $resource;
2024-08-29 16:01:16 +00:00
public $resourceName;
2023-12-11 08:41:31 +00:00
public $projectUuid;
2024-11-22 15:03:20 +00:00
public $environmentUuid;
public bool $delete_configurations = true;
2024-07-11 10:38:54 +00:00
public bool $delete_volumes = true;
2024-08-27 12:19:37 +00:00
public bool $docker_cleanup = true;
2024-08-08 22:30:11 +00:00
public bool $delete_connected_networks = true;
public ?string $modalId = null;
2024-08-29 16:01:16 +00:00
public string $resourceDomain = '';
2023-05-22 10:47:15 +00:00
public bool $canDelete = false;
2023-05-22 10:47:15 +00:00
public function mount()
{
2023-12-11 08:41:31 +00:00
$parameters = get_route_parameters();
2024-08-29 16:01:16 +00:00
$this->modalId = new Cuid2;
2024-01-31 13:22:48 +00:00
$this->projectUuid = data_get($parameters, 'project_uuid');
2024-11-22 15:03:20 +00:00
$this->environmentUuid = data_get($parameters, 'environment_uuid');
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
if ($this->resource === null) {
if (isset($parameters['service_uuid'])) {
$this->resource = Service::ownedByCurrentTeam()->where('uuid', $parameters['service_uuid'])->first();
2024-08-29 16:53:49 +00:00
} elseif (isset($parameters['stack_service_uuid'])) {
$this->resource = ServiceApplication::ownedByCurrentTeam()->where('uuid', $parameters['stack_service_uuid'])->first()
?? ServiceDatabase::ownedByCurrentTeam()->where('uuid', $parameters['stack_service_uuid'])->first();
2024-08-29 16:53:49 +00:00
}
}
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
if ($this->resource === null) {
$this->resourceName = 'Unknown Resource';
2024-08-29 16:53:49 +00:00
return;
}
2024-08-29 19:47:17 +00:00
if (! method_exists($this->resource, 'type')) {
2024-08-29 16:53:49 +00:00
$this->resourceName = 'Unknown Resource';
2024-08-29 16:53:49 +00:00
return;
}
2024-08-29 19:47:17 +00:00
2024-10-31 14:14:30 +00:00
$this->resourceName = match ($this->resource->type()) {
'application' => $this->resource->name ?? 'Application',
'standalone-postgresql',
'standalone-redis',
'standalone-mongodb',
'standalone-mysql',
'standalone-mariadb',
'standalone-keydb',
'standalone-dragonfly',
'standalone-clickhouse' => $this->resource->name ?? 'Database',
'service' => $this->resource->name ?? 'Service',
'service-application' => $this->resource->name ?? 'Service Application',
'service-database' => $this->resource->name ?? 'Service Database',
default => 'Unknown Resource',
};
// Check if user can delete this resource
try {
$this->canDelete = auth()->user()->can('delete', $this->resource);
} catch (\Exception $e) {
$this->canDelete = false;
}
2023-05-22 10:47:15 +00:00
}
2024-06-10 20:43:34 +00:00
public function delete($password, $selectedActions = [])
2023-05-22 10:47:15 +00:00
{
if (! verifyPasswordConfirmation($password, $this)) {
return 'The provided password is incorrect.';
2024-08-27 11:44:12 +00:00
}
if (! $this->resource) {
return 'Resource not found.';
}
if (! empty($selectedActions)) {
$this->delete_volumes = in_array('delete_volumes', $selectedActions);
$this->delete_connected_networks = in_array('delete_connected_networks', $selectedActions);
$this->delete_configurations = in_array('delete_configurations', $selectedActions);
$this->docker_cleanup = in_array('docker_cleanup', $selectedActions);
2024-08-29 16:01:16 +00:00
}
2023-09-22 09:23:49 +00:00
try {
$this->authorize('delete', $this->resource);
$this->resource->delete();
2024-08-08 22:30:11 +00:00
DeleteResourceJob::dispatch(
$this->resource,
$this->delete_volumes,
$this->delete_connected_networks,
$this->delete_configurations,
$this->docker_cleanup
2024-08-08 22:30:11 +00:00
);
2024-06-10 20:43:34 +00:00
return redirectRoute($this, 'project.resource.index', [
2023-12-11 08:41:31 +00:00
'project_uuid' => $this->projectUuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $this->environmentUuid,
2023-12-27 15:45:01 +00:00
]);
} catch (\Throwable $e) {
return handleError($e, $this);
2023-09-22 09:23:49 +00:00
}
2023-05-22 10:47:15 +00:00
}
public function render()
{
return view('livewire.project.shared.danger', [
'checkboxes' => [
['id' => 'delete_volumes', 'label' => __('resource.delete_volumes')],
['id' => 'delete_connected_networks', 'label' => __('resource.delete_connected_networks')],
['id' => 'delete_configurations', 'label' => __('resource.delete_configurations')],
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
2024-09-03 16:31:06 +00:00
// ['id' => 'delete_associated_backups_locally', 'label' => 'All backups associated with this Ressource will be permanently deleted from local storage.'],
// ['id' => 'delete_associated_backups_s3', 'label' => 'All backups associated with this Ressource will be permanently deleted from the selected S3 Storage.'],
// ['id' => 'delete_associated_backups_sftp', 'label' => 'All backups associated with this Ressource will be permanently deleted from the selected SFTP Storage.']
],
]);
}
2023-05-18 11:26:35 +00:00
}