Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
4
5
use Pterodactyl\Models\Server;
6
use Illuminate\Support\Collection;
7
8
class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
9
{
10
/**
11
* Return the rules to validate this request against.
12
*/
13
public function rules(): array
14
{
15
$rules = Server::getRulesForUpdate($this->parameter('server', Server::class));
16
17
return [
18
'allocation' => $rules['allocation_id'],
19
'oom_disabled' => $rules['oom_disabled'],
20
21
'limits' => 'sometimes|array',
22
'limits.memory' => $this->requiredToOptional('memory', $rules['memory'], true),
23
'limits.swap' => $this->requiredToOptional('swap', $rules['swap'], true),
24
'limits.io' => $this->requiredToOptional('io', $rules['io'], true),
25
'limits.cpu' => $this->requiredToOptional('cpu', $rules['cpu'], true),
26
'limits.threads' => $this->requiredToOptional('threads', $rules['threads'], true),
27
'limits.disk' => $this->requiredToOptional('disk', $rules['disk'], true),
28
29
// Legacy rules to maintain backwards compatable API support without requiring
30
// a major version bump.
31
//
32
// @see https://github.com/pterodactyl/panel/issues/1500
33
'memory' => $this->requiredToOptional('memory', $rules['memory']),
34
'swap' => $this->requiredToOptional('swap', $rules['swap']),
35
'io' => $this->requiredToOptional('io', $rules['io']),
36
'cpu' => $this->requiredToOptional('cpu', $rules['cpu']),
37
'threads' => $this->requiredToOptional('threads', $rules['threads']),
38
'disk' => $this->requiredToOptional('disk', $rules['disk']),
39
40
'add_allocations' => 'bail|array',
41
'add_allocations.*' => 'integer',
42
'remove_allocations' => 'bail|array',
43
'remove_allocations.*' => 'integer',
44
45
'feature_limits' => 'required|array',
46
'feature_limits.databases' => $rules['database_limit'],
47
'feature_limits.allocations' => $rules['allocation_limit'],
48
'feature_limits.backups' => $rules['backup_limit'],
49
];
50
}
51
52
/**
53
* Convert the allocation field into the expected format for the service handler.
54
*/
55
public function validated($key = null, $default = null): array
56
{
57
$data = parent::validated();
58
59
$data['allocation_id'] = $data['allocation'];
60
$data['database_limit'] = $data['feature_limits']['databases'] ?? null;
61
$data['allocation_limit'] = $data['feature_limits']['allocations'] ?? null;
62
$data['backup_limit'] = $data['feature_limits']['backups'] ?? null;
63
unset($data['allocation'], $data['feature_limits']);
64
65
// Adjust the limits field to match what is expected by the model.
66
if (!empty($data['limits'])) {
67
foreach ($data['limits'] as $key => $value) {
68
$data[$key] = $value;
69
}
70
71
unset($data['limits']);
72
}
73
74
return $data;
75
}
76
77
/**
78
* Custom attributes to use in error message responses.
79
*/
80
public function attributes(): array
81
{
82
return [
83
'add_allocations' => 'allocations to add',
84
'remove_allocations' => 'allocations to remove',
85
'add_allocations.*' => 'allocation to add',
86
'remove_allocations.*' => 'allocation to remove',
87
'feature_limits.databases' => 'Database Limit',
88
'feature_limits.allocations' => 'Allocation Limit',
89
'feature_limits.backups' => 'Backup Limit',
90
];
91
}
92
93
/**
94
* Converts existing rules for certain limits into a format that maintains backwards
95
* compatability with the old API endpoint while also supporting a more correct API
96
* call.
97
*
98
* @see https://github.com/pterodactyl/panel/issues/1500
99
*/
100
protected function requiredToOptional(string $field, array $rules, bool $limits = false): array
101
{
102
if (!in_array('required', $rules)) {
103
return $rules;
104
}
105
106
return (new Collection($rules))
107
->filter(function ($value) {
108
return $value !== 'required';
109
})
110
->prepend($limits ? 'required_with:limits' : 'required_without:limits')
111
->toArray();
112
}
113
}
114
115