2017-07-19 20:49:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-07-19 20:49:41 -05:00
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Arr;
|
2017-10-26 23:49:54 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2026-10-12 00:21:31 -04:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2017-11-25 13:15:46 -06:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Data\ValidatedEggVariable;
|
|
|
|
|
use Pterodactyl\Models\Egg;
|
|
|
|
|
use Pterodactyl\Models\EggVariable;
|
|
|
|
|
use Pterodactyl\Models\User;
|
2017-07-19 20:49:41 -05:00
|
|
|
|
|
|
|
|
class VariableValidatorService
|
|
|
|
|
{
|
|
|
|
|
/**
|
2026-10-12 00:21:31 -04:00
|
|
|
* Checks the submitted values against the rules of the egg's variables. Below the admin
|
|
|
|
|
* level only variables the user can view and edit are checked and returned.
|
2017-07-19 20:49:41 -05:00
|
|
|
*
|
2026-10-11 19:54:55 -04:00
|
|
|
* @param array<string, bool|float|int|string|null> $fields environment variable name => submitted value
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return Collection<int, ValidatedEggVariable>
|
|
|
|
|
*
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws ValidationException
|
2017-07-19 20:49:41 -05:00
|
|
|
*/
|
2026-10-12 00:21:31 -04:00
|
|
|
public function validate(Egg $egg, array $fields = [], int $userLevel = User::USER_LEVEL_USER): Collection
|
2017-07-19 20:49:41 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$query = $egg->variables();
|
2026-10-12 00:21:31 -04:00
|
|
|
if ($userLevel !== User::USER_LEVEL_ADMIN) {
|
2020-10-10 16:46:56 -07:00
|
|
|
$query = $query->where('user_editable', true)->where('user_viewable', true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$variables = $query->get();
|
2026-10-05 18:10:18 -04:00
|
|
|
$data = [];
|
|
|
|
|
$rules = [];
|
|
|
|
|
$customAttributes = [];
|
2020-10-10 16:46:56 -07:00
|
|
|
foreach ($variables as $variable) {
|
2026-10-05 18:10:18 -04:00
|
|
|
$data['environment'][$variable->env_variable] = Arr::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]);
|
2018-01-28 17:14:14 -06:00
|
|
|
}
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2026-10-12 00:21:31 -04:00
|
|
|
Validator::make($data, $rules, [], $customAttributes)->validate();
|
2017-07-19 20:49:41 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return $variables->map(fn (EggVariable $item): ValidatedEggVariable => new ValidatedEggVariable(
|
|
|
|
|
$item->id,
|
|
|
|
|
$item->env_variable,
|
|
|
|
|
$fields[$item->env_variable] ?? null,
|
|
|
|
|
));
|
2017-07-19 20:49:41 -05:00
|
|
|
}
|
|
|
|
|
}
|