2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-07-20 01:49:41 +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
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2020-10-10 23:46:56 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-11-25 19:15:46 +00:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
class VariableValidatorService
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
use HasUserLevels;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-23 01:15:01 +00:00
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $validator;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
/**
|
|
|
|
* VariableValidatorService constructor.
|
|
|
|
*/
|
2020-10-10 23:46:56 +00:00
|
|
|
public function __construct(ValidationFactory $validator)
|
|
|
|
{
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->validator = $validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Validate all of the passed data against the given service option variables.
|
2017-07-20 01:49:41 +00:00
|
|
|
*
|
2017-11-25 19:15:46 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
public function handle(int $egg, array $fields = []): Collection
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2020-10-10 23:46:56 +00:00
|
|
|
$query = EggVariable::query()->where('egg_id', $egg);
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
2018-02-16 02:58:51 +00:00
|
|
|
// Don't attempt to validate variables if they aren't user editable
|
|
|
|
// and we're not running this at an admin level.
|
2020-10-10 23:46:56 +00:00
|
|
|
$query = $query->where('user_editable', true)->where('user_viewable', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\EggVariable[] $variables */
|
|
|
|
$variables = $query->get();
|
2018-02-16 02:58:51 +00:00
|
|
|
|
2020-10-10 23:46:56 +00:00
|
|
|
$data = $rules = $customAttributes = [];
|
|
|
|
foreach ($variables as $variable) {
|
2018-01-28 23:14:14 +00:00
|
|
|
$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]);
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$validator = $this->validator->make($data, $rules, [], $customAttributes);
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2020-10-10 23:46:56 +00:00
|
|
|
return Collection::make($variables)->map(function ($item) use ($fields) {
|
2021-01-23 20:09:16 +00:00
|
|
|
return (object) [
|
2017-07-20 01:49:41 +00:00
|
|
|
'id' => $item->id,
|
|
|
|
'key' => $item->env_variable,
|
2020-10-10 23:46:56 +00:00
|
|
|
'value' => $fields[$item->env_variable] ?? null,
|
2017-07-20 01:49:41 +00:00
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|