Skip to content

Commit 65957e7

Browse files
committed
Begin implementation of new request validation, closes pterodactyl#470
1 parent 265b697 commit 65957e7

File tree

5 files changed

+86
-13
lines changed

5 files changed

+86
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Dockerfile
2222
docker-compose.yml
2323
# for image related files
2424
misc
25+
.phpstorm.meta.php

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ disabled:
44
- concat_without_spaces
55
enabled:
66
- concat_with_spaces
7+
- no_unused_imports

app/Http/Controllers/Admin/OptionController.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Pterodactyl\Repositories\OptionRepository;
3636
use Pterodactyl\Repositories\VariableRepository;
3737
use Pterodactyl\Exceptions\DisplayValidationException;
38+
use Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable;
3839

3940
class OptionController extends Controller
4041
{
@@ -198,28 +199,23 @@ public function editConfiguration(Request $request, $id)
198199
/**
199200
* Handles POST when editing a configration for a service option.
200201
*
201-
* @param \Illuminate\Http\Request $request
202-
* @param int $option
203-
* @param int $variable
202+
* @param \Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable $request
203+
* @param int $option
204+
* @param int $variable
204205
* @return \Illuminate\Http\RedirectResponse
205206
*/
206-
public function editVariable(Request $request, $option, $variable)
207+
public function editVariable(StoreOptionVariable $request, $option, $variable)
207208
{
208209
$repo = new VariableRepository;
209210

210211
try {
211212
if ($request->input('action') !== 'delete') {
212-
$variable = $repo->update($variable, $request->intersect([
213-
'name', 'description', 'env_variable',
214-
'default_value', 'options', 'rules',
215-
]));
213+
$variable = $repo->update($variable, $request->normalize());
216214
Alert::success("The service variable '{$variable->name}' has been updated.")->flash();
217215
} else {
218216
$repo->delete($variable);
219217
Alert::success('That service variable has been deleted.')->flash();
220218
}
221-
} catch (DisplayValidationException $ex) {
222-
return redirect()->route('admin.services.option.variables', $option)->withErrors(json_decode($ex->getMessage()));
223219
} catch (DisplayException $ex) {
224220
Alert::danger($ex->getMessage())->flash();
225221
} catch (\Exception $ex) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Http\Requests\Admin\Service;
26+
27+
use Pterodactyl\Models\User;
28+
use Illuminate\Foundation\Http\FormRequest;
29+
30+
class StoreOptionVariable extends FormRequest
31+
{
32+
/**
33+
* Determine if user is allowed to access this request.
34+
*
35+
* @return bool
36+
*/
37+
public function authorize()
38+
{
39+
if (! $this->user() instanceof User) {
40+
return false;
41+
}
42+
43+
return $this->user()->isRootAdmin();
44+
}
45+
46+
/**
47+
* Set the rules to be used for data passed to the request.
48+
*
49+
* @return array
50+
*/
51+
public function rules()
52+
{
53+
return [
54+
'name' => 'required|string|min:1|max:255',
55+
'description' => 'nullable|string',
56+
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
57+
'rules' => 'bail|required|string',
58+
'default_value' => explode('|', $this->input('rules')),
59+
'options' => 'sometimes|required|array',
60+
];
61+
}
62+
63+
/**
64+
* Return only the fields that we are interested in from the request.
65+
* This will include empty fields as a null value.
66+
*
67+
* @return array
68+
*/
69+
public function normalize()
70+
{
71+
return $this->only(
72+
array_keys($this->rules())
73+
);
74+
}
75+
}

resources/lang/en/base.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@
5757
],
5858
'view' => [
5959
'title' => 'View Server',
60-
'desc'=> 'Allows viewing of specific server user can access.',
60+
'desc' => 'Allows viewing of specific server user can access.',
6161
],
6262
'power' => [
6363
'title' => 'Toggle Power',
64-
'desc'=> 'Allow toggling of power status for a server.',
64+
'desc' => 'Allow toggling of power status for a server.',
6565
],
6666
'command' => [
6767
'title' => 'Send Command',
68-
'desc'=> 'Allow sending of a command to a running server.',
68+
'desc' => 'Allow sending of a command to a running server.',
6969
],
7070
],
7171
],

0 commit comments

Comments
 (0)