clusev/app/Livewire/Modals/AddSshKey.php

72 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\AuditEvent;
use App\Models\Server;
use App\Services\FleetService;
use Illuminate\Support\Facades\Auth;
use LivewireUI\Modal\ModalComponent;
use Throwable;
/**
* Form modal: paste an SSH public key, append it to the server's authorized_keys
* over SSH (real control), write an AuditEvent, and tell the page to reload.
*/
class AddSshKey extends ModalComponent
{
public int $serverId;
public string $publicKey = '';
public ?string $error = null;
public function mount(int $serverId): void
{
$this->serverId = $serverId;
}
public static function modalMaxWidth(): string
{
return 'lg';
}
public function save(FleetService $fleet): void
{
$this->error = null;
$server = Server::find($this->serverId);
if (! $server) {
$this->error = 'Server nicht gefunden.';
return;
}
try {
$fleet->addAuthorizedKey($server, $this->publicKey);
} catch (Throwable $e) {
$this->error = $e->getMessage();
return;
}
AuditEvent::create([
'user_id' => Auth::id(),
'server_id' => $server->id,
'actor' => Auth::user()?->name ?? 'system',
'action' => 'ssh_key.add',
'target' => $server->name,
'ip' => request()->ip(),
]);
$this->dispatch('keyChanged');
$this->dispatch('notify', message: 'SSH-Schlüssel hinzugefügt.');
$this->closeModal();
}
public function render()
{
return view('livewire.modals.add-ssh-key');
}
}