95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Modals;
|
|
|
|
use App\Models\AuditEvent;
|
|
use App\Models\Server;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
/**
|
|
* Form modal: set/update a server's SSH credential (encrypted vault). This is
|
|
* where an operator deposits e.g. a root/sudo login to enable service control.
|
|
*/
|
|
class EditCredential extends ModalComponent
|
|
{
|
|
public int $serverId;
|
|
|
|
public string $name = '';
|
|
|
|
public string $username = '';
|
|
|
|
public string $authType = 'password';
|
|
|
|
public string $secret = '';
|
|
|
|
public string $passphrase = '';
|
|
|
|
public ?string $error = null;
|
|
|
|
public function mount(int $serverId): void
|
|
{
|
|
$this->serverId = $serverId;
|
|
|
|
if ($cred = Server::find($serverId)?->credential) {
|
|
$this->name = $cred->name ?? '';
|
|
$this->username = $cred->username;
|
|
$this->authType = $cred->auth_type;
|
|
// secret/passphrase are never pre-filled (encrypted at rest)
|
|
}
|
|
}
|
|
|
|
public static function modalMaxWidth(): string
|
|
{
|
|
return 'lg';
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$this->error = null;
|
|
|
|
$server = Server::find($this->serverId);
|
|
if (! $server) {
|
|
$this->error = 'Server nicht gefunden.';
|
|
|
|
return;
|
|
}
|
|
if (trim($this->username) === '' || trim($this->secret) === '') {
|
|
$this->error = 'Benutzer und Passwort/Schlüssel sind erforderlich.';
|
|
|
|
return;
|
|
}
|
|
if (mb_strlen(trim($this->name)) > 120) {
|
|
$this->error = 'Name darf höchstens 120 Zeichen lang sein.';
|
|
|
|
return;
|
|
}
|
|
|
|
$server->credential()->updateOrCreate([], [
|
|
'name' => trim($this->name) !== '' ? trim($this->name) : null,
|
|
'username' => trim($this->username),
|
|
'auth_type' => $this->authType === 'key' ? 'key' : 'password',
|
|
'secret' => $this->secret,
|
|
'passphrase' => $this->passphrase !== '' ? $this->passphrase : null,
|
|
]);
|
|
|
|
AuditEvent::create([
|
|
'user_id' => Auth::id(),
|
|
'server_id' => $server->id,
|
|
'actor' => Auth::user()?->name ?? 'system',
|
|
'action' => 'server.credential_updated',
|
|
'target' => $server->name.' · '.trim($this->username),
|
|
'ip' => request()->ip(),
|
|
]);
|
|
|
|
$this->dispatch('credentialChanged');
|
|
$this->dispatch('notify', message: 'Zugangsdaten gespeichert.');
|
|
$this->closeModal();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.modals.edit-credential');
|
|
}
|
|
}
|