2424
2525namespace Pterodactyl \Http \Controllers \Admin ;
2626
27- use Log ;
2827use Alert ;
2928use Javascript ;
3029use Illuminate \Http \Request ;
3130use Pterodactyl \Models \Node ;
3231use Pterodactyl \Models \Allocation ;
3332use Prologue \Alerts \AlertsMessageBag ;
34- use Pterodactyl \Exceptions \DisplayException ;
3533use Pterodactyl \Http \Controllers \Controller ;
36- use Pterodactyl \Repositories \NodeRepository ;
3734use Pterodactyl \Services \Nodes \UpdateService ;
3835use Pterodactyl \Services \Nodes \CreationService ;
3936use Pterodactyl \Services \Nodes \DeletionService ;
4037use Illuminate \Contracts \Translation \Translator ;
4138use 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 ;
4441use Pterodactyl \Contracts \Repository \NodeRepositoryInterface ;
42+ use Pterodactyl \Http \Requests \Admin \Node \AllocationFormRequest ;
4543use Pterodactyl \Contracts \Repository \LocationRepositoryInterface ;
44+ use Pterodactyl \Contracts \Repository \AllocationRepositoryInterface ;
45+ use Pterodactyl \Http \Requests \Admin \Node \AllocationAliasFormRequest ;
4646
4747class 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 /**
0 commit comments