2025-10-08 18:47:50 +00:00
< ? php
namespace App\Livewire\Security ;
use App\Models\CloudProviderToken ;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests ;
use Livewire\Component ;
class CloudProviderTokens extends Component
{
use AuthorizesRequests ;
public $tokens ;
public function mount ()
{
$this -> authorize ( 'viewAny' , CloudProviderToken :: class );
$this -> loadTokens ();
}
2025-10-09 10:53:57 +00:00
public function getListeners ()
2025-10-08 18:47:50 +00:00
{
return [
2025-10-09 10:53:57 +00:00
'tokenAdded' => 'loadTokens' ,
2025-10-08 18:47:50 +00:00
];
}
public function loadTokens ()
{
$this -> tokens = CloudProviderToken :: ownedByCurrentTeam () -> get ();
}
public function deleteToken ( int $tokenId )
{
try {
2025-10-09 08:41:29 +00:00
$token = CloudProviderToken :: ownedByCurrentTeam () -> findOrFail ( $tokenId );
2025-10-08 18:47:50 +00:00
$this -> authorize ( 'delete' , $token );
2025-10-09 10:53:57 +00:00
// Check if any servers are using this token
if ( $token -> hasServers ()) {
$serverCount = $token -> servers () -> count ();
$this -> dispatch ( 'error' , " Cannot delete this token. It is currently used by { $serverCount } server(s). Please reassign those servers to a different token first. " );
return ;
}
2025-10-08 18:47:50 +00:00
$token -> delete ();
$this -> loadTokens ();
$this -> dispatch ( 'success' , 'Cloud provider token deleted successfully.' );
} catch ( \Throwable $e ) {
return handleError ( $e , $this );
}
}
public function render ()
{
return view ( 'livewire.security.cloud-provider-tokens' );
}
}