Skip to content

Commit 11c4f3f

Browse files
committed
Finish putting permissions on the API
1 parent d644a53 commit 11c4f3f

File tree

14 files changed

+434
-82
lines changed

14 files changed

+434
-82
lines changed

app/Http/Controllers/API/Admin/Nodes/AllocationController.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
namespace Pterodactyl\Http\Controllers\API\Admin\Nodes;
44

55
use Spatie\Fractal\Fractal;
6-
use Illuminate\Http\Request;
6+
use Pterodactyl\Models\Node;
77
use Illuminate\Http\Response;
88
use Pterodactyl\Models\Allocation;
99
use Pterodactyl\Http\Controllers\Controller;
1010
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
1111
use Pterodactyl\Transformers\Api\Admin\AllocationTransformer;
1212
use Pterodactyl\Services\Allocations\AllocationDeletionService;
1313
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
14+
use Pterodactyl\Http\Requests\API\Admin\Allocations\GetAllocationsRequest;
15+
use Pterodactyl\Http\Requests\API\Admin\Allocations\DeleteAllocationRequest;
1416

1517
class AllocationController extends Controller
1618
{
@@ -46,16 +48,16 @@ public function __construct(AllocationDeletionService $deletionService, Allocati
4648
/**
4749
* Return all of the allocations that exist for a given node.
4850
*
49-
* @param \Illuminate\Http\Request $request
50-
* @param int $node
51+
* @param \Pterodactyl\Http\Requests\API\Admin\Allocations\GetAllocationsRequest $request
52+
* @param \Pterodactyl\Models\Node $node
5153
* @return array
5254
*/
53-
public function index(Request $request, int $node): array
55+
public function index(GetAllocationsRequest $request, Node $node): array
5456
{
55-
$allocations = $this->repository->getPaginatedAllocationsForNode($node, 100);
57+
$allocations = $this->repository->getPaginatedAllocationsForNode($node->id, 100);
5658

5759
return $this->fractal->collection($allocations)
58-
->transformWith(new AllocationTransformer($request))
60+
->transformWith((new AllocationTransformer)->setKey($request->key()))
5961
->withResourceName('allocation')
6062
->paginateWith(new IlluminatePaginatorAdapter($allocations))
6163
->toArray();
@@ -64,14 +66,14 @@ public function index(Request $request, int $node): array
6466
/**
6567
* Delete a specific allocation from the Panel.
6668
*
67-
* @param \Illuminate\Http\Request $request
68-
* @param int $node
69-
* @param \Pterodactyl\Models\Allocation $allocation
69+
* @param \Pterodactyl\Http\Requests\API\Admin\Allocations\DeleteAllocationRequest $request
70+
* @param \Pterodactyl\Models\Node $node
71+
* @param \Pterodactyl\Models\Allocation $allocation
7072
* @return \Illuminate\Http\Response
7173
*
7274
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
7375
*/
74-
public function delete(Request $request, int $node, Allocation $allocation): Response
76+
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): Response
7577
{
7678
$this->deletionService->handle($allocation);
7779

app/Http/Controllers/API/Admin/Nodes/NodeController.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Pterodactyl\Http\Controllers\API\Admin\Nodes;
44

55
use Spatie\Fractal\Fractal;
6-
use Illuminate\Http\Request;
76
use Pterodactyl\Models\Node;
87
use Illuminate\Http\Response;
98
use Illuminate\Http\JsonResponse;
@@ -13,8 +12,12 @@
1312
use Pterodactyl\Services\Nodes\NodeDeletionService;
1413
use Pterodactyl\Transformers\Api\Admin\NodeTransformer;
1514
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
16-
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
1715
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
16+
use Pterodactyl\Http\Requests\API\Admin\Nodes\GetNodeRequest;
17+
use Pterodactyl\Http\Requests\API\Admin\Nodes\GetNodesRequest;
18+
use Pterodactyl\Http\Requests\API\Admin\Nodes\StoreNodeRequest;
19+
use Pterodactyl\Http\Requests\API\Admin\Nodes\DeleteNodeRequest;
20+
use Pterodactyl\Http\Requests\API\Admin\Nodes\UpdateNodeRequest;
1821

1922
class NodeController extends Controller
2023
{
@@ -69,52 +72,50 @@ public function __construct(
6972
/**
7073
* Return all of the nodes currently available on the Panel.
7174
*
72-
* @param \Illuminate\Http\Request $request
75+
* @param \Pterodactyl\Http\Requests\API\Admin\Nodes\GetNodesRequest $request
7376
* @return array
7477
*/
75-
public function index(Request $request): array
78+
public function index(GetNodesRequest $request): array
7679
{
7780
$nodes = $this->repository->paginated(100);
7881

79-
$fractal = $this->fractal->collection($nodes)
80-
->transformWith(new NodeTransformer($request))
82+
return $this->fractal->collection($nodes)
83+
->transformWith((new NodeTransformer)->setKey($request->key()))
8184
->withResourceName('node')
82-
->paginateWith(new IlluminatePaginatorAdapter($nodes));
83-
84-
return $fractal->toArray();
85+
->paginateWith(new IlluminatePaginatorAdapter($nodes))
86+
->toArray();
8587
}
8688

8789
/**
8890
* Return data for a single instance of a node.
8991
*
90-
* @param \Illuminate\Http\Request $request
91-
* @param \Pterodactyl\Models\Node $node
92+
* @param \Pterodactyl\Http\Requests\API\Admin\Nodes\GetNodeRequest $request
93+
* @param \Pterodactyl\Models\Node $node
9294
* @return array
9395
*/
94-
public function view(Request $request, Node $node): array
96+
public function view(GetNodeRequest $request, Node $node): array
9597
{
96-
$fractal = $this->fractal->item($node)
97-
->transformWith(new NodeTransformer($request))
98-
->withResourceName('node');
99-
100-
return $fractal->toArray();
98+
return $this->fractal->item($node)
99+
->transformWith((new NodeTransformer)->setKey($request->key()))
100+
->withResourceName('node')
101+
->toArray();
101102
}
102103

103104
/**
104105
* Create a new node on the Panel. Returns the created node and a HTTP/201
105106
* status response on success.
106107
*
107-
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
108+
* @param \Pterodactyl\Http\Requests\API\Admin\Nodes\StoreNodeRequest $request
108109
* @return \Illuminate\Http\JsonResponse
109110
*
110111
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
111112
*/
112-
public function store(NodeFormRequest $request): JsonResponse
113+
public function store(StoreNodeRequest $request): JsonResponse
113114
{
114-
$node = $this->creationService->handle($request->normalize());
115+
$node = $this->creationService->handle($request->validated());
115116

116117
return $this->fractal->item($node)
117-
->transformWith(new NodeTransformer($request))
118+
->transformWith((new NodeTransformer)->setKey($request->key()))
118119
->withResourceName('node')
119120
->addMeta([
120121
'link' => route('api.admin.node.view', ['node' => $node->id]),
@@ -125,20 +126,20 @@ public function store(NodeFormRequest $request): JsonResponse
125126
/**
126127
* Update an existing node on the Panel.
127128
*
128-
* @param \Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest $request
129-
* @param \Pterodactyl\Models\Node $node
129+
* @param \Pterodactyl\Http\Requests\API\Admin\Nodes\UpdateNodeRequest $request
130+
* @param \Pterodactyl\Models\Node $node
130131
* @return array
131132
*
132133
* @throws \Pterodactyl\Exceptions\DisplayException
133134
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
134135
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
135136
*/
136-
public function update(NodeFormRequest $request, Node $node): array
137+
public function update(UpdateNodeRequest $request, Node $node): array
137138
{
138-
$node = $this->updateService->returnUpdatedModel()->handle($node, $request->normalize());
139+
$node = $this->updateService->returnUpdatedModel()->handle($node, $request->validated());
139140

140141
return $this->fractal->item($node)
141-
->transformWith(new NodeTransformer($request))
142+
->transformWith((new NodeTransformer)->setKey($request->key()))
142143
->withResourceName('node')
143144
->toArray();
144145
}
@@ -147,15 +148,16 @@ public function update(NodeFormRequest $request, Node $node): array
147148
* Deletes a given node from the Panel as long as there are no servers
148149
* currently attached to it.
149150
*
150-
* @param \Pterodactyl\Models\Node $node
151+
* @param \Pterodactyl\Http\Requests\API\Admin\Nodes\DeleteNodeRequest $request
152+
* @param \Pterodactyl\Models\Node $node
151153
* @return \Illuminate\Http\Response
152154
*
153155
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
154156
*/
155-
public function delete(Node $node): Response
157+
public function delete(DeleteNodeRequest $request, Node $node): Response
156158
{
157159
$this->deletionService->handle($node);
158160

159-
return response('', 201);
161+
return response('', 204);
160162
}
161163
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\API\Admin\Allocations;
4+
5+
use Pterodactyl\Models\Node;
6+
use Pterodactyl\Models\Allocation;
7+
use Pterodactyl\Services\Acl\Api\AdminAcl;
8+
use Pterodactyl\Http\Requests\API\Admin\ApiAdminRequest;
9+
10+
class DeleteAllocationRequest extends ApiAdminRequest
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected $resource = AdminAcl::RESOURCE_ALLOCATIONS;
16+
17+
/**
18+
* @var int
19+
*/
20+
protected $permission = AdminAcl::WRITE;
21+
22+
/**
23+
* Determine if the requested allocation exists and belongs to the node that
24+
* is being passed in the URL.
25+
*
26+
* @return bool
27+
*/
28+
public function resourceExists(): bool
29+
{
30+
$node = $this->route()->parameter('node');
31+
$allocation = $this->route()->parameter('allocation');
32+
33+
if ($node instanceof Node && $node->exists) {
34+
if ($allocation instanceof Allocation && $allocation->exists && $allocation->node_id === $node->id) {
35+
return true;
36+
}
37+
}
38+
39+
return false;
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\API\Admin\Allocations;
4+
5+
use Pterodactyl\Models\Node;
6+
use Pterodactyl\Services\Acl\Api\AdminAcl;
7+
use Pterodactyl\Http\Requests\API\Admin\ApiAdminRequest;
8+
9+
class GetAllocationsRequest extends ApiAdminRequest
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $resource = AdminAcl::RESOURCE_ALLOCATIONS;
15+
16+
/**
17+
* @var int
18+
*/
19+
protected $permission = AdminAcl::READ;
20+
21+
/**
22+
* Determine if the node that we are requesting the allocations
23+
* for exists on the Panel.
24+
*
25+
* @return bool
26+
*/
27+
public function resourceExists(): bool
28+
{
29+
$node = $this->route()->parameter('node');
30+
31+
return $node instanceof Node && $node->exists;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\API\Admin\Nodes;
4+
5+
use Pterodactyl\Models\Node;
6+
use Pterodactyl\Services\Acl\Api\AdminAcl;
7+
use Pterodactyl\Http\Requests\API\Admin\ApiAdminRequest;
8+
9+
class DeleteNodeRequest extends ApiAdminRequest
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $resource = AdminAcl::RESOURCE_NODES;
15+
16+
/**
17+
* @var int
18+
*/
19+
protected $permission = AdminAcl::WRITE;
20+
21+
/**
22+
* Determine if the node being requested for editing exists
23+
* on the Panel before validating the data.
24+
*
25+
* @return bool
26+
*/
27+
public function resourceExists(): bool
28+
{
29+
$node = $this->route()->parameter('node');
30+
31+
return $node instanceof Node && $node->exists;
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\API\Admin\Nodes;
4+
5+
use Pterodactyl\Models\Node;
6+
use Pterodactyl\Http\Requests\API\Admin\ApiAdminRequest;
7+
8+
class GetNodeRequest extends ApiAdminRequest
9+
{
10+
/**
11+
* Determine if the requested node exists on the Panel.
12+
*
13+
* @return bool
14+
*/
15+
public function resourceExists(): bool
16+
{
17+
$node = $this->route()->parameter('node');
18+
19+
return $node instanceof Node && $node->exists;
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\API\Admin\Nodes;
4+
5+
use Pterodactyl\Services\Acl\Api\AdminAcl;
6+
use Pterodactyl\Http\Requests\API\Admin\ApiAdminRequest;
7+
8+
class GetNodesRequest extends ApiAdminRequest
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $resource = AdminAcl::RESOURCE_NODES;
14+
15+
/**
16+
* @var int
17+
*/
18+
protected $permission = AdminAcl::READ;
19+
}

0 commit comments

Comments
 (0)