Fix front and backend views with new service variable setups

This commit is contained in:
Dane Everitt 2017-03-12 19:34:06 -04:00
parent 66e94dd7c0
commit fcadee7e67
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 104 additions and 93 deletions

View file

@ -493,6 +493,8 @@ class ServersController extends Controller
$repo->updateStartup($id, $request->except('_token'), true); $repo->updateStartup($id, $request->except('_token'), true);
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash(); Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view.startup', $id)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
} catch (TransferException $ex) { } catch (TransferException $ex) {

View file

@ -209,37 +209,20 @@ class ServerController extends Controller
public function getStartup(Request $request, $uuid) public function getStartup(Request $request, $uuid)
{ {
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid);
$server->load(['allocations' => function ($query) use ($server) { $server->load(['node', 'allocation', 'variables.variable']);
$query->where('id', $server->allocation_id);
}]);
$this->authorize('view-startup', $server); $this->authorize('view-startup', $server);
$variables = Models\ServiceVariable::select( $replacements = [
'service_variables.*',
DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue')
)->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
->where('service_variables.option_id', $server->option_id)
->where('server_variables.server_id', $server->id)
->get();
$service = Models\Service::select(
DB::raw('IFNULL(service_options.executable, services.executable) as executable')
)->leftJoin('service_options', 'service_options.service_id', '=', 'services.id')
->where('service_options.id', $server->option_id)
->where('services.id', $server->service_id)
->first();
$allocation = $server->allocations->pop();
$ServerVariable = [
'{{SERVER_MEMORY}}' => $server->memory, '{{SERVER_MEMORY}}' => $server->memory,
'{{SERVER_IP}}' => $allocation->ip, '{{SERVER_IP}}' => $server->allocation->ip,
'{{SERVER_PORT}}' => $allocation->port, '{{SERVER_PORT}}' => $server->allocation->port,
]; ];
$processed = str_replace(array_keys($ServerVariable), array_values($ServerVariable), $server->startup); $processed = str_replace(array_keys($replacements), array_values($replacements), $server->startup);
foreach ($variables as &$variable) { foreach ($server->variables as $v) {
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '[hidden]'; $replace = ($v->user_can_view) ? $v->variable_value : '[hidden]';
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed); $processed = str_replace('{{' . $v->variable->env_variable . '}}', $replace, $processed);
} }
$server->js(); $server->js();
@ -247,8 +230,8 @@ class ServerController extends Controller
return view('server.settings.startup', [ return view('server.settings.startup', [
'server' => $server, 'server' => $server,
'node' => $server->node, 'node' => $server->node,
'variables' => $variables->where('user_viewable', 1), 'variables' => $server->variables->where('user_can_view', true),
'service' => $service, 'service' => $server->service,
'processedStartup' => $processed, 'processedStartup' => $processed,
]); ]);
} }
@ -311,6 +294,8 @@ class ServerController extends Controller
$repo = new ServerRepository; $repo = new ServerRepository;
$repo->updateStartup($server->id, $request->except('_token')); $repo->updateStartup($server->id, $request->except('_token'));
Alert::success('Server startup variables were successfully updated.')->flash(); Alert::success('Server startup variables were successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('server.settings.startup', $uuid)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) { } catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) { } catch (\Exception $ex) {

View file

@ -111,7 +111,7 @@ class Server extends Model
* @param string $uuid The Short-UUID of the server to return an object about. * @param string $uuid The Short-UUID of the server to return an object about.
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public static function byUuid($uuid) public static function byUuid($uuid, array $with = [], array $withCount = [])
{ {
if (! Auth::check()) { if (! Auth::check()) {
throw new \Exception('You must call Server:byUuid as an authenticated user.'); throw new \Exception('You must call Server:byUuid as an authenticated user.');

View file

@ -52,6 +52,36 @@ class ServerVariable extends Model
'variable_id' => 'integer', 'variable_id' => 'integer',
]; ];
/**
* Determine if variable is viewable by users.
*
* @return bool
*/
public function getUserCanViewAttribute()
{
return (bool) $this->variable->user_viewable;
}
/**
* Determine if variable is editable by users.
*
* @return bool
*/
public function getUserCanEditAttribute()
{
return (bool) $this->variable->user_editable;
}
/**
* Determine if variable is required.
*
* @return bool
*/
public function getRequiredAttribute()
{
return $this->variable->required;
}
/** /**
* Returns information about a given variables parent. * Returns information about a given variables parent.
* *

View file

@ -51,17 +51,6 @@ class ServiceOption extends Model
'service_id' => 'integer', 'service_id' => 'integer',
]; ];
/**
* Returns the display executable for the option and will use the parent
* service one if the option does not have one defined.
*
* @return string
*/
public function getDisplayExecutableAttribute($value)
{
return (is_null($this->executable)) ? $this->service->executable : $this->executable;
}
/** /**
* Returns the display startup string for the option and will use the parent * Returns the display startup string for the option and will use the parent
* service one if the option does not have one defined. * service one if the option does not have one defined.

View file

@ -47,13 +47,29 @@ class ServiceVariable extends Model
* *
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'option_id' => 'integer', 'option_id' => 'integer',
'user_viewable' => 'integer', 'user_viewable' => 'integer',
'user_editable' => 'integer', 'user_editable' => 'integer',
'required' => 'integer', 'required' => 'integer',
]; ];
/**
* Returns the display executable for the option and will use the parent
* service one if the option does not have one defined.
*
* @return string
*/
public function getRequiredAttribute($value)
{
return ($this->rules === 'required' || str_contains($this->rules, ['required|', '|required']));
}
/**
* Return server variables associated with this variable.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function serverVariable() public function serverVariable()
{ {
return $this->hasMany(ServerVariable::class, 'variable_id'); return $this->hasMany(ServerVariable::class, 'variable_id');

View file

@ -627,23 +627,25 @@ class ServerRepository
foreach ($server->option->variables as &$variable) { foreach ($server->option->variables as &$variable) {
$set = isset($data['env_' . $variable->id]); $set = isset($data['env_' . $variable->id]);
// Variable is required but was not passed into the function.
if ($variable->required && ! $set) {
throw new DisplayException('A required variable (' . $variable->env_variable . ') was not passed in the request.');
}
// If user is not an admin and are trying to edit a non-editable field // If user is not an admin and are trying to edit a non-editable field
// or an invisible field just silently skip the variable. // or an invisible field just silently skip the variable.
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) { if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
continue; continue;
} }
// Confirm value is valid when compared aganist regex. // Perform Field Validation
// @TODO: switch to Laravel validation rules. $validator = Validator::make([
if ($set && ! is_null($variable->regex)) { 'variable_value' => ($set) ? $data['env_' . $variable->id] : null,
if (! preg_match($variable->regex, $data['env_' . $variable->id])) { ], [
throw new DisplayException('The value passed for a variable (' . $variable->env_variable . ') could not be matched aganist the regex for that field (' . $variable->regex . ').'); 'variable_value' => $variable->rules,
} ]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode(
collect([
'notice' => ['There was a validation error with the `' . $variable->name . '` variable.']
])->merge($validator->errors()->toArray())
));
} }
$svar = Models\ServerVariable::firstOrNew([ $svar = Models\ServerVariable::firstOrNew([

View file

@ -80,7 +80,6 @@ class VariableRepository
$data['option_id'] = $option->id; $data['option_id'] = $option->id;
$data['user_viewable'] = (in_array('user_viewable', $data['options'])); $data['user_viewable'] = (in_array('user_viewable', $data['options']));
$data['user_editable'] = (in_array('user_editable', $data['options'])); $data['user_editable'] = (in_array('user_editable', $data['options']));
$data['required'] = (in_array('required', $data['options']));
// Remove field that isn't used. // Remove field that isn't used.
unset($data['options']); unset($data['options']);
@ -156,7 +155,6 @@ class VariableRepository
$data['user_viewable'] = (in_array('user_viewable', $data['options'])); $data['user_viewable'] = (in_array('user_viewable', $data['options']));
$data['user_editable'] = (in_array('user_editable', $data['options'])); $data['user_editable'] = (in_array('user_editable', $data['options']));
$data['required'] = (in_array('required', $data['options']));
// Remove field that isn't used. // Remove field that isn't used.
unset($data['options']); unset($data['options']);

View file

@ -24,6 +24,10 @@ class ChangeServiceVariablesValidationRules extends Migration
$variable->save(); $variable->save();
} }
}); });
Schema::table('service_variables', function (Blueprint $table) {
$table->dropColumn('required');
});
} }
/** /**
@ -35,6 +39,7 @@ class ChangeServiceVariablesValidationRules extends Migration
{ {
Schema::table('service_variables', function (Blueprint $table) { Schema::table('service_variables', function (Blueprint $table) {
$table->renameColumn('rules', 'regex'); $table->renameColumn('rules', 'regex');
$table->boolean('required')->default(true)->before('regex');
}); });
DB::transaction(function () { DB::transaction(function () {

View file

@ -246,7 +246,7 @@ EOF;
'user_viewable' => 0, 'user_viewable' => 0,
'user_editable' => 0, 'user_editable' => 0,
'required' => 0, 'required' => 0,
'rules' => 'required|string', 'rules' => 'string',
]); ]);
} }

View file

@ -234,7 +234,7 @@ return [
'edit_params' => 'Edit Parameters', 'edit_params' => 'Edit Parameters',
'update' => 'Update Startup Parameters', 'update' => 'Update Startup Parameters',
'startup_var' => 'Startup Command Variable', 'startup_var' => 'Startup Command Variable',
'startup_regex' => 'Verification Regex', 'startup_regex' => 'Input Rules',
], ],
'sftp' => [ 'sftp' => [
'header' => 'SFTP Configuration', 'header' => 'SFTP Configuration',

View file

@ -62,10 +62,7 @@
</div> </div>
<div class="box-body"> <div class="box-body">
<label for="pStartup" class="form-label">Startup Command</label> <label for="pStartup" class="form-label">Startup Command</label>
<div class="input-group"> <input id="pStartup" name="startup" class="form-control" type="text" value="{{ old('startup', $server->startup) }}" />
<span class="input-group-addon bg-gray">{{ $server->option->display_executable }}</span>
<input id="pStartup" name="startup" class="form-control" type="text" value="{{ old('startup', $server->startup) }}" />
</div>
<p class="small text-muted">Edit your server's startup command here. The following variables are available by default: <code>@{{SERVER_MEMORY}}</code>, <code>@{{SERVER_IP}}</code>, and <code>@{{SERVER_PORT}}</code>.</p> <p class="small text-muted">Edit your server's startup command here. The following variables are available by default: <code>@{{SERVER_MEMORY}}</code>, <code>@{{SERVER_IP}}</code>, and <code>@{{SERVER_PORT}}</code>.</p>
</div> </div>
<div class="box-footer"> <div class="box-footer">
@ -91,7 +88,7 @@
</div> </div>
<div class="box-footer"> <div class="box-footer">
<p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>{{ $variable->env_variable }}</code></p> <p class="no-margin text-muted small"><strong>Startup Command Variable:</strong> <code>{{ $variable->env_variable }}</code></p>
<p class="no-margin text-muted small"><strong>Verification Regex:</strong> <code>{{ $variable->regex }}</code></p> <p class="no-margin text-muted small"><strong>Input Rules:</strong> <code>{{ $variable->rules }}</code></p>
</div> </div>
</div> </div>
</div> </div>

View file

@ -81,7 +81,6 @@
<select name="options[]" class="pOptions form-control" multiple> <select name="options[]" class="pOptions form-control" multiple>
<option value="user_viewable" {{ (! $variable->user_viewable) ?: 'selected' }}>Users Can View</option> <option value="user_viewable" {{ (! $variable->user_viewable) ?: 'selected' }}>Users Can View</option>
<option value="user_editable" {{ (! $variable->user_editable) ?: 'selected' }}>Users Can Edit</option> <option value="user_editable" {{ (! $variable->user_editable) ?: 'selected' }}>Users Can Edit</option>
<option value="required" {{ (! $variable->required) ?: 'selected' }}>Field Is Required</option>
</select> </select>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -135,7 +134,6 @@
<select name="options[]" class="pOptions form-control" multiple> <select name="options[]" class="pOptions form-control" multiple>
<option value="user_viewable">Users Can View</option> <option value="user_viewable">Users Can View</option>
<option value="user_editable">Users Can Edit</option> <option value="user_editable">Users Can Edit</option>
<option value="required">Field Is Required</option>
</select> </select>
</div> </div>
<div class="form-group"> <div class="form-group">

View file

@ -42,9 +42,8 @@
<h3 class="box-title">@lang('server.config.startup.command')</h3> <h3 class="box-title">@lang('server.config.startup.command')</h3>
</div> </div>
<div class="box-body"> <div class="box-body">
<div class="input-group"> <div class="form-group">
<span class="input-group-addon">{{ $service->executable }}</span> <input type="text" class="form-control" readonly value="{{ $processedStartup }}" />
<input type="text" class="form-control" readonly="readonly" value="{{ $processedStartup }}" />
</div> </div>
</div> </div>
@can('edit-startup', $server) @can('edit-startup', $server)
@ -56,35 +55,35 @@
</div> </div>
</div> </div>
@can('edit-startup', $server) @can('edit-startup', $server)
@foreach($variables as $variable) @foreach($variables as $v)
<div class="col-xs-12 col-md-4 col-sm-6"> <div class="col-xs-12 col-md-4 col-sm-6">
<div class="box"> <div class="box">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ $variable->name }}</h3> <h3 class="box-title">{{ $v->variable->name }}</h3>
</div> </div>
<div class="box-body"> <div class="box-body">
<input data-action="match-regex" data-regex="{{ $variable->regex }}" <input
@if($variable->user_editable) @if($v->user_can_edit)
name="env_{{ $variable->id }}" name="env_{{ $v->variable->id }}"
@else @else
readonly readonly
@endif @endif
class="form-control" type="text" value="{{ old('env_' . $variable->id, $variable->server_value) }}" /> class="form-control" type="text" value="{{ old('env_' . $v->id, $v->variable_value) }}" />
<p class="small text-muted">{{ $variable->description }}</p> <p class="small text-muted">{{ $v->variable->description }}</p>
<p class="no-margin"> <p class="no-margin">
@if($variable->required && $variable->user_editable) @if($v->required && $v->user_can_edit)
<span class="label label-danger">@lang('strings.required')</span> <span class="label label-danger">@lang('strings.required')</span>
@elseif(! $variable->required && $variable->user_editable) @elseif(! $v->required && $v->user_can_edit)
<span class="label label-default">@lang('strings.optional')</span> <span class="label label-default">@lang('strings.optional')</span>
@endif @endif
@if(! $variable->user_editable) @if(! $v->user_can_edit)
<span class="label label-warning">@lang('strings.read_only')</span> <span class="label label-warning">@lang('strings.read_only')</span>
@endif @endif
</p> </p>
</div> </div>
<div class="box-footer"> <div class="box-footer">
<p class="no-margin text-muted small"><strong>@lang('server.config.startup.startup_var'):</strong> <code>{{ $variable->env_variable }}</code></p> <p class="no-margin text-muted small"><strong>@lang('server.config.startup.startup_var'):</strong> <code>{{ $v->variable->env_variable }}</code></p>
<p class="no-margin text-muted small"><strong>@lang('server.config.startup.startup_regex'):</strong> <code>{{ $variable->regex }}</code></p> <p class="no-margin text-muted small"><strong>@lang('server.config.startup.startup_regex'):</strong> <code>{{ $v->variable->rules }}</code></p>
</div> </div>
</div> </div>
</div> </div>
@ -97,14 +96,4 @@
@section('footer-scripts') @section('footer-scripts')
@parent @parent
{!! Theme::js('js/frontend/server.socket.js') !!} {!! Theme::js('js/frontend/server.socket.js') !!}
<script>
$('input[data-action="match-regex"]').on('keyup', function (event) {
if (! $(this).data('regex')) return;
var input = $(this).val();
var regex = new RegExp($(this).data('regex').replace(/^\/|\/$/g, ''));
$(this).parent().parent().removeClass('has-success has-error').addClass((! regex.test(input)) ? 'has-error' : 'has-success');
});
</script>
@endsection @endsection