Complete new service, option, and variable management interface in Admin CP
This commit is contained in:
parent
bccbb309b2
commit
d7682bb7c9
16 changed files with 698 additions and 925 deletions
|
@ -28,10 +28,74 @@ use DB;
|
|||
use Validator;
|
||||
use Pterodactyl\Models\ServiceOption;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Repositories\VariableRepository;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class OptionRepository
|
||||
{
|
||||
/**
|
||||
* Creates a new service option on the system.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\ServiceOption
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$validator = Validator::make($data, [
|
||||
'service_id' => 'required|numeric|exists:services,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'tag' => 'required|string|max:255|unique:service_options,tag',
|
||||
'docker_image' => 'required|string|max:255',
|
||||
'startup' => 'required|string',
|
||||
'config_from' => 'sometimes|required|numeric|exists:service_options,id',
|
||||
'config_startup' => 'required_without:config_from|json',
|
||||
'config_stop' => 'required_without:config_from|string|max:255',
|
||||
'config_logs' => 'required_without:config_from|json',
|
||||
'config_files' => 'required_without:config_from|json',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
if (isset($data['config_from'])) {
|
||||
if (! ServiceOption::where('service_id', $data['service_id'])->where('id', $data['config_from'])->first()) {
|
||||
throw new DisplayException('The `configuration from` directive must be a child of the assigned service.');
|
||||
}
|
||||
}
|
||||
|
||||
return ServiceOption::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a service option from the system.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$option = ServiceOption::with('variables')->withCount('servers')->findOrFail($id);
|
||||
|
||||
if ($option->servers_count > 0) {
|
||||
throw new DisplayException('You cannot delete a service option that has servers associated with it.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($option) {
|
||||
foreach($option->variables as $variable) {
|
||||
(new VariableRepository)->delete($variable->id);
|
||||
}
|
||||
|
||||
$option->delete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a service option in the database which can then be used
|
||||
* on nodes.
|
||||
|
@ -39,11 +103,25 @@ class OptionRepository
|
|||
* @param int $id
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\ServiceOption
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$option = ServiceOption::findOrFail($id);
|
||||
|
||||
// Due to code limitations (at least when I am writing this currently)
|
||||
// we have to make an assumption that if config_from is not passed
|
||||
// that we should be telling it that no config is wanted anymore.
|
||||
//
|
||||
// This really is only an issue if we open API access to this function,
|
||||
// in which case users will always need to pass `config_from` in order
|
||||
// to keep it assigned.
|
||||
if (! isset($data['config_from']) && ! is_null($option->config_from)) {
|
||||
$option->config_from = null;
|
||||
}
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'name' => 'sometimes|required|string|max:255',
|
||||
'description' => 'sometimes|required|string',
|
||||
|
@ -73,6 +151,12 @@ class OptionRepository
|
|||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
if (isset($data['config_from'])) {
|
||||
if (! ServiceOption::where('service_id', $option->service_id)->where('id', $data['config_from'])->first()) {
|
||||
throw new DisplayException('The `configuration from` directive must be a child of the assigned service.');
|
||||
}
|
||||
}
|
||||
|
||||
$option->fill($data)->save();
|
||||
|
||||
return $option;
|
||||
|
|
|
@ -31,6 +31,7 @@ use Validator;
|
|||
use Pterodactyl\Models\Service;
|
||||
use Pterodactyl\Models\ServiceVariable;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Repositories\OptionRepository;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class ServiceRepository
|
||||
|
@ -55,13 +56,14 @@ class ServiceRepository
|
|||
}
|
||||
|
||||
$service = DB::transaction(function () use ($data) {
|
||||
$service = Service::create([
|
||||
'author' => config('pterodactyl.service.author'),
|
||||
$service = new Service;
|
||||
$service->author = config('pterodactyl.service.author');
|
||||
$service->fill([
|
||||
'name' => $data['name'],
|
||||
'description' => (isset($data['description'])) ? $data['description'] : null,
|
||||
'folder' => $data['folder'],
|
||||
'startup' => (isset($data['startup'])) ? $data['startup'] : null,
|
||||
]);
|
||||
])->save();
|
||||
|
||||
// It is possible for an event to return false or throw an exception
|
||||
// which won't necessarily be detected by this transaction.
|
||||
|
@ -105,8 +107,7 @@ class ServiceRepository
|
|||
$moveFiles = (isset($data['folder']) && $data['folder'] !== $service->folder);
|
||||
$oldFolder = $service->folder;
|
||||
|
||||
$service->fill($data);
|
||||
$service->save();
|
||||
$service->fill($data)->save();
|
||||
|
||||
if ($moveFiles) {
|
||||
Storage::move(sprintf('services/%s/index.js', $oldFolder), sprintf('services/%s/index.js', $service->folder));
|
||||
|
@ -124,18 +125,16 @@ class ServiceRepository
|
|||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$service = Service::withCount('servers', 'options')->findOrFail($id);
|
||||
$service = Service::withCount('servers')->with('options')->findOrFail($id);
|
||||
|
||||
if ($service->servers_count > 0) {
|
||||
throw new DisplayException('You cannot delete a service that has servers associated with it.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($service) {
|
||||
ServiceVariable::whereIn('option_id', $service->options->pluck('id')->all())->delete();
|
||||
|
||||
$service->options->each(function ($item) {
|
||||
$item->delete();
|
||||
});
|
||||
foreach($service->options as $option) {
|
||||
(new OptionRepository)->delete($option->id);
|
||||
}
|
||||
|
||||
$service->delete();
|
||||
Storage::deleteDirectory('services/' . $service->folder);
|
||||
|
|
|
@ -26,7 +26,7 @@ namespace Pterodactyl\Repositories;
|
|||
|
||||
use DB;
|
||||
use Validator;
|
||||
use Pterodactyl\Models;
|
||||
use Pterodactyl\Models\ServiceVariable;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
|
@ -37,108 +37,126 @@ class VariableRepository
|
|||
//
|
||||
}
|
||||
|
||||
public function create($id, array $data)
|
||||
public function create(array $data)
|
||||
{
|
||||
$option = Models\ServiceOption::select('id')->findOrFail($id);
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'option_id' => 'required|numeric|exists:service_options,id',
|
||||
'name' => 'required|string|min:1|max:255',
|
||||
'description' => 'required|string',
|
||||
'description' => 'sometimes|nullable|string',
|
||||
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
|
||||
'default_value' => 'string|max:255',
|
||||
'user_viewable' => 'sometimes|required|nullable|boolean',
|
||||
'user_editable' => 'sometimes|required|nullable|boolean',
|
||||
'required' => 'sometimes|required|nullable|boolean',
|
||||
'regex' => 'required|string|min:1',
|
||||
'default_value' => 'string',
|
||||
'options' => 'sometimes|required|array',
|
||||
'rules' => 'bail|required|string|min:1',
|
||||
]);
|
||||
|
||||
// Ensure the default value is allowed by the rules provided.
|
||||
$rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
|
||||
$validator->sometimes('default_value', $rules, function ($input) {
|
||||
return $input->default_value;
|
||||
});
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
if ($data['default_value'] !== '' && ! preg_match($data['regex'], $data['default_value'])) {
|
||||
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
|
||||
if (isset($data['env_variable'])) {
|
||||
$search = ServiceVariable::where('env_variable', $data['env_variable'])
|
||||
->where('option_id', $variable->option_id)
|
||||
->where('id', '!=', $variable->id);
|
||||
if ($search->first()) {
|
||||
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
|
||||
}
|
||||
}
|
||||
|
||||
if (Models\ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id)->first()) {
|
||||
throw new DisplayException('An environment variable with that name already exists for this option.');
|
||||
if (! isset($data['options']) || ! is_array($data['options'])) {
|
||||
$data['options'] = [];
|
||||
}
|
||||
|
||||
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0;
|
||||
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0;
|
||||
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0;
|
||||
$data['option_id'] = $option->id;
|
||||
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
|
||||
$data['user_editable'] = (in_array('user_editable', $data['options']));
|
||||
$data['required'] = (in_array('required', $data['options']));
|
||||
|
||||
$variable = Models\ServiceVariable::create($data);
|
||||
// Remove field that isn't used.
|
||||
unset($data['options']);
|
||||
|
||||
return $variable;
|
||||
return ServiceVariable::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specified option variable as well as all server
|
||||
* variables currently assigned.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$variable = Models\ServiceVariable::with('serverVariable')->findOrFail($id);
|
||||
$variable = ServiceVariable::with('serverVariable')->findOrFail($id);
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
foreach ($variable->serverVariable as $svar) {
|
||||
$svar->delete();
|
||||
DB::transaction(function () use ($variable) {
|
||||
foreach ($variable->serverVariable as $v) {
|
||||
$v->delete();
|
||||
}
|
||||
$variable->delete();
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
$variable->delete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a given service variable.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\ServiceVariable
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$variable = Models\ServiceVariable::findOrFail($id);
|
||||
$variable = ServiceVariable::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'name' => 'sometimes|required|string|min:1|max:255',
|
||||
'description' => 'sometimes|required|string',
|
||||
'description' => 'sometimes|nullable|string',
|
||||
'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/',
|
||||
'default_value' => 'sometimes|string|max:255',
|
||||
'user_viewable' => 'sometimes|required|nullable|boolean',
|
||||
'user_editable' => 'sometimes|required|nullable|boolean',
|
||||
'required' => 'sometimes|required|nullable|boolean',
|
||||
'regex' => 'sometimes|required|string|min:1',
|
||||
'default_value' => 'string',
|
||||
'options' => 'sometimes|required|array',
|
||||
'rules' => 'bail|sometimes|required|string|min:1',
|
||||
]);
|
||||
|
||||
// Ensure the default value is allowed by the rules provided.
|
||||
$rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
|
||||
$validator->sometimes('default_value', $rules, function ($input) {
|
||||
return $input->default_value;
|
||||
});
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException($validator->errors());
|
||||
}
|
||||
|
||||
$data['default_value'] = (isset($data['default_value'])) ? $data['default_value'] : $variable->default_value;
|
||||
$data['regex'] = (isset($data['regex'])) ? $data['regex'] : $variable->regex;
|
||||
|
||||
if ($data['default_value'] !== '' && ! preg_match($data['regex'], $data['default_value'])) {
|
||||
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
|
||||
if (isset($data['env_variable'])) {
|
||||
$search = ServiceVariable::where('env_variable', $data['env_variable'])
|
||||
->where('option_id', $variable->option_id)
|
||||
->where('id', '!=', $variable->id);
|
||||
if ($search->first()) {
|
||||
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
|
||||
}
|
||||
}
|
||||
|
||||
if (Models\ServiceVariable::where('id', '!=', $variable->id)->where('env_variable', $data['env_variable'])->where('option_id', $variable->option_id)->first()) {
|
||||
throw new DisplayException('An environment variable with that name already exists for this option.');
|
||||
if (! isset($data['options']) || ! is_array($data['options'])) {
|
||||
$data['options'] = [];
|
||||
}
|
||||
|
||||
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : $variable->user_viewable;
|
||||
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable;
|
||||
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required;
|
||||
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
|
||||
$data['user_editable'] = (in_array('user_editable', $data['options']));
|
||||
$data['required'] = (in_array('required', $data['options']));
|
||||
|
||||
// Not using $data because the function that passes into this function
|
||||
// can't do $requst->only() due to the page setup.
|
||||
$variable->fill([
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'],
|
||||
'env_variable' => $data['env_variable'],
|
||||
'default_value' => $data['default_value'],
|
||||
'user_viewable' => $data['user_viewable'],
|
||||
'user_editable' => $data['user_editable'],
|
||||
'required' => $data['required'],
|
||||
'regex' => $data['regex'],
|
||||
]);
|
||||
// Remove field that isn't used.
|
||||
unset($data['options']);
|
||||
|
||||
return $variable->save();
|
||||
$variable->fill($data)->save();
|
||||
|
||||
return $variable;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue