Skip to content

Commit 65630bd

Browse files
committed
Move API to use JSON:API standards and fractal serializer
Makes the data slightly more complex, but forces a standard and can always be changed down the road simply by changing the default serializer.
1 parent c071efd commit 65630bd

15 files changed

+315
-22
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Controllers\API\Admin;
26+
27+
use Fractal;
28+
use Illuminate\Http\Request;
29+
use Pterodactyl\Models\Server;
30+
use Pterodactyl\Http\Controllers\Controller;
31+
use Pterodactyl\Transformers\Admin\ServerTransformer;
32+
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
33+
34+
class ServerController extends Controller
35+
{
36+
/**
37+
* Controller to handle returning all servers on the system.
38+
*
39+
* @param \Illuminate\Http\Request $request
40+
* @return array
41+
*/
42+
public function index(Request $request)
43+
{
44+
$servers = Server::paginate(20);
45+
46+
return Fractal::create()
47+
->collection($servers)
48+
->transformWith(new ServerTransformer)
49+
->paginateWith(new IlluminatePaginatorAdapter($servers))
50+
->withResourceName('server')
51+
->toArray();
52+
}
53+
54+
/**
55+
* Controller to handle returning information on a single server.
56+
*
57+
* @param \Illuminate\Http\Request $request
58+
* @return array
59+
*/
60+
public function view(Request $request, $id)
61+
{
62+
$server = Server::findOrFail($id);
63+
64+
$fractal = Fractal::create()->item($server);
65+
66+
if ($request->input('include')) {
67+
$fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([
68+
'allocations', 'subusers', 'user',
69+
'pack', 'service', 'option',
70+
])->toArray());
71+
}
72+
73+
return $fractal->transformWith(new ServerTransformer)
74+
->withResourceName('server')
75+
->toArray();
76+
}
77+
}

app/Http/Controllers/API/User/CoreController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use Fractal;
2828
use Illuminate\Http\Request;
2929
use Pterodactyl\Http\Controllers\Controller;
30-
use Pterodactyl\Transformers\User\ServerTransformer;
30+
use Pterodactyl\Transformers\User\OverviewTransformer;
3131

3232
class CoreController extends Controller
3333
{
@@ -41,6 +41,9 @@ public function index(Request $request)
4141
{
4242
$servers = $request->user()->access('service', 'node', 'allocation', 'option')->get();
4343

44-
return Fractal::collection($servers)->transformWith(new ServerTransformer)->toArray();
44+
return Fractal::collection($servers)
45+
->transformWith(new OverviewTransformer)
46+
->withResourceName('server')
47+
->toArray();
4548
}
4649
}

app/Http/Controllers/API/User/ServerController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ public function index(Request $request, $uuid)
4646
$server = Server::byUuid($uuid);
4747
$fractal = Fractal::create()->item($server);
4848

