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;
|
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-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
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-10-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $optionVariableRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $serverRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
2017-11-25 19:15:46 +00:00
|
|
|
private $serverVariableRepository;
|
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.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $optionVariableRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
|
2017-07-23 01:15:01 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Contracts\Validation\Factory $validator
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-07-20 01:49:41 +00:00
|
|
|
public function __construct(
|
2017-10-07 04:57:53 +00:00
|
|
|
EggVariableRepositoryInterface $optionVariableRepository,
|
2017-07-20 01:49:41 +00:00
|
|
|
ServerRepositoryInterface $serverRepository,
|
|
|
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
|
|
|
ValidationFactory $validator
|
|
|
|
) {
|
|
|
|
$this->optionVariableRepository = $optionVariableRepository;
|
|
|
|
$this->serverRepository = $serverRepository;
|
|
|
|
$this->serverVariableRepository = $serverVariableRepository;
|
|
|
|
$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
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param int $egg
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param array $fields
|
|
|
|
* @return \Illuminate\Support\Collection
|
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
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$variables = $this->optionVariableRepository->findWhere([['egg_id', '=', $egg]]);
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$data = $rules = $customAttributes = [];
|
|
|
|
foreach ($variables as $variable) {
|
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.
|
|
|
|
if (! $variable->user_editable && ! $this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$response = $variables->filter(function ($item) {
|
|
|
|
// Skip doing anything if user is not an admin and variable is not user viewable or editable.
|
|
|
|
if (! $this->isUserLevel(User::USER_LEVEL_ADMIN) && (! $item->user_editable || ! $item->user_viewable)) {
|
|
|
|
return false;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
return true;
|
|
|
|
})->map(function ($item) use ($fields) {
|
2017-10-27 04:49:54 +00:00
|
|
|
return (object) [
|
2017-07-20 01:49:41 +00:00
|
|
|
'id' => $item->id,
|
|
|
|
'key' => $item->env_variable,
|
2017-10-27 04:49:54 +00:00
|
|
|
'value' => array_get($fields, $item->env_variable),
|
2017-07-20 01:49:41 +00:00
|
|
|
];
|
2017-10-27 04:49:54 +00:00
|
|
|
})->filter(function ($item) {
|
|
|
|
return is_object($item);
|
2017-07-20 01:49:41 +00:00
|
|
|
});
|
2017-11-25 19:15:46 +00:00
|
|
|
|
|
|
|
return $response;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
}
|