Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/VariableValidatorService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Pterodactyl\Models\User;
6
use Illuminate\Support\Collection;
7
use Pterodactyl\Models\EggVariable;
8
use Illuminate\Validation\ValidationException;
9
use Pterodactyl\Traits\Services\HasUserLevels;
10
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
11
12
class VariableValidatorService
13
{
14
use HasUserLevels;
15
16
/**
17
* VariableValidatorService constructor.
18
*/
19
public function __construct(private ValidationFactory $validator)
20
{
21
}
22
23
/**
24
* Validate all of the passed data against the given service option variables.
25
*
26
* @throws ValidationException
27
*/
28
public function handle(int $egg, array $fields = []): Collection
29
{
30
$query = EggVariable::query()->where('egg_id', $egg);
31
if (!$this->isUserLevel(User::USER_LEVEL_ADMIN)) {
32
// Don't attempt to validate variables if they aren't user editable,
33
// and we're not running this at an admin level.
34
$query = $query->where('user_editable', true)->where('user_viewable', true);
35
}
36
37
/** @var \Pterodactyl\Models\EggVariable[] $variables */
38
$variables = $query->get();
39
40
$data = $rules = $customAttributes = [];
41
foreach ($variables as $variable) {
42
$data['environment'][$variable->env_variable] = array_get($fields, $variable->env_variable);
43
$rules['environment.' . $variable->env_variable] = $variable->rules;
44
$customAttributes['environment.' . $variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]);
45
}
46
47
$validator = $this->validator->make($data, $rules, [], $customAttributes);
48
if ($validator->fails()) {
49
throw new ValidationException($validator);
50
}
51
52
return Collection::make($variables)->map(function ($item) use ($fields) {
53
return (object) [
54
'id' => $item->id,
55
'key' => $item->env_variable,
56
'value' => $fields[$item->env_variable] ?? null,
57
];
58
});
59
}
60
}
61
62