Skip to content

Commit fcadee7

Browse files
committed
Fix front and backend views with new service variable setups
1 parent 66e94dd commit fcadee7

File tree

14 files changed

+104
-93
lines changed

14 files changed

+104
-93
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ public function saveStartup(Request $request, $id)
493493
$repo->updateStartup($id, $request->except('_token'), true);
494494

495495
Alert::success('Startup variables were successfully modified and assigned for this server.')->flash();
496+
} catch (DisplayValidationException $ex) {
497+
return redirect()->route('admin.servers.view.startup', $id)->withErrors(json_decode($ex->getMessage()));
496498
} catch (DisplayException $ex) {
497499
Alert::danger($ex->getMessage())->flash();
498500
} catch (TransferException $ex) {

app/Http/Controllers/Server/ServerController.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,46 +209,29 @@ public function getAllocation(Request $request, $uuid)
209209
public function getStartup(Request $request, $uuid)
210210
{
211211
$server = Models\Server::byUuid($uuid);
212-
$server->load(['allocations' => function ($query) use ($server) {
213-
$query->where('id', $server->allocation_id);
214-
}]);
212+
$server->load(['node', 'allocation', 'variables.variable']);
213+
215214
$this->authorize('view-startup', $server);
216215

217-
$variables = Models\ServiceVariable::select(
218-
'service_variables.*',
219-
DB::raw('COALESCE(server_variables.variable_value, service_variables.default_value) as a_serverValue')
220-
)->leftJoin('server_variables', 'server_variables.variable_id', '=', 'service_variables.id')
221-
->where('service_variables.option_id', $server->option_id)
222-
->where('server_variables.server_id', $server->id)
223-
->get();
224-
225-
$service = Models\Service::select(
226-
DB::raw('IFNULL(service_options.executable, services.executable) as executable')
227-
)->leftJoin('service_options', 'service_options.service_id', '=', 'services.id')
228-
->where('service_options.id', $server->option_id)
229-
->where('services.id', $server->service_id)
230-
->first();
231-
232-
$allocation = $server->allocations->pop();
233-
$ServerVariable = [
216+
$replacements = [
234217
'{{SERVER_MEMORY}}' => $server->memory,
235-
'{{SERVER_IP}}' => $allocation->ip,
236-
'{{SERVER_PORT}}' => $allocation->port,
218+
'{{SERVER_IP}}' => $server->allocation->ip,
219+
'{{SERVER_PORT}}' => $server->allocation->port,
237220
];
238221

239-
$processed = str_replace(array_keys($ServerVariable), array_values($ServerVariable), $server->startup);
240-
foreach ($variables as &$variable) {
241-
$replace = ($variable->user_viewable === 1) ? $variable->a_serverValue : '[hidden]';
242-
$processed = str_replace('{{' . $variable->env_variable . '}}', $replace, $processed);
222+
$processed = str_replace(array_keys($replacements), array_values($replacements), $server->startup);
223+
foreach ($server->variables as $v) {
224+
$replace = ($v->user_can_view) ? $v->variable_value : '[hidden]';
225+
$processed = str_replace('{{' . $v->variable->env_variable . '}}', $replace, $processed);
243226
}
244227

245228
$server->js();
246229

247230
return view('server.settings.startup', [
248231
'server' => $server,
249232
'node' => $server->node,
250-
'variables' => $variables->where('user_viewable', 1),
251-
'service' => $service,
233+
'variables' => $server->variables->where('user_can_view', true),
234+
'service' => $server->service,
252235
'processedStartup' => $processed,
253236
]);
254237
}
@@ -311,6 +294,8 @@ public function postSettingsStartup(Request $request, $uuid)
311294
$repo = new ServerRepository;
312295
$repo->updateStartup($server->id, $request->except('_token'));
313296
Alert::success('Server startup variables were successfully updated.')->flash();
297+
} catch (DisplayValidationException $ex) {
298+
return redirect()->route('server.settings.startup', $uuid)->withErrors(json_decode($ex->getMessage()));
314299
} catch (DisplayException $ex) {
315300
Alert::danger($ex->getMessage())->flash();
316301
} catch (\Exception $ex) {

app/Models/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Server extends Model
111111
* @param string $uuid The Short-UUID of the server to return an object about.
112112
* @return \Illuminate\Database\Eloquent\Collection
113113
*/
114-
public static function byUuid($uuid)
114+
public static function byUuid($uuid, array $with = [], array $withCount = [])
115115
{
116116
if (! Auth::check()) {
117117
throw new \Exception('You must call Server:byUuid as an authenticated user.');

app/Models/ServerVariable.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ class ServerVariable extends Model
5252
'variable_id' => 'integer',
5353
];
5454

55+
/**
56+
* Determine if variable is viewable by users.
57+
*
58+
* @return bool
59+
*/
60+
public function getUserCanViewAttribute()
61+
{
62+
return (bool) $this->variable->user_viewable;
63+
}
64+
65+
/**
66+
* Determine if variable is editable by users.
67+
*
68+
* @return bool
69+
*/
70+
public function getUserCanEditAttribute()
71+
{
72+
return (bool) $this->variable->user_editable;
73+
}
74+
75+
/**
76+
* Determine if variable is required.
77+
*
78+
* @return bool
79+
*/
80+
public function getRequiredAttribute()
81+
{
82+
return $this->variable->required;
83+
}
84+
5585
/**
5686
* Returns information about a given variables parent.
5787
*

app/Models/ServiceOption.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ class ServiceOption extends Model
5151
'service_id' => 'integer',
5252
];
5353

54-
/**
55-
* Returns the display executable for the option and will use the parent
56-
* service one if the option does not have one defined.
57-
*
58-
* @return string
59-
*/
60-
public function getDisplayExecutableAttribute($value)
61-
{
62-
return (is_null($this->executable)) ? $this->service->executable : $this->executable;
63-
}
64-
6554
/**
6655
* Returns the display startup string for the option and will use the parent
6756
* service one if the option does not have one defined.

app/Models/ServiceVariable.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,29 @@ class ServiceVariable extends Model
4747
*
4848
* @var array
4949
*/
50-
protected $casts = [
51-
'option_id' => 'integer',
52-
'user_viewable' => 'integer',
53-
'user_editable' => 'integer',
54-
'required' => 'integer',
55-
];
50+
protected $casts = [
51+
'option_id' => 'integer',
52+
'user_viewable' => 'integer',
53+
'user_editable' => 'integer',
54+
'required' => 'integer',
55+
];
5656

57+
/**
58+
* Returns the display executable for the option and will use the parent
59+
* service one if the option does not have one defined.
60+
*
61+
* @return string
62+
*/
63+
public function getRequiredAttribute($value)
64+
{
65+
return ($this->rules === 'required' || str_contains($this->rules, ['required|', '|required']));
66+
}
67+
68+
/**
69+
* Return server variables associated with this variable.
70+
*
71+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
72+
*/
5773
public function serverVariable()
5874
{
5975
return $this->hasMany(ServerVariable::class, 'variable_id');

app/Repositories/ServerRepository.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -627,23 +627,25 @@ public function updateStartup($id, array $data, $admin = false)
627627
foreach ($server->option->variables as &$variable) {
628628
$set = isset($data['env_' . $variable->id]);
629629

630-
// Variable is required but was not passed into the function.
631-
if ($variable->required && ! $set) {
632-
throw new DisplayException('A required variable (' . $variable->env_variable . ') was not passed in the request.');
633-
}
634-
635630
// If user is not an admin and are trying to edit a non-editable field
636631
// or an invisible field just silently skip the variable.
637632
if (! $admin && (! $variable->user_editable || ! $variable->user_viewable)) {
638633
continue;
639634
}
640635

641-
// Confirm value is valid when compared aganist regex.
642-
// @TODO: switch to Laravel validation rules.
643-
if ($set && ! is_null($variable->regex)) {
644-
if (! preg_match($variable->regex, $data['env_' . $variable->id])) {
645-
throw new DisplayException('The value passed for a variable (' . $variable->env_variable . ') could not be matched aganist the regex for that field (' . $variable->regex . ').');
646-
}
636+
// Perform Field Validation
637+
$validator = Validator::make([
638+
'variable_value' => ($set) ? $data['env_' . $variable->id] : null,
639+
], [
640+
'variable_value' => $variable->rules,
641+
]);
642+
643+
if ($validator->fails()) {
644+
throw new DisplayValidationException(json_encode(
645+
collect([
646+
'notice' => ['There was a validation error with the `' . $variable->name . '` variable.']
647+
])->merge($validator->errors()->toArray())
648+
));
647649
}
648650

649651
$svar = Models\ServerVariable::firstOrNew([

app/Repositories/VariableRepository.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public function create($option, array $data)
8080
$data['option_id'] = $option->id;
8181
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
8282
$data['user_editable'] = (in_array('user_editable', $data['options']));
83-
$data['required'] = (in_array('required', $data['options']));
8483

8584
// Remove field that isn't used.
8685
unset($data['options']);
@@ -156,7 +155,6 @@ public function update($id, array $data)
156155

157156
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
158157
$data['user_editable'] = (in_array('user_editable', $data['options']));
159-
$data['required'] = (in_array('required', $data['options']));
160158

161159
// Remove field that isn't used.
162160
unset($data['options']);

database/migrations/2017_03_11_215455_ChangeServiceVariablesValidationRules.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function up()
2424
$variable->save();
2525
}
2626
});
27+
28+
Schema::table('service_variables', function (Blueprint $table) {
29+
$table->dropColumn('required');
30+
});
2731
}
2832

2933
/**
@@ -35,6 +39,7 @@ public function down()
3539
{
3640
Schema::table('service_variables', function (Blueprint $table) {
3741
$table->renameColumn('rules', 'regex');
42+
$table->boolean('required')->default(true)->before('regex');
3843
});
3944

4045
DB::transaction(function () {

database/seeds/MinecraftServiceTableSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private function addSpigotVariables()
246246
'user_viewable' => 0,
247247
'user_editable' => 0,
248248
'required' => 0,
249-
'rules' => 'required|string',
249+
'rules' => 'string',
250250
]);
251251
}
252252

0 commit comments

Comments
 (0)