Skip to content

Commit 508ff8c

Browse files
committed
Finish front-end server modification changes.
Everything is back to the point that it was before this massive code overhaul began. FInal steps before merging this into develop will be some unit tests.
1 parent 5fb4b2c commit 508ff8c

File tree

13 files changed

+315
-198
lines changed

13 files changed

+315
-198
lines changed

app/Contracts/Repository/EggVariableRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
namespace Pterodactyl\Contracts\Repository;
1111

12+
use Illuminate\Support\Collection;
13+
1214
interface EggVariableRepositoryInterface extends RepositoryInterface
1315
{
16+
/**
17+
* Return editable variables for a given egg. Editable variables must be set to
18+
* user viewable in order to be picked up by this function.
19+
*
20+
* @param int $egg
21+
* @return \Illuminate\Support\Collection
22+
*/
23+
public function getEditableVariables(int $egg): Collection;
1424
}

app/Contracts/Repository/ServerRepositoryInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Pterodactyl\Contracts\Repository;
1111

12+
use Pterodactyl\Models\Server;
1213
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
1314

1415
interface ServerRepositoryInterface extends RepositoryInterface, SearchableInterface
@@ -52,6 +53,19 @@ public function findWithVariables($id);
5253
*/
5354
public function getVariablesWithValues($id, $returnAsObject = false);
5455

