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']);