1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-10-27 04:12:28 +01:00

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);
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) {
Alert::danger($ex->getMessage())->flash();
} catch (TransferException $ex) {

View File

@ -209,37 +209,20 @@ class ServerController extends Controller
public function getStartup(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
$server->load(['allocations' => function ($query) use ($server) {
$query->where('id', $server->allocation_id);
}]);
$server->load(['node', 'allocation', 'variables.variable']);
$this->authorize('view-startup', $server);
$variables = Models\ServiceVariable::select(
'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 = [
$replacements = [
'{{SERVER_MEMORY}}' => $server->memory,
'{{SERVER_IP}}' => $allocation->ip,
'{{SERVER_PORT}}' => $allocation->port,
'{{SERVER_IP}}' => $server->allocation->ip,
'{{SERVER_PORT}}' => $server->allocation->port,
];
$processed = str_replace(array_keys($ServerVariable), array_values($ServerVariable), $server->startup);
foreach ($variables as &$variable) {
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '[hidden]';
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
$processed = str_replace(array_keys($replacements), array_values($replacements), $server->startup);
foreach ($server->variables as $v) {
$replace = ($v->user_can_view) ? $v->variable_value : '[hidden]';
$processed = str_replace('{{' . $v->variable->env_variable . '}}', $replace, $processed);
}
$server->js();
@ -247,8 +230,8 @@ class ServerController extends Controller
return view('server.settings.startup', [
'server' => $server,
'node' => $server->node,
'variables' => $variables->where('user_viewable', 1),
'service' => $service,
'variables' => $server->variables->where('user_can_view', true),
'service' => $server->service,
'processedStartup' => $processed,
]);
}
@ -311,6 +294,8 @@ class ServerController extends Controller
$repo = new ServerRepository;
$repo->updateStartup($server->id, $request->except('_token'));
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) {
Alert::danger($ex->getMessage())->flash();
} 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.
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function byUuid($uuid)
public static function byUuid($uuid, array $with = [], array $withCount = [])
{
if (! Auth::check()) {
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',
];
/**
* 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.
*

View File

@ -51,17 +51,6 @@ class ServiceOption extends Model
'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
* service one if the option does not have one defined.

View File

@ -47,13 +47,29 @@ class ServiceVariable extends Model
*
* @var array
*/
protected $casts = [
'option_id' => 'integer',
'user_viewable' => 'integer',
'user_editable' => 'integer',
'required' => 'integer',
];
protected $casts = [
'option_id' => 'integer',
'user_viewable' => 'integer',
'user_editable' => '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()
{
return $this->hasMany(ServerVariable::class, 'variable_id');

View File

@ -627,23 +627,25 @@ class ServerRepository
foreach ($server->option->variables as &$variable) {
$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
// or an invisible field just silently skip the variable.
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
continue;
}
// Confirm value is valid when compared aganist regex.
// @TODO: switch to Laravel validation rules.
if ($set && ! is_null($variable->regex)) {
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 . ').');
}
// Perform Field Validation
$validator = Validator::make([
'variable_value' => ($set) ? $data['env_' . $variable->id] : null,
], [
'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([

View File

@ -80,7 +80,6 @@ class VariableRepository
$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']));
// Remove field that isn't used.
unset($data['options']);
@ -156,7 +155,6 @@ class VariableRepository
$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']));
// Remove field that isn't used.
unset($data['options']);

View File

@ -24,6 +24,10 @@ class ChangeServiceVariablesValidationRules extends Migration
$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) {
$table->renameColumn('rules', 'regex');
$table->boolean('required')->default(true)->before('regex');
});
DB::transaction(function () {

View File

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

View File

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

View File

@ -62,10 +62,7 @@
</div>
<div class="box-body">
<label for="pStartup" class="form-label">Startup Command</label>
<div class="input-group">
<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>
<input id="pStartup" name="startup" class="form-control" type="text" value="{{ old('startup', $server->startup) }}" />
<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 class="box-footer">
@ -91,7 +88,7 @@
</div>
<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>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>

View File

@ -81,7 +81,6 @@
<select name="options[]" class="pOptions form-control" multiple>
<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="required" {{ (! $variable->required) ?: 'selected' }}>Field Is Required</option>
</select>
</div>
<div class="form-group">
@ -135,7 +134,6 @@
<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">

View File

@ -42,9 +42,8 @@
<h3 class="box-title">@lang('server.config.startup.command')</h3>
</div>
<div class="box-body">
<div class="input-group">
<span class="input-group-addon">{{ $service->executable }}</span>
<input type="text" class="form-control" readonly="readonly" value="{{ $processedStartup }}" />
<div class="form-group">
<input type="text" class="form-control" readonly value="{{ $processedStartup }}" />
</div>
</div>
@can('edit-startup', $server)
@ -56,35 +55,35 @@
</div>
</div>
@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="box">
<div class="box-header with-border">
<h3 class="box-title">{{ $variable->name }}</h3>
<h3 class="box-title">{{ $v->variable->name }}</h3>
</div>
<div class="box-body">
<input data-action="match-regex" data-regex="{{ $variable->regex }}"
@if($variable->user_editable)
name="env_{{ $variable->id }}"
<input
@if($v->user_can_edit)
name="env_{{ $v->variable->id }}"
@else
readonly
@endif
class="form-control" type="text" value="{{ old('env_' . $variable->id, $variable->server_value) }}" />
<p class="small text-muted">{{ $variable->description }}</p>
class="form-control" type="text" value="{{ old('env_' . $v->id, $v->variable_value) }}" />
<p class="small text-muted">{{ $v->variable->description }}</p>
<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>
@elseif(! $variable->required && $variable->user_editable)
@elseif(! $v->required && $v->user_can_edit)
<span class="label label-default">@lang('strings.optional')</span>
@endif
@if(! $variable->user_editable)
@if(! $v->user_can_edit)
<span class="label label-warning">@lang('strings.read_only')</span>
@endif
</p>
</div>
<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_regex'):</strong> <code>{{ $variable->regex }}</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>{{ $v->variable->rules }}</code></p>
</div>
</div>
</div>
@ -97,14 +96,4 @@
@section('footer-scripts')
@parent
{!! 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