From 19103b16b865f7ea213721317ed9a1f8536c7b59 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 2 Oct 2025 17:55:20 +0200 Subject: [PATCH] Allow both nodes for server requests when doing transfers (#1701) --- .../Requests/Api/Remote/ServerRequest.php | 10 +++++- .../Servers/TransferServerService.php | 34 ++++++++++--------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/Http/Requests/Api/Remote/ServerRequest.php b/app/Http/Requests/Api/Remote/ServerRequest.php index 32e1d2b1e..12da385f1 100644 --- a/app/Http/Requests/Api/Remote/ServerRequest.php +++ b/app/Http/Requests/Api/Remote/ServerRequest.php @@ -16,6 +16,14 @@ class ServerRequest extends FormRequest /** @var ?Server $server */ $server = $this->route()->parameter('server'); - return $server && $server->node_id === $node->id; + if ($server) { + if ($server->transfer) { + return $server->transfer->old_node === $node->id || $server->transfer->new_node === $node->id; + } + + return $server->node_id === $node->id; + } + + return false; } } diff --git a/app/Services/Servers/TransferServerService.php b/app/Services/Servers/TransferServerService.php index 420042cf6..65a472ff4 100644 --- a/app/Services/Servers/TransferServerService.php +++ b/app/Services/Servers/TransferServerService.php @@ -62,13 +62,15 @@ class TransferServerService $server->validateTransferState(); - $this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) { + /** @var ServerTransfer $transfer */ + $transfer = $this->connection->transaction(function () use ($server, $node_id, $allocation_id, $additional_allocations) { // Create a new ServerTransfer entry. - $transfer = new ServerTransfer(); + $transfer = ServerTransfer::create([ + 'server_id' => $server->id, + 'old_node' => $server->node_id, + 'new_node' => $node_id, + ]); - $transfer->server_id = $server->id; - $transfer->old_node = $server->node_id; - $transfer->new_node = $node_id; if ($server->allocation_id) { $transfer->old_allocation = $server->allocation_id; $transfer->new_allocation = $allocation_id; @@ -81,18 +83,18 @@ class TransferServerService $transfer->save(); - // Generate a token for the destination node that the source node can use to authenticate with. - $token = $this->nodeJWTService - ->setExpiresAt(CarbonImmutable::now()->addMinutes(15)) - ->setSubject($server->uuid) - ->handle($transfer->newNode, $server->uuid, 'sha256'); - - // Notify the source node of the pending outgoing transfer. - $this->notify($transfer, $token); - return $transfer; }); + // Generate a token for the destination node that the source node can use to authenticate with. + $token = $this->nodeJWTService + ->setExpiresAt(CarbonImmutable::now()->addMinutes(15)) + ->setSubject($server->uuid) + ->handle($transfer->newNode, $server->uuid, 'sha256'); + + // Notify the source node of the pending outgoing transfer. + $this->notify($transfer, $token); + return true; } @@ -106,7 +108,7 @@ class TransferServerService $allocations = $additional_allocations; $allocations[] = $allocation_id; - $node = Node::query()->findOrFail($node_id); + $node = Node::findOrFail($node_id); $unassigned = $node->allocations() ->whereNull('server_id') ->pluck('id') @@ -122,7 +124,7 @@ class TransferServerService } if (!empty($updateIds)) { - Allocation::query()->whereIn('id', $updateIds)->update(['server_id' => $server->id]); + Allocation::whereIn('id', $updateIds)->update(['server_id' => $server->id]); } } }