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
2023-11-06 10:51:20 +00:00
use App\Jobs\DeleteResourceJob ;
2024-08-29 16:01:16 +00:00
use App\Models\Service ;
use App\Models\ServiceApplication ;
2024-09-19 08:27:44 +00:00
use App\Models\ServiceDatabase ;
2025-08-22 14:47:59 +00:00
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
{
2025-08-22 14:47:59 +00:00
use AuthorizesRequests ;
2023-08-07 20:14:21 +00:00
public $resource ;
2024-09-19 08:27:44 +00:00
2024-08-29 16:01:16 +00:00
public $resourceName ;
2024-09-19 08:27:44 +00:00
2023-12-11 08:41:31 +00:00
public $projectUuid ;
2024-09-19 08:27:44 +00:00
2024-11-22 15:03:20 +00:00
public $environmentUuid ;
2024-09-19 08:27:44 +00:00
2024-04-12 10:44:49 +00:00
public bool $delete_configurations = true ;
2024-09-19 08:27:44 +00:00
2024-07-11 10:38:54 +00:00
public bool $delete_volumes = true ;
2024-09-19 08:27:44 +00:00
2024-08-27 12:19:37 +00:00
public bool $docker_cleanup = true ;
2024-09-19 08:27:44 +00:00
2024-08-08 22:30:11 +00:00
public bool $delete_connected_networks = true ;
2024-09-19 08:27:44 +00:00
2023-10-14 12:22:07 +00:00
public ? string $modalId = null ;
2024-09-19 08:27:44 +00:00
2024-08-29 16:01:16 +00:00
public string $resourceDomain = '' ;
2023-05-22 10:47:15 +00:00
2025-08-24 15:14:55 +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' ])) {
2026-03-10 21:11:52 +00:00
$this -> resource = Service :: ownedByCurrentTeam () -> where ( 'uuid' , $parameters [ 'service_uuid' ]) -> first ();
2024-08-29 16:53:49 +00:00
} elseif ( isset ( $parameters [ 'stack_service_uuid' ])) {
2026-03-10 21:11:52 +00:00
$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-09-19 08:27:44 +00:00
2024-08-29 16:53:49 +00:00
return ;
}
2024-08-29 19:47:17 +00:00
2024-09-19 08:27:44 +00:00
if ( ! method_exists ( $this -> resource , 'type' )) {
2024-08-29 16:53:49 +00:00
$this -> resourceName = 'Unknown Resource' ;
2024-09-19 08:27:44 +00:00
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' ,
};
2025-08-24 15:14:55 +00:00
// 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
2026-03-11 14:04:45 +00:00
public function delete ( $password , $selectedActions = [])
2023-05-22 10:47:15 +00:00
{
2025-12-12 13:12:02 +00:00
if ( ! verifyPasswordConfirmation ( $password , $this )) {
2026-03-11 14:04:45 +00:00
return 'The provided password is incorrect.' ;
2024-08-27 11:44:12 +00:00
}
2024-09-19 08:27:44 +00:00
if ( ! $this -> resource ) {
2026-03-11 14:04:45 +00:00
return 'Resource not found.' ;
}
2024-09-19 08:27:44 +00:00
2026-03-11 14:04:45 +00:00
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 {
2025-08-22 14:47:59 +00:00
$this -> authorize ( 'delete' , $this -> resource );
2024-01-30 16:38:07 +00:00
$this -> resource -> delete ();
2024-08-08 22:30:11 +00:00
DeleteResourceJob :: dispatch (
$this -> resource ,
$this -> delete_volumes ,
2025-08-04 19:15:56 +00:00
$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
2025-12-26 12:22:18 +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
]);
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2023-09-22 12:47:25 +00:00
return handleError ( $e , $this );
2023-09-22 09:23:49 +00:00
}
2023-05-22 10:47:15 +00:00
}
2024-08-31 14:19:39 +00:00
public function render ()
{
return view ( 'livewire.project.shared.danger' , [
'checkboxes' => [
2024-09-19 08:27:44 +00:00
[ '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.']
2024-09-19 08:27:44 +00:00
],
2024-08-31 14:19:39 +00:00
]);
}
2023-05-18 11:26:35 +00:00
}