56+
/**
57+
* Get the primary allocation for a given server. If a model is passed into
58+
* the function, load the allocation relationship onto it. Otherwise, find and
59+
* return the server from the database.
60+
*
61+
* @param int|\Pterodactyl\Models\Server $server
62+
* @param bool $refresh
63+
* @return \Pterodactyl\Models\Server
64+
*
65+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
66+
*/
67+
public function getPrimaryAllocation($server, bool $refresh = false): Server;
68+
5569
/**
5670
* Return enough data to be used for the creation of a server via the daemon.
5771
*

app/Http/Controllers/Server/ServerController.php

Lines changed: 0 additions & 163 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Server\Settings;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\RedirectResponse;
7+
use Prologue\Alerts\AlertsMessageBag;
8+
use Pterodactyl\Http\Controllers\Controller;
9+
use Pterodactyl\Traits\Controllers\JavascriptInjection;
10+
use Pterodactyl\Services\Servers\StartupCommandViewService;
11+
use Pterodactyl\Services\Servers\StartupModificationService;
12+
use Pterodactyl\Http\Requests\Server\UpdateStartupParametersFormRequest;
13+
14+
class StartupController extends Controller
15+
{
16+
use JavascriptInjection;
17+
18+
/**
19+
* @var \Prologue\Alerts\AlertsMessageBag
20+
*/
21+
private $alert;
22+
23+
/**
24+
* @var \Pterodactyl\Services\Servers\StartupCommandViewService
25+
*/
26+
private $commandViewService;
27+
28+
/**
29+
* @var \Pterodactyl\Services\Servers\StartupModificationService
30+
*/
31+
private $modificationService;
32+
33+
/**
34+
* StartupController constructor.
35+
*
36+
* @param \Prologue\Alerts\AlertsMessageBag $alert
37+
* @param \Pterodactyl\Services\Servers\StartupCommandViewService $commandViewService
38+
* @param \Pterodactyl\Services\Servers\StartupModificationService $modificationService
39+
*/
40+
public function __construct(
41+
AlertsMessageBag $alert,
42+
StartupCommandViewService $commandViewService,
43+
StartupModificationService $modificationService
44+
) {
45+
$this->alert = $alert;
46+
$this->commandViewService = $commandViewService;
47+
$this->modificationService = $modificationService;
48+
}
49+
50+
/**
51+
* Render the server startup page.
52+
*
53+
* @param \Illuminate\Http\Request $request
54+
* @return \Illuminate\View\View
55+
*
56+
* @throws \Illuminate\Auth\Access\AuthorizationException
57+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
58+
*/
59+
public function index(Request $request)
60+
{
61+
$server = $request->attributes->get('server');
62+
$this->authorize('view-startup', $server);
63+
$this->injectJavascript();
64+
65+
$data = $this->commandViewService->handle($server->id);
66+
67+
return view('server.settings.startup', [
68+
'variables' => $data->get('variables'),
69+
'server_values' => $data->get('server_values'),
70+
'startup' => $data->get('startup'),
71+
]);
72+
}
73+
74+
/**
75+
* Handle request to update the startup variables for a server. Authorization
76+
* is handled in the form request.
77+
*
78+
* @param \Pterodactyl\Http\Requests\Server\UpdateStartupParametersFormRequest $request
79+
* @return \Illuminate\Http\RedirectResponse
80+
*
81+
* @throws \Pterodactyl\Exceptions\DisplayException
82+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
83+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
84+
*/
85+
public function update(UpdateStartupParametersFormRequest $request): RedirectResponse
86+
{
87+
$this->modificationService->handle($request->attributes->get('server'), $request->normalize());
88+
$this->alert->success(trans('server.config.startup.edited'))->flash();
89+
90+
return redirect()->route('server.settings.startup', ['server' => $request->attributes->get('server')->uuidShort]);
91+
}
92+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Server;
4+
5+
use Pterodactyl\Http\Requests\FrontendUserFormRequest;
6+
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
7+
8+
class UpdateStartupParametersFormRequest extends FrontendUserFormRequest
9+
{
10+
/**
11+
* @var array
12+
*/
13+
private $validationAttributes = [];
14+
15+
/**
16+
* Determine if the user has permission to update the startup parameters
17+
* for this server.
18+
*
19+
* @return bool
20+
*/
21+
public function authorize()
22+
{
23+
if (! parent::authorize()) {
24+
return false;
25+
}
26+
27+
return $this->user()->can('edit-startup', $this->attributes->get('server'));
28+
}
29+
30+
/**
31+
* Validate that all of the required fields were passed and that the environment
32+
* variable values meet the defined criteria for those fields.
33+
*
34+
* @return array
35+
*/
36+
public function rules()
37+
{
38+
$repository = $this->container->make(EggVariableRepositoryInterface::class);
39+
40+
$variables = $repository->getEditableVariables($this->attributes->get('server')->egg_id);
41+
$rules = $variables->mapWithKeys(function ($variable) {
42+
$this->validationAttributes['environment.' . $variable->env_variable] = $variable->name;
43+
44+
return ['environment.' . $variable->env_variable => $variable->rules];
45+
})->toArray();
46+
47+
return array_merge($rules, [
48+
'environment' => 'required|array',
49+
]);
50+
}
51+
52+
/**
53+
* Return attributes to provide better naming conventions for error messages.
54+
*
55+
* @return array
56+
*/
57+
public function attributes()
58+
{
59+
return $this->validationAttributes;
60+
}
61+
}

app/Repositories/Eloquent/EggVariableRepository.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Pterodactyl\Repositories\Eloquent;
1111

12+
use Illuminate\Support\Collection;
1213
use Pterodactyl\Models\EggVariable;
1314
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
1415

@@ -21,4 +22,20 @@ public function model()
2122
{
2223
return EggVariable::class;
2324
}
25+
26+
/**
27+
* Return editable variables for a given egg. Editable variables must be set to
28+
* user viewable in order to be picked up by this function.
29+
*
30+
* @param int $egg
31+
* @return \Illuminate\Support\Collection
32+
*/
33+
public function getEditableVariables(int $egg): Collection
34+
{
35+
return $this->getBuilder()->where([
36+
['egg_id', '=', $egg],
37+
['user_viewable', '=', 1],
38+
['user_editable', '=', 1],
39+
])->get($this->getColumns());
40+
}
2441
}

0 commit comments

Comments
 (0)