Skip to content

Commit 2bd691e

Browse files
committed
Add database list endpoint, add more resource name magic
1 parent 407120a commit 2bd691e

36 files changed

+416
-187
lines changed

app/Extensions/Spatie/Fractalistic/Fractal.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Extensions\Spatie\Fractalistic;
44

5-
use Illuminate\Database\Eloquent\Model;
5+
use League\Fractal\TransformerAbstract;
66
use League\Fractal\Serializer\JsonApiSerializer;
77
use Spatie\Fractalistic\Fractal as SpatieFractal;
88
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -31,21 +31,14 @@ public function createData()
3131
$this->paginator = new IlluminatePaginatorAdapter($this->data);
3232
}
3333

34-
// Automatically set the resource name if the response data is a model
35-
// and the resource name is available on the model.
36-
if (is_null($this->resourceName) && $this->data instanceof Model) {
37-
if (defined(get_class($this->data) . '::RESOURCE_NAME')) {
38-
$this->resourceName = constant(get_class($this->data) . '::RESOURCE_NAME');
39-
}
40-
}
41-
42-
if (is_null($this->resourceName) && $this->data instanceof LengthAwarePaginator) {
43-
$item = collect($this->data->items())->first();
44-
if ($item instanceof Model) {
45-
if (defined(get_class($item) . '::RESOURCE_NAME')) {
46-
$this->resourceName = constant(get_class($item) . '::RESOURCE_NAME');
47-
}
48-
}
34+
// If the resource name is not set attempt to pull it off the transformer
35+
// itself and set it automatically.
36+
if (
37+
is_null($this->resourceName)
38+
&& $this->transformer instanceof TransformerAbstract
39+
&& method_exists($this->transformer, 'getResourceName')
40+
) {
41+
$this->resourceName = $this->transformer->getResourceName();
4942
}
5043

5144
return parent::createData();

app/Http/Controllers/Api/Application/ApplicationApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class ApplicationApiController extends Controller
1616
private $request;
1717

1818
/**
19-
* @var \Spatie\Fractalistic\Fractal
19+
* @var \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal
2020
*/
2121
protected $fractal;
2222

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
7+
use Pterodactyl\Transformers\Api\Application\ServerDatabaseTransformer;
8+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
9+
10+
class DatabaseController extends ApplicationApiController
11+
{
12+
/**
13+
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
14+
*/
15+
private $repository;
16+
17+
/**
18+
* DatabaseController constructor.
19+
*
20+
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
21+
*/
22+
public function __construct(DatabaseRepositoryInterface $repository)
23+
{
24+
parent::__construct();
25+
26+
$this->repository = $repository;
27+
}
28+
29+
/**
30+
* Return a listing of all databases currently available to a single
31+
* server.
32+
*
33+
* @param \Pterodactyl\Models\Server $server
34+
* @return array
35+
*/
36+
public function index(Server $server): array
37+
{
38+
$databases = $this->repository->getDatabasesForServer($server->id);
39+
40+
return $this->fractal->collection($databases)
41+
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))
42+
->toArray();
43+
}
44+
}

app/Models/APILog.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Models;
114

app/Models/Allocation.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Models;
114

@@ -19,6 +12,12 @@ class Allocation extends Model implements CleansAttributes, ValidableContract
1912
{
2013
use Eloquence, Validable;
2114

15+
/**
16+
* The resource name for this model when it is transformed into an
17+
* API representation using fractal.
18+
*/
19+
const RESOURCE_NAME = 'allocation';
20+
2221
/**
2322
* The table associated with the model.
2423
*

app/Models/ApiKey.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class ApiKey extends Model implements CleansAttributes, ValidableContract
5050
'user_id' => 'int',
5151
'r_' . AdminAcl::RESOURCE_USERS => 'int',
5252
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
53-
'r_' . AdminAcl::RESOURCE_DATABASES => 'int',
53+
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',
54+
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',
5455
'r_' . AdminAcl::RESOURCE_EGGS => 'int',
5556
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',
5657
'r_' . AdminAcl::RESOURCE_NESTS => 'int',
@@ -108,7 +109,8 @@ class ApiKey extends Model implements CleansAttributes, ValidableContract
108109
'last_used_at' => 'nullable|date',
109110
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
110111
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',
111-
'r_' . AdminAcl::RESOURCE_DATABASES => 'integer|min:0|max:3',
112+
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',
113+
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',
112114
'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',
113115
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',
114116
'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',

app/Models/Checksum.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

app/Models/DaemonKey.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
<?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-
*/
242

253
namespace Pterodactyl\Models;
264

app/Models/Database.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Models;
114

@@ -19,6 +12,12 @@ class Database extends Model implements CleansAttributes, ValidableContract
1912
{
2013
use Eloquence, Validable;
2114

15+
/**
16+
* The resource name for this model when it is transformed into an
17+
* API representation using fractal.
18+
*/
19+
const RESOURCE_NAME = 'server_database';
20+
2221
/**
2322
* The table associated with the model.
2423
*

app/Models/DatabaseHost.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Models;
114

@@ -19,6 +12,12 @@ class DatabaseHost extends Model implements CleansAttributes, ValidableContract
1912
{
2013
use Eloquence, Validable;
2114

15+
/**
16+
* The resource name for this model when it is transformed into an
17+
* API representation using fractal.
18+
*/
19+
const RESOURCE_NAME = 'database_host';
20+
2221
/**
2322
* The table associated with the model.
2423
*

0 commit comments

Comments
 (0)