Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ public function show(Server $server): \Illuminate\View\View
]);
}

/**
* Determine whether the user has reached the per-product server limit.
*/
private function productLimitReached(User $user, Product $product): bool
{
if ($product->serverlimit == 0) {
return false;
}

$productCount = $user->servers()->where('product_id', $product->id)->count();

return $productCount >= $product->serverlimit;
}

private function getUpgradeOptions(Server $server, array $serverInfo): \Illuminate\Database\Eloquent\Collection
{
$currentProduct = Product::find($server->product_id);
Expand All @@ -396,6 +410,7 @@ private function getUpgradeOptions(Server $server, array $serverInfo): \Illumina
$currentEgg = $serverInfo['egg'];

//$currentProductEggs = $currentProduct->eggs->pluck('id')->toArray();
$user = Auth::user();

return Product::orderBy('price', 'asc')
->with('nodes')->with('eggs')
Expand All @@ -406,7 +421,7 @@ private function getUpgradeOptions(Server $server, array $serverInfo): \Illumina
$builder->where('id', $currentEgg);
})
->get()
->map(function ($product) use ($currentProduct, $pteroNode) {
->map(function ($product) use ($currentProduct, $pteroNode, $user) {
$product->eggs = $product->eggs->pluck('name')->toArray();

$memoryDiff = $product->memory - $currentProduct->memory;
Expand All @@ -420,6 +435,11 @@ private function getUpgradeOptions(Server $server, array $serverInfo): \Illumina
$product->doesNotFit = true;
}

// Check server limit for the product
if ($this->productLimitReached($user, $product)) {
$product->doesNotFit = true;
}

return $product;
});
}
Expand Down Expand Up @@ -448,6 +468,12 @@ public function upgrade(Server $server, Request $request): RedirectResponse
->with('error', __('Selected product not found'));
}

// Check server limit for the new product
if ($oldProduct->id !== $newProduct->id && $this->productLimitReached($user, $newProduct)) {
return redirect()->route('servers.show', ['server' => $server->id])
->with('error', __('You can not create any more Servers with this product!'));
}

if (!$this->validateUpgrade($server, $oldProduct, $newProduct)) {
return redirect()->route('servers.show', ['server' => $server->id])
->with('error', __('Insufficient resources or credits for upgrade'));
Expand Down Expand Up @@ -515,6 +541,11 @@ private function validateUpgrade(Server $server, Product $oldProduct, Product $n
return false;
}

// Check server limit for the new product
if ($oldProduct->id !== $newProduct->id && $this->productLimitReached($user, $newProduct)) {
return false;
}

return true;
}

Expand Down
Loading