Path: blob/1.0-develop/app/Services/Servers/VariableValidatorService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Pterodactyl\Models\User;5use Illuminate\Support\Collection;6use Pterodactyl\Models\EggVariable;7use Illuminate\Validation\ValidationException;8use Pterodactyl\Traits\Services\HasUserLevels;9use Illuminate\Contracts\Validation\Factory as ValidationFactory;1011class VariableValidatorService12{13use HasUserLevels;1415/**16* VariableValidatorService constructor.17*/18public function __construct(private ValidationFactory $validator)19{20}2122/**23* Validate all of the passed data against the given service option variables.24*25* @throws ValidationException26*/27public function handle(int $egg, array $fields = []): Collection28{29$query = EggVariable::query()->where('egg_id', $egg);30if (!$this->isUserLevel(User::USER_LEVEL_ADMIN)) {31// Don't attempt to validate variables if they aren't user editable,32// and we're not running this at an admin level.33$query = $query->where('user_editable', true)->where('user_viewable', true);34}3536/** @var \Pterodactyl\Models\EggVariable[] $variables */37$variables = $query->get();3839$data = $rules = $customAttributes = [];40foreach ($variables as $variable) {41$data['environment'][$variable->env_variable] = array_get($fields, $variable->env_variable);42$rules['environment.' . $variable->env_variable] = $variable->rules;43$customAttributes['environment.' . $variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]);44}4546$validator = $this->validator->make($data, $rules, [], $customAttributes);47if ($validator->fails()) {48throw new ValidationException($validator);49}5051return Collection::make($variables)->map(function ($item) use ($fields) {52return (object) [53'id' => $item->id,54'key' => $item->env_variable,55'value' => $fields[$item->env_variable] ?? null,56];57});58}59}606162