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;
|
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
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
|
|
|
*/
|
|
|
|
protected $optionVariableRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverVariableRepository;
|
|
|
|
|
|
|
|
/**
|
2017-07-23 01:15:01 +00:00
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
2017-07-20 01:49:41 +00:00
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
/**
|
|
|
|
* VariableValidatorService constructor.
|
|
|
|
*
|
2017-10-07 04:57:53 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $optionVariableRepository
|
2017-07-23 01:15:01 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
|
|
|
* @param \Illuminate\Contracts\Validation\Factory $validator
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate all of the passed data aganist the given service option variables.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param int $egg
|
|
|
|
* @param array $fields
|
|
|
|
* @return \Illuminate\Support\Collection
|
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
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
return $variables->map(function ($item) use ($fields) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
$validator = $this->validator->make([
|
2017-10-27 04:49:54 +00:00
|
|
|
'variable_value' => array_get($fields, $item->env_variable),
|
2017-07-20 01:49:41 +00:00
|
|
|
], [
|
|
|
|
'variable_value' => $item->rules,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new DisplayValidationException(json_encode(
|
2017-07-22 02:17:42 +00:00
|
|
|
collect([
|
|
|
|
'notice' => [
|
2017-07-23 01:15:01 +00:00
|
|
|
trans('admin/server.exceptions.bad_variable', ['name' => $item->name]),
|
2017-07-22 02:17:42 +00:00
|
|
|
],
|
|
|
|
])->merge($validator->errors()->toArray())
|
2017-07-20 01:49:41 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|