misc_pterodactyl-panel/app/Services/Servers/VariableValidatorService.php

79 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +00:00
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* 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.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(ValidationFactory $validator)
{
$this->validator = $validator;
}
/**
2018-05-13 14:50:56 +00:00
* Validate all of the passed data against the given service option variables.
*
2019-09-06 04:32:57 +00:00
* @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,
];
});
}
}