Add ability to create new service variable.

This commit is contained in:
Dane Everitt 2017-03-12 16:28:38 -04:00
parent b1b1f448e9
commit f5a4ec981d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 104 additions and 13 deletions

View file

@ -55,7 +55,7 @@ class OptionController extends Controller
/**
* Handles POST request to create a new option.
*
* @param Request $request
* @return \Illuminate\Response\RedirectResponse
*/
@ -84,6 +84,36 @@ class OptionController extends Controller
return redirect()->route('admin.services.option.new')->withInput();
}
/**
* Handles POST request to create a new option variable.
*
* @param Request $request
* @param int $id The ID of the service option to assign this variable to.
* @return \Illuminate\Response\RedirectResponse
*/
public function createVariable(Request $request, $id)
{
$repo = new VariableRepository;
try {
$variable = $repo->create($id, $request->only([
'name', 'description', 'env_variable',
'default_value', 'options', 'rules',
]));
Alert::success('New variable successfully assigned to this service option.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.services.option.variables', $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 was encountered while attempting to process that request. This error has been logged.')->flash();
}
return redirect()->route('admin.services.option.variables', $id);
}
/**
* Display option overview page.
*

View file

@ -429,18 +429,19 @@ class AdminRoutes
'uses' => 'Admin\OptionController@viewConfiguration',
]);
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
$router->get('/option/{id}/variables', [
'as' => 'admin.services.option.variables',
'uses' => 'Admin\OptionController@viewVariables',
]);
$router->post('/option/{id}/variables', 'Admin\OptionController@createVariable');
$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

View file

@ -26,21 +26,28 @@ namespace Pterodactyl\Repositories;
use DB;
use Validator;
use Pterodactyl\Models\ServiceOption;
use Pterodactyl\Models\ServiceVariable;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class VariableRepository
{
public function __construct()
/**
* Create a new service variable.
*
* @param int $option
* @param array $data
* @return \Pterodactyl\Models\ServiceVariable
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function create($option, array $data)
{
//
}
$option = ServiceOption::select('id')->findOrFail($option);
public function create(array $data)
{
$validator = Validator::make($data, [
'option_id' => 'required|numeric|exists:service_options,id',
'name' => 'required|string|min:1|max:255',
'description' => 'sometimes|nullable|string',
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
@ -60,9 +67,7 @@ class VariableRepository
}
if (isset($data['env_variable'])) {
$search = ServiceVariable::where('env_variable', $data['env_variable'])
->where('option_id', $variable->option_id)
->where('id', '!=', $variable->id);
$search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
if ($search->first()) {
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
}
@ -72,6 +77,7 @@ class VariableRepository
$data['options'] = [];
}
$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']));

View file

@ -41,6 +41,7 @@
<ul class="nav nav-tabs">
<li><a href="{{ route('admin.services.option.view', $option->id) }}">Configuration</a></li>
<li class="active"><a href="{{ route('admin.services.option.variables', $option->id) }}">Variables</a></li>
<li class="tab-success"><a href="#modal" data-toggle="modal" data-target="#newVariableModal">New Variable</a></li>
</ul>
</div>
</div>
@ -99,6 +100,59 @@
</div>
@endforeach
</div>
<div class="modal fade" id="newVariableModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Create New Option Variable</h4>
</div>
<form action="{{ route('admin.services.option.variables', $option->id) }}" method="POST">
<div class="modal-body">
<div class="form-group">
<label class="form-label">Name</label>
<input type="text" name="name" class="form-control" />
</div>
<div class="form-group">
<label class="form-label">Description</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="form-label">Environment Variable</label>
<input type="text" name="env_variable" class="form-control" />
</div>
<div class="form-group col-md-6">
<label class="form-label">Default Value</label>
<input type="text" name="default_value" class="form-control" />
</div>
<div class="col-xs-12">
<p class="text-muted small">This variable can be accessed in the statup command by entering <code>@{{environment variable value}}</code>.</p>
</div>
</div>
<div class="form-group">
<label class="form-label">Permissions</label>
<select name="options[]" class="pOptions form-control" multiple>
<option value="user_viewable">Users Can View</option>
<option value="user_editable">Users Can Edit</option>
<option value="required">Field Is Required</option>
</select>
</div>
<div class="form-group">
<label class="form-label">Input Rules</label>
<input type="text" name="rules" class="form-control" placeholder="required|string|max:20" />
<p class="text-muted small">These rules are defined using standard Laravel Framework validation rules.</p>
</div>
</div>
<div class="modal-footer">
{!! csrf_field() !!}
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Create Variable</button>
</div>
</form>
</div>
</div>
</div>
@endsection
@section('footer-scripts')