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/StoreServerRequest.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
4
5
use Pterodactyl\Models\Server;
6
use Illuminate\Validation\Rule;
7
use Illuminate\Validation\Validator;
8
use Pterodactyl\Services\Acl\Api\AdminAcl;
9
use Pterodactyl\Models\Objects\DeploymentObject;
10
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
11
12
class StoreServerRequest extends ApplicationApiRequest
13
{
14
protected ?string $resource = AdminAcl::RESOURCE_SERVERS;
15
16
protected int $permission = AdminAcl::WRITE;
17
18
/**
19
* Rules to be applied to this request.
20
*/
21
public function rules(): array
22
{
23
$rules = Server::getRules();
24
25
return [
26
'external_id' => $rules['external_id'],
27
'name' => $rules['name'],
28
'description' => array_merge(['nullable'], $rules['description']),
29
'user' => $rules['owner_id'],
30
'egg' => $rules['egg_id'],
31
'docker_image' => $rules['image'],
32
'startup' => $rules['startup'],
33
'environment' => 'present|array',
34
'skip_scripts' => 'sometimes|boolean',
35
'oom_disabled' => 'sometimes|boolean',
36
37
// Resource limitations
38
'limits' => 'required|array',
39
'limits.memory' => $rules['memory'],
40
'limits.swap' => $rules['swap'],
41
'limits.disk' => $rules['disk'],
42
'limits.io' => $rules['io'],
43
'limits.threads' => $rules['threads'],
44
'limits.cpu' => $rules['cpu'],
45
46
// Application Resource Limits
47
'feature_limits' => 'required|array',
48
'feature_limits.databases' => $rules['database_limit'],
49
'feature_limits.allocations' => $rules['allocation_limit'],
50
'feature_limits.backups' => $rules['backup_limit'],
51
52
// Placeholders for rules added in withValidator() function.
53
'allocation.default' => '',
54
'allocation.additional.*' => '',
55
56
// Automatic deployment rules
57
'deploy' => 'sometimes|required|array',
58
'deploy.locations' => 'array',
59
'deploy.locations.*' => 'integer|min:1',
60
'deploy.dedicated_ip' => 'required_with:deploy,boolean',
61
'deploy.port_range' => 'array',
62
'deploy.port_range.*' => 'string',
63
64
'start_on_completion' => 'sometimes|boolean',
65
];
66
}
67
68
/**
69
* Normalize the data into a format that can be consumed by the service.
70
*/
71
public function validated($key = null, $default = null): array
72
{
73
$data = parent::validated();
74
75
return [
76
'external_id' => array_get($data, 'external_id'),
77
'name' => array_get($data, 'name'),
78
'description' => array_get($data, 'description'),
79
'owner_id' => array_get($data, 'user'),
80
'egg_id' => array_get($data, 'egg'),
81
'image' => array_get($data, 'docker_image'),
82
'startup' => array_get($data, 'startup'),
83
'environment' => array_get($data, 'environment'),
84
'memory' => array_get($data, 'limits.memory'),
85
'swap' => array_get($data, 'limits.swap'),
86
'disk' => array_get($data, 'limits.disk'),
87
'io' => array_get($data, 'limits.io'),
88
'cpu' => array_get($data, 'limits.cpu'),
89
'threads' => array_get($data, 'limits.threads'),
90
'skip_scripts' => array_get($data, 'skip_scripts', false),
91
'allocation_id' => array_get($data, 'allocation.default'),
92
'allocation_additional' => array_get($data, 'allocation.additional'),
93
'start_on_completion' => array_get($data, 'start_on_completion', false),
94
'database_limit' => array_get($data, 'feature_limits.databases'),
95
'allocation_limit' => array_get($data, 'feature_limits.allocations'),
96
'backup_limit' => array_get($data, 'feature_limits.backups'),
97
'oom_disabled' => array_get($data, 'oom_disabled'),
98
];
99
}
100
101
/*
102
* Run validation after the rules above have been applied.
103
*
104
* @param \Illuminate\Validation\Validator $validator
105
*/
106
public function withValidator(Validator $validator): void
107
{
108
$validator->sometimes('allocation.default', [
109
'required', 'integer', 'bail',
110
Rule::exists('allocations', 'id')->where(function ($query) {
111
$query->whereNull('server_id');
112
}),
113
], function ($input) {
114
return !$input->deploy;
115
});
116
117
$validator->sometimes('allocation.additional.*', [
118
'integer',
119
Rule::exists('allocations', 'id')->where(function ($query) {
120
$query->whereNull('server_id');
121
}),
122
], function ($input) {
123
return !$input->deploy;
124
});
125
126
$validator->sometimes('deploy.locations', 'present', function ($input) {
127
return $input->deploy;
128
});
129
130
$validator->sometimes('deploy.port_range', 'present', function ($input) {
131
return $input->deploy;
132
});
133
}
134
135
/**
136
* Return a deployment object that can be passed to the server creation service.
137
*/
138
public function getDeploymentObject(): ?DeploymentObject
139
{
140
if (is_null($this->input('deploy'))) {
141
return null;
142
}
143
144
$object = new DeploymentObject();
145
$object->setDedicated($this->input('deploy.dedicated_ip', false));
146
$object->setLocations($this->input('deploy.locations', []));
147
$object->setPorts($this->input('deploy.port_range', []));
148
149
return $object;
150
}
151
}
152
153