forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableValidatorService.php
More file actions
78 lines (68 loc) · 2.56 KB
/
VariableValidatorService.php
File metadata and controls
78 lines (68 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\User;
use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable;
use Illuminate\Validation\ValidationException;
use Pterodactyl\Traits\Services\HasUserLevels;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
class VariableValidatorService
{
use HasUserLevels;
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
private $validator;
/**
* VariableValidatorService constructor.
*
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(ValidationFactory $validator)
{
$this->validator = $validator;
}
/**
* Validate all of the passed data against the given service option variables.
*
* @param int $egg
* @param array $fields
* @return \Illuminate\Support\Collection
* @throws \Illuminate\Validation\ValidationException
*/
public function handle(int $egg, array $fields = []): Collection
{
$query = EggVariable::query()->where('egg_id', $egg);
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN)) {
// Don't attempt to validate variables if they aren't user editable
// and we're not running this at an admin level.
$query = $query->where('user_editable', true)->where('user_viewable', true);
}
/** @var \Pterodactyl\Models\EggVariable[] $variables */
$variables = $query->get();
$data = $rules = $customAttributes = [];
foreach ($variables as $variable) {
$data['environment'][$variable->env_variable] = array_get($fields, $variable->env_variable);
$rules['environment.' . $variable->env_variable] = $variable->rules;
$customAttributes['environment.' . $variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]);
}
$validator = $this->validator->make($data, $rules, [], $customAttributes);
if ($validator->fails()) {
throw new ValidationException($validator);
}
return Collection::make($variables)->map(function ($item) use ($fields) {
return (object)[
'id' => $item->id,
'key' => $item->env_variable,
'value' => $fields[$item->env_variable] ?? null,
];
});
}
}