coolify/app/Livewire/Security/PrivateKey/Show.php

94 lines
3.2 KiB
PHP
Raw Normal View History

2023-05-03 10:38:57 +00:00
<?php
2024-01-07 15:23:41 +00:00
namespace App\Livewire\Security\PrivateKey;
2023-05-03 10:38:57 +00:00
use App\Models\PrivateKey;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2023-05-03 10:38:57 +00:00
use Livewire\Component;
2024-01-07 15:23:41 +00:00
class Show extends Component
2023-05-03 10:38:57 +00:00
{
use AuthorizesRequests;
2023-05-03 12:24:18 +00:00
public PrivateKey $private_key;
2024-06-10 20:43:34 +00:00
public $public_key = 'Loading...';
protected function rules(): array
{
return [
'private_key.name' => ValidationPatterns::nameRules(),
'private_key.description' => ValidationPatterns::descriptionRules(),
'private_key.private_key' => 'required|string',
'private_key.is_git_related' => 'nullable|boolean',
];
}
protected function messages(): array
{
return array_merge(
ValidationPatterns::combinedMessages(),
[
'private_key.name.required' => 'The Name field is required.',
'private_key.name.regex' => 'The Name may only contain letters, numbers, spaces, dashes (-), underscores (_), dots (.), slashes (/), colons (:), and parentheses ().',
'private_key.description.regex' => 'The Description contains invalid characters. Only letters, numbers, spaces, and common punctuation (- _ . : / () \' " , ! ? @ # % & + = [] {} | ~ ` *) are allowed.',
'private_key.private_key.required' => 'The Private Key field is required.',
'private_key.private_key.string' => 'The Private Key must be a valid string.',
]
);
}
2024-06-10 20:43:34 +00:00
2023-06-16 10:35:40 +00:00
protected $validationAttributes = [
'private_key.name' => 'name',
'private_key.description' => 'description',
2024-06-10 20:43:34 +00:00
'private_key.private_key' => 'private key',
2023-06-16 10:35:40 +00:00
];
public function mount()
{
try {
2024-01-07 15:23:41 +00:00
$this->private_key = PrivateKey::ownedByCurrentTeam(['name', 'description', 'private_key', 'is_git_related'])->whereUuid(request()->private_key_uuid)->firstOrFail();
} catch (\Throwable) {
2024-09-19 09:24:21 +00:00
abort(404);
}
}
2024-06-10 20:43:34 +00:00
public function loadPublicKey()
{
$this->public_key = $this->private_key->getPublicKey();
2024-09-17 11:06:50 +00:00
if ($this->public_key === 'Error loading private key') {
$this->dispatch('error', 'Failed to load public key. The private key may be invalid.');
}
2024-03-30 17:58:41 +00:00
}
2024-06-10 20:43:34 +00:00
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 {
$this->authorize('delete', $this->private_key);
2024-09-17 11:06:50 +00:00
$this->private_key->safeDelete();
currentTeam()->privateKeys = PrivateKey::where('team_id', currentTeam()->id)->get();
2024-09-19 09:24:21 +00:00
2024-09-17 11:06:50 +00:00
return redirect()->route('security.private-key.index');
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-16 11:13:09 +00:00
}
2023-05-03 10:38:57 +00:00
}
2023-05-03 10:38:57 +00:00
public function changePrivateKey()
{
try {
$this->authorize('update', $this->private_key);
2024-09-17 11:06:50 +00:00
$this->private_key->updatePrivateKey([
2024-09-19 09:24:21 +00:00
'private_key' => formatPrivateKey($this->private_key->private_key),
2024-09-17 11:06:50 +00:00
]);
2023-08-16 15:18:50 +00:00
refresh_server_connection($this->private_key);
2024-03-21 11:44:32 +00:00
$this->dispatch('success', 'Private key updated.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-03 10:38:57 +00:00
}
}
}