Skip to content

Commit 0757d88

Browse files
committed
Add base code to support retrieving allocations as a client
1 parent d59c38e commit 0757d88

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
7+
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
8+
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
9+
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
10+
11+
class NetworkController extends ClientApiController
12+
{
13+
/**
14+
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
15+
*/
16+
private $repository;
17+
18+
/**
19+
* NetworkController constructor.
20+
*
21+
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
22+
*/
23+
public function __construct(AllocationRepositoryInterface $repository)
24+
{
25+
parent::__construct();
26+
27+
$this->repository = $repository;
28+
}
29+
30+
/**
31+
* Lists all of the allocations available to a server and wether or
32+
* not they are currently assigned as the primary for this server.
33+
*
34+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request
35+
* @return array
36+
*/
37+
public function index(GetNetworkRequest $request): array
38+
{
39+
$server = $request->getModel(Server::class);
40+
41+
$allocations = $this->repository->findWhere([
42+
['server_id', '=', $server->id],
43+
]);
44+
45+
return $this->fractal->collection($allocations)
46+
->transformWith($this->getTransformer(AllocationTransformer::class))
47+
->toArray();
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class GetNetworkRequest extends ClientApiRequest
9+
{
10+
/**
11+
* Check that the user has permission to view the allocations for
12+
* this server.
13+
*
14+
* @return bool
15+
*/
16+
public function authorize(): bool
17+
{
18+
return $this->user()->can('view-allocations', $this->getModel(Server::class));
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Pterodactyl\Transformers\Api\Client;
4+
5+
use Pterodactyl\Models\Allocation;
6+
7+
class AllocationTransformer extends BaseClientTransformer
8+
{
9+
/**
10+
* Return the resource name for the JSONAPI output.
11+
*
12+
* @return string
13+
*/
14+
public function getResourceName(): string
15+
{
16+
return 'allocation';
17+
}
18+
19+
/**
20+
* Return basic information about the currently logged in user.
21+
*
22+
* @param \Pterodactyl\Models\Allocation $model
23+
* @return array
24+
*/
25+
public function transform(Allocation $model)
26+
{
27+
$model->loadMissing('server');
28+
29+
return [
30+
'ip' => $model->ip,
31+
'alias' => $model->ip_alias,
32+
'port' => $model->port,
33+
'default' => $model->getRelation('server')->allocation_id === $model->id,
34+
];
35+
}
36+
}

routes/api-client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@
4646
->where('file', '.*')
4747
->name('api.client.servers.files.download');
4848
});
49+
50+
Route::group(['prefix' => '/network'], function () {
51+
Route::get('/', 'Servers\NetworkController@index')->name('api.client.servers.network');
52+
});
4953
});

0 commit comments

Comments
 (0)