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
|
@ -27,15 +27,63 @@ namespace Pterodactyl\Http\Controllers\Admin;
|
|||
use Log;
|
||||
use Alert;
|
||||
use Storage;
|
||||
use Pterodactyl\Models;
|
||||
use Javascript;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Service;
|
||||
use Pterodactyl\Models\ServiceOption;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Repositories\OptionRepository;
|
||||
use Pterodactyl\Repositories\VariableRepository;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class OptionController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handles request to view page for adding new option.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function new(Request $request)
|
||||
{
|
||||
$services = Service::with('options')->get();
|
||||
Javascript::put(['services' => $services->keyBy('id')]);
|
||||
|
||||
return view('admin.services.options.new', ['services' => $services]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles POST request to create a new option.
|
||||
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
$repo = new OptionRepository;
|
||||
|
||||
try {
|
||||
$option = $repo->create($request->intersect([
|
||||
'service_id', 'name', 'description', 'tag',
|
||||
'docker_image', 'startup', 'config_from', 'config_startup',
|
||||
'config_logs', 'config_files', 'config_stop'
|
||||
]));
|
||||
Alert::success('Successfully created new service option.')->flash();
|
||||
|
||||
return redirect()->route('admin.services.option.view', $option->id);
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.option.new')->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occurred while attempting to create this service. This error has been logged.')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.services.option.new')->withInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display option overview page.
|
||||
*
|
||||
|
@ -45,27 +93,89 @@ class OptionController extends Controller
|
|||
*/
|
||||
public function viewConfiguration(Request $request, $id)
|
||||
{
|
||||
return view('admin.services.options.view', ['option' => Models\ServiceOption::findOrFail($id)]);
|
||||
return view('admin.services.options.view', ['option' => ServiceOption::findOrFail($id)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display variable overview page for a service option.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function viewVariables(Request $request, $id)
|
||||
{
|
||||
return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')->findOrFail($id)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles POST when editing a configration for a service option.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
public function editConfiguration(Request $request, $id)
|
||||
{
|
||||
$repo = new OptionRepository;
|
||||
|
||||
try {
|
||||
$repo->update($id, $request->intersect([
|
||||
'name', 'description', 'tag', 'docker_image', 'startup',
|
||||
'config_from', 'config_stop', 'config_logs', 'config_files', 'config_startup',
|
||||
]));
|
||||
if ($request->input('action') !== 'delete') {
|
||||
$repo->update($id, $request->intersect([
|
||||
'name', 'description', 'tag', 'docker_image', 'startup',
|
||||
'config_from', 'config_stop', 'config_logs', 'config_files', 'config_startup',
|
||||
]));
|
||||
Alert::success('Service option configuration has been successfully updated.')->flash();
|
||||
} else {
|
||||
$option = ServiceOption::with('service')->where('id', $id)->first();
|
||||
$repo->delete($id);
|
||||
Alert::success('Successfully deleted service option from the system.')->flash();
|
||||
|
||||
Alert::success('Service option configuration has been successfully updated.')->flash();
|
||||
return redirect()->route('admin.services.view', $option->service_id);
|
||||
}
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.option.view', $id)->withErrors(json_decode($ex->getMessage()));
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception occurred while attempting to update this service option. This error has been logged.')->flash();
|
||||
Alert::danger('An unhandled exception occurred while attempting to perform that action. This error has been logged.')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.services.option.view', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles POST when editing a configration for a service option.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $option
|
||||
* @param int $variable
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
public function editVariable(Request $request, $option, $variable)
|
||||
{
|
||||
$repo = new VariableRepository;
|
||||
|
||||
try {
|
||||
if ($request->input('action') !== 'delete') {
|
||||
$variable = $repo->update($variable, $request->only([
|
||||
'name', 'description', 'env_variable',
|
||||
'default_value', 'options', 'rules',
|
||||
]));
|
||||
Alert::success("The service variable '{$variable->name}' has been updated.")->flash();
|
||||
} else {
|
||||
$repo->delete($variable);
|
||||
Alert::success("That service variable has been deleted.")->flash();
|
||||
}
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.option.variables', $option)->withErrors(json_decode($ex->getMessage()));
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.services.option.variables', $option);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,32 +103,6 @@ class ServiceController extends Controller
|
|||
return redirect()->route('admin.services.new')->withInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a service from the system.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
public function delete(Request $request, $id)
|
||||
{
|
||||
$repo = new ServiceRepository;
|
||||
|
||||
try {
|
||||
$repo->delete($id);
|
||||
Alert::success('Successfully deleted service.')->flash();
|
||||
|
||||
return redirect()->route('admin.services');
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An error was encountered while attempting to delete that service. This error has been logged')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.services.view', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits configuration for a specific service.
|
||||
*
|
||||
|
@ -141,10 +115,17 @@ class ServiceController extends Controller
|
|||
$repo = new ServiceRepository;
|
||||
|
||||
try {
|
||||
$repo->update($id, $request->intersect([
|
||||
'name', 'description', 'folder', 'startup',
|
||||
]));
|
||||
Alert::success('Service has been updated successfully.')->flash();
|
||||
if ($request->input('action') !== 'delete') {
|
||||
$repo->update($id, $request->intersect([
|
||||
'name', 'description', 'folder', 'startup',
|
||||
]));
|
||||
Alert::success('Service has been updated successfully.')->flash();
|
||||
} else {
|
||||
$repo->delete($id);
|
||||
Alert::success('Successfully deleted service from the system.')->flash();
|
||||
|
||||
return redirect()->route('admin.services');
|
||||
}
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.view', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
|
||||
} catch (DisplayException $ex) {
|
||||
|
|
|
@ -419,20 +419,25 @@ class AdminRoutes
|
|||
'uses' => 'Admin\OptionController@new',
|
||||
]);
|
||||
|
||||
$router->post('/option/new', 'Admin\OptionController@create');
|
||||
|
||||
$router->get('/option/{id}', [
|
||||
'as' => 'admin.services.option.view',
|
||||
'uses' => 'Admin\OptionController@viewConfiguration',
|
||||
]);
|
||||
|
||||
$router->get('/option/{id}/variables', [
|
||||
'as' => 'admin.services.option.view.variables',
|
||||
'as' => 'admin.services.option.variables',
|
||||
'uses' => 'Admin\OptionController@viewVariables',
|
||||
]);
|
||||
|
||||
$router->post('/option/{id}', [
|
||||
'uses' => 'Admin\OptionController@editConfiguration',
|
||||
$router->post('/option/{id}/variables/{variable}', [
|
||||
'as' => 'admin.services.option.variables.edit',
|
||||
'uses' => 'Admin\OptionController@editVariable',
|
||||
]);
|
||||
|
||||
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
|
||||
|
||||
});
|
||||
|
||||
// Service Packs
|
||||
|
|
|
@ -41,7 +41,7 @@ class Service extends Model
|
|||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'description', 'file', 'executable', 'startup',
|
||||
'name', 'description', 'folder', 'startup',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue