mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-14 08:42:38 -04:00
Scope resource permissions and related records to the owning team across team switches, cloning, deployments, and resource operations. Merge ClickHouse volume mounts, preserve anonymous data volumes on stop, and require adopting current data before starting or restarting. Fix file storage deletion checks and add regression coverage.
114 lines
3.8 KiB
PHP
114 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Server\PrivateKey;
|
|
|
|
use App\Models\PrivateKey;
|
|
use App\Models\Server;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Component;
|
|
|
|
class Show extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public Server $server;
|
|
|
|
public $privateKeys = [];
|
|
|
|
public $parameters = [];
|
|
|
|
public function mount(string $server_uuid)
|
|
{
|
|
try {
|
|
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
|
|
$this->loadPrivateKeys();
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
private function loadPrivateKeys(): void
|
|
{
|
|
$this->privateKeys = PrivateKey::where('team_id', $this->server->team_id)->get()->where('is_git_related', false);
|
|
}
|
|
|
|
public function setPrivateKey($privateKeyId)
|
|
{
|
|
$ownedPrivateKey = PrivateKey::where('team_id', $this->server->team_id)->find($privateKeyId);
|
|
if (is_null($ownedPrivateKey)) {
|
|
$this->dispatch('error', 'You are not allowed to use this private key.');
|
|
|
|
return;
|
|
}
|
|
try {
|
|
$this->authorize('update', $this->server);
|
|
DB::transaction(function () use ($ownedPrivateKey) {
|
|
$this->server->privateKey()->associate($ownedPrivateKey);
|
|
$this->server->save();
|
|
['uptime' => $uptime, 'error' => $error] = $this->server->validateConnection(justCheckingNewKey: true);
|
|
if (! $uptime) {
|
|
throw new \Exception($error);
|
|
}
|
|
});
|
|
$this->dispatch('success', 'Private key updated successfully.');
|
|
$this->dispatch('refreshServerShow');
|
|
} catch (\Exception $e) {
|
|
$this->server->refresh();
|
|
$this->server->validateConnection();
|
|
$this->dispatch('error', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function generatePrivateKey(string $type): void
|
|
{
|
|
try {
|
|
$this->authorize('create', PrivateKey::class);
|
|
$this->authorize('update', $this->server);
|
|
|
|
if (! in_array($type, ['ed25519', 'rsa'], true)) {
|
|
$this->dispatch('error', 'Invalid private key type.');
|
|
|
|
return;
|
|
}
|
|
|
|
$keyData = PrivateKey::generateNewKeyPair($type);
|
|
$privateKey = PrivateKey::createAndStore([
|
|
'name' => $keyData['name'],
|
|
'description' => $keyData['description'],
|
|
'private_key' => $keyData['private_key'],
|
|
'team_id' => $this->server->team_id,
|
|
]);
|
|
|
|
$this->loadPrivateKeys();
|
|
$this->dispatch('copyPublicKeyToClipboard', publicKey: $privateKey->public_key);
|
|
$this->dispatch('success', 'Private key created successfully.');
|
|
} catch (\Throwable $e) {
|
|
handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function checkConnection()
|
|
{
|
|
try {
|
|
['uptime' => $uptime, 'error' => $error] = $this->server->validateConnection();
|
|
if ($uptime) {
|
|
$this->dispatch('success', 'Server is reachable.');
|
|
$this->dispatch('refreshServerShow');
|
|
} else {
|
|
$sanitizedError = htmlspecialchars($error ?? '', ENT_QUOTES, 'UTF-8');
|
|
$this->dispatch('error', 'Server is not reachable.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help.<br><br>Error: '.$sanitizedError);
|
|
|
|
return;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.server.private-key.show');
|
|
}
|
|
}
|