Skip to content

Commit 669119c

Browse files
committed
Handle allocation assignment using services
Function is significantly quicker and uses 1 SQL query per IP rather than 1 query per port.
1 parent 396b5c2 commit 669119c

File tree

17 files changed

+754
-5915
lines changed

17 files changed

+754
-5915
lines changed

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,12 @@ public function all();
186186
* @return bool
187187
*/
188188
public function insert(array $data);
189+
190+
/**
191+
* Insert multiple records into the database and ignore duplicates.
192+
*
193+
* @param array $values
194+
* @return bool
195+
*/
196+
public function insertIgnore(array $values);
189197
}

app/Http/Controllers/Admin/LocationController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use Pterodactyl\Services\LocationService;
3030
use Pterodactyl\Exceptions\DisplayException;
3131
use Pterodactyl\Http\Controllers\Controller;
32-
use Pterodactyl\Http\Requests\Admin\LocationRequest;
32+
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
3333
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
3434

3535
class LocationController extends Controller
@@ -94,13 +94,13 @@ public function view($id)
9494
/**
9595
* Handle request to create new location.
9696
*
97-
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
97+
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
9898
* @return \Illuminate\Http\RedirectResponse
9999
*
100100
* @throws \Throwable
101101
* @throws \Watson\Validating\ValidationException
102102
*/
103-
public function create(LocationRequest $request)
103+
public function create(LocationFormRequest $request)
104104
{
105105
$location = $this->service->create($request->normalize());
106106
$this->alert->success('Location was created successfully.')->flash();
@@ -111,14 +111,14 @@ public function create(LocationRequest $request)
111111
/**
112112
* Handle request to update or delete location.
113113
*
114-
* @param \Pterodactyl\Http\Requests\Admin\LocationRequest $request
115-
* @param \Pterodactyl\Models\Location $location
114+
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
115+
* @param \Pterodactyl\Models\Location $location
116116
* @return \Illuminate\Http\RedirectResponse
117117
*
118118
* @throws \Throwable
119119
* @throws \Watson\Validating\ValidationException
120120
*/
121-
public function update(LocationRequest $request, Location $location)
121+
public function update(LocationFormRequest $request, Location $location)
122122
{
123123
if ($request->input('action') === 'delete') {
124124
return $this->delete($location);

app/Http/Controllers/Admin/NodesController.php

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Log;
2827
use Alert;
2928
use Javascript;
3029
use Illuminate\Http\Request;
3130
use Pterodactyl\Models\Node;
3231
use Pterodactyl\Models\Allocation;
3332
use Prologue\Alerts\AlertsMessageBag;
34-
use Pterodactyl\Exceptions\DisplayException;
3533
use Pterodactyl\Http\Controllers\Controller;
36-
use Pterodactyl\Repositories\NodeRepository;
3734
use Pterodactyl\Services\Nodes\UpdateService;
3835
use Pterodactyl\Services\Nodes\CreationService;
3936
use Pterodactyl\Services\Nodes\DeletionService;
4037
use Illuminate\Contracts\Translation\Translator;
4138
use Illuminate\Cache\Repository as CacheRepository;
42-
use Pterodactyl\Http\Requests\Admin\NodeFormRequest;
43-
use Pterodactyl\Exceptions\DisplayValidationException;
39+
use Pterodactyl\Services\Allocations\AssignmentService;
40+
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
4441
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
42+
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
4543
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
44+
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
45+
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
4646

4747
class NodesController extends Controller
4848
{
@@ -51,6 +51,16 @@ class NodesController extends Controller
5151
*/
5252
protected $alert;
5353

54+
/**
55+
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
56+
*/
57+
protected $allocationRepository;
58+
59+
/**
60+
* @var \Pterodactyl\Services\Allocations\AssignmentService
61+
*/
62+
protected $assignmentService;
63+
5464
/**
5565
* @var \Illuminate\Cache\Repository
5666
*/
@@ -86,8 +96,24 @@ class NodesController extends Controller
8696
*/
8797
protected $updateService;
8898

99+
/**
100+
* NodesController constructor.
101+
*
102+
* @param \Prologue\Alerts\AlertsMessageBag $alert
103+
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
104+
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
105+
* @param \Illuminate\Cache\Repository $cache
106+
* @param \Pterodactyl\Services\Nodes\CreationService $creationService
107+
* @param \Pterodactyl\Services\Nodes\DeletionService $deletionService
108+
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
109+
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
110+
* @param \Illuminate\Contracts\Translation\Translator $translator
111+
* @param \Pterodactyl\Services\Nodes\UpdateService $updateService
112+
*/
89113
public function __construct(
90114
AlertsMessageBag $alert,
115+
AllocationRepositoryInterface $allocationRepository,
116+
AssignmentService $assignmentService,
91117
CacheRepository $cache,
92118
CreationService $creationService,
93119
DeletionService $deletionService,
@@ -97,6 +123,8 @@ public function __construct(
97123
UpdateService $updateService
98124
) {
99125
$this->alert = $alert;
126+
$this->allocationRepository = $allocationRepository;
127+
$this->assignmentService = $assignmentService;
100128
$this->cache = $cache;
101129
$this->creationService = $creationService;
102130
$this->deletionService = $deletionService;
@@ -139,7 +167,7 @@ public function create()
139167
/**
140168
* Post controller to create a new node on the system.
141169
*
142-
* @param \Pterodactyl\Http\Requests\Admin\NodeFormRequest $request
170+
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
143171
* @return \Illuminate\Http\RedirectResponse
144172
*
145173
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
@@ -224,8 +252,8 @@ public function viewServers($node)
224252
/**
225253
* Updates settings for a node.
226254
*
227-
* @param \Pterodactyl\Http\Requests\Admin\NodeFormRequest $request
228-
* @param \Pterodactyl\Models\Node $node
255+
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
256+
* @param \Pterodactyl\Models\Node $node
229257
* @return \Illuminate\Http\RedirectResponse
230258
*
231259
* @throws \Pterodactyl\Exceptions\DisplayException
@@ -242,12 +270,11 @@ public function updateSettings(NodeFormRequest $request, Node $node)
242270
/**
243271
* Removes a single allocation from a node.
244272
*
245-
* @param \Illuminate\Http\Request $request
246-
* @param int $node
247-
* @param int $allocation
273+
* @param int $node
274+
* @param int $allocation
248275
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
249276
*/
250-
public function allocationRemoveSingle(Request $request, $node, $allocation)
277+
public function allocationRemoveSingle($node, $allocation)
251278
{
252279
$query = Allocation::where('node_id', $node)->whereNull('server_id')->where('id', $allocation)->delete();
253280
if ($query < 1) {
@@ -284,55 +311,35 @@ public function allocationRemoveBlock(Request $request, $node)
284311
/**
285312
* Sets an alias for a specific allocation on a node.
286313
*
287-
* @param \Illuminate\Http\Request $request
288-
* @param int $node
289-
* @return \Illuminate\Http\Response
314+
* @param \Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest $request
315+
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
316+
*
317+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
290318
*/
291-
public function allocationSetAlias(Request $request, $node)
319+
public function allocationSetAlias(AllocationAliasFormRequest $request)
292320
{
293-
if (! $request->input('allocation_id')) {
294-
return response('Missing required parameters.', 422);
295-
}
296-
297-
try {
298-
$update = Allocation::findOrFail($request->input('allocation_id'));
299-
$update->ip_alias = (empty($request->input('alias'))) ? null : $request->input('alias');
300-
$update->save();
321+
$this->allocationRepository->update($request->input('allocation_id'), [
322+
'ip_alias' => (empty($request->input('alias'))) ? null : $request->input('alias'),
323+
]);
301324

302-
return response('', 204);
303-
} catch (\Exception $ex) {
304-
throw $ex;
305-
}
325+
return response('', 204);
306326
}
307327

308328
/**
309329
* Creates new allocations on a node.
310330
*
311-
* @param \Illuminate\Http\Request $request
312-
* @param int $node
331+
* @param \Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest $request
332+
* @param int|\Pterodactyl\Models\Node $node
313333
* @return \Illuminate\Http\RedirectResponse
334+
*
335+
* @throws \Pterodactyl\Exceptions\DisplayException
314336
*/
315-
public function createAllocation(Request $request, $node)
337+
public function createAllocation(AllocationFormRequest $request, Node $node)
316338
{
317-
$repo = new NodeRepository;
318-
319-
try {
320-
$repo->addAllocations($node, $request->intersect(['allocation_ip', 'allocation_alias', 'allocation_ports']));
321-
Alert::success('Successfully added new allocations!')->flash();
322-
} catch (DisplayValidationException $ex) {
323-
return redirect()
324-
->route('admin.nodes.view.allocation', $node)
325-
->withErrors(json_decode($ex->getMessage()))
326-
->withInput();
327-
} catch (DisplayException $ex) {
328-
Alert::danger($ex->getMessage())->flash();
329-
} catch (\Exception $ex) {
330-
Log::error($ex);
331-
Alert::danger('An unhandled exception occured while attempting to add allocations this node. This error has been logged.')
332-
->flash();
333-
}
339+
$this->assignmentService->handle($node, $request->normalize());
340+
$this->alert->success($this->translator->trans('admin/node.notices.allocations_added'))->flash();
334341

335-
return redirect()->route('admin.nodes.view.allocation', $node);
342+
return redirect()->route('admin.nodes.view.allocation', $node->id);
336343
}
337344

338345
/**

app/Http/Requests/Admin/LocationRequest.php renamed to app/Http/Requests/Admin/LocationFormRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
use Pterodactyl\Models\Location;
2828

29-
class LocationRequest extends AdminFormRequest
29+
class LocationFormRequest extends AdminFormRequest
3030
{
3131
/**
3232
* Setup the validation rules to use for these requests.
@@ -36,7 +36,7 @@ class LocationRequest extends AdminFormRequest
3636
public function rules()
3737
{
3838
if ($this->method() === 'PATCH') {
39-
return Location::getUpdateRulesForId($this->location->id);
39+
return Location::getUpdateRulesForId($this->route()->parameter('location')->id);
4040
}
4141

4242
return Location::getCreateRules();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Node;
26+
27+
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
28+
29+
class AllocationAliasFormRequest extends AdminFormRequest
30+
{
31+
/**
32+
* @return array
33+
*/
34+
public function rules()
35+
{
36+
return [
37+
'alias' => 'required|nullable|string',
38+
'allocation_id' => 'required|numeric|exists:allocations,id',
39+
];
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Node;
26+
27+
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
28+
29+
class AllocationFormRequest extends AdminFormRequest
30+
{
31+
/**
32+
* @return array
33+
*/
34+
public function rules()
35+
{
36+
return [
37+
'allocation_ip' => 'required|string',
38+
'allocation_alias' => 'sometimes|string|max:255',
39+
'allocation_ports' => 'required|array',
40+
];
41+
}
42+
}

app/Http/Requests/Admin/NodeFormRequest.php renamed to app/Http/Requests/Admin/Node/NodeFormRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
* SOFTWARE.
2323
*/
2424

25-
namespace Pterodactyl\Http\Requests\Admin;
25+
namespace Pterodactyl\Http\Requests\Admin\Node;
2626

2727
use Pterodactyl\Models\Node;
28+
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
2829

2930
class NodeFormRequest extends AdminFormRequest
3031
{

app/Http/Requests/Admin/UserFormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UserFormRequest extends AdminFormRequest
3434
public function rules()
3535
{
3636
if ($this->method() === 'PATCH') {
37-
return User::getUpdateRulesForId($this->user->id);
37+
return User::getUpdateRulesForId($this->route()->parameter('user')->id);
3838
}
3939

4040
return User::getCreateRules();

0 commit comments

Comments
 (0)