49-
if ($request->input('with')) {
50-
$fractal->parseIncludes(collect(explode(',', $request->input('with')))->intersect([
49+
if ($request->input('include')) {
50+
$fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([
5151
'allocations', 'subusers', 'stats',
5252
])->toArray());
5353
}
5454

55-
return $fractal->transformWith(new ServerTransformer)->toArray();
55+
return $fractal->transformWith(new ServerTransformer)
56+
->withResourceName('server')
57+
->toArray();
5658
}
5759

5860
/**

app/Extensions/NoDataSerializer.php renamed to app/Transformers/Admin/AllocationTransformer.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@
2222
* SOFTWARE.
2323
*/
2424

25-
namespace Pterodactyl\Extensions;
25+
namespace Pterodactyl\Transformers\Admin;
2626

27-
use League\Fractal\Serializer\ArraySerializer;
27+
use Pterodactyl\Models\Allocation;
28+
use League\Fractal\TransformerAbstract;
2829

29-
class NoDataSerializer extends ArraySerializer
30+
class AllocationTransformer extends TransformerAbstract
3031
{
3132
/**
32-
* Serialize a collection and don't insert as a member of `data`
33-
*
34-
* @param string $resourceKey
35-
* @param array $data
33+
* Return a generic transformed server array.
3634
*
3735
* @return array
3836
*/
39-
public function collection($resourceKey, array $data)
37+
public function transform(Allocation $allocation)
4038
{
41-
return $data;
39+
return $allocation->toArray();
4240
}
4341
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\Transformers\Admin;
26+
27+
use Pterodactyl\Models\Server;
28+
use League\Fractal\TransformerAbstract;
29+
30+
class ServerTransformer extends TransformerAbstract
31+
{
32+
/**
33+
* List of resources that can be included.
34+
*
35+
* @var array
36+
*/
37+
protected $availableIncludes = [
38+
'allocations',
39+
'user',
40+
'subusers',
41+
];
42+
43+
/**
44+
* Return a generic transformed server array.
45+
*
46+
* @return array
47+
*/
48+
public function transform(Server $server)
49+
{
50+
return $server->toArray();
51+
}
52+
53+
/**
54+
* Return a generic array of allocations for this server.
55+
*
56+
* @return \Leauge\Fractal\Resource\Collection
57+
*/
58+
public function includeAllocations(Server $server)
59+
{
60+
return $this->collection($server->allocations, new AllocationTransformer, 'allocation');
61+
}
62+
63+
/**
64+
* Return a generic array of data about subusers for this server.
65+
*
66+
* @return \Leauge\Fractal\Resource\Collection
67+
*/
68+
public function includeSubusers(Server $server)
69+
{
70+
return $this->collection($server->subusers, new SubuserTransformer, 'subuser');
71+
}
72+
73+
/**
74+
* Return a generic array of data about subusers for this server.
75+
*
76+
* @return \Leauge\Fractal\Resource\Item
77+
*/
78+
public function includeUser(Server $server)
79+
{
80+
return $this->item($server->user, new UserTransformer, 'user');
81+
}
82+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Transformers\Admin;
26+
27+
use Pterodactyl\Models\Subuser;
28+
use Pterodactyl\Models\Permission;
29+
use League\Fractal\TransformerAbstract;
30+
31+
class SubuserTransformer extends TransformerAbstract
32+
{
33+
/**
34+
* Return a generic transformed subuser array.
35+
*
36+
* @return array
37+
*/
38+
public function transform(Subuser $subuser)
39+
{
40+
return [
41+
'id' => $subuser->id,
42+
'username' => $subuser->user->username,
43+
'email' => $subuser->user->email,
44+
'2fa' => (bool) $subuser->user->use_totp,
45+
'permissions' => $subuser->permissions->pluck('permission'),
46+
'created_at' => $subuser->created_at,
47+
'updated_at' => $subuser->updated_at,
48+
];
49+
}
50+
}
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\Transformers\Admin;
26+
27+
use Pterodactyl\Models\User;
28+
use League\Fractal\TransformerAbstract;
29+
30+
class UserTransformer extends TransformerAbstract
31+
{
32+
/**
33+
* Return a generic transformed subuser array.
34+
*
35+
* @return array
36+
*/
37+
public function transform(User $user)
38+
{
39+
return $user->toArray();
40+
}
41+
}

app/Transformers/User/AllocationTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function __construct(Server $server)
5555
public function transform(Allocation $allocation)
5656
{
5757
return [
58+
'id' => $allocation->id,
5859
'ip' => $allocation->alias,
5960
'port' => $allocation->port,
6061
'default' => ($allocation->id === $this->server->allocation_id),

app/Transformers/User/OverviewTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class OverviewTransformer extends TransformerAbstract
3737
public function transform(Server $server)
3838
{
3939
return [
40-
'uuidShort' => $server->uuidShort,
40+
'id' => $server->uuidShort,
4141
'uuid' => $server->uuid,
4242
'name' => $server->name,
4343
'node' => $server->node->name,

app/Transformers/User/ServerTransformer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ServerTransformer extends TransformerAbstract
4848
public function transform(Server $server)
4949
{
5050
return [
51-
'uuidShort' => $server->uuidShort,
51+
'id' => $server->uuidShort,
5252
'uuid' => $server->uuid,
5353
'name' => $server->name,
5454
'description' => $server->description,
@@ -73,7 +73,7 @@ public function includeAllocations(Server $server)
7373
{
7474
$allocations = $server->allocations;
7575

76-
return $this->collection($allocations, new AllocationTransformer($server));
76+
return $this->collection($allocations, new AllocationTransformer($server), 'allocation');
7777
}
7878

7979
/**
@@ -85,7 +85,7 @@ public function includeSubusers(Server $server)
8585
{
8686
$server->load('subusers.permissions', 'subusers.user');
8787

88-
return $this->collection($server->subusers, new SubuserTransformer);
88+
return $this->collection($server->subusers, new SubuserTransformer, 'subuser');
8989
}
9090

9191
/**
@@ -95,6 +95,6 @@ public function includeSubusers(Server $server)
9595
*/
9696
public function includeStats(Server $server)
9797
{
98-
return $this->item($server->guzzleClient(), new StatsTransformer);
98+
return $this->item($server->guzzleClient(), new StatsTransformer, 'stat');
9999
}
100100
}

0 commit comments

Comments
 (0)