2023-05-03 10:38:57 +00:00
< ? php
namespace App\Http\Livewire\PrivateKey ;
use App\Models\PrivateKey ;
use Livewire\Component ;
class Change extends Component
{
2023-05-03 12:24:18 +00:00
public PrivateKey $private_key ;
2023-09-11 08:15:45 +00:00
public $public_key ;
2023-05-03 12:24:18 +00:00
protected $rules = [
'private_key.name' => 'required|string' ,
'private_key.description' => 'nullable|string' ,
2023-06-19 08:58:00 +00:00
'private_key.private_key' => 'required|string' ,
'private_key.is_git_related' => 'nullable|boolean'
2023-05-03 12:24:18 +00:00
];
2023-06-16 10:35:40 +00:00
protected $validationAttributes = [
'private_key.name' => 'name' ,
'private_key.description' => 'description' ,
'private_key.private_key' => 'private key'
];
2023-08-08 09:51:36 +00:00
2023-09-11 08:15:45 +00:00
public function mount ()
{
try {
$this -> public_key = $this -> private_key -> publicKey ();
} catch ( \Exception $e ) {
return general_error_handler ( err : $e , that : $this );
}
}
2023-05-04 13:45:53 +00:00
public function delete ()
2023-05-03 10:38:57 +00:00
{
2023-06-16 11:13:09 +00:00
try {
2023-06-19 07:44:39 +00:00
if ( $this -> private_key -> isEmpty ()) {
$this -> private_key -> delete ();
2023-08-22 15:44:49 +00:00
currentTeam () -> privateKeys = PrivateKey :: where ( 'team_id' , currentTeam () -> id ) -> get ();
2023-09-04 07:44:44 +00:00
return redirect () -> route ( 'security.private-key.index' );
2023-06-19 07:44:39 +00:00
}
$this -> emit ( 'error' , 'This private key is in use and cannot be deleted. Please delete all servers, applications, and GitHub/GitLab apps that use this private key before deleting it.' );
2023-09-11 15:36:30 +00:00
} catch ( \Throwable $e ) {
2023-06-16 11:13:09 +00:00
return general_error_handler ( err : $e , that : $this );
}
2023-05-03 10:38:57 +00:00
}
2023-08-08 09:51:36 +00:00
2023-05-03 10:38:57 +00:00
public function changePrivateKey ()
{
try {
2023-08-22 15:44:49 +00:00
$this -> private_key -> private_key = formatPrivateKey ( $this -> private_key -> private_key );
2023-05-03 12:24:18 +00:00
$this -> private_key -> save ();
2023-08-16 15:18:50 +00:00
refresh_server_connection ( $this -> private_key );
2023-09-11 15:36:30 +00:00
} catch ( \Throwable $e ) {
2023-06-09 13:55:21 +00:00
return general_error_handler ( err : $e , that : $this );
2023-05-03 10:38:57 +00:00
}
}